Index: src/libcfa/concurrency/alarm.c
===================================================================
--- src/libcfa/concurrency/alarm.c	(revision aa3d77b01f0bccec37e2709bdd14bd335e4d24ee)
+++ src/libcfa/concurrency/alarm.c	(revision 1c273d04b68cc57e237ec5bb3b198fd7b17ebde3)
@@ -37,20 +37,10 @@
 	clock_gettime( CLOCK_REALTIME, &curr );
 	__cfa_time_t curr_time = ((__cfa_time_t)curr.tv_sec * TIMEGRAN) + curr.tv_nsec;
-	LIB_DEBUG_DO(
-		char text[256];
-		__attribute__((unused)) int len = snprintf( text, 256, "Kernel : current time is %lu\n", curr_time );
-		LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
-	);
+	LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : current time is %lu\n", curr_time );
 	return curr_time;
 }
 
 void __kernel_set_timer( __cfa_time_t alarm ) {
-
-	LIB_DEBUG_DO(
-		char text[256];
-		__attribute__((unused)) int len = snprintf( text, 256, "Kernel : set timer to %lu\n", (__cfa_time_t)alarm );
-		LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
-	);
-
+	LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : set timer to %lu\n", (__cfa_time_t)alarm );
 	itimerval val;
 	val.it_value.tv_sec = alarm / TIMEGRAN;			// seconds
@@ -164,5 +154,5 @@
 	disable_interrupts();
 	verify( !systemProcessor->pending_alarm );
-	lock( &systemProcessor->alarm_lock );
+	lock( &systemProcessor->alarm_lock, __PRETTY_FUNCTION__ );
 	{
 		verify( validate( &systemProcessor->alarms ) );
@@ -183,11 +173,7 @@
 
 void unregister_self( alarm_node_t * this ) {
-	LIB_DEBUG_DO(
-		char text[256];
-		__attribute__((unused)) int len = snprintf( text, 256, "Kernel : unregister %p start\n", this );
-		LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
-	);
+	// LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : unregister %p start\n", this );
 	disable_interrupts();
-	lock( &systemProcessor->alarm_lock );
+	lock( &systemProcessor->alarm_lock, __PRETTY_FUNCTION__ );
 	{
 		verify( validate( &systemProcessor->alarms ) );
@@ -197,7 +183,4 @@
 	disable_interrupts();
 	this->set = false;
-	LIB_DEBUG_DO(
-		len = snprintf( text, 256, "Kernel : unregister %p end\n", this );
-		LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
-	);
+	// LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Kernel : unregister %p end\n", this );
 }
Index: src/libcfa/concurrency/coroutine
===================================================================
--- src/libcfa/concurrency/coroutine	(revision aa3d77b01f0bccec37e2709bdd14bd335e4d24ee)
+++ src/libcfa/concurrency/coroutine	(revision 1c273d04b68cc57e237ec5bb3b198fd7b17ebde3)
@@ -63,5 +63,5 @@
 
 // Get current coroutine
-coroutine_desc * this_coroutine(void);
+extern volatile thread_local coroutine_desc * this_coroutine;
 
 // Private wrappers for context switch and stack creation
@@ -71,5 +71,5 @@
 // Suspend implementation inlined for performance
 static inline void suspend() {
-	coroutine_desc * src = this_coroutine();		// optimization
+	coroutine_desc * src = this_coroutine;		// optimization
 
 	assertf( src->last != 0,
@@ -88,5 +88,5 @@
 forall(dtype T | is_coroutine(T))
 static inline void resume(T * cor) {
-	coroutine_desc * src = this_coroutine();		// optimization
+	coroutine_desc * src = this_coroutine;		// optimization
 	coroutine_desc * dst = get_coroutine(cor);
 
@@ -112,5 +112,5 @@
 
 static inline void resume(coroutine_desc * dst) {
-	coroutine_desc * src = this_coroutine();		// optimization
+	coroutine_desc * src = this_coroutine;		// optimization
 
 	// not resuming self ?
Index: src/libcfa/concurrency/coroutine.c
===================================================================
--- src/libcfa/concurrency/coroutine.c	(revision aa3d77b01f0bccec37e2709bdd14bd335e4d24ee)
+++ src/libcfa/concurrency/coroutine.c	(revision 1c273d04b68cc57e237ec5bb3b198fd7b17ebde3)
@@ -106,10 +106,11 @@
 
 	// set state of current coroutine to inactive
-	src->state = Inactive;
+	src->state = src->state == Halted ? Halted : Inactive;
 
 	// set new coroutine that task is executing
-	this_processor->current_coroutine = dst;
+	this_coroutine = dst;
 
 	// context switch to specified coroutine
+	assert( src->stack.context );
 	CtxSwitch( src->stack.context, dst->stack.context );
 	// when CtxSwitch returns we are back in the src coroutine		
Index: src/libcfa/concurrency/invoke.c
===================================================================
--- src/libcfa/concurrency/invoke.c	(revision aa3d77b01f0bccec37e2709bdd14bd335e4d24ee)
+++ src/libcfa/concurrency/invoke.c	(revision 1c273d04b68cc57e237ec5bb3b198fd7b17ebde3)
@@ -29,11 +29,11 @@
 
 extern void __suspend_internal(void);
-extern void __leave_monitor_desc( struct monitor_desc * this );
+extern void __leave_thread_monitor( struct thread_desc * this );
 extern void disable_interrupts();
 extern void enable_interrupts( const char * );
 
 void CtxInvokeCoroutine(
-      void (*main)(void *), 
-      struct coroutine_desc *(*get_coroutine)(void *), 
+      void (*main)(void *),
+      struct coroutine_desc *(*get_coroutine)(void *),
       void *this
 ) {
@@ -58,22 +58,28 @@
 
 void CtxInvokeThread(
-      void (*dtor)(void *), 
-      void (*main)(void *), 
-      struct thread_desc *(*get_thread)(void *), 
+      void (*dtor)(void *),
+      void (*main)(void *),
+      struct thread_desc *(*get_thread)(void *),
       void *this
 ) {
+      // First suspend, once the thread arrives here,
+      // the function pointer to main can be invalidated without risk
       __suspend_internal();
 
+      // Fetch the thread handle from the user defined thread structure
       struct thread_desc* thrd = get_thread( this );
-      struct coroutine_desc* cor = &thrd->cor;
-      struct monitor_desc* mon = &thrd->mon;
-      cor->state = Active;
+
+      // Officially start the thread by enabling preemption
       enable_interrupts( __PRETTY_FUNCTION__ );
 
-      // LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this);
+      // Call the main of the thread
       main( this );
 
-      disable_interrupts();
-      __leave_monitor_desc( mon );
+      // To exit a thread we must :
+      // 1 - Mark it as halted
+      // 2 - Leave its monitor
+      // 3 - Disable the interupts
+      // The order of these 3 operations is very important
+      __leave_thread_monitor( thrd );
 
       //Final suspend, should never return
@@ -84,7 +90,7 @@
 
 void CtxStart(
-      void (*main)(void *), 
-      struct coroutine_desc *(*get_coroutine)(void *), 
-      void *this, 
+      void (*main)(void *),
+      struct coroutine_desc *(*get_coroutine)(void *),
+      void *this,
       void (*invoke)(void *)
 ) {
@@ -112,5 +118,5 @@
 	((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
       ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
-      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7 
+      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
 
 #elif defined( __x86_64__ )
@@ -132,5 +138,5 @@
       ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
       ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
-      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7 
+      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
 #else
       #error Only __i386__ and __x86_64__ is supported for threads in cfa
Index: src/libcfa/concurrency/invoke.h
===================================================================
--- src/libcfa/concurrency/invoke.h	(revision aa3d77b01f0bccec37e2709bdd14bd335e4d24ee)
+++ src/libcfa/concurrency/invoke.h	(revision 1c273d04b68cc57e237ec5bb3b198fd7b17ebde3)
@@ -31,4 +31,7 @@
       struct spinlock {
             volatile int lock;
+            #ifdef __CFA_DEBUG__
+                  const char * prev;
+            #endif
       };
 
@@ -83,5 +86,4 @@
             struct __thread_queue_t entry_queue;      // queue of threads that are blocked waiting for the monitor
             struct __condition_stack_t signal_stack;  // stack of conditions to run next once we exit the monitor
-            struct monitor_desc * stack_owner;        // if bulk acquiring was used we need to synchronize signals with an other monitor
             unsigned int recursion;                   // monitor routines can be called recursively, we need to keep track of that
       };
Index: src/libcfa/concurrency/kernel
===================================================================
--- src/libcfa/concurrency/kernel	(revision aa3d77b01f0bccec37e2709bdd14bd335e4d24ee)
+++ src/libcfa/concurrency/kernel	(revision 1c273d04b68cc57e237ec5bb3b198fd7b17ebde3)
@@ -28,6 +28,16 @@
 //-----------------------------------------------------------------------------
 // Locks
-bool try_lock( spinlock * );
-void lock( spinlock * );
+bool try_lock( spinlock *
+	#ifdef __CFA_DEBUG__
+		, const char * caller
+	#endif
+);
+
+void lock( spinlock *
+	#ifdef __CFA_DEBUG__
+		, const char * caller
+	#endif
+);
+
 void unlock( spinlock * );
 
@@ -78,6 +88,4 @@
 	struct processorCtx_t * runner;
 	cluster * cltr;
-	coroutine_desc * current_coroutine;
-	thread_desc * current_thread;
 	pthread_t kernel_thread;
 	
Index: src/libcfa/concurrency/kernel.c
===================================================================
--- src/libcfa/concurrency/kernel.c	(revision aa3d77b01f0bccec37e2709bdd14bd335e4d24ee)
+++ src/libcfa/concurrency/kernel.c	(revision 1c273d04b68cc57e237ec5bb3b198fd7b17ebde3)
@@ -60,18 +60,12 @@
 
 volatile thread_local processor * this_processor;
-volatile thread_local unsigned short disable_preempt_count;
-
-coroutine_desc * this_coroutine(void) {
-	return this_processor->current_coroutine;
-}
-
-thread_desc * this_thread(void) {
-	return this_processor->current_thread;
-}
+volatile thread_local coroutine_desc * this_coroutine;
+volatile thread_local thread_desc * this_thread;
+volatile thread_local unsigned short disable_preempt_count = 1;
 
 //-----------------------------------------------------------------------------
 // Main thread construction
 struct current_stack_info_t {
-	machine_context_t ctx;	
+	machine_context_t ctx;
 	unsigned int size;		// size of stack
 	void *base;				// base of stack
@@ -107,5 +101,5 @@
 
 void ?{}( coroutine_desc * this, current_stack_info_t * info) {
-	(&this->stack){ info };	
+	(&this->stack){ info };
 	this->name = "Main Thread";
 	this->errno_ = 0;
@@ -137,6 +131,4 @@
 void ?{}(processor * this, cluster * cltr) {
 	this->cltr = cltr;
-	this->current_coroutine = NULL;
-	this->current_thread = NULL;
 	(&this->terminated){};
 	this->is_terminated = false;
@@ -150,6 +142,4 @@
 void ?{}(processor * this, cluster * cltr, processorCtx_t * runner) {
 	this->cltr = cltr;
-	this->current_coroutine = NULL;
-	this->current_thread = NULL;
 	(&this->terminated){};
 	this->is_terminated = false;
@@ -190,5 +180,5 @@
 
 void ^?{}(cluster * this) {
-	
+
 }
 
@@ -209,5 +199,5 @@
 
 		thread_desc * readyThread = NULL;
-		for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ ) 
+		for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ )
 		{
 			readyThread = nextThread( this->cltr );
@@ -239,15 +229,15 @@
 }
 
-// runThread runs a thread by context switching 
-// from the processor coroutine to the target thread 
+// runThread runs a thread by context switching
+// from the processor coroutine to the target thread
 void runThread(processor * this, thread_desc * dst) {
 	coroutine_desc * proc_cor = get_coroutine(this->runner);
 	coroutine_desc * thrd_cor = get_coroutine(dst);
-	
+
 	//Reset the terminating actions here
 	this->finish.action_code = No_Action;
 
 	//Update global state
-	this->current_thread = dst;
+	this_thread = dst;
 
 	// Context Switch to the thread
@@ -256,5 +246,5 @@
 }
 
-// Once a thread has finished running, some of 
+// Once a thread has finished running, some of
 // its final actions must be executed from the kernel
 void finishRunning(processor * this) {
@@ -266,5 +256,5 @@
 	}
 	else if( this->finish.action_code == Release_Schedule ) {
-		unlock( this->finish.lock );		
+		unlock( this->finish.lock );
 		ScheduleThread( this->finish.thrd );
 	}
@@ -299,8 +289,10 @@
 	processor * proc = (processor *) arg;
 	this_processor = proc;
+	this_coroutine = NULL;
+	this_thread = NULL;
 	disable_preempt_count = 1;
 	// SKULLDUGGERY: We want to create a context for the processor coroutine
 	// which is needed for the 2-step context switch. However, there is no reason
-	// to waste the perfectly valid stack create by pthread. 
+	// to waste the perfectly valid stack create by pthread.
 	current_stack_info_t info;
 	machine_context_t ctx;
@@ -311,13 +303,13 @@
 
 	//Set global state
-	proc->current_coroutine = &proc->runner->__cor;
-	proc->current_thread = NULL;
+	this_coroutine = &proc->runner->__cor;
+	this_thread = NULL;
 
 	//We now have a proper context from which to schedule threads
 	LIB_DEBUG_PRINT_SAFE("Kernel : core %p created (%p, %p)\n", proc, proc->runner, &ctx);
 
-	// SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't 
-	// resume it to start it like it normally would, it will just context switch 
-	// back to here. Instead directly call the main since we already are on the 
+	// SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
+	// resume it to start it like it normally would, it will just context switch
+	// back to here. Instead directly call the main since we already are on the
 	// appropriate stack.
 	proc_cor_storage.__cor.state = Active;
@@ -326,5 +318,5 @@
 
 	// Main routine of the core returned, the core is now fully terminated
-	LIB_DEBUG_PRINT_SAFE("Kernel : core %p main ended (%p)\n", proc, proc->runner);	
+	LIB_DEBUG_PRINT_SAFE("Kernel : core %p main ended (%p)\n", proc, proc->runner);
 
 	return NULL;
@@ -363,5 +355,5 @@
 	} // if
 
-	LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);	
+	LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);
 }
 
@@ -369,17 +361,25 @@
 // Scheduler routines
 void ScheduleThread( thread_desc * thrd ) {
-	if( !thrd ) return;
+	// if( !thrd ) return;
+	assert( thrd );
+	assert( thrd->cor.state != Halted );
+
+	verify( disable_preempt_count > 0 );
 
 	verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
-	
-	lock( &systemProcessor->proc.cltr->lock );
+
+	lock( &systemProcessor->proc.cltr->lock, __PRETTY_FUNCTION__ );
 	append( &systemProcessor->proc.cltr->ready_queue, thrd );
 	unlock( &systemProcessor->proc.cltr->lock );
+
+	verify( disable_preempt_count > 0 );
 }
 
 thread_desc * nextThread(cluster * this) {
-	lock( &this->lock );
+	verify( disable_preempt_count > 0 );
+	lock( &this->lock, __PRETTY_FUNCTION__ );
 	thread_desc * head = pop_head( &this->ready_queue );
 	unlock( &this->lock );
+	verify( disable_preempt_count > 0 );
 	return head;
 }
@@ -407,4 +407,5 @@
 void BlockInternal( thread_desc * thrd ) {
 	disable_interrupts();
+	assert( thrd->cor.state != Halted );
 	this_processor->finish.action_code = Schedule;
 	this_processor->finish.thrd = thrd;
@@ -464,8 +465,8 @@
 // Kernel boot procedures
 void kernel_startup(void) {
-	LIB_DEBUG_PRINT_SAFE("Kernel : Starting\n");	
+	LIB_DEBUG_PRINT_SAFE("Kernel : Starting\n");
 
 	// Start by initializing the main thread
-	// SKULLDUGGERY: the mainThread steals the process main thread 
+	// SKULLDUGGERY: the mainThread steals the process main thread
 	// which will then be scheduled by the systemProcessor normally
 	mainThread = (thread_desc *)&mainThread_storage;
@@ -486,5 +487,5 @@
 	systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtx_storage };
 
-	// Add the main thread to the ready queue 
+	// Add the main thread to the ready queue
 	// once resume is called on systemProcessor->runner the mainThread needs to be scheduled like any normal thread
 	ScheduleThread(mainThread);
@@ -492,6 +493,6 @@
 	//initialize the global state variables
 	this_processor = &systemProcessor->proc;
-	this_processor->current_thread = mainThread;
-	this_processor->current_coroutine = &mainThread->cor;
+	this_thread = mainThread;
+	this_coroutine = &mainThread->cor;
 	disable_preempt_count = 1;
 
@@ -501,5 +502,5 @@
 	// SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX
 	// context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
-	// mainThread is on the ready queue when this call is made. 
+	// mainThread is on the ready queue when this call is made.
 	resume( systemProcessor->proc.runner );
 
@@ -537,5 +538,5 @@
 	^(mainThread){};
 
-	LIB_DEBUG_PRINT_SAFE("Kernel : Shutdown complete\n");	
+	LIB_DEBUG_PRINT_SAFE("Kernel : Shutdown complete\n");
 }
 
@@ -547,5 +548,5 @@
 	// abort cannot be recursively entered by the same or different processors because all signal handlers return when
 	// the globalAbort flag is true.
-	lock( &kernel_abort_lock );
+	lock( &kernel_abort_lock, __PRETTY_FUNCTION__ );
 
 	// first task to abort ?
@@ -553,8 +554,8 @@
 		kernel_abort_called = true;
 		unlock( &kernel_abort_lock );
-	} 
+	}
 	else {
 		unlock( &kernel_abort_lock );
-		
+
 		sigset_t mask;
 		sigemptyset( &mask );
@@ -562,8 +563,8 @@
 		sigaddset( &mask, SIGUSR1 );			// block SIGUSR1 signals
 		sigsuspend( &mask );				// block the processor to prevent further damage during abort
-		_exit( EXIT_FAILURE );				// if processor unblocks before it is killed, terminate it		
-	}
-
-	return this_thread();
+		_exit( EXIT_FAILURE );				// if processor unblocks before it is killed, terminate it
+	}
+
+	return this_thread;
 }
 
@@ -574,8 +575,8 @@
 	__lib_debug_write( STDERR_FILENO, abort_text, len );
 
-	if ( thrd != this_coroutine() ) {
-		len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", this_coroutine()->name, this_coroutine() );
+	if ( thrd != this_coroutine ) {
+		len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", this_coroutine->name, this_coroutine );
 		__lib_debug_write( STDERR_FILENO, abort_text, len );
-	} 
+	}
 	else {
 		__lib_debug_write( STDERR_FILENO, ".\n", 2 );
@@ -585,5 +586,5 @@
 extern "C" {
 	void __lib_debug_acquire() {
-		lock(&kernel_debug_lock);
+		lock(&kernel_debug_lock, __PRETTY_FUNCTION__);
 	}
 
@@ -605,12 +606,15 @@
 }
 
-bool try_lock( spinlock * this ) {
-	return this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0;
-}
-
-void lock( spinlock * this ) {
+bool try_lock( spinlock * this, const char * caller ) {
+	bool ret = this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0;
+	this->prev = caller;
+	return ret;
+}
+
+void lock( spinlock * this, const char * caller ) {
 	for ( unsigned int i = 1;; i += 1 ) {
 	  	if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) break;
 	}
+	this->prev = caller;
 }
 
@@ -627,7 +631,7 @@
 
 void wait( signal_once * this ) {
-	lock( &this->lock );
+	lock( &this->lock, __PRETTY_FUNCTION__ );
 	if( !this->cond ) {
-		append( &this->blocked, this_thread() );
+		append( &this->blocked, (thread_desc*)this_thread );
 		BlockInternal( &this->lock );
 	}
@@ -638,5 +642,5 @@
 
 void signal( signal_once * this ) {
-	lock( &this->lock );
+	lock( &this->lock, __PRETTY_FUNCTION__ );
 	{
 		this->cond = true;
@@ -673,5 +677,5 @@
 		}
 		head->next = NULL;
-	}	
+	}
 	return head;
 }
@@ -692,5 +696,5 @@
 		this->top = top->next;
 		top->next = NULL;
-	}	
+	}
 	return top;
 }
Index: src/libcfa/concurrency/kernel_private.h
===================================================================
--- src/libcfa/concurrency/kernel_private.h	(revision aa3d77b01f0bccec37e2709bdd14bd335e4d24ee)
+++ src/libcfa/concurrency/kernel_private.h	(revision 1c273d04b68cc57e237ec5bb3b198fd7b17ebde3)
@@ -27,5 +27,19 @@
 //-----------------------------------------------------------------------------
 // Scheduler
+
+extern "C" {
+	void disable_interrupts();
+	void enable_interrupts_noRF();
+	void enable_interrupts( const char * );
+}
+
 void ScheduleThread( thread_desc * );
+static inline void WakeThread( thread_desc * thrd ) {
+	if( !thrd ) return;
+
+	disable_interrupts();
+	ScheduleThread( thrd );
+	enable_interrupts( __PRETTY_FUNCTION__ );
+}
 thread_desc * nextThread(cluster * this);
 
@@ -61,11 +75,7 @@
 extern system_proc_t * systemProcessor;
 extern volatile thread_local processor * this_processor;
+extern volatile thread_local coroutine_desc * this_coroutine;
+extern volatile thread_local thread_desc * this_thread;
 extern volatile thread_local unsigned short disable_preempt_count;
-
-extern "C" {
-	void disable_interrupts();
-	void enable_interrupts_noRF();
-	void enable_interrupts( const char * );
-}
 
 //-----------------------------------------------------------------------------
Index: src/libcfa/concurrency/monitor
===================================================================
--- src/libcfa/concurrency/monitor	(revision aa3d77b01f0bccec37e2709bdd14bd335e4d24ee)
+++ src/libcfa/concurrency/monitor	(revision 1c273d04b68cc57e237ec5bb3b198fd7b17ebde3)
@@ -26,5 +26,4 @@
 static inline void ?{}(monitor_desc * this) {
 	this->owner = NULL;
-	this->stack_owner = NULL;
 	this->recursion = 0;
 }
Index: src/libcfa/concurrency/monitor.c
===================================================================
--- src/libcfa/concurrency/monitor.c	(revision aa3d77b01f0bccec37e2709bdd14bd335e4d24ee)
+++ src/libcfa/concurrency/monitor.c	(revision 1c273d04b68cc57e237ec5bb3b198fd7b17ebde3)
@@ -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( &this->lock, __PRETTY_FUNCTION__ );
+		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,5 +62,5 @@
 			//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);
+			// LIB_DEBUG_PRINT_SAFE("%p Blocking on entry\n", thrd);
 			BlockInternal( &this->lock );
 
@@ -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( &this->lock, __PRETTY_FUNCTION__ );
+
+		// 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( &this->lock, __PRETTY_FUNCTION__ );
+
+		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;
 }
 
@@ -174,5 +203,5 @@
 	LIB_DEBUG_PRINT_SAFE("count %i\n", count);
 
-	__condition_node_t waiter = { this_thread(), count, user_info };
+	__condition_node_t waiter = { (thread_desc*)this_thread, count, user_info };
 
 	__condition_criterion_t criteria[count];
@@ -234,5 +263,5 @@
 	//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 );
@@ -286,5 +315,5 @@
 
 	//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];
@@ -335,5 +364,5 @@
 // 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;
@@ -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( locks[i], __PRETTY_FUNCTION__ );
 	}
 }
@@ -400,5 +429,5 @@
 	for( int i = 0; i < count; i++ ) {
 		spinlock * l = &source[i]->lock;
-		lock( l );
+		lock( l, __PRETTY_FUNCTION__ );
 		if(locks) locks[i] = l;
 	}
@@ -457,5 +486,5 @@
 
 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");
Index: src/libcfa/concurrency/preemption.c
===================================================================
--- src/libcfa/concurrency/preemption.c	(revision aa3d77b01f0bccec37e2709bdd14bd335e4d24ee)
+++ src/libcfa/concurrency/preemption.c	(revision 1c273d04b68cc57e237ec5bb3b198fd7b17ebde3)
@@ -20,4 +20,5 @@
 extern "C" {
 #include <errno.h>
+#include <execinfo.h>
 #define __USE_GNU
 #include <signal.h>
@@ -28,4 +29,8 @@
 }
 
+
+#ifdef __USE_STREAM__
+#include "fstream"
+#endif
 #include "libhdr.h"
 
@@ -44,4 +49,6 @@
 void sigHandler_ctxSwitch( __CFA_SIGPARMS__ );
 void sigHandler_alarm    ( __CFA_SIGPARMS__ );
+void sigHandler_segv     ( __CFA_SIGPARMS__ );
+void sigHandler_abort    ( __CFA_SIGPARMS__ );
 
 static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags );
@@ -55,4 +62,7 @@
 	__kernel_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO );
 	__kernel_sigaction( SIGALRM, sigHandler_alarm    , SA_SIGINFO );
+	__kernel_sigaction( SIGSEGV, sigHandler_segv     , SA_SIGINFO );
+	__kernel_sigaction( SIGBUS , sigHandler_segv     , SA_SIGINFO );
+	// __kernel_sigaction( SIGABRT, sigHandler_abort    , SA_SIGINFO );
 }
 
@@ -64,6 +74,6 @@
 	LIB_DEBUG_PRINT_SAFE("Kernel : Preemption stopped\n");
 
-	assert( !systemProcessor->alarms.head );
-	assert( systemProcessor->alarms.tail == &systemProcessor->alarms.head );
+	// assert( !systemProcessor->alarms.head );
+	// assert( systemProcessor->alarms.tail == &systemProcessor->alarms.head );
 }
 
@@ -71,9 +81,5 @@
 
 void tick_preemption() {
-	LIB_DEBUG_DO(
-		char text[256];
-		__attribute__((unused)) int len = snprintf( text, 256, "Ticking preemption\n" );
-		LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
-	);
+	// LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Ticking preemption\n" );
 
 	alarm_list_t * alarms = &systemProcessor->alarms;
@@ -81,8 +87,6 @@
 	while( alarms->head && alarms->head->alarm < currtime ) {
 		alarm_node_t * node = pop(alarms);
-		LIB_DEBUG_DO(
-			len = snprintf( text, 256, "Ticking %p\n", node );
-			LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
-		);
+		// LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ticking %p\n", node );
+
 		if( node->kernel_alarm ) {
 			preempt( node->proc );
@@ -92,5 +96,5 @@
 		}
 
-		LIB_DEBUG_DO( assert( validate( alarms ) ) );
+		verify( validate( alarms ) );
 
 		if( node->period > 0 ) {
@@ -108,16 +112,9 @@
 
 	verify( validate( alarms ) );
-	LIB_DEBUG_DO(
-		len = snprintf( text, 256, "Ticking preemption done\n" );
-		LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
-	);
+	// LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ticking preemption done\n" );
 }
 
 void update_preemption( processor * this, __cfa_time_t duration ) {
-	LIB_DEBUG_DO(
-		char text[256];
-		__attribute__((unused)) int len = snprintf( text, 256, "Processor : %p updating preemption to %lu\n", this, duration );
-		LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
-	);
+	LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Processor : %p updating preemption to %lu\n", this, duration );
 
 	alarm_node_t * alarm = this->preemption_alarm;
@@ -175,26 +172,25 @@
 
 	void enable_interrupts( const char * func ) {
+		processor * proc   = this_processor;
+		thread_desc * thrd = this_thread;
 		unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST );
 		verify( prev != (unsigned short) 0 );
-		if( prev == 1 && this_processor->pending_preemption ) {
-			this_processor->pending_preemption = false;
-			LIB_DEBUG_DO(
-				char text[256];
-				__attribute__((unused)) int len = snprintf( text, 256, "Executing deferred CtxSwitch on %p\n", this_processor );
-				LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
-			);
-			BlockInternal( this_processor->current_thread );
-		}
-
-		this_processor->last_enable = func;
-	}
-}
-
-static inline void signal_unblock( bool alarm ) {
+		if( prev == 1 && proc->pending_preemption ) {
+			proc->pending_preemption = false;
+			LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Executing deferred CtxSwitch on %p\n", this_processor );
+			BlockInternal( thrd );
+			LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Executing deferred back\n" );
+		}
+
+		proc->last_enable = func;
+	}
+}
+
+static inline void signal_unblock( int sig ) {
+	LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Processor : %p unblocking sig %i\n", this_processor, sig );
+
 	sigset_t mask;
 	sigemptyset( &mask );
-	sigaddset( &mask, SIGUSR1 );
-
-	if( alarm ) sigaddset( &mask, SIGALRM );
+	sigaddset( &mask, sig );
 
 	if ( sigprocmask( SIG_UNBLOCK, &mask, NULL ) == -1 ) {
@@ -215,39 +211,32 @@
 }
 
+extern "C" {
+	__attribute__((noinline)) void __debug_break() {
+		pthread_kill( pthread_self(), SIGTRAP );
+	}
+}
+
 void sigHandler_ctxSwitch( __CFA_SIGPARMS__ ) {
-
-	LIB_DEBUG_DO(
-		char text[256];
-		__attribute__((unused)) int len = snprintf( text, 256, "Ctx Switch IRH %p\n", (void *)(cxt->uc_mcontext.gregs[REG_RIP]));
-		LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
-	);
-
-	signal_unblock( false );
+	LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Ctx Switch IRH %p running %p @ %p\n", this_processor, this_thread, (void *)(cxt->uc_mcontext.gregs[REG_RIP]) );
+
 	if( preemption_ready() ) {
-		LIB_DEBUG_DO(
-			len = snprintf( text, 256, "Ctx Switch IRH : Blocking thread %p on %p\n", this_processor->current_thread, this_processor );
-			LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
-		);
-		BlockInternal( this_processor->current_thread );
+		LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ctx Switch IRH : Blocking thread %p on %p\n", this_thread, this_processor );
+		signal_unblock( SIGUSR1 );
+		BlockInternal( (thread_desc*)this_thread );
+		LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ctx Switch IRH : Back\n\n");
 	}
 	else {
-		LIB_DEBUG_DO(
-			len = snprintf( text, 256, "Ctx Switch IRH : Defering\n" );
-			LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
-		);
+		LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ctx Switch IRH : Defering\n" );
 		defer_ctxSwitch();
+		signal_unblock( SIGUSR1 );
 	}
 }
 
 void sigHandler_alarm( __CFA_SIGPARMS__ ) {
-
-	LIB_DEBUG_DO(
-		char text[256];
-		__attribute__((unused)) int len = snprintf( text, 256, "\nAlarm IRH %p\n", (void *)(cxt->uc_mcontext.gregs[REG_RIP]) );
-		LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
-	);
-
-	signal_unblock( true );
-	if( try_lock( &systemProcessor->alarm_lock ) ) {
+	LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "\nAlarm IRH %p running %p @ %p\n", this_processor, this_thread, (void *)(cxt->uc_mcontext.gregs[REG_RIP]) );
+
+	// if( ((intptr_t)cxt->uc_mcontext.gregs[REG_RIP]) > 0xFFFFFF ) __debug_break();
+
+	if( try_lock( &systemProcessor->alarm_lock, __PRETTY_FUNCTION__ ) ) {
 		tick_preemption();
 		unlock( &systemProcessor->alarm_lock );
@@ -257,20 +246,16 @@
 	}
 
+	signal_unblock( SIGALRM );
+
 	if( preemption_ready() && this_processor->pending_preemption ) {
-		LIB_DEBUG_DO(
-			len = snprintf( text, 256, "Alarm IRH : Blocking thread\n" );
-			LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
-		);
+		LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Alarm IRH : Blocking thread %p on %p\n", this_thread, this_processor );
 		this_processor->pending_preemption = false;
-		BlockInternal( this_processor->current_thread );
+		BlockInternal( (thread_desc*)this_thread );
+		LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Alarm Switch IRH : Back\n\n");
 	}
 }
 
 static void preempt( processor * this ) {
-	LIB_DEBUG_DO(
-		char text[256];
-		__attribute__((unused)) int len = snprintf( text, 256, "Processor : signalling %p\n", this );
-		LIB_DEBUG_WRITE( STDERR_FILENO, text, len );
-	);
+	// LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Processor : signalling %p\n", this );
 
 	if( this != systemProcessor ) {
@@ -290,17 +275,130 @@
 
 	act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler;
+	act.sa_flags = flags;
+
+	// disabled during signal handler
 	sigemptyset( &act.sa_mask );
-	sigaddset( &act.sa_mask, SIGALRM );		// disabled during signal handler
-	sigaddset( &act.sa_mask, SIGUSR1 );
-
-	act.sa_flags = flags;
+	sigaddset( &act.sa_mask, sig );
 
 	if ( sigaction( sig, &act, NULL ) == -1 ) {
-		// THE KERNEL IS NOT STARTED SO CALL NO uC++ ROUTINES!
-		char helpText[256];
-		__attribute__((unused)) int len = snprintf( helpText, 256, " __kernel_sigaction( sig:%d, handler:%p, flags:%d ), problem installing signal handler, error(%d) %s.\n",
-				sig, handler, flags, errno, strerror( errno ) );
-		LIB_DEBUG_WRITE( STDERR_FILENO, helpText, len );
+		LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO,
+			" __kernel_sigaction( sig:%d, handler:%p, flags:%d ), problem installing signal handler, error(%d) %s.\n",
+			sig, handler, flags, errno, strerror( errno )
+		);
 		_exit( EXIT_FAILURE );
-	} // if
-}
+	}
+}
+
+typedef void (*sa_handler_t)(int);
+
+static void __kernel_sigdefault( int sig ) {
+	struct sigaction act;
+
+	// act.sa_handler = SIG_DFL;
+	act.sa_flags = 0;
+	sigemptyset( &act.sa_mask );
+
+	if ( sigaction( sig, &act, NULL ) == -1 ) {
+		LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO,
+			" __kernel_sigdefault( sig:%d ), problem reseting signal handler, error(%d) %s.\n",
+			sig, errno, strerror( errno )
+		);
+		_exit( EXIT_FAILURE );
+	}
+}
+
+//=============================================================================================
+// Terminating Signals logic
+//=============================================================================================
+
+LIB_DEBUG_DO(
+	static void __kernel_backtrace( int start ) {
+		// skip first N stack frames
+
+		enum { Frames = 50 };
+		void * array[Frames];
+		int size = backtrace( array, Frames );
+		char ** messages = backtrace_symbols( array, size );
+
+		// find executable name
+		*index( messages[0], '(' ) = '\0';
+		#ifdef __USE_STREAM__
+		serr | "Stack back trace for:" | messages[0] | endl;
+		#else
+		fprintf( stderr, "Stack back trace for: %s\n", messages[0]);
+		#endif
+
+		// skip last 2 stack frames after main
+		for ( int i = start; i < size && messages != NULL; i += 1 ) {
+			char * name = NULL;
+			char * offset_begin = NULL;
+			char * offset_end = NULL;
+
+			for ( char *p = messages[i]; *p; ++p ) {
+				// find parantheses and +offset
+				if ( *p == '(' ) {
+					name = p;
+				}
+				else if ( *p == '+' ) {
+					offset_begin = p;
+				}
+				else if ( *p == ')' ) {
+					offset_end = p;
+					break;
+				}
+			}
+
+			// if line contains symbol print it
+			int frameNo = i - start;
+			if ( name && offset_begin && offset_end && name < offset_begin ) {
+				// delimit strings
+				*name++ = '\0';
+				*offset_begin++ = '\0';
+				*offset_end++ = '\0';
+
+				#ifdef __USE_STREAM__
+				serr 	| "("  | frameNo | ")" | messages[i] | ":"
+					| name | "+" | offset_begin | offset_end | endl;
+				#else
+				fprintf( stderr, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
+				#endif
+			}
+			// otherwise, print the whole line
+			else {
+				#ifdef __USE_STREAM__
+				serr | "(" | frameNo | ")" | messages[i] | endl;
+				#else
+				fprintf( stderr, "(%i) %s\n", frameNo, messages[i] );
+				#endif
+			}
+		}
+
+		free( messages );
+	}
+)
+
+void sigHandler_segv( __CFA_SIGPARMS__ ) {
+	LIB_DEBUG_DO(
+		#ifdef __USE_STREAM__
+		serr 	| "*CFA runtime error* program cfa-cpp terminated with"
+			| (sig == SIGSEGV ? "segment fault." : "bus error.")
+			| endl;
+		#else
+		fprintf( stderr, "*CFA runtime error* program cfa-cpp terminated with %s\n", sig == SIGSEGV ? "segment fault." : "bus error." );
+		#endif
+
+		// skip first 2 stack frames
+		__kernel_backtrace( 1 );
+	)
+	exit( EXIT_FAILURE );
+}
+
+// void sigHandler_abort( __CFA_SIGPARMS__ ) {
+// 	// skip first 6 stack frames
+// 	LIB_DEBUG_DO( __kernel_backtrace( 6 ); )
+
+// 	// reset default signal handler
+// 	__kernel_sigdefault( SIGABRT );
+
+// 	raise( SIGABRT );
+// }
Index: src/libcfa/concurrency/thread
===================================================================
--- src/libcfa/concurrency/thread	(revision aa3d77b01f0bccec37e2709bdd14bd335e4d24ee)
+++ src/libcfa/concurrency/thread	(revision 1c273d04b68cc57e237ec5bb3b198fd7b17ebde3)
@@ -54,5 +54,5 @@
 }
 
-thread_desc * this_thread(void);
+extern volatile thread_local thread_desc * this_thread;
 
 forall( dtype T | is_thread(T) )
Index: src/libcfa/concurrency/thread.c
===================================================================
--- src/libcfa/concurrency/thread.c	(revision aa3d77b01f0bccec37e2709bdd14bd335e4d24ee)
+++ src/libcfa/concurrency/thread.c	(revision 1c273d04b68cc57e237ec5bb3b198fd7b17ebde3)
@@ -71,18 +71,21 @@
 	coroutine_desc* thrd_c = get_coroutine(this);
 	thread_desc*  thrd_h = get_thread   (this);
-	thrd_c->last = this_coroutine();
-	this_processor->current_coroutine = thrd_c;
+	thrd_c->last = this_coroutine;
 
-	LIB_DEBUG_PRINT_SAFE("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
+	// LIB_DEBUG_PRINT_SAFE("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
 
+	disable_interrupts();
 	create_stack(&thrd_c->stack, thrd_c->stack.size);
+	this_coroutine = thrd_c;
 	CtxStart(this, CtxInvokeThread);
+	assert( thrd_c->last->stack.context );
 	CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
 
 	ScheduleThread(thrd_h);
+	enable_interrupts( __PRETTY_FUNCTION__ );
 }
 
 void yield( void ) {
-	BlockInternal( this_processor->current_thread );
+	BlockInternal( (thread_desc *)this_thread );
 }
 
@@ -95,5 +98,5 @@
 void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
 	// set state of current coroutine to inactive
-	src->state = Inactive;
+	src->state = src->state == Halted ? Halted : Inactive;
 	dst->state = Active;
 
@@ -103,10 +106,11 @@
 	// set new coroutine that the processor is executing
 	// and context switch to it
-	this_processor->current_coroutine = dst;
+	this_coroutine = dst;
+	assert( src->stack.context );
 	CtxSwitch( src->stack.context, dst->stack.context );
-	this_processor->current_coroutine = src;
+	this_coroutine = src;
 
 	// set state of new coroutine to active
-	dst->state = Inactive;
+	dst->state = dst->state == Halted ? Halted : Inactive;
 	src->state = Active;
 }
