diff --git a/src/lock.h b/src/lock.h
index f79583c90fb5cfd3ab6718a2c72681c9cdd6c33f..c3377ce6bcbbb86e18c6c6194bd6ec84ac0f1232 100644
--- a/src/lock.h
+++ b/src/lock.h
@@ -45,5 +45,5 @@
         return 0;
         }
     #define lock_trylock( l ) ( ( *(l) ) ? 1 : __sync_val_compare_and_swap( l , 0 , 1 ) )
-    #define lock_unlock( l ) ( *l = 0 )
+    #define lock_unlock( l ) ( __sync_lock_test_and_set( l , 0 ) != 1 )
 #endif
diff --git a/src/runner.c b/src/runner.c
index 08ee5701d461030d4e7b9361d34736528865fc22..078007ff6fa3d3e05c50109fc8cdea4538f8ae4a 100644
--- a/src/runner.c
+++ b/src/runner.c
@@ -1371,7 +1371,8 @@ void *runner_main ( void *data ) {
             
             /* Resolve any dependencies. */
             for ( k = 0 ; k < t->nr_unlock_tasks ; k++ )
-                __sync_fetch_and_sub( &t->unlock_tasks[k]->wait , 1 );
+                if ( __sync_fetch_and_sub( &t->unlock_tasks[k]->wait , 1 ) == 0 )
+                    abort();
             for ( k = 0 ; k < t->nr_unlock_cells ; k++ )
                 __sync_fetch_and_sub( &t->unlock_cells[k]->wait , 1 );