amoralej / rpms / 389-ds-base

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

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

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