Index: libcfa/src/concurrency/locks.cfa
===================================================================
--- libcfa/src/concurrency/locks.cfa	(revision be73f30f9fc916c25e69cf8448092b2fe7d7d2b8)
+++ libcfa/src/concurrency/locks.cfa	(revision b5d51b0425ffb223de40252899e6cd34651705f4)
@@ -15,4 +15,5 @@
 		this.t = t;
 		this.lock = 0p;
+		this.listed = false;
 	}
 
@@ -21,4 +22,5 @@
 		this.info = info;
 		this.lock = 0p;
+		this.listed = false;
 	}
 
@@ -78,10 +80,10 @@
 	if ( owner == thrd && !multi_acquisition) {
 		fprintf(stderr, "A single acquisition lock holder attempted to reacquire the lock resulting in a deadlock."); // Possibly throw instead
-		exit(EXIT_FAILURE);
+    	exit(EXIT_FAILURE);
 	} else if ( owner != 0p && owner != thrd ) {
 		append( blocked_threads, thrd );
 		wait_count++;
 		unlock( lock );
-		park( __cfaabi_dbg_ctx );
+		park( );
 	} else if ( owner == thrd && multi_acquisition ) {
 		recursion_count++;
@@ -115,5 +117,5 @@
 		fprintf( stderr, "There was an attempt to release a lock that isn't held" );
 		return;
-	} else if ( strict_owner && owner != active_thread() ) {
+	} else if ( strict_owner && active_thread() ) {
 		fprintf( stderr, "A thread other than the owner attempted to release an owner lock" );
 		return;
@@ -125,5 +127,5 @@
 		recursion_count = ( thrd && multi_acquisition ? 1 : 0 );
 		wait_count--;
-		unpark( thrd __cfaabi_dbg_ctx2 );
+		unpark( thrd );
 	}
 	unlock( lock );
@@ -152,5 +154,8 @@
 		owner = t;
 		if ( multi_acquisition ) recursion_count = 1;
-		unpark( t __cfaabi_dbg_ctx2 );
+		#if !defined( __CFA_NO_STATISTICS__ )
+			kernelTLS.this_stats = t->curr_cluster->stats;
+		#endif
+		unpark( t );
 		unlock( lock );
 	}
@@ -161,5 +166,5 @@
 	if ( owner == 0p ){ // no owner implies lock isn't held
 		fprintf( stderr, "A lock that is not held was passed to a synchronization lock" );
-	} else if ( strict_owner && owner != active_thread() ) {
+	} else if ( strict_owner && active_thread() ) {
 		fprintf( stderr, "A thread other than the owner of a lock passed it to a synchronization lock" );
 	} else {
@@ -168,5 +173,5 @@
 		recursion_count = ( thrd && multi_acquisition ? 1 : 0 );
 		wait_count--;
-		unpark( thrd __cfaabi_dbg_ctx2 );
+		unpark( thrd );
 	}
 	unlock( lock );
@@ -177,7 +182,5 @@
 ///////////////////////////////////////////////////////////////////
 
-// In an ideal world this may not be necessary
-// Is it possible for nominal inheritance to inherit traits??
-// If that occurs we would avoid all this extra code
+// This is temporary until an inheritance bug is fixed
 
 void lock( mutex_lock & this ){
@@ -230,21 +233,38 @@
 
 ///////////////////////////////////////////////////////////////////
-//// Synchronization Locks
+//// condition variable
 ///////////////////////////////////////////////////////////////////
 
 forall(dtype L | is_blocking_lock(L)) {
-	void ?{}( synchronization_lock(L) & this, bool reacquire_after_signal ){
+
+	void timeout_handler ( alarm_node_wrap(L) & this ) with( this ) {
+    	// This condition_variable member is called from the kernel, and therefore, cannot block, but it can spin.
+	    lock( cond->lock __cfaabi_dbg_ctx2 );
+	    if ( (*i)->listed ) {			// is thread on queue
+	    	info_thread(L) * copy = *i;
+			remove( cond->blocked_threads, i );		 //remove this thread O(1)
+			cond->wait_count--;
+			if( !copy->lock ) {
+				unlock( cond->lock );
+				#if !defined( __CFA_NO_STATISTICS__ )
+					#warning unprotected access to tls TODO discuss this
+					kernelTLS.this_stats = copy->t->curr_cluster->stats;
+				#endif
+				unpark( copy->t );
+	    	} else {
+	    		add_(*copy->lock, copy->t);			// call lock's add_
+	    	}
+	    }
+	    unlock( cond->lock );
+	}
+
+	void alarm_node_wrap_cast( alarm_node_t & a ) {
+		timeout_handler( (alarm_node_wrap(L) &)a );
+	}
+
+	void ?{}( condition_variable(L) & this ){
 		this.lock{};
 		this.blocked_threads{};
 		this.count = 0;
-		this.reacquire_after_signal = reacquire_after_signal;
-	}
-
-	void ^?{}( synchronization_lock(L) & this ){
-		// default
-	}
-
-	void ?{}( condition_variable(L) & this ){
-		((synchronization_lock(L) &)this){ true };
 	}
 
@@ -253,23 +273,23 @@
 	}
 
-	void ?{}( thread_queue(L) & this ){
-		((synchronization_lock(L) &)this){ false };
-	}
-
-	void ^?{}( thread_queue(L) & this ){
+	void ?{}( alarm_node_wrap(L) & this, $thread * thrd, Time alarm, Duration period, Alarm_Callback callback ) {
+		this.alarm_node{ thrd, alarm, period, callback };
+	}
+
+	void ^?{}( alarm_node_wrap(L) & this ) {
 		// default
 	}
 
-	bool notify_one( synchronization_lock(L) & this ) with( this ) {
+	bool notify_one( condition_variable(L) & this ) with( this ) {
 		lock( lock __cfaabi_dbg_ctx2 );
 		bool ret = !!blocked_threads;
 		info_thread(L) * popped = pop_head( blocked_threads );
+		popped->listed = false;
 		if(popped != 0p) {
-			if( reacquire_after_signal ){
+			count--;
+			if (popped->lock) {
 				add_(*popped->lock, popped->t);
 			} else {
-				unpark(
-					popped->t __cfaabi_dbg_ctx2
-				);
+				unpark(popped->t);
 			}
 		}
@@ -278,16 +298,16 @@
 	}
 
-	bool notify_all( synchronization_lock(L) & this ) with(this) {
+	bool notify_all( condition_variable(L) & this ) with(this) {
 		lock( lock __cfaabi_dbg_ctx2 );
 		bool ret = blocked_threads ? true : false;
 		while( blocked_threads ) {
 			info_thread(L) * popped = pop_head( blocked_threads );
+			popped->listed = false;
 			if(popped != 0p){
-				if( reacquire_after_signal ){
+				count--;
+				if (popped->lock) {
 					add_(*popped->lock, popped->t);
 				} else {
-					unpark(
-						popped->t __cfaabi_dbg_ctx2
-					);
+					unpark(popped->t);
 				}
 			}
@@ -297,134 +317,127 @@
 	}
 
-	uintptr_t front( synchronization_lock(L) & this ) with(this) {
-		return (*peek(blocked_threads)).info;
-	}
-
-	bool empty( synchronization_lock(L) & this ) with(this) {
+	uintptr_t front( condition_variable(L) & this ) with(this) {
+		if(!blocked_threads) return NULL;
+		return peek(blocked_threads)->info;
+	}
+
+	bool empty( condition_variable(L) & this ) with(this) {
 		return blocked_threads ? false : true;
 	}
 
-	int counter( synchronization_lock(L) & this ) with(this) {
+	int counter( condition_variable(L) & this ) with(this) {
 		return count;
 	}
 
-	void queue_info_thread( synchronization_lock(L) & this, info_thread(L) & i ) with(this) {
-		lock( lock __cfaabi_dbg_ctx2 );
-		append( blocked_threads, &i );
-		count++;
-		unlock( lock );
-		park( __cfaabi_dbg_ctx );
-	}
-
-
-	void wait( synchronization_lock(L) & this ) with(this) {
-		info_thread( L ) i = { active_thread() };
-		queue_info_thread( this, i );
-	}
-
-	void wait( synchronization_lock(L) & this, uintptr_t info ) with(this) {
-		info_thread( L ) i = { active_thread(), info };
-		queue_info_thread( this, i );
-	}
-	// I still need to implement the time delay wait routines
-	bool wait( synchronization_lock(L) & this, Duration duration ) with(this) {
-		timeval tv = { time(0) };
-		Time t = { tv };
-		return wait( this, t + duration );
-	}
-
-	bool wait( synchronization_lock(L) & this, uintptr_t info, Duration duration ) with(this) {
-		// TODO: ADD INFO
-		return wait( this, duration );
-	}
-
-	bool wait( synchronization_lock(L) & this, Time time ) with(this) {
-		return false; //default
-	}
-
-	bool wait( synchronization_lock(L) & this, uintptr_t info, Time time ) with(this) {
-		// TODO: ADD INFO
-		return wait( this, time );
-	}
-
-	void queue_info_thread_unlock( synchronization_lock(L) & this, L & l, info_thread(L) & i ) with(this) {
+	// helper for wait()'s' without a timeout
+	void queue_info_thread( condition_variable(L) & this, info_thread(L) & i ) with(this) {
 		lock( lock __cfaabi_dbg_ctx2 );
 		append( this.blocked_threads, &i );
 		count++;
-		i.lock = &l;
-		size_t recursion_count = get_recursion_count(l);
-		remove_( l );
-		unlock( lock );
-		park( __cfaabi_dbg_ctx ); // blocks here
-
-		set_recursion_count(l, recursion_count); // resets recursion count here after waking
-	}
-
-	void wait( synchronization_lock(L) & this, L & l ) with(this) {
+		i.listed = true;
+		size_t recursion_count;
+		if (i.lock) {
+			recursion_count = get_recursion_count(*i.lock);
+			remove_( *i.lock );
+		}
+
+		unlock( lock );
+		park( ); // blocks here
+
+		if (i.lock) set_recursion_count(*i.lock, recursion_count); // resets recursion count here after waking
+	}
+
+	// helper for wait()'s' with a timeout
+	void queue_info_thread_timeout( condition_variable(L) & this, info_thread(L) & info, Time t ) with(this) {
+		lock( lock __cfaabi_dbg_ctx2 );
+
+		info_thread(L) * queue_ptr = &info;
+
+		alarm_node_wrap(L) node_wrap = { info.t, t, 0`s, alarm_node_wrap_cast };
+		node_wrap.cond = &this;
+		node_wrap.i = &queue_ptr;
+
+		register_self( &node_wrap.alarm_node );
+
+		append( blocked_threads, queue_ptr );
+		info.listed = true;
+		count++;
+
+		size_t recursion_count;
+		if (info.lock) {
+			recursion_count = get_recursion_count(*info.lock);
+			remove_( *info.lock );
+		}
+
+		unlock( lock );
+		park();
+
+		if (info.lock) set_recursion_count(*info.lock, recursion_count);
+	}
+
+	void wait( condition_variable(L) & this ) with(this) {
+		info_thread( L ) i = { active_thread() };
+		queue_info_thread( this, i );
+	}
+
+	void wait( condition_variable(L) & this, uintptr_t info ) with(this) {
+		info_thread( L ) i = { active_thread(), info };
+		queue_info_thread( this, i );
+	}
+
+	void wait( condition_variable(L) & this, Duration duration ) with(this) {
+		info_thread( L ) i = { active_thread() };
+		queue_info_thread_timeout(this, i, __kernel_get_time() + duration );
+	}
+
+	void wait( condition_variable(L) & this, uintptr_t info, Duration duration ) with(this) {
+		info_thread( L ) i = { active_thread(), info };
+		queue_info_thread_timeout(this, i, __kernel_get_time() + duration );
+	}
+
+	void wait( condition_variable(L) & this, Time time ) with(this) {
+		info_thread( L ) i = { active_thread() };
+		queue_info_thread_timeout(this, i, time);
+	}
+
+	void wait( condition_variable(L) & this, uintptr_t info, Time time ) with(this) {
+		info_thread( L ) i = { active_thread(), info };
+		queue_info_thread_timeout(this, i, time);
+	}
+
+	void wait( condition_variable(L) & this, L & l ) with(this) {
 		info_thread(L) i = { active_thread() };
-		queue_info_thread_unlock( this, l, i );
-	}
-
-	void wait( synchronization_lock(L) & this, L & l, uintptr_t info ) with(this) {
+		i.lock = &l;
+		queue_info_thread( this, i );
+	}
+
+	void wait( condition_variable(L) & this, L & l, uintptr_t info ) with(this) {
 		info_thread(L) i = { active_thread(), info };
-		queue_info_thread_unlock( this, l, i );
-	}
-
-	bool wait( synchronization_lock(L) & this, L & l, Duration duration ) with(this) {
-		timeval tv = { time(0) };
-		Time t = { tv };
-		return wait( this, l, t + duration );
-	}
-
-	bool wait( synchronization_lock(L) & this, L & l, uintptr_t info, Duration duration ) with(this) {
-		// TODO: ADD INFO
-		return wait( this, l, duration );
-	}
-
-	bool wait( synchronization_lock(L) & this, L & l, Time time ) with(this) {
-		return false; //default
-	}
-
-	bool wait( synchronization_lock(L) & this, L & l, uintptr_t info, Time time ) with(this) {
-		// TODO: ADD INFO
-		return wait( this, l, time );
-	}
-}
-
-///////////////////////////////////////////////////////////////////
-//// condition lock alternative approach
-///////////////////////////////////////////////////////////////////
-
-// the solution below is less efficient but does not require the lock to have a specific add/remove routine
-
-///////////////////////////////////////////////////////////////////
-//// is_simple_lock
-///////////////////////////////////////////////////////////////////
-
-forall(dtype L | is_simple_lock(L)) {
-	void ?{}( condition_lock(L) & this ){
-		// default
-	}
-
-	void ^?{}( condition_lock(L) & this ){
-		// default
-	}
-
-	bool notify_one( condition_lock(L) & this ) with(this) {
-		return notify_one( c_var );
-	}
-
-	bool notify_all( condition_lock(L) & this ) with(this) {
-		return notify_all( c_var );
-	}
-
-	void wait( condition_lock(L) & this, L & l ) with(this) {
-		lock( m_lock );
-		size_t recursion = get_recursion_count( l );
-		unlock( l );
-		wait( c_var, m_lock );
-		lock( l );
-		set_recursion_count( l , recursion );
-		unlock( m_lock );
-	}
-}
+		i.lock = &l;
+		queue_info_thread( this, i );
+	}
+
+	void wait( condition_variable(L) & this, L & l, Duration duration ) with(this) {
+		info_thread(L) i = { active_thread() };
+		i.lock = &l;
+		queue_info_thread_timeout(this, i, __kernel_get_time() + duration );
+	}
+
+	void wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration ) with(this) {
+		info_thread(L) i = { active_thread(), info };
+		i.lock = &l;
+		queue_info_thread_timeout(this, i, __kernel_get_time() + duration );
+	}
+
+	void wait( condition_variable(L) & this, L & l, Time time ) with(this) {
+		info_thread(L) i = { active_thread() };
+		i.lock = &l;
+		queue_info_thread_timeout(this, i, time );
+	}
+
+	void wait( condition_variable(L) & this, L & l, uintptr_t info, Time time ) with(this) {
+		info_thread(L) i = { active_thread(), info };
+		i.lock = &l;
+		queue_info_thread_timeout(this, i, time );
+	}
+}
