tests: use named semaphores

to allow testing on linux and Mac OS X.
Use locks to protect counters.