cleanup mutex when cancel'd in pthread_cond_wait
authorTero Marttila <terom@fixme.fi>
Thu, 31 Dec 2009 16:53:07 +0200
changeset 20 f0d1011e8874
parent 19 ebcc49de97d0
child 21 47f15166e25a
cleanup mutex when cancel'd in pthread_cond_wait
src/lib/ctx.c
--- a/src/lib/ctx.c	Thu Dec 31 16:40:13 2009 +0200
+++ b/src/lib/ctx.c	Thu Dec 31 16:53:07 2009 +0200
@@ -6,6 +6,13 @@
 #include <assert.h>
 #include <stdio.h> // for perror
 
+static void pt_mutex_unlock (void *arg)
+{
+    pthread_mutex_t *mutex = arg;
+
+    assert(!pthread_mutex_unlock(mutex));
+}
+
 /**
  * Enqueue the given piece of work
  *
@@ -31,7 +38,8 @@
  */
 static void pt_work_dequeue (struct pt_ctx *ctx, struct pt_work **work_ptr)
 {
-    // acquire
+    // acquire, cancel-safe
+    pthread_cleanup_push(pt_mutex_unlock, &ctx->work_mutex);
     assert(!pthread_mutex_lock(&ctx->work_mutex));
 
     // idle?
@@ -40,6 +48,7 @@
 
     // wait for work
     while (TAILQ_EMPTY(&ctx->work))
+        // we can expect to get pthread_cancel'd here
         assert(!pthread_cond_wait(&ctx->work_cond, &ctx->work_mutex));
 
     // pop work
@@ -47,7 +56,7 @@
     TAILQ_REMOVE(&ctx->work, *work_ptr, ctx_work);
 
     // release
-    assert(!pthread_mutex_unlock(&ctx->work_mutex));
+    pthread_cleanup_pop(true);
 }
 
 /**