Index: pjproject/trunk/pjlib/src/pj/os_core_unix.c
===================================================================
--- pjproject/trunk/pjlib/src/pj/os_core_unix.c	(revision 5757)
+++ pjproject/trunk/pjlib/src/pj/os_core_unix.c	(revision 5758)
@@ -880,7 +880,14 @@
 PJ_DEF(pj_status_t) pj_atomic_destroy( pj_atomic_t *atomic_var )
 {
+    pj_status_t status;
+
     PJ_ASSERT_RETURN(atomic_var, PJ_EINVAL);
-#if PJ_HAS_THREADS
-    return pj_mutex_destroy( atomic_var->mutex );
+    
+#if PJ_HAS_THREADS
+    status = pj_mutex_destroy( atomic_var->mutex );
+    if (status == PJ_SUCCESS) {
+        atomic_var->mutex = NULL;
+    }
+    return status;
 #else
     return 0;
@@ -893,4 +900,28 @@
 PJ_DEF(void) pj_atomic_set(pj_atomic_t *atomic_var, pj_atomic_value_t value)
 {
+    pj_status_t status;
+
+    PJ_CHECK_STACK();
+    PJ_ASSERT_ON_FAIL(atomic_var, return);
+
+#if PJ_HAS_THREADS
+    status = pj_mutex_lock( atomic_var->mutex );
+    if (status != PJ_SUCCESS) {
+        return;
+    }
+#endif
+    atomic_var->value = value;
+#if PJ_HAS_THREADS
+    pj_mutex_unlock( atomic_var->mutex);
+#endif
+}
+
+/*
+ * pj_atomic_get()
+ */
+PJ_DEF(pj_atomic_value_t) pj_atomic_get(pj_atomic_t *atomic_var)
+{
+    pj_atomic_value_t oldval;
+
     PJ_CHECK_STACK();
 
@@ -898,16 +929,17 @@
     pj_mutex_lock( atomic_var->mutex );
 #endif
-    atomic_var->value = value;
+    oldval = atomic_var->value;
 #if PJ_HAS_THREADS
     pj_mutex_unlock( atomic_var->mutex);
 #endif
-}
-
-/*
- * pj_atomic_get()
- */
-PJ_DEF(pj_atomic_value_t) pj_atomic_get(pj_atomic_t *atomic_var)
-{
-    pj_atomic_value_t oldval;
+    return oldval;
+}
+
+/*
+ * pj_atomic_inc_and_get()
+ */
+PJ_DEF(pj_atomic_value_t) pj_atomic_inc_and_get(pj_atomic_t *atomic_var)
+{
+    pj_atomic_value_t new_value;
 
     PJ_CHECK_STACK();
@@ -916,15 +948,24 @@
     pj_mutex_lock( atomic_var->mutex );
 #endif
-    oldval = atomic_var->value;
+    new_value = ++atomic_var->value;
 #if PJ_HAS_THREADS
     pj_mutex_unlock( atomic_var->mutex);
 #endif
-    return oldval;
-}
-
-/*
- * pj_atomic_inc_and_get()
- */
-PJ_DEF(pj_atomic_value_t) pj_atomic_inc_and_get(pj_atomic_t *atomic_var)
+
+    return new_value;
+}
+/*
+ * pj_atomic_inc()
+ */
+PJ_DEF(void) pj_atomic_inc(pj_atomic_t *atomic_var)
+{
+    PJ_ASSERT_ON_FAIL(atomic_var, return);
+    pj_atomic_inc_and_get(atomic_var);
+}
+
+/*
+ * pj_atomic_dec_and_get()
+ */
+PJ_DEF(pj_atomic_value_t) pj_atomic_dec_and_get(pj_atomic_t *atomic_var)
 {
     pj_atomic_value_t new_value;
@@ -935,5 +976,5 @@
     pj_mutex_lock( atomic_var->mutex );
 #endif
-    new_value = ++atomic_var->value;
+    new_value = --atomic_var->value;
 #if PJ_HAS_THREADS
     pj_mutex_unlock( atomic_var->mutex);
@@ -942,31 +983,4 @@
     return new_value;
 }
-/*
- * pj_atomic_inc()
- */
-PJ_DEF(void) pj_atomic_inc(pj_atomic_t *atomic_var)
-{
-    pj_atomic_inc_and_get(atomic_var);
-}
-
-/*
- * pj_atomic_dec_and_get()
- */
-PJ_DEF(pj_atomic_value_t) pj_atomic_dec_and_get(pj_atomic_t *atomic_var)
-{
-    pj_atomic_value_t new_value;
-
-    PJ_CHECK_STACK();
-
-#if PJ_HAS_THREADS
-    pj_mutex_lock( atomic_var->mutex );
-#endif
-    new_value = --atomic_var->value;
-#if PJ_HAS_THREADS
-    pj_mutex_unlock( atomic_var->mutex);
-#endif
-
-    return new_value;
-}
 
 /*
@@ -975,4 +989,5 @@
 PJ_DEF(void) pj_atomic_dec(pj_atomic_t *atomic_var)
 {
+    PJ_ASSERT_ON_FAIL(atomic_var, return);
     pj_atomic_dec_and_get(atomic_var);
 }
@@ -1006,4 +1021,5 @@
                             pj_atomic_value_t value )
 {
+    PJ_ASSERT_ON_FAIL(atomic_var, return);
     pj_atomic_add_and_get(atomic_var, value);
 }
Index: pjproject/trunk/pjlib/src/pj/os_core_win32.c
===================================================================
--- pjproject/trunk/pjlib/src/pj/os_core_win32.c	(revision 5757)
+++ pjproject/trunk/pjlib/src/pj/os_core_win32.c	(revision 5758)
@@ -751,4 +751,5 @@
 {
     PJ_CHECK_STACK();
+    PJ_ASSERT_ON_FAIL(atomic_var, return);
 
     InterlockedExchange(&atomic_var->value, value);
@@ -785,4 +786,5 @@
 PJ_DEF(void) pj_atomic_inc(pj_atomic_t *atomic_var)
 {
+    PJ_ASSERT_ON_FAIL(atomic_var, return);
     pj_atomic_inc_and_get(atomic_var);
 }
@@ -807,4 +809,5 @@
 PJ_DEF(void) pj_atomic_dec(pj_atomic_t *atomic_var)
 {
+    PJ_ASSERT_ON_FAIL(atomic_var, return);
     pj_atomic_dec_and_get(atomic_var);
 }
@@ -816,4 +819,5 @@
 			    pj_atomic_value_t value )
 {
+    PJ_ASSERT_ON_FAIL(atomic_var, return);
 #if defined(PJ_WIN32_WINNT) && PJ_WIN32_WINNT >= 0x0400
     InterlockedExchangeAdd( &atomic_var->value, value );
