Blame SOURCES/0030-Ticket-49435-Fix-NS-race-condition-on-loaded-test-sy.patch

b045b9
From 04605da5c813ffc818d874ae0a14790c166d792d Mon Sep 17 00:00:00 2001
b045b9
From: William Brown <firstyear@redhat.com>
b045b9
Date: Mon, 6 Nov 2017 08:56:01 +1000
b045b9
Subject: [PATCH] Ticket 49435 - Fix NS race condition on loaded test systems
b045b9
b045b9
Bug Description:  During a test run, on a heavily loaded systems
b045b9
some events would time out before they could occur correctly.
b045b9
b045b9
Fix Description:  Change the structure of events to mitigate
b045b9
a deref performance hit, and add a ns_job_wait conditional
b045b9
that allows blocking on a job to complete so that tests do not
b045b9
require time based checks.
b045b9
b045b9
https://pagure.io/389-ds-base/issue/49435
b045b9
b045b9
Author: wibrown
b045b9
b045b9
Review by: mreynolds (Thanks!)
b045b9
---
b045b9
 src/nunc-stans/include/nunc-stans.h  |  12 +++
b045b9
 src/nunc-stans/ns/ns_event_fw.h      |   3 +-
b045b9
 src/nunc-stans/ns/ns_thrpool.c       | 175 +++++++++++++++++++++--------------
b045b9
 src/nunc-stans/test/test_nuncstans.c | 156 ++++++++++++++++++-------------
b045b9
 4 files changed, 209 insertions(+), 137 deletions(-)
b045b9
b045b9
diff --git a/src/nunc-stans/include/nunc-stans.h b/src/nunc-stans/include/nunc-stans.h
b045b9
index 386a8d283..192e38ec3 100644
b045b9
--- a/src/nunc-stans/include/nunc-stans.h
b045b9
+++ b/src/nunc-stans/include/nunc-stans.h
b045b9
@@ -77,6 +77,10 @@ typedef enum _ns_result_t {
b045b9
      * This occurs when a lower level OS issue occurs, generally thread related.
b045b9
      */
b045b9
     NS_THREAD_FAILURE = 5,
b045b9
+    /**
b045b9
+     * The job is being deleted
b045b9
+     */
b045b9
+    NS_DELETING = 6,
b045b9
 } ns_result_t;
b045b9
 
b045b9
 /**
b045b9
@@ -837,6 +841,14 @@ ns_job_type_t ns_job_get_output_type(struct ns_job_t *job);
b045b9
 ns_result_t ns_job_set_done_cb(struct ns_job_t *job, ns_job_func_t func);
b045b9
 
b045b9
 /**
b045b9
+ * Block until a job is completed. This returns the next state of the job as as a return.
b045b9
+ *
b045b9
+ * \param job The job to set the callback for.
b045b9
+ * \retval ns_job_state_t The next state the job will move to. IE, WAITING, DELETED, ARMED.
b045b9
+ */
b045b9
+ns_result_t ns_job_wait(struct ns_job_t *job);
b045b9
+
b045b9
+/**
b045b9
  * Creates a new thread pool
b045b9
  *
b045b9
  * Must be called with a struct ns_thrpool_config that has been
b045b9
diff --git a/src/nunc-stans/ns/ns_event_fw.h b/src/nunc-stans/ns/ns_event_fw.h
b045b9
index 436b28269..88997b24d 100644
b045b9
--- a/src/nunc-stans/ns/ns_event_fw.h
b045b9
+++ b/src/nunc-stans/ns/ns_event_fw.h
b045b9
@@ -80,7 +80,8 @@ typedef enum _ns_job_state {
b045b9
    interface between the app/thread pool/event framework */
b045b9
 typedef struct ns_job_t
b045b9
 {
b045b9
-    pthread_mutex_t *monitor;
b045b9
+    pthread_mutex_t monitor;
b045b9
+    pthread_cond_t notify;
b045b9
     struct ns_thrpool_t *tp;
b045b9
     ns_job_func_t func;
b045b9
     struct ns_job_data_t *data;
b045b9
diff --git a/src/nunc-stans/ns/ns_thrpool.c b/src/nunc-stans/ns/ns_thrpool.c
b045b9
index 2ad0bd799..1d8bb03f1 100644
b045b9
--- a/src/nunc-stans/ns/ns_thrpool.c
b045b9
+++ b/src/nunc-stans/ns/ns_thrpool.c
b045b9
@@ -214,7 +214,7 @@ job_queue_cleanup(void *arg)
b045b9
 static void
b045b9
 internal_ns_job_done(ns_job_t *job)
b045b9
 {
b045b9
-    pthread_mutex_lock(job->monitor);
b045b9
+    pthread_mutex_lock(&(job->monitor));
b045b9
 #ifdef DEBUG
b045b9
     ns_log(LOG_DEBUG, "internal_ns_job_done %x state %d moving to NS_JOB_DELETED\n", job, job->state);
b045b9
 #endif
b045b9
@@ -239,9 +239,9 @@ internal_ns_job_done(ns_job_t *job)
b045b9
         job->done_cb(job);
b045b9
     }
b045b9
 
b045b9
-    pthread_mutex_unlock(job->monitor);
b045b9
-    pthread_mutex_destroy(job->monitor);
b045b9
-    ns_free(job->monitor);
b045b9
+    pthread_mutex_unlock(&(job->monitor));
b045b9
+    pthread_mutex_destroy(&(job->monitor));
b045b9
+    pthread_cond_destroy(&(job->notify));
b045b9
 
b045b9
     ns_free(job);
b045b9
 }
b045b9
@@ -250,7 +250,7 @@ internal_ns_job_done(ns_job_t *job)
b045b9
 static void
b045b9
 internal_ns_job_rearm(ns_job_t *job)
b045b9
 {
b045b9
-    pthread_mutex_lock(job->monitor);
b045b9
+    pthread_mutex_lock(&(job->monitor));
b045b9
     PR_ASSERT(job->state == NS_JOB_NEEDS_ARM);
b045b9
 /* Don't think I need to check persistence here, it could be the first arm ... */
b045b9
 #ifdef DEBUG
b045b9
@@ -267,7 +267,7 @@ internal_ns_job_rearm(ns_job_t *job)
b045b9
         /* Prevents an un-necessary queue / dequeue to the event_q */
b045b9
         work_q_notify(job);
b045b9
     }
b045b9
-    pthread_mutex_unlock(job->monitor);
b045b9
+    pthread_mutex_unlock(&(job->monitor));
b045b9
 }
b045b9
 
b045b9
 static void
b045b9
@@ -281,7 +281,7 @@ work_job_execute(ns_job_t *job)
b045b9
      * DELETED! Crashes abound, you have been warned ...
b045b9
      */
b045b9
     PR_ASSERT(job);
b045b9
-    pthread_mutex_lock(job->monitor);
b045b9
+    pthread_mutex_lock(&(job->monitor));
b045b9
 #ifdef DEBUG
b045b9
     ns_log(LOG_DEBUG, "work_job_execute %x state %d moving to NS_JOB_RUNNING\n", job, job->state);
b045b9
 #endif
b045b9
@@ -303,7 +303,12 @@ work_job_execute(ns_job_t *job)
b045b9
 #ifdef DEBUG
b045b9
         ns_log(LOG_DEBUG, "work_job_execute %x state %d job func complete, sending to job_done...\n", job, job->state);
b045b9
 #endif
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        /*
b045b9
+         * Let waiters know we are done, they'll pick up once
b045b9
+         * we unlock.
b045b9
+         */
b045b9
+        pthread_cond_signal(&(job->notify));
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         internal_ns_job_done(job);
b045b9
         /* MUST NOT ACCESS JOB AGAIN.*/
b045b9
     } else if (job->state == NS_JOB_NEEDS_ARM) {
b045b9
@@ -311,7 +316,8 @@ work_job_execute(ns_job_t *job)
b045b9
         ns_log(LOG_DEBUG, "work_job_execute %x state %d job func complete, sending to rearm...\n", job, job->state);
b045b9
 #endif
b045b9
         /* Rearm the job! */
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        /* We *don't* notify here because we ARE NOT done! */
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         internal_ns_job_rearm(job);
b045b9
     } else {
b045b9
 #ifdef DEBUG
b045b9
@@ -321,7 +327,12 @@ work_job_execute(ns_job_t *job)
b045b9
         PR_ASSERT(!NS_JOB_IS_PERSIST(job->job_type));
b045b9
         /* We are now idle, set waiting. */
b045b9
         job->state = NS_JOB_WAITING;
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        /*
b045b9
+         * Let waiters know we are done, they'll pick up once
b045b9
+         * we unlock.
b045b9
+         */
b045b9
+        pthread_cond_signal(&(job->notify));
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
     }
b045b9
     /* MUST NOT ACCESS JOB AGAIN */
b045b9
 }
b045b9
@@ -338,7 +349,7 @@ static void
b045b9
 work_q_notify(ns_job_t *job)
b045b9
 {
b045b9
     PR_ASSERT(job);
b045b9
-    pthread_mutex_lock(job->monitor);
b045b9
+    pthread_mutex_lock(&(job->monitor));
b045b9
 #ifdef DEBUG
b045b9
     ns_log(LOG_DEBUG, "work_q_notify %x state %d\n", job, job->state);
b045b9
 #endif
b045b9
@@ -346,12 +357,12 @@ work_q_notify(ns_job_t *job)
b045b9
     if (job->state != NS_JOB_ARMED) {
b045b9
         /* Maybe we should return some error here? */
b045b9
         ns_log(LOG_ERR, "work_q_notify %x state %d is not ARMED, cannot queue!\n", job, job->state);
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return;
b045b9
     }
b045b9
     /* MUST NOT ACCESS job after enqueue. So we stash tp.*/
b045b9
     ns_thrpool_t *ltp = job->tp;
b045b9
-    pthread_mutex_unlock(job->monitor);
b045b9
+    pthread_mutex_unlock(&(job->monitor));
b045b9
     sds_lqueue_enqueue(ltp->work_q, (void *)job);
b045b9
     pthread_mutex_lock(&(ltp->work_q_lock));
b045b9
     pthread_cond_signal(&(ltp->work_q_cv));
b045b9
@@ -411,13 +422,13 @@ static void
b045b9
 update_event(ns_job_t *job)
b045b9
 {
b045b9
     PR_ASSERT(job);
b045b9
-    pthread_mutex_lock(job->monitor);
b045b9
+    pthread_mutex_lock(&(job->monitor));
b045b9
 #ifdef DEBUG
b045b9
     ns_log(LOG_DEBUG, "update_event %x state %d\n", job, job->state);
b045b9
 #endif
b045b9
     PR_ASSERT(job->state == NS_JOB_NEEDS_DELETE || job->state == NS_JOB_ARMED);
b045b9
     if (job->state == NS_JOB_NEEDS_DELETE) {
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         internal_ns_job_done(job);
b045b9
         return;
b045b9
     } else if (NS_JOB_IS_IO(job->job_type) || job->ns_event_fw_fd) {
b045b9
@@ -426,7 +437,7 @@ update_event(ns_job_t *job)
b045b9
         } else {
b045b9
             job->tp->ns_event_fw->ns_event_fw_mod_io(job->tp->ns_event_fw_ctx, job);
b045b9
         }
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         /* We need these returns to prevent a race on the next else if condition when we release job->monitor */
b045b9
         return;
b045b9
     } else if (NS_JOB_IS_TIMER(job->job_type) || job->ns_event_fw_time) {
b045b9
@@ -435,7 +446,7 @@ update_event(ns_job_t *job)
b045b9
         } else {
b045b9
             job->tp->ns_event_fw->ns_event_fw_mod_timer(job->tp->ns_event_fw_ctx, job);
b045b9
         }
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return;
b045b9
     } else if (NS_JOB_IS_SIGNAL(job->job_type) || job->ns_event_fw_sig) {
b045b9
         if (!job->ns_event_fw_sig) {
b045b9
@@ -443,15 +454,15 @@ update_event(ns_job_t *job)
b045b9
         } else {
b045b9
             job->tp->ns_event_fw->ns_event_fw_mod_signal(job->tp->ns_event_fw_ctx, job);
b045b9
         }
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return;
b045b9
     } else {
b045b9
         /* It's a "run now" job. */
b045b9
         if (NS_JOB_IS_THREAD(job->job_type)) {
b045b9
-            pthread_mutex_unlock(job->monitor);
b045b9
+            pthread_mutex_unlock(&(job->monitor));
b045b9
             work_q_notify(job);
b045b9
         } else {
b045b9
-            pthread_mutex_unlock(job->monitor);
b045b9
+            pthread_mutex_unlock(&(job->monitor));
b045b9
             event_q_notify(job);
b045b9
         }
b045b9
     }
b045b9
@@ -602,14 +613,14 @@ event_cb(ns_job_t *job)
b045b9
      */
b045b9
 
b045b9
     /* There is no guarantee this won't be called once we start to enter the shutdown, especially with timers .... */
b045b9
-    pthread_mutex_lock(job->monitor);
b045b9
+    pthread_mutex_lock(&(job->monitor));
b045b9
 
b045b9
     PR_ASSERT(job->state == NS_JOB_ARMED || job->state == NS_JOB_NEEDS_DELETE);
b045b9
     if (job->state == NS_JOB_ARMED && NS_JOB_IS_THREAD(job->job_type)) {
b045b9
 #ifdef DEBUG
b045b9
         ns_log(LOG_DEBUG, "event_cb %x state %d threaded, send to work_q\n", job, job->state);
b045b9
 #endif
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         work_q_notify(job);
b045b9
     } else if (job->state == NS_JOB_NEEDS_DELETE) {
b045b9
 #ifdef DEBUG
b045b9
@@ -620,14 +631,14 @@ event_cb(ns_job_t *job)
b045b9
          * It's here because it's been QUEUED for deletion and *may* be coming
b045b9
          * from the thrpool destroy thread!
b045b9
          */
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
 
b045b9
     } else {
b045b9
 #ifdef DEBUG
b045b9
         ns_log(LOG_DEBUG, "event_cb %x state %d non-threaded, execute right meow\n", job, job->state);
b045b9
 #endif
b045b9
         /* Not threaded, execute now! */
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         work_job_execute(job);
b045b9
         /* MUST NOT ACCESS JOB FROM THIS POINT */
b045b9
     }
b045b9
@@ -682,12 +693,12 @@ static ns_job_t *
b045b9
 new_ns_job(ns_thrpool_t *tp, PRFileDesc *fd, ns_job_type_t job_type, ns_job_func_t func, struct ns_job_data_t *data)
b045b9
 {
b045b9
     ns_job_t *job = ns_calloc(1, sizeof(ns_job_t));
b045b9
-    job->monitor = ns_calloc(1, sizeof(pthread_mutex_t));
b045b9
 
b045b9
     pthread_mutexattr_t *monitor_attr = ns_calloc(1, sizeof(pthread_mutexattr_t));
b045b9
     pthread_mutexattr_init(monitor_attr);
b045b9
     pthread_mutexattr_settype(monitor_attr, PTHREAD_MUTEX_RECURSIVE);
b045b9
-    assert(pthread_mutex_init(job->monitor, monitor_attr) == 0);
b045b9
+    assert(pthread_mutex_init(&(job->monitor), monitor_attr) == 0);
b045b9
+    assert(pthread_cond_init(&(job->notify), NULL) == 0);
b045b9
     ns_free(monitor_attr);
b045b9
 
b045b9
     job->tp = tp;
b045b9
@@ -746,14 +757,14 @@ ns_job_done(ns_job_t *job)
b045b9
     /* Get the shutdown state ONCE at the start, atomically */
b045b9
     int32_t shutdown_state = ns_thrpool_is_shutdown(job->tp);
b045b9
 
b045b9
-    pthread_mutex_lock(job->monitor);
b045b9
+    pthread_mutex_lock(&(job->monitor));
b045b9
 
b045b9
     if (job->state == NS_JOB_NEEDS_DELETE || job->state == NS_JOB_DELETED) {
b045b9
 /* Just return if the job has been marked for deletion */
b045b9
 #ifdef DEBUG
b045b9
         ns_log(LOG_DEBUG, "ns_job_done %x tp shutdown -> %x state %d return early\n", job, shutdown_state, job->state);
b045b9
 #endif
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return NS_SUCCESS;
b045b9
     }
b045b9
 
b045b9
@@ -762,7 +773,7 @@ ns_job_done(ns_job_t *job)
b045b9
 #ifdef DEBUG
b045b9
         ns_log(LOG_DEBUG, "ns_job_done %x tp shutdown -> false state %d failed to mark as done\n", job, job->state);
b045b9
 #endif
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return NS_INVALID_STATE;
b045b9
     }
b045b9
 
b045b9
@@ -773,13 +784,13 @@ ns_job_done(ns_job_t *job)
b045b9
         ns_log(LOG_DEBUG, "ns_job_done %x tp shutdown -> false state %d setting to async NS_JOB_NEEDS_DELETE\n", job, job->state);
b045b9
 #endif
b045b9
         job->state = NS_JOB_NEEDS_DELETE;
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
     } else if (!shutdown_state) {
b045b9
 #ifdef DEBUG
b045b9
         ns_log(LOG_DEBUG, "ns_job_done %x tp shutdown -> false state %d setting NS_JOB_NEEDS_DELETE and queuing\n", job, job->state);
b045b9
 #endif
b045b9
         job->state = NS_JOB_NEEDS_DELETE;
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         event_q_notify(job);
b045b9
     } else {
b045b9
 #ifdef DEBUG
b045b9
@@ -787,7 +798,7 @@ ns_job_done(ns_job_t *job)
b045b9
 #endif
b045b9
         job->state = NS_JOB_NEEDS_DELETE;
b045b9
         /* We are shutting down, just remove it! */
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         internal_ns_job_done(job);
b045b9
     }
b045b9
     return NS_SUCCESS;
b045b9
@@ -849,12 +860,12 @@ ns_add_io_job(ns_thrpool_t *tp, PRFileDesc *fd, ns_job_type_t job_type, ns_job_f
b045b9
         return NS_ALLOCATION_FAILURE;
b045b9
     }
b045b9
 
b045b9
-    pthread_mutex_lock(_job->monitor);
b045b9
+    pthread_mutex_lock(&(_job->monitor));
b045b9
 #ifdef DEBUG
b045b9
     ns_log(LOG_DEBUG, "ns_add_io_job state %d moving to NS_JOB_ARMED\n", (_job)->state);
b045b9
 #endif
b045b9
     _job->state = NS_JOB_NEEDS_ARM;
b045b9
-    pthread_mutex_unlock(_job->monitor);
b045b9
+    pthread_mutex_unlock(&(_job->monitor));
b045b9
     internal_ns_job_rearm(_job);
b045b9
 
b045b9
     /* fill in a pointer to the job for the caller if requested */
b045b9
@@ -889,12 +900,12 @@ ns_add_timeout_job(ns_thrpool_t *tp, struct timeval *tv, ns_job_type_t job_type,
b045b9
         return NS_ALLOCATION_FAILURE;
b045b9
     }
b045b9
 
b045b9
-    pthread_mutex_lock(_job->monitor);
b045b9
+    pthread_mutex_lock(&(_job->monitor));
b045b9
 #ifdef DEBUG
b045b9
     ns_log(LOG_DEBUG, "ns_add_timeout_job state %d moving to NS_JOB_ARMED\n", (_job)->state);
b045b9
 #endif
b045b9
     _job->state = NS_JOB_NEEDS_ARM;
b045b9
-    pthread_mutex_unlock(_job->monitor);
b045b9
+    pthread_mutex_unlock(&(_job->monitor));
b045b9
     internal_ns_job_rearm(_job);
b045b9
 
b045b9
     /* fill in a pointer to the job for the caller if requested */
b045b9
@@ -944,14 +955,14 @@ ns_add_io_timeout_job(ns_thrpool_t *tp, PRFileDesc *fd, struct timeval *tv, ns_j
b045b9
     if (!_job) {
b045b9
         return NS_ALLOCATION_FAILURE;
b045b9
     }
b045b9
-    pthread_mutex_lock(_job->monitor);
b045b9
+    pthread_mutex_lock(&(_job->monitor));
b045b9
     _job->tv = *tv;
b045b9
 
b045b9
 #ifdef DEBUG
b045b9
     ns_log(LOG_DEBUG, "ns_add_io_timeout_job state %d moving to NS_JOB_ARMED\n", (_job)->state);
b045b9
 #endif
b045b9
     _job->state = NS_JOB_NEEDS_ARM;
b045b9
-    pthread_mutex_unlock(_job->monitor);
b045b9
+    pthread_mutex_unlock(&(_job->monitor));
b045b9
     internal_ns_job_rearm(_job);
b045b9
 
b045b9
     /* fill in a pointer to the job for the caller if requested */
b045b9
@@ -982,12 +993,12 @@ ns_add_signal_job(ns_thrpool_t *tp, int32_t signum, ns_job_type_t job_type, ns_j
b045b9
         return NS_ALLOCATION_FAILURE;
b045b9
     }
b045b9
 
b045b9
-    pthread_mutex_lock(_job->monitor);
b045b9
+    pthread_mutex_lock(&(_job->monitor));
b045b9
 #ifdef DEBUG
b045b9
     ns_log(LOG_DEBUG, "ns_add_signal_job state %d moving to NS_JOB_ARMED\n", (_job)->state);
b045b9
 #endif
b045b9
     _job->state = NS_JOB_NEEDS_ARM;
b045b9
-    pthread_mutex_unlock(_job->monitor);
b045b9
+    pthread_mutex_unlock(&(_job->monitor));
b045b9
     internal_ns_job_rearm(_job);
b045b9
 
b045b9
     /* fill in a pointer to the job for the caller if requested */
b045b9
@@ -1038,9 +1049,9 @@ ns_add_shutdown_job(ns_thrpool_t *tp)
b045b9
     if (!_job) {
b045b9
         return NS_ALLOCATION_FAILURE;
b045b9
     }
b045b9
-    pthread_mutex_lock(_job->monitor);
b045b9
+    pthread_mutex_lock(&(_job->monitor));
b045b9
     _job->state = NS_JOB_NEEDS_ARM;
b045b9
-    pthread_mutex_unlock(_job->monitor);
b045b9
+    pthread_mutex_unlock(&(_job->monitor));
b045b9
     internal_ns_job_rearm(_job);
b045b9
     return NS_SUCCESS;
b045b9
 }
b045b9
@@ -1061,13 +1072,13 @@ void *
b045b9
 ns_job_get_data(ns_job_t *job)
b045b9
 {
b045b9
     PR_ASSERT(job);
b045b9
-    pthread_mutex_lock(job->monitor);
b045b9
+    pthread_mutex_lock(&(job->monitor));
b045b9
     PR_ASSERT(job->state != NS_JOB_DELETED);
b045b9
     if (job->state != NS_JOB_DELETED) {
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return job->data;
b045b9
     } else {
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return NULL;
b045b9
     }
b045b9
 }
b045b9
@@ -1076,14 +1087,14 @@ ns_result_t
b045b9
 ns_job_set_data(ns_job_t *job, void *data)
b045b9
 {
b045b9
     PR_ASSERT(job);
b045b9
-    pthread_mutex_lock(job->monitor);
b045b9
+    pthread_mutex_lock(&(job->monitor));
b045b9
     PR_ASSERT(job->state == NS_JOB_WAITING || job->state == NS_JOB_RUNNING);
b045b9
     if (job->state == NS_JOB_WAITING || job->state == NS_JOB_RUNNING) {
b045b9
         job->data = data;
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return NS_SUCCESS;
b045b9
     } else {
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return NS_INVALID_STATE;
b045b9
     }
b045b9
 }
b045b9
@@ -1092,13 +1103,13 @@ ns_thrpool_t *
b045b9
 ns_job_get_tp(ns_job_t *job)
b045b9
 {
b045b9
     PR_ASSERT(job);
b045b9
-    pthread_mutex_lock(job->monitor);
b045b9
+    pthread_mutex_lock(&(job->monitor));
b045b9
     PR_ASSERT(job->state != NS_JOB_DELETED);
b045b9
     if (job->state != NS_JOB_DELETED) {
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return job->tp;
b045b9
     } else {
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return NULL;
b045b9
     }
b045b9
 }
b045b9
@@ -1107,13 +1118,13 @@ ns_job_type_t
b045b9
 ns_job_get_output_type(ns_job_t *job)
b045b9
 {
b045b9
     PR_ASSERT(job);
b045b9
-    pthread_mutex_lock(job->monitor);
b045b9
+    pthread_mutex_lock(&(job->monitor));
b045b9
     PR_ASSERT(job->state == NS_JOB_RUNNING);
b045b9
     if (job->state == NS_JOB_RUNNING) {
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return job->output_job_type;
b045b9
     } else {
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return 0;
b045b9
     }
b045b9
 }
b045b9
@@ -1122,13 +1133,13 @@ ns_job_type_t
b045b9
 ns_job_get_type(ns_job_t *job)
b045b9
 {
b045b9
     PR_ASSERT(job);
b045b9
-    pthread_mutex_lock(job->monitor);
b045b9
+    pthread_mutex_lock(&(job->monitor));
b045b9
     PR_ASSERT(job->state != NS_JOB_DELETED);
b045b9
     if (job->state != NS_JOB_DELETED) {
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return job->job_type;
b045b9
     } else {
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return 0;
b045b9
     }
b045b9
 }
b045b9
@@ -1137,13 +1148,13 @@ PRFileDesc *
b045b9
 ns_job_get_fd(ns_job_t *job)
b045b9
 {
b045b9
     PR_ASSERT(job);
b045b9
-    pthread_mutex_lock(job->monitor);
b045b9
+    pthread_mutex_lock(&(job->monitor));
b045b9
     PR_ASSERT(job->state != NS_JOB_DELETED);
b045b9
     if (job->state != NS_JOB_DELETED) {
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return job->fd;
b045b9
     } else {
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return NULL;
b045b9
     }
b045b9
 }
b045b9
@@ -1152,18 +1163,40 @@ ns_result_t
b045b9
 ns_job_set_done_cb(struct ns_job_t *job, ns_job_func_t func)
b045b9
 {
b045b9
     PR_ASSERT(job);
b045b9
-    pthread_mutex_lock(job->monitor);
b045b9
+    pthread_mutex_lock(&(job->monitor));
b045b9
     PR_ASSERT(job->state == NS_JOB_WAITING || job->state == NS_JOB_RUNNING);
b045b9
     if (job->state == NS_JOB_WAITING || job->state == NS_JOB_RUNNING) {
b045b9
         job->done_cb = func;
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return NS_SUCCESS;
b045b9
     } else {
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return NS_INVALID_STATE;
b045b9
     }
b045b9
 }
b045b9
 
b045b9
+ns_result_t
b045b9
+ns_job_wait(struct ns_job_t *job) {
b045b9
+    PR_ASSERT(job);
b045b9
+    pthread_mutex_lock(&(job->monitor));
b045b9
+    if (job->state == NS_JOB_WAITING) {
b045b9
+        /* It's done */
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
+        return NS_SUCCESS;
b045b9
+    } else {
b045b9
+        pthread_cond_wait(&(job->notify), &(job->monitor));
b045b9
+        ns_job_state_t result = job->state;
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
+        if (result == NS_JOB_WAITING) {
b045b9
+            return NS_SUCCESS;
b045b9
+        } else if (result == NS_JOB_NEEDS_DELETE) {
b045b9
+            return NS_DELETING;
b045b9
+        } else {
b045b9
+            PR_ASSERT(1 == 0);
b045b9
+            return NS_INVALID_STATE;
b045b9
+        }
b045b9
+    }
b045b9
+}
b045b9
 
b045b9
 /*
b045b9
  * This is a convenience function - use if you need to re-arm the same event
b045b9
@@ -1173,7 +1206,7 @@ ns_result_t
b045b9
 ns_job_rearm(ns_job_t *job)
b045b9
 {
b045b9
     PR_ASSERT(job);
b045b9
-    pthread_mutex_lock(job->monitor);
b045b9
+    pthread_mutex_lock(&(job->monitor));
b045b9
     PR_ASSERT(job->state == NS_JOB_WAITING || job->state == NS_JOB_RUNNING);
b045b9
 
b045b9
     if (ns_thrpool_is_shutdown(job->tp)) {
b045b9
@@ -1186,7 +1219,7 @@ ns_job_rearm(ns_job_t *job)
b045b9
 #endif
b045b9
         job->state = NS_JOB_NEEDS_ARM;
b045b9
         internal_ns_job_rearm(job);
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return NS_SUCCESS;
b045b9
     } else if (!NS_JOB_IS_PERSIST(job->job_type) && job->state == NS_JOB_RUNNING) {
b045b9
 /* For this to be called, and NS_JOB_RUNNING, we *must* be the callback thread! */
b045b9
@@ -1195,10 +1228,10 @@ ns_job_rearm(ns_job_t *job)
b045b9
         ns_log(LOG_DEBUG, "ns_rearm_job %x state %d setting NS_JOB_NEEDS_ARM\n", job, job->state);
b045b9
 #endif
b045b9
         job->state = NS_JOB_NEEDS_ARM;
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return NS_SUCCESS;
b045b9
     } else {
b045b9
-        pthread_mutex_unlock(job->monitor);
b045b9
+        pthread_mutex_unlock(&(job->monitor));
b045b9
         return NS_INVALID_STATE;
b045b9
     }
b045b9
     /* Unreachable code .... */
b045b9
@@ -1254,7 +1287,7 @@ setup_event_q_wakeup(ns_thrpool_t *tp)
b045b9
                            NS_JOB_READ | NS_JOB_PERSIST | NS_JOB_PRESERVE_FD,
b045b9
                            wakeup_cb, NULL);
b045b9
 
b045b9
-    pthread_mutex_lock(job->monitor);
b045b9
+    pthread_mutex_lock(&(job->monitor));
b045b9
 
b045b9
 /* The event_queue wakeup is ready, arm it. */
b045b9
 #ifdef DEBUG
b045b9
@@ -1267,7 +1300,7 @@ setup_event_q_wakeup(ns_thrpool_t *tp)
b045b9
 
b045b9
     /* Stash the wakeup job in tp so we can release it later. */
b045b9
     tp->event_q_wakeup_job = job;
b045b9
-    pthread_mutex_unlock(job->monitor);
b045b9
+    pthread_mutex_unlock(&(job->monitor));
b045b9
 }
b045b9
 
b045b9
 /* Initialize the thrpool config */
b045b9
@@ -1463,7 +1496,7 @@ ns_thrpool_destroy(struct ns_thrpool_t *tp)
b045b9
          * and use it to wake up the event loop.
b045b9
          */
b045b9
 
b045b9
-        pthread_mutex_lock(tp->event_q_wakeup_job->monitor);
b045b9
+        pthread_mutex_lock(&(tp->event_q_wakeup_job->monitor));
b045b9
 
b045b9
 // tp->event_q_wakeup_job->job_type |= NS_JOB_THREAD;
b045b9
 /* This triggers the job to "run", which will cause a shutdown cascade */
b045b9
@@ -1471,7 +1504,7 @@ ns_thrpool_destroy(struct ns_thrpool_t *tp)
b045b9
         ns_log(LOG_DEBUG, "ns_thrpool_destroy %x state %d moving to NS_JOB_NEEDS_DELETE\n", tp->event_q_wakeup_job, tp->event_q_wakeup_job->state);
b045b9
 #endif
b045b9
         tp->event_q_wakeup_job->state = NS_JOB_NEEDS_DELETE;
b045b9
-        pthread_mutex_unlock(tp->event_q_wakeup_job->monitor);
b045b9
+        pthread_mutex_unlock(&(tp->event_q_wakeup_job->monitor));
b045b9
         /* Has to be event_q_notify, not internal_job_done */
b045b9
         event_q_notify(tp->event_q_wakeup_job);
b045b9
 
b045b9
diff --git a/src/nunc-stans/test/test_nuncstans.c b/src/nunc-stans/test/test_nuncstans.c
b045b9
index 629377a89..afe3c02fc 100644
b045b9
--- a/src/nunc-stans/test/test_nuncstans.c
b045b9
+++ b/src/nunc-stans/test/test_nuncstans.c
b045b9
@@ -55,14 +55,21 @@
b045b9
 /* We need the internal headers for state checks */
b045b9
 #include "../ns/ns_event_fw.h"
b045b9
 
b045b9
+#include <assert.h>
b045b9
+
b045b9
+#include <time.h>
b045b9
+
b045b9
 #ifdef HAVE_STDLIB_H
b045b9
 #include <stdlib.h>
b045b9
 #endif
b045b9
 
b045b9
 
b045b9
 static int cb_check = 0;
b045b9
-static PRLock *cb_lock = NULL;
b045b9
-static PRCondVar *cb_cond = NULL;
b045b9
+
b045b9
+static pthread_mutex_t cb_lock;
b045b9
+static pthread_cond_t cb_cond;
b045b9
+// static PRLock *cb_lock = NULL;
b045b9
+// static PRCondVar *cb_cond = NULL;
b045b9
 
b045b9
 void
b045b9
 ns_test_logger(int priority __attribute__((unused)), const char *fmt, va_list varg)
b045b9
@@ -71,6 +78,19 @@ ns_test_logger(int priority __attribute__((unused)), const char *fmt, va_list va
b045b9
     vprintf(fmt, varg);
b045b9
 }
b045b9
 
b045b9
+static int
b045b9
+cond_wait_rel(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict reltime) {
b045b9
+    struct timespec now;
b045b9
+    struct timespec abswait;
b045b9
+
b045b9
+    clock_gettime(CLOCK_REALTIME, &now;;
b045b9
+
b045b9
+    abswait.tv_sec = now.tv_sec + reltime->tv_sec;
b045b9
+    abswait.tv_nsec = now.tv_nsec + reltime->tv_nsec;
b045b9
+
b045b9
+    return pthread_cond_timedwait(cond, mutex, &abswait);
b045b9
+}
b045b9
+
b045b9
 /* All our other tests will use this in some form. */
b045b9
 static int
b045b9
 ns_test_setup(void **state)
b045b9
@@ -81,8 +101,8 @@ ns_test_setup(void **state)
b045b9
     /* Reset the callback check */
b045b9
     cb_check = 0;
b045b9
     /* Create the cond var the CB check will use. */
b045b9
-    cb_lock = PR_NewLock();
b045b9
-    cb_cond = PR_NewCondVar(cb_lock);
b045b9
+    assert(pthread_mutex_init(&cb_lock, NULL) == 0);
b045b9
+    assert(pthread_cond_init(&cb_cond, NULL) == 0);
b045b9
 
b045b9
     ns_thrpool_config_init(&ns_config);
b045b9
 
b045b9
@@ -105,8 +125,8 @@ ns_test_teardown(void **state)
b045b9
 
b045b9
     ns_thrpool_destroy(tp);
b045b9
 
b045b9
-    PR_DestroyCondVar(cb_cond);
b045b9
-    PR_DestroyLock(cb_lock);
b045b9
+    pthread_cond_destroy(&cb_cond);
b045b9
+    pthread_mutex_destroy(&cb_lock);
b045b9
 
b045b9
     return 0;
b045b9
 }
b045b9
@@ -114,24 +134,23 @@ ns_test_teardown(void **state)
b045b9
 static void
b045b9
 ns_init_test_job_cb(struct ns_job_t *job __attribute__((unused)))
b045b9
 {
b045b9
+    pthread_mutex_lock(&cb_lock);
b045b9
     cb_check += 1;
b045b9
-    PR_Lock(cb_lock);
b045b9
-    PR_NotifyCondVar(cb_cond);
b045b9
-    PR_Unlock(cb_lock);
b045b9
+    pthread_cond_signal(&cb_cond);
b045b9
+    pthread_mutex_unlock(&cb_lock);
b045b9
 }
b045b9
 
b045b9
 static void
b045b9
 ns_init_disarm_job_cb(struct ns_job_t *job)
b045b9
 {
b045b9
     if (ns_job_done(job) == NS_SUCCESS) {
b045b9
+        pthread_mutex_lock(&cb_lock);
b045b9
         cb_check = 1;
b045b9
+        pthread_cond_signal(&cb_cond);
b045b9
+        pthread_mutex_unlock(&cb_lock);
b045b9
     } else {
b045b9
         assert_int_equal(1, 0);
b045b9
     }
b045b9
-    PR_Lock(cb_lock);
b045b9
-    PR_NotifyCondVar(cb_cond);
b045b9
-    /* Disarm ourselves */
b045b9
-    PR_Unlock(cb_lock);
b045b9
 }
b045b9
 
b045b9
 static void
b045b9
@@ -146,20 +165,20 @@ ns_init_test(void **state)
b045b9
 {
b045b9
     struct ns_thrpool_t *tp = *state;
b045b9
     struct ns_job_t *job = NULL;
b045b9
+    struct timespec timeout = {1, 0};
b045b9
 
b045b9
-    PR_Lock(cb_lock);
b045b9
+    pthread_mutex_lock(&cb_lock);
b045b9
     assert_int_equal(
b045b9
         ns_add_job(tp, NS_JOB_NONE | NS_JOB_THREAD, ns_init_test_job_cb, NULL, &job),
b045b9
         0);
b045b9
 
b045b9
-    PR_WaitCondVar(cb_cond, PR_SecondsToInterval(1));
b045b9
-    PR_Unlock(cb_lock);
b045b9
+    assert(cond_wait_rel(&cb_cond, &cb_lock, &timeout) == 0);
b045b9
+    pthread_mutex_unlock(&cb_lock);
b045b9
 
b045b9
     assert_int_equal(cb_check, 1);
b045b9
 
b045b9
     /* Once the job is done, it's not in the event queue, and it's complete */
b045b9
-    /* We have to stall momentarily to let the work_job_execute release the job to us */
b045b9
-    PR_Sleep(PR_SecondsToInterval(1));
b045b9
+    assert(ns_job_wait(job) == NS_SUCCESS);
b045b9
     assert_int_equal(ns_job_done(job), NS_SUCCESS);
b045b9
 }
b045b9
 
b045b9
@@ -169,19 +188,20 @@ ns_set_data_test(void **state)
b045b9
     /* Add a job with data */
b045b9
     struct ns_thrpool_t *tp = *state;
b045b9
     struct ns_job_t *job = NULL;
b045b9
+    struct timespec timeout = {1, 0};
b045b9
 
b045b9
     char *data = malloc(6);
b045b9
 
b045b9
     strcpy(data, "first");
b045b9
 
b045b9
-    PR_Lock(cb_lock);
b045b9
+    pthread_mutex_lock(&cb_lock);
b045b9
     assert_int_equal(
b045b9
         ns_add_job(tp, NS_JOB_NONE | NS_JOB_THREAD, ns_init_test_job_cb, data, &job),
b045b9
         NS_SUCCESS);
b045b9
 
b045b9
     /* Let the job run */
b045b9
-    PR_WaitCondVar(cb_cond, PR_SecondsToInterval(1));
b045b9
-    PR_Unlock(cb_lock);
b045b9
+    assert(cond_wait_rel(&cb_cond, &cb_lock, &timeout) == 0);
b045b9
+    pthread_mutex_unlock(&cb_lock);
b045b9
 
b045b9
     /* Check that the data is correct */
b045b9
     char *retrieved = (char *)ns_job_get_data(job);
b045b9
@@ -193,16 +213,14 @@ ns_set_data_test(void **state)
b045b9
     data = malloc(7);
b045b9
     strcpy(data, "second");
b045b9
 
b045b9
-    while (job->state != NS_JOB_WAITING) {
b045b9
-        PR_Sleep(PR_MillisecondsToInterval(50));
b045b9
-    }
b045b9
+    assert(ns_job_wait(job) == NS_SUCCESS);
b045b9
     ns_job_set_data(job, data);
b045b9
 
b045b9
     /* Rearm, and let it run again. */
b045b9
-    PR_Lock(cb_lock);
b045b9
+    pthread_mutex_lock(&cb_lock);
b045b9
     ns_job_rearm(job);
b045b9
-    PR_WaitCondVar(cb_cond, PR_SecondsToInterval(1));
b045b9
-    PR_Unlock(cb_lock);
b045b9
+    assert(cond_wait_rel(&cb_cond, &cb_lock, &timeout) == 0);
b045b9
+    pthread_mutex_unlock(&cb_lock);
b045b9
 
b045b9
     /* Make sure it's now what we expect */
b045b9
     retrieved = (char *)ns_job_get_data(job);
b045b9
@@ -218,9 +236,7 @@ ns_set_data_test(void **state)
b045b9
      * waiting. we might need a load barrier here ...
b045b9
      */
b045b9
 
b045b9
-    while (job->state != NS_JOB_WAITING) {
b045b9
-        PR_Sleep(PR_MillisecondsToInterval(50));
b045b9
-    }
b045b9
+    assert(ns_job_wait(job) == NS_SUCCESS);
b045b9
 
b045b9
     assert_int_equal(ns_job_done(job), NS_SUCCESS);
b045b9
 }
b045b9
@@ -230,8 +246,9 @@ ns_job_done_cb_test(void **state)
b045b9
 {
b045b9
     struct ns_thrpool_t *tp = *state;
b045b9
     struct ns_job_t *job = NULL;
b045b9
+    struct timespec timeout = {1, 0};
b045b9
 
b045b9
-    PR_Lock(cb_lock);
b045b9
+    pthread_mutex_lock(&cb_lock);
b045b9
     assert_int_equal(
b045b9
         ns_create_job(tp, NS_JOB_NONE | NS_JOB_THREAD, ns_init_do_nothing_cb, &job),
b045b9
         NS_SUCCESS);
b045b9
@@ -240,8 +257,8 @@ ns_job_done_cb_test(void **state)
b045b9
     /* Remove it */
b045b9
     assert_int_equal(ns_job_done(job), NS_SUCCESS);
b045b9
 
b045b9
-    PR_WaitCondVar(cb_cond, PR_SecondsToInterval(1));
b045b9
-    PR_Unlock(cb_lock);
b045b9
+    assert(cond_wait_rel(&cb_cond, &cb_lock, &timeout) == 0);
b045b9
+    pthread_mutex_unlock(&cb_lock);
b045b9
 
b045b9
     assert_int_equal(cb_check, 1);
b045b9
 }
b045b9
@@ -250,16 +267,15 @@ static void
b045b9
 ns_init_rearm_job_cb(struct ns_job_t *job)
b045b9
 {
b045b9
     if (ns_job_rearm(job) != NS_SUCCESS) {
b045b9
+        pthread_mutex_lock(&cb_lock);
b045b9
         cb_check = 1;
b045b9
         /* we failed to re-arm as expected, let's go away ... */
b045b9
         assert_int_equal(ns_job_done(job), NS_SUCCESS);
b045b9
+        pthread_cond_signal(&cb_cond);
b045b9
+        pthread_mutex_unlock(&cb_lock);
b045b9
     } else {
b045b9
         assert_int_equal(1, 0);
b045b9
     }
b045b9
-    PR_Lock(cb_lock);
b045b9
-    PR_NotifyCondVar(cb_cond);
b045b9
-    /* Disarm ourselves */
b045b9
-    PR_Unlock(cb_lock);
b045b9
 }
b045b9
 
b045b9
 static void
b045b9
@@ -268,8 +284,9 @@ ns_job_persist_rearm_ignore_test(void **state)
b045b9
     /* Test that rearm ignores the persistent job. */
b045b9
     struct ns_thrpool_t *tp = *state;
b045b9
     struct ns_job_t *job = NULL;
b045b9
+    struct timespec timeout = {1, 0};
b045b9
 
b045b9
-    PR_Lock(cb_lock);
b045b9
+    pthread_mutex_lock(&cb_lock);
b045b9
     assert_int_equal(
b045b9
         ns_create_job(tp, NS_JOB_NONE | NS_JOB_THREAD | NS_JOB_PERSIST, ns_init_rearm_job_cb, &job),
b045b9
         NS_SUCCESS);
b045b9
@@ -281,8 +298,8 @@ ns_job_persist_rearm_ignore_test(void **state)
b045b9
      * should see only 1 in the cb_check.
b045b9
      */
b045b9
 
b045b9
-    PR_WaitCondVar(cb_cond, PR_SecondsToInterval(1));
b045b9
-    PR_Unlock(cb_lock);
b045b9
+    assert(cond_wait_rel(&cb_cond, &cb_lock, &timeout) == 0);
b045b9
+    pthread_mutex_unlock(&cb_lock);
b045b9
 
b045b9
     /* If we fail to rearm, this is set to 1 Which is what we want. */
b045b9
     assert_int_equal(cb_check, 1);
b045b9
@@ -294,6 +311,7 @@ ns_job_persist_disarm_test(void **state)
b045b9
     /* Make a persistent job */
b045b9
     struct ns_thrpool_t *tp = *state;
b045b9
     struct ns_job_t *job = NULL;
b045b9
+    struct timespec timeout = {2, 0};
b045b9
 
b045b9
     assert_int_equal(
b045b9
         ns_create_job(tp, NS_JOB_NONE | NS_JOB_PERSIST, ns_init_disarm_job_cb, &job),
b045b9
@@ -302,9 +320,9 @@ ns_job_persist_disarm_test(void **state)
b045b9
     assert_int_equal(ns_job_rearm(job), NS_SUCCESS);
b045b9
 
b045b9
     /* In the callback it should disarm */
b045b9
-    PR_Lock(cb_lock);
b045b9
-    PR_WaitCondVar(cb_cond, PR_SecondsToInterval(1));
b045b9
-    PR_Unlock(cb_lock);
b045b9
+    pthread_mutex_lock(&cb_lock);
b045b9
+    assert(cond_wait_rel(&cb_cond, &cb_lock, &timeout) == 0);
b045b9
+    pthread_mutex_unlock(&cb_lock);
b045b9
     /* Make sure it did */
b045b9
     assert_int_equal(cb_check, 1);
b045b9
 }
b045b9
@@ -329,14 +347,13 @@ ns_job_persist_disarm_test(void **state)
b045b9
 static void
b045b9
 ns_init_race_done_job_cb(struct ns_job_t *job)
b045b9
 {
b045b9
-    cb_check += 1;
b045b9
     ns_job_done(job);
b045b9
     /* We need to sleep to let the job race happen */
b045b9
     PR_Sleep(PR_SecondsToInterval(2));
b045b9
-    PR_Lock(cb_lock);
b045b9
-    PR_NotifyCondVar(cb_cond);
b045b9
-    /* Disarm ourselves */
b045b9
-    PR_Unlock(cb_lock);
b045b9
+    pthread_mutex_lock(&cb_lock);
b045b9
+    cb_check += 1;
b045b9
+    pthread_cond_signal(&cb_cond);
b045b9
+    pthread_mutex_unlock(&cb_lock);
b045b9
 }
b045b9
 
b045b9
 static void
b045b9
@@ -344,14 +361,15 @@ ns_job_race_done_test(void **state)
b045b9
 {
b045b9
     struct ns_thrpool_t *tp = *state;
b045b9
     struct ns_job_t *job = NULL;
b045b9
+    struct timespec timeout = {5, 0};
b045b9
 
b045b9
-    PR_Lock(cb_lock);
b045b9
+    pthread_mutex_lock(&cb_lock);
b045b9
     assert_int_equal(
b045b9
         ns_add_job(tp, NS_JOB_NONE | NS_JOB_THREAD, ns_init_race_done_job_cb, NULL, &job),
b045b9
         NS_SUCCESS);
b045b9
 
b045b9
-    PR_WaitCondVar(cb_cond, PR_SecondsToInterval(5));
b045b9
-    PR_Unlock(cb_lock);
b045b9
+    assert(cond_wait_rel(&cb_cond, &cb_lock, &timeout) == 0);
b045b9
+    pthread_mutex_unlock(&cb_lock);
b045b9
 
b045b9
     assert_int_equal(cb_check, 1);
b045b9
 }
b045b9
@@ -365,8 +383,9 @@ ns_job_signal_cb_test(void **state)
b045b9
 {
b045b9
     struct ns_thrpool_t *tp = *state;
b045b9
     struct ns_job_t *job = NULL;
b045b9
+    struct timespec timeout = {1, 0};
b045b9
 
b045b9
-    PR_Lock(cb_lock);
b045b9
+    pthread_mutex_lock(&cb_lock);
b045b9
     assert_int_equal(
b045b9
         ns_add_signal_job(tp, SIGUSR1, NS_JOB_SIGNAL, ns_init_test_job_cb, NULL, &job),
b045b9
         NS_SUCCESS);
b045b9
@@ -376,8 +395,8 @@ ns_job_signal_cb_test(void **state)
b045b9
     /* Send the signal ... */
b045b9
     raise(SIGUSR1);
b045b9
 
b045b9
-    PR_WaitCondVar(cb_cond, PR_SecondsToInterval(1));
b045b9
-    PR_Unlock(cb_lock);
b045b9
+    assert(cond_wait_rel(&cb_cond, &cb_lock, &timeout) == 0);
b045b9
+    pthread_mutex_unlock(&cb_lock);
b045b9
 
b045b9
     assert_int_equal(cb_check, 1);
b045b9
 
b045b9
@@ -408,12 +427,11 @@ ns_job_neg_timeout_test(void **state)
b045b9
 static void
b045b9
 ns_timer_job_cb(struct ns_job_t *job)
b045b9
 {
b045b9
-    cb_check += 1;
b045b9
     ns_job_done(job);
b045b9
-    PR_Lock(cb_lock);
b045b9
-    PR_NotifyCondVar(cb_cond);
b045b9
-    /* Disarm ourselves */
b045b9
-    PR_Unlock(cb_lock);
b045b9
+    pthread_mutex_lock(&cb_lock);
b045b9
+    cb_check += 1;
b045b9
+    pthread_cond_signal(&cb_cond);
b045b9
+    pthread_mutex_unlock(&cb_lock);
b045b9
 }
b045b9
 
b045b9
 static void
b045b9
@@ -421,16 +439,19 @@ ns_job_timer_test(void **state)
b045b9
 {
b045b9
     struct ns_thrpool_t *tp = *state;
b045b9
     struct ns_job_t *job = NULL;
b045b9
-    struct timeval tv = {2, 0};
b045b9
+    struct timeval tv = {3, 0};
b045b9
+    struct timespec timeout = {2, 0};
b045b9
 
b045b9
-    PR_Lock(cb_lock);
b045b9
+    pthread_mutex_lock(&cb_lock);
b045b9
     assert_true(ns_add_timeout_job(tp, &tv, NS_JOB_THREAD, ns_timer_job_cb, NULL, &job) == NS_SUCCESS);
b045b9
 
b045b9
-    PR_WaitCondVar(cb_cond, PR_SecondsToInterval(1));
b045b9
+    cond_wait_rel(&cb_cond, &cb_lock, &timeout);
b045b9
+    // pthread_mutex_unlock(&cb_lock);
b045b9
     assert_int_equal(cb_check, 0);
b045b9
 
b045b9
-    PR_WaitCondVar(cb_cond, PR_SecondsToInterval(2));
b045b9
-    PR_Unlock(cb_lock);
b045b9
+    // pthread_mutex_lock(&cb_lock);
b045b9
+    cond_wait_rel(&cb_cond, &cb_lock, &timeout);
b045b9
+    pthread_mutex_unlock(&cb_lock);
b045b9
     assert_int_equal(cb_check, 1);
b045b9
 }
b045b9
 
b045b9
@@ -441,7 +462,9 @@ ns_job_timer_test(void **state)
b045b9
 static void
b045b9
 ns_timer_persist_job_cb(struct ns_job_t *job)
b045b9
 {
b045b9
+    pthread_mutex_lock(&cb_lock);
b045b9
     cb_check += 1;
b045b9
+    pthread_mutex_unlock(&cb_lock);
b045b9
     if (cb_check < 10) {
b045b9
         ns_job_rearm(job);
b045b9
     } else {
b045b9
@@ -456,16 +479,19 @@ ns_job_timer_persist_test(void **state)
b045b9
     struct ns_job_t *job = NULL;
b045b9
     struct timeval tv = {1, 0};
b045b9
 
b045b9
-    PR_Lock(cb_lock);
b045b9
     assert_true(ns_add_timeout_job(tp, &tv, NS_JOB_THREAD, ns_timer_persist_job_cb, NULL, &job) == NS_SUCCESS);
b045b9
 
b045b9
     PR_Sleep(PR_SecondsToInterval(5));
b045b9
 
b045b9
+    pthread_mutex_lock(&cb_lock);
b045b9
     assert_true(cb_check <= 6);
b045b9
+    pthread_mutex_unlock(&cb_lock);
b045b9
 
b045b9
     PR_Sleep(PR_SecondsToInterval(6));
b045b9
 
b045b9
+    pthread_mutex_lock(&cb_lock);
b045b9
     assert_int_equal(cb_check, 10);
b045b9
+    pthread_mutex_unlock(&cb_lock);
b045b9
 }
b045b9
 
b045b9
 int
b045b9
-- 
b045b9
2.13.6
b045b9