Index: src/libcfa/concurrency/monitor.c
===================================================================
--- src/libcfa/concurrency/monitor.c	(revision 4aa2fb2fad2ee92eaf9a9a953a06997b44a330f0)
+++ src/libcfa/concurrency/monitor.c	(revision b227f6896f9a2f0cc65768eef401bb62ec7dc754)
@@ -19,6 +19,6 @@
 #include <stdlib>
 
+#include "libhdr.h"
 #include "kernel_private.h"
-#include "libhdr.h"
 
 //-----------------------------------------------------------------------------
@@ -44,9 +44,9 @@
 
 extern "C" {
-	void __enter_monitor_desc(monitor_desc * this) {
-		lock( &this->lock );
-		thread_desc * thrd = this_thread();
-
-		LIB_DEBUG_PRINT_SAFE("%p Entering %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion);
+	void __enter_monitor_desc( monitor_desc * this ) {
+		lock_yield( &this->lock DEBUG_CTX2 );
+		thread_desc * thrd = this_thread;
+
+		// LIB_DEBUG_PRINT_SAFE("%p Entering %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion);
 
 		if( !this->owner ) {
@@ -62,9 +62,9 @@
 			//Some one else has the monitor, wait in line for it
 			append( &this->entry_queue, thrd );
-			LIB_DEBUG_PRINT_SAFE("%p Blocking on entry\n", thrd);
-			ScheduleInternal( &this->lock );
-
-			//ScheduleInternal will unlock spinlock, no need to unlock ourselves
-			return; 
+			// LIB_DEBUG_PRINT_SAFE("%p Blocking on entry\n", thrd);
+			BlockInternal( &this->lock );
+
+			//BlockInternal will unlock spinlock, no need to unlock ourselves
+			return;
 		}
 
@@ -75,9 +75,9 @@
 	// leave pseudo code :
 	//	TODO
-	void __leave_monitor_desc(monitor_desc * this) {
-		lock( &this->lock );
-
-		LIB_DEBUG_PRINT_SAFE("%p Leaving %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion);
-		verifyf( this_thread() == this->owner, "Expected owner to be %p, got %p (r: %i)", this_thread(), this->owner, this->recursion );
+	void __leave_monitor_desc( monitor_desc * this ) {
+		lock_yield( &this->lock DEBUG_CTX2 );
+
+		// LIB_DEBUG_PRINT_SAFE("%p Leaving %p (o: %p, r: %i). ", this_thread, this, this->owner, this->recursion);
+		verifyf( this_thread == this->owner, "Expected owner to be %p, got %p (r: %i)", this_thread, this->owner, this->recursion );
 
 		//Leaving a recursion level, decrement the counter
@@ -96,8 +96,37 @@
 		unlock( &this->lock );
 
-		LIB_DEBUG_PRINT_SAFE("Next owner is %p\n", new_owner);
+		// LIB_DEBUG_PRINT_SAFE("Next owner is %p\n", new_owner);
 
 		//We need to wake-up the thread
-		ScheduleThread( new_owner );
+		WakeThread( new_owner );
+	}
+
+	void __leave_thread_monitor( thread_desc * thrd ) {
+		monitor_desc * this = &thrd->mon;
+		lock_yield( &this->lock DEBUG_CTX2 );
+
+		disable_interrupts();
+
+		thrd->cor.state = Halted;
+
+		verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i)", thrd, this->owner, this->recursion );
+
+		//Leaving a recursion level, decrement the counter
+		this->recursion -= 1;
+
+		//If we haven't left the last level of recursion
+		//it means we don't need to do anything
+		if( this->recursion != 0) {
+			unlock( &this->lock );
+			return;
+		}
+
+		thread_desc * new_owner = next_thread( this );
+
+		//We can now let other threads in safely
+		unlock( &this->lock );
+
+		//We need to wake-up the thread
+		if( new_owner) ScheduleThread( new_owner );
 	}
 }
@@ -121,9 +150,9 @@
 	enter( this->m, this->count );
 
-	this->prev_mntrs = this_thread()->current_monitors;
-	this->prev_count = this_thread()->current_monitor_count;
-
-	this_thread()->current_monitors      = m;
-	this_thread()->current_monitor_count = count;
+	this->prev_mntrs = this_thread->current_monitors;
+	this->prev_count = this_thread->current_monitor_count;
+
+	this_thread->current_monitors      = m;
+	this_thread->current_monitor_count = count;
 }
 
@@ -131,6 +160,6 @@
 	leave( this->m, this->count );
 
-	this_thread()->current_monitors      = this->prev_mntrs;
-	this_thread()->current_monitor_count = this->prev_count;
+	this_thread->current_monitors      = this->prev_mntrs;
+	this_thread->current_monitor_count = this->prev_count;
 }
 
@@ -159,5 +188,5 @@
 // Internal scheduling
 void wait( condition * this, uintptr_t user_info = 0 ) {
-	LIB_DEBUG_PRINT_SAFE("Waiting\n");
+	// LIB_DEBUG_PRINT_SAFE("Waiting\n");
 
 	brand_condition( this );
@@ -170,14 +199,14 @@
 	unsigned short count = this->monitor_count;
 	unsigned int recursions[ count ];		//Save the current recursion levels to restore them later
-	spinlock *   locks     [ count ];		//We need to pass-in an array of locks to ScheduleInternal
-
-	LIB_DEBUG_PRINT_SAFE("count %i\n", count);
-
-	__condition_node_t waiter = { this_thread(), count, user_info };
+	spinlock *   locks     [ count ];		//We need to pass-in an array of locks to BlockInternal
+
+	// LIB_DEBUG_PRINT_SAFE("count %i\n", count);
+
+	__condition_node_t waiter = { (thread_desc*)this_thread, count, user_info };
 
 	__condition_criterion_t criteria[count];
 	for(int i = 0; i < count; i++) {
 		(&criteria[i]){ this->monitors[i], &waiter };
-		LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
+		// LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
 	}
 
@@ -201,12 +230,12 @@
 	}
 
-	LIB_DEBUG_PRINT_SAFE("Will unblock: ");
+	// LIB_DEBUG_PRINT_SAFE("Will unblock: ");
 	for(int i = 0; i < thread_count; i++) {
-		LIB_DEBUG_PRINT_SAFE("%p ", threads[i]);
-	}
-	LIB_DEBUG_PRINT_SAFE("\n");
+		// LIB_DEBUG_PRINT_SAFE("%p ", threads[i]);
+	}
+	// LIB_DEBUG_PRINT_SAFE("\n");
 
 	// Everything is ready to go to sleep
-	ScheduleInternal( locks, count, threads, thread_count );
+	BlockInternal( locks, count, threads, thread_count );
 
 
@@ -222,5 +251,5 @@
 bool signal( condition * this ) {
 	if( is_empty( this ) ) {
-		LIB_DEBUG_PRINT_SAFE("Nothing to signal\n");
+		// LIB_DEBUG_PRINT_SAFE("Nothing to signal\n");
 		return false;
 	}
@@ -231,8 +260,8 @@
 
 	unsigned short count = this->monitor_count;
-	
+
 	//Some more checking in debug
 	LIB_DEBUG_DO(
-		thread_desc * this_thrd = this_thread();
+		thread_desc * this_thrd = this_thread;
 		if ( this->monitor_count != this_thrd->current_monitor_count ) {
 			abortf( "Signal on condition %p made with different number of monitor(s), expected %i got %i", this, this->monitor_count, this_thrd->current_monitor_count );
@@ -248,5 +277,5 @@
 	//Lock all the monitors
 	lock_all( this->monitors, NULL, count );
-	LIB_DEBUG_PRINT_SAFE("Signalling");
+	// LIB_DEBUG_PRINT_SAFE("Signalling");
 
 	//Pop the head of the waiting queue
@@ -256,10 +285,10 @@
 	for(int i = 0; i < count; i++) {
 		__condition_criterion_t * crit = &node->criteria[i];
-		LIB_DEBUG_PRINT_SAFE(" %p", crit->target);
+		// LIB_DEBUG_PRINT_SAFE(" %p", crit->target);
 		assert( !crit->ready );
 		push( &crit->target->signal_stack, crit );
 	}
 
-	LIB_DEBUG_PRINT_SAFE("\n");
+	// LIB_DEBUG_PRINT_SAFE("\n");
 
 	//Release
@@ -281,15 +310,15 @@
 	unsigned short count = this->monitor_count;
 	unsigned int recursions[ count ];		//Save the current recursion levels to restore them later
-	spinlock *   locks     [ count ];		//We need to pass-in an array of locks to ScheduleInternal
+	spinlock *   locks     [ count ];		//We need to pass-in an array of locks to BlockInternal
 
 	lock_all( this->monitors, locks, count );
 
 	//create creteria
-	__condition_node_t waiter = { this_thread(), count, 0 };
+	__condition_node_t waiter = { (thread_desc*)this_thread, count, 0 };
 
 	__condition_criterion_t criteria[count];
 	for(int i = 0; i < count; i++) {
 		(&criteria[i]){ this->monitors[i], &waiter };
-		LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
+		// LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
 		push( &criteria[i].target->signal_stack, &criteria[i] );
 	}
@@ -309,5 +338,5 @@
 
 	//Everything is ready to go to sleep
-	ScheduleInternal( locks, count, &signallee, 1 );
+	BlockInternal( locks, count, &signallee, 1 );
 
 
@@ -325,5 +354,5 @@
 
 uintptr_t front( condition * this ) {
-	verifyf( !is_empty(this), 
+	verifyf( !is_empty(this),
 		"Attempt to access user data on an empty condition.\n"
 		"Possible cause is not checking if the condition is empty before reading stored data."
@@ -335,9 +364,9 @@
 // Internal scheduling
 void __accept_internal( unsigned short count, __acceptable_t * acceptables, void (*func)(void) ) {
-	// thread_desc * this = this_thread();
+	// thread_desc * this = this_thread;
 
 	// unsigned short count = this->current_monitor_count;
 	// unsigned int recursions[ count ];		//Save the current recursion levels to restore them later
-	// spinlock *   locks     [ count ];		//We need to pass-in an array of locks to ScheduleInternal
+	// spinlock *   locks     [ count ];		//We need to pass-in an array of locks to BlockInternal
 
 	// lock_all( this->current_monitors, locks, count );
@@ -348,5 +377,5 @@
 
 	// // // Everything is ready to go to sleep
-	// // ScheduleInternal( locks, count, threads, thread_count );
+	// // BlockInternal( locks, count, threads, thread_count );
 
 
@@ -393,5 +422,5 @@
 static inline void lock_all( spinlock ** locks, unsigned short count ) {
 	for( int i = 0; i < count; i++ ) {
-		lock( locks[i] );
+		lock_yield( locks[i] DEBUG_CTX2 );
 	}
 }
@@ -400,5 +429,5 @@
 	for( int i = 0; i < count; i++ ) {
 		spinlock * l = &source[i]->lock;
-		lock( l );
+		lock_yield( l DEBUG_CTX2 );
 		if(locks) locks[i] = l;
 	}
@@ -443,8 +472,8 @@
 	for(	int i = 0; i < count; i++ ) {
 
-		LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target );
+		// LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target );
 		if( &criteria[i] == target ) {
 			criteria[i].ready = true;
-			LIB_DEBUG_PRINT_SAFE( "True\n" );
+			// LIB_DEBUG_PRINT_SAFE( "True\n" );
 		}
 
@@ -452,12 +481,12 @@
 	}
 
-	LIB_DEBUG_PRINT_SAFE( "Runing %i\n", ready2run );
+	// LIB_DEBUG_PRINT_SAFE( "Runing %i\n", ready2run );
 	return ready2run ? node->waiting_thread : NULL;
 }
 
 static inline void brand_condition( condition * this ) {
-	thread_desc * thrd = this_thread();
+	thread_desc * thrd = this_thread;
 	if( !this->monitors ) {
-		LIB_DEBUG_PRINT_SAFE("Branding\n");
+		// LIB_DEBUG_PRINT_SAFE("Branding\n");
 		assertf( thrd->current_monitors != NULL, "No current monitor to brand condition", thrd->current_monitors );
 		this->monitor_count = thrd->current_monitor_count;
