bjoernknafla / amp

C portable low-level assemblies for parallelism and threading
Other
44 stars 2 forks source link

Semaphore TryWait #6

Open ZeroStride opened 14 years ago

ZeroStride commented 14 years ago

Proposal: int amp_semaphore_trywait(amp_semaphore_t*)

Invokes: Windows: WaitForSingleObject(HANDLE, 0); pthreads: sem_trywait(sem_t*);

Notes: Windows uses WaitForSingleObject to perform the functionality of sem_trywait as well as sem_timedwait however the MacOS implementation of pthreads does not contain a sem_timedwait. It does support pthread_cond_timedwait, however. For this reason I am not proposing an implementation of timedwait.

References: http://msdn.microsoft.com/en-us/library/ms687032(v=VS.85).aspx?ppud=4 http://developer.apple.com/macosx/multithreadedprogramming.html

bjoernknafla commented 14 years ago

There are four different backends for semaphores:

  1. windows - can use WaitForSingleObject as you proposed
  2. POSIX 1003.1b - can use sem_trywait as you proposed
  3. pthreads - uses a mutex to emulate a semaphore, so locking and eyeing the semaphore count would be sufficient. A wait timeout variation would work via your proposed pthread_cond_timedwait.
  4. libdispatch is used as a backend on OSX - dispatch_semaphore_wait can be used.

This is doable and definitly needed.