Change semaphores into resource counters.

This pr closes #36.