gallium: added pipe_semaphore and related code
authorBrian Paul <brianp@vmware.com>
Tue, 8 Dec 2009 00:58:46 +0000 (17:58 -0700)
committerBrian Paul <brianp@vmware.com>
Tue, 8 Dec 2009 01:04:54 +0000 (18:04 -0700)
src/gallium/include/pipe/p_thread.h

index 25e414823256d4f41202049acde9630052aed758..45c35a87d0e61c06d7d6b76ba982067fb15c5228 100644 (file)
@@ -207,6 +207,56 @@ typedef unsigned pipe_condvar;
 #endif  /* PIPE_OS_? */
 
 
+/*
+ * Semaphores
+ */
+
+typedef struct
+{
+   pipe_mutex mutex;
+   pipe_condvar cond;
+   int counter;
+} pipe_semaphore;
+
+
+static INLINE void
+pipe_semaphore_init(pipe_semaphore *sema, int init_val)
+{
+   pipe_mutex_init(sema->mutex);
+   pipe_condvar_init(sema->cond);
+   sema->counter = init_val;
+}
+
+static INLINE void
+pipe_semaphore_destroy(pipe_semaphore *sema)
+{
+   pipe_mutex_destroy(sema->mutex);
+   pipe_condvar_destroy(sema->cond);
+}
+
+/** Signal/increment semaphore counter */
+static INLINE void
+pipe_semaphore_signal(pipe_semaphore *sema)
+{
+   pipe_mutex_lock(sema->mutex);
+   sema->counter++;
+   pipe_condvar_signal(sema->cond);
+   pipe_mutex_unlock(sema->mutex);
+}
+
+/** Wait for semaphore counter to be greater than zero */
+static INLINE void
+pipe_semaphore_wait(pipe_semaphore *sema)
+{
+   pipe_mutex_lock(sema->mutex);
+   while (sema->counter <= 0) {
+      pipe_condvar_wait(sema->cond, sema->mutex);
+   }
+   sema->counter--;
+   pipe_mutex_unlock(sema->mutex);
+}
+
+
 
 /*
  * Thread-specific data.