Index: libcfa/src/concurrency/monitor.cfa
===================================================================
--- libcfa/src/concurrency/monitor.cfa	(revision 9f575eacc1e359b231c369fde8ace4f941121f26)
+++ libcfa/src/concurrency/monitor.cfa	(revision 3381ed7a703d0c964d6e94bffab7c58f0cdf0d17)
@@ -27,10 +27,10 @@
 //-----------------------------------------------------------------------------
 // Forward declarations
-static inline void set_owner ( monitor_desc * this, thread_desc * owner );
-static inline void set_owner ( monitor_desc * storage [], __lock_size_t count, thread_desc * owner );
+static inline void set_owner ( monitor_desc * this, thread_desc * owner, enum __Owner_Reason );
+static inline void set_owner ( monitor_desc * storage [], __lock_size_t count, thread_desc * owner, enum __Owner_Reason );
 static inline void set_mask  ( monitor_desc * storage [], __lock_size_t count, const __waitfor_mask_t & mask );
 static inline void reset_mask( monitor_desc * this );
 
-static inline thread_desc * next_thread( monitor_desc * this );
+static inline thread_desc * next_thread( monitor_desc * this, enum __Owner_Reason );
 static inline bool is_accepted( monitor_desc * this, const __monitor_group_t & monitors );
 
@@ -94,5 +94,5 @@
 		if( !this->owner ) {
 			// No one has the monitor, just take it
-			set_owner( this, thrd );
+			set_owner( this, thrd, __ENTER_FREE );
 
 			__cfaabi_dbg_print_safe( "Kernel :  mon is free \n" );
@@ -106,5 +106,5 @@
 		else if( is_accepted( this, group) ) {
 			// Some one was waiting for us, enter
-			set_owner( this, thrd );
+			set_owner( this, thrd, __ENTER_ACCEPT );
 
 			// Reset mask
@@ -117,15 +117,21 @@
 
 			// Some one else has the monitor, wait in line for it
+			/* paranoid */ verify( thrd->next == 0p );
 			append( this->entry_queue, thrd );
-
-			BlockInternal( &this->lock );
+			/* paranoid */ verify( thrd->next == 1p );
+
+			unlock( this->lock );
+			park();
 
 			__cfaabi_dbg_print_safe( "Kernel : %10p Entered  mon %p\n", thrd, this);
 
-			// BlockInternal will unlock spinlock, no need to unlock ourselves
+			/* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
 			return;
 		}
 
 		__cfaabi_dbg_print_safe( "Kernel : %10p Entered  mon %p\n", thrd, this);
+
+		/* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
+		/* paranoid */ verify( this->lock.lock );
 
 		// Release the lock and leave
@@ -147,5 +153,7 @@
 
 			// No one has the monitor, just take it
-			set_owner( this, thrd );
+			set_owner( this, thrd, __ENTER_DTOR_FREE );
+
+			verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
 
 			unlock( this->lock );
@@ -166,5 +174,5 @@
 			// Wake the thread that is waiting for this
 			__condition_criterion_t * urgent = pop( this->signal_stack );
-			verify( urgent );
+			/* paranoid */ verify( urgent );
 
 			// Reset mask
@@ -175,8 +183,15 @@
 
 			// Some one else has the monitor, wait for him to finish and then run
-			BlockInternal( &this->lock, urgent->owner->waiting_thread );
+			unlock( this->lock );
+
+			// Release the next thread
+			/* paranoid */ verifyf( urgent->owner->waiting_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
+			unpark( urgent->owner->waiting_thread );
+
+			// Park current thread waiting
+			park();
 
 			// Some one was waiting for us, enter
-			set_owner( this, thrd );
+			/* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
 		}
 		else {
@@ -187,8 +202,13 @@
 
 			// Some one else has the monitor, wait in line for it
+			/* paranoid */ verify( thrd->next == 0p );
 			append( this->entry_queue, thrd );
-			BlockInternal( &this->lock );
-
-			// BlockInternal will unlock spinlock, no need to unlock ourselves
+			/* paranoid */ verify( thrd->next == 1p );
+			unlock( this->lock );
+
+			// Park current thread waiting
+			park();
+
+			/* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
 			return;
 		}
@@ -205,5 +225,5 @@
 		__cfaabi_dbg_print_safe( "Kernel : %10p Leaving mon %p (%p)\n", kernelTLS.this_thread, this, this->owner);
 
-		verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
+		/* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
 
 		// Leaving a recursion level, decrement the counter
@@ -219,5 +239,9 @@
 
 		// Get the next thread, will be null on low contention monitor
-		thread_desc * new_owner = next_thread( this );
+		thread_desc * new_owner = next_thread( this, __LEAVE );
+
+		// Check the new owner is consistent with who we wake-up
+		// new_owner might be null even if someone owns the monitor when the owner is still waiting for another monitor
+		/* paranoid */ verifyf( !new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );
 
 		// We can now let other threads in safely
@@ -225,5 +249,6 @@
 
 		//We need to wake-up the thread
-		WakeThread( new_owner, false );
+		/* paranoid */ verifyf( !new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );
+		unpark( new_owner );
 	}
 
@@ -254,5 +279,5 @@
 		thrd->self_cor.state = Halted;
 
-		verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", thrd, this->owner, this->recursion, this );
+		/* paranoid */ verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", thrd, this->owner, this->recursion, this );
 
 		// Leaving a recursion level, decrement the counter
@@ -264,10 +289,17 @@
 
 		// Fetch the next thread, can be null
-		thread_desc * new_owner = next_thread( this );
+		thread_desc * new_owner = next_thread( this, __LEAVE_THREAD );
+
+		// Release the monitor lock
+		unlock( this->lock );
+
+		// Unpark the next owner if needed
+		/* paranoid */ verifyf( !new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );
+		unpark( new_owner );
 
 		// Leave the thread, this will unlock the spinlock
-		// Use leave thread instead of BlockInternal which is
-		// specialized for this case and supports null new_owner
-		LeaveThread( &this->lock, new_owner );
+		// Use leave thread instead of park which is
+		// specialized for this case
+		LeaveThread();
 
 		// Control flow should never reach here!
@@ -400,5 +432,7 @@
 	// Append the current wait operation to the ones already queued on the condition
 	// We don't need locks for that since conditions must always be waited on inside monitor mutual exclusion
+	/* paranoid */ verify( waiter.next == 0p );
 	append( this.blocked, &waiter );
+	/* paranoid */ verify( waiter.next == 1p );
 
 	// Lock all monitors (aggregates the locks as well)
@@ -415,10 +449,20 @@
 	// Remove any duplicate threads
 	for( __lock_size_t i = 0; i < count; i++) {
-		thread_desc * new_owner = next_thread( monitors[i] );
+		thread_desc * new_owner = next_thread( monitors[i], __WAIT );
 		insert_unique( threads, thread_count, new_owner );
 	}
 
+	// Unlock the locks, we don't need them anymore
+	for(int i = 0; i < count; i++) {
+		unlock( *locks[i] );
+	}
+
+	// Wake the threads
+	for(int i = 0; i < thread_count; i++) {
+		unpark( threads[i] );
+	}
+
 	// Everything is ready to go to sleep
-	BlockInternal( locks, count, threads, thread_count );
+	park();
 
 	// We are back, restore the owners and recursions
@@ -490,10 +534,17 @@
 	//Find the thread to run
 	thread_desc * signallee = pop_head( this.blocked )->waiting_thread;
-	set_owner( monitors, count, signallee );
+	/* paranoid */ verify( signallee->next == 0p );
+	set_owner( monitors, count, signallee, __ENTER_SIGNAL_BLOCK );
 
 	__cfaabi_dbg_print_buffer_decl( "Kernel : signal_block condition %p (s: %p)\n", &this, signallee );
 
+	// unlock all the monitors
+	unlock_all( locks, count );
+
+	// unpark the thread we signalled
+	unpark( signallee );
+
 	//Everything is ready to go to sleep
-	BlockInternal( locks, count, &signallee, 1 );
+	park();
 
 
@@ -590,8 +641,14 @@
 
 				// Set the owners to be the next thread
-				set_owner( monitors, count, next );
-
-				// Everything is ready to go to sleep
-				BlockInternal( locks, count, &next, 1 );
+				set_owner( monitors, count, next, __WAITFOR );
+
+				// unlock all the monitors
+				unlock_all( locks, count );
+
+				// unpark the thread we signalled
+				unpark( next );
+
+				//Everything is ready to go to sleep
+				park();
 
 				// We are back, restore the owners and recursions
@@ -631,6 +688,9 @@
 	}
 
+	// unlock all the monitors
+	unlock_all( locks, count );
+
 	//Everything is ready to go to sleep
-	BlockInternal( locks, count );
+	park();
 
 
@@ -649,9 +709,10 @@
 // Utilities
 
-static inline void set_owner( monitor_desc * this, thread_desc * owner ) {
-	// __cfaabi_dbg_print_safe( "Kernal :   Setting owner of %p to %p ( was %p)\n", this, owner, this->owner );
+static inline void set_owner( monitor_desc * this, thread_desc * owner, enum __Owner_Reason reason ) {
+	/* paranoid */ verify( this->lock.lock );
 
 	//Pass the monitor appropriately
 	this->owner = owner;
+	this->owner_reason = reason;
 
 	//We are passing the monitor to someone else, which means recursion level is not 0
@@ -659,10 +720,16 @@
 }
 
-static inline void set_owner( monitor_desc * monitors [], __lock_size_t count, thread_desc * owner ) {
-	monitors[0]->owner     = owner;
-	monitors[0]->recursion = 1;
+static inline void set_owner( monitor_desc * monitors [], __lock_size_t count, thread_desc * owner, enum __Owner_Reason reason ) {
+	/* paranoid */ verify ( monitors[0]->lock.lock );
+	/* paranoid */ verifyf( monitors[0]->owner == kernelTLS.this_thread, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, monitors[0]->owner, monitors[0]->recursion, monitors[0] );
+	monitors[0]->owner        = owner;
+	monitors[0]->owner_reason = reason;
+	monitors[0]->recursion    = 1;
 	for( __lock_size_t i = 1; i < count; i++ ) {
-		monitors[i]->owner     = owner;
-		monitors[i]->recursion = 0;
+		/* paranoid */ verify ( monitors[i]->lock.lock );
+		/* paranoid */ verifyf( monitors[i]->owner == kernelTLS.this_thread, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, monitors[i]->owner, monitors[i]->recursion, monitors[i] );
+		monitors[i]->owner        = owner;
+		monitors[i]->owner_reason = reason;
+		monitors[i]->recursion    = 0;
 	}
 }
@@ -680,5 +747,5 @@
 }
 
-static inline thread_desc * next_thread( monitor_desc * this ) {
+static inline thread_desc * next_thread( monitor_desc * this, enum __Owner_Reason reason ) {
 	//Check the signaller stack
 	__cfaabi_dbg_print_safe( "Kernel :  mon %p AS-stack top %p\n", this, this->signal_stack.top);
@@ -688,5 +755,6 @@
 		//regardless of if we are ready to baton pass,
 		//we need to set the monitor as in use
-		set_owner( this,  urgent->owner->waiting_thread );
+		/* paranoid */ verifyf( !this->owner || kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
+		set_owner( this,  urgent->owner->waiting_thread, reason );
 
 		return check_condition( urgent );
@@ -696,5 +764,7 @@
 	// Get the next thread in the entry_queue
 	thread_desc * new_owner = pop_head( this->entry_queue );
-	set_owner( this, new_owner );
+	/* paranoid */ verifyf( !this->owner || kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
+	/* paranoid */ verify( !new_owner || new_owner->next == 0p );
+	set_owner( this, new_owner, reason );
 
 	return new_owner;
@@ -841,5 +911,5 @@
 	// For each thread in the entry-queue
 	for(	thread_desc ** thrd_it = &entry_queue.head;
-		*thrd_it;
+		*thrd_it != 1p;
 		thrd_it = &(*thrd_it)->next
 	) {
