Permalink
Browse files

sem: add new g_sem_down_timed()

So that we don't hang forever.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
  • Loading branch information...
1 parent f014b1d commit a1e1efc708f4225b132843b583e83a9a4d56cbe3 @felipec committed Mar 5, 2011
Showing with 20 additions and 0 deletions.
  1. +20 −0 sem.h
View
20 sem.h
@@ -52,6 +52,26 @@ g_sem_down(GSem *sem)
g_mutex_unlock(sem->mutex);
}
+static inline bool
+g_sem_down_timed(GSem *sem, int seconds)
+{
+ GTimeVal tv;
+
+ g_mutex_lock(sem->mutex);
+ while (sem->count == 0) {
+ g_get_current_time(&tv);
+ tv.tv_sec += seconds;
+ if (!g_cond_timed_wait(sem->condition, sem->mutex, &tv)) {
+ g_mutex_unlock(sem->mutex);
+ return false;
+ }
+ }
+ sem->count--;
+ g_mutex_unlock(sem->mutex);
+
+ return true;
+}
+
static inline void
g_sem_up(GSem *sem)
{

0 comments on commit a1e1efc

Please sign in to comment.