apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Dan Johnson" <...@nrtc.northrop.com>
Subject RE: counting semaphores
Date Wed, 18 May 2005 15:46:07 GMT
I ran into the same problem and (since I was bound to using just APR)
wound up using atomic integer operations combined with a mutex.
Basically, a semaphore wait looks like:

if (apr_atomic_dec32 (&count) < 0)
	apr_thread_mutex_lock (mutex);

and a semaphore post looks like:

if ((apr_int32_t) apr_atomic_inc32 (&count) <= 0)
	apr_thread_mutex_unlock (mutex);

You can get the effect of cross-process semaphores by using
cross-process mutexes and putting your count variable in shared memory.

I also don't understand why semaphores are missing from APR, especially
since mutexes seem to have been implemented on top of them in many
cases. This must have been a deliberate choice, but why?

Dan


-----Original Message-----
From: Bruno Haas [mailto:grinob.haas@laposte.net] 
Sent: Wednesday, May 18, 2005 4:10 AM
To: dev@apr.apache.org
Subject: counting semaphores

I am quite surprised that there are no counting semaphores in apr. What 
I would need to do is have thread A releasing a semaphore for as many 
times as an event has happened and thread B blocked waiting for the 
semaphore. The counting semaphore would guarantee that thread B is 
"woken up" as many times as the event has happened.

I seem to be unable to do this sort of thing using only mutexes, any
ideas ?

Bruno


Mime
View raw message