andykimpe / rpms / 389-ds-base

Forked from rpms/389-ds-base 5 months ago
Clone

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

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