Index: src/libcfa/concurrency/CtxSwitch-i386.S
===================================================================
--- src/libcfa/concurrency/CtxSwitch-i386.S	(revision 83a071f98d287fe0dd150376b0c6ea5ff812b18b)
+++ src/libcfa/concurrency/CtxSwitch-i386.S	(revision 92360603d942184e66e5f92706ecc75c6b04f121)
@@ -1,3 +1,2 @@
-//                               -*- Mode: Asm -*-
 //
 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
@@ -10,7 +9,7 @@
 // Author           : Thierry Delisle
 // Created On       : Tue Dec 6 12:27:26 2016
-// Last Modified By : Thierry Delisle
-// Last Modified On : Tue Dec 6 12:27:26 2016
-// Update Count     : 0
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Fri Jul 21 22:29:25 2017
+// Update Count     : 1
 //
 // This  library is free  software; you  can redistribute  it and/or  modify it
@@ -99,4 +98,5 @@
 
 // Local Variables: //
-// compile-command: "make install" //
+// mode: c //
+// tab-width: 4 //
 // End: //
Index: src/libcfa/concurrency/CtxSwitch-x86_64.S
===================================================================
--- src/libcfa/concurrency/CtxSwitch-x86_64.S	(revision 83a071f98d287fe0dd150376b0c6ea5ff812b18b)
+++ src/libcfa/concurrency/CtxSwitch-x86_64.S	(revision 92360603d942184e66e5f92706ecc75c6b04f121)
@@ -1,3 +1,2 @@
-//                               -*- Mode: Asm -*-
 //
 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
@@ -10,7 +9,7 @@
 // Author           : Thierry Delisle
 // Created On       : Mon Nov 28 12:27:26 2016
-// Last Modified By : Thierry Delisle
-// Last Modified On : Mon Nov 28 12:27:26 2016
-// Update Count     : 0
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Fri Jul 21 22:28:11 2017
+// Update Count     : 1
 //
 // This  library is free  software; you  can redistribute  it and/or  modify it
Index: src/libcfa/concurrency/alarm.c
===================================================================
--- src/libcfa/concurrency/alarm.c	(revision 83a071f98d287fe0dd150376b0c6ea5ff812b18b)
+++ src/libcfa/concurrency/alarm.c	(revision 92360603d942184e66e5f92706ecc75c6b04f121)
@@ -1,3 +1,2 @@
-//                              -*- Mode: CFA -*-
 //
 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
@@ -10,7 +9,7 @@
 // Author           : Thierry Delisle
 // Created On       : Fri Jun 2 11:31:25 2017
-// Last Modified By : Thierry Delisle
-// Last Modified On : --
-// Update Count     : 0
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Fri Jul 21 22:35:18 2017
+// Update Count     : 1
 //
 
@@ -31,4 +30,42 @@
 
 //=============================================================================================
+// time type
+//=============================================================================================
+
+#define one_second         1_000_000_000ul
+#define one_milisecond         1_000_000ul
+#define one_microsecond            1_000ul
+#define one_nanosecond                 1ul
+
+__cfa_time_t zero_time = { 0 };
+
+void ?{}( __cfa_time_t & this ) { this.val = 0; }
+void ?{}( __cfa_time_t & this, zero_t zero ) { this.val = 0; }
+
+void ?{}( itimerval & this, __cfa_time_t * alarm ) {
+	this.it_value.tv_sec = alarm->val / one_second;			// seconds
+	this.it_value.tv_usec = max( (alarm->val % one_second) / one_microsecond, 1000 ); // microseconds
+	this.it_interval.tv_sec = 0;
+	this.it_interval.tv_usec = 0;
+}
+
+
+void ?{}( __cfa_time_t & this, timespec * curr ) {
+	uint64_t secs  = curr->tv_sec;
+	uint64_t nsecs = curr->tv_nsec;
+	this.val = (secs * one_second) + nsecs;
+}
+
+__cfa_time_t ?=?( __cfa_time_t & this, zero_t rhs ) {
+	this.val = 0;
+	return this;
+}
+
+__cfa_time_t from_s ( uint64_t val ) { __cfa_time_t ret; ret.val = val * 1_000_000_000ul; return ret; }
+__cfa_time_t from_ms( uint64_t val ) { __cfa_time_t ret; ret.val = val *     1_000_000ul; return ret; }
+__cfa_time_t from_us( uint64_t val ) { __cfa_time_t ret; ret.val = val *         1_000ul; return ret; }
+__cfa_time_t from_ns( uint64_t val ) { __cfa_time_t ret; ret.val = val *             1ul; return ret; }
+
+//=============================================================================================
 // Clock logic
 //=============================================================================================
@@ -37,16 +74,9 @@
 	timespec curr;
 	clock_gettime( CLOCK_REALTIME, &curr );
-	__cfa_time_t curr_time = ((__cfa_time_t)curr.tv_sec * TIMEGRAN) + curr.tv_nsec;
-	// LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : current time is %lu\n", curr_time );
-	return curr_time;
+	return (__cfa_time_t){ &curr };
 }
 
 void __kernel_set_timer( __cfa_time_t alarm ) {
-	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
-	val.it_value.tv_usec = (alarm % TIMEGRAN) / ( TIMEGRAN / 1_000_000L ); // microseconds
-	val.it_interval.tv_sec = 0;
-	val.it_interval.tv_usec = 0;
+	itimerval val = { &alarm };
 	setitimer( ITIMER_REAL, &val, NULL );
 }
@@ -56,5 +86,5 @@
 //=============================================================================================
 
-void ?{}( alarm_node_t & this, thread_desc * thrd, __cfa_time_t alarm = 0, __cfa_time_t period = 0 ) {
+void ?{}( alarm_node_t & this, thread_desc * thrd, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) {
 	this.thrd = thrd;
 	this.alarm = alarm;
@@ -65,5 +95,5 @@
 }
 
-void ?{}( alarm_node_t & this, processor   * proc, __cfa_time_t alarm = 0, __cfa_time_t period = 0 ) {
+void ?{}( alarm_node_t & this, processor   * proc, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) {
 	this.proc = proc;
 	this.alarm = alarm;
@@ -153,20 +183,18 @@
 
 void register_self( alarm_node_t * this ) {
+	alarm_list_t * alarms = &event_kernel->alarms;
+
 	disable_interrupts();
-	verify( !systemProcessor->pending_alarm );
-	lock( &systemProcessor->alarm_lock DEBUG_CTX2 );
+	lock( &event_kernel->lock DEBUG_CTX2 );
 	{
-		verify( validate( &systemProcessor->alarms ) );
-		bool first = !systemProcessor->alarms.head;
-
-		insert( &systemProcessor->alarms, this );
-		if( systemProcessor->pending_alarm ) {
-			tick_preemption();
+		verify( validate( alarms ) );
+		bool first = !alarms->head;
+
+		insert( alarms, this );
+		if( first ) {
+			__kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
 		}
-		if( first ) {
-			__kernel_set_timer( systemProcessor->alarms.head->alarm - __kernel_get_time() );
-		}
-	}
-	unlock( &systemProcessor->alarm_lock );
+	}
+	unlock( &event_kernel->lock );
 	this->set = true;
 	enable_interrupts( DEBUG_CTX );
@@ -174,14 +202,17 @@
 
 void unregister_self( alarm_node_t * this ) {
-	// LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : unregister %p start\n", this );
 	disable_interrupts();
-	lock( &systemProcessor->alarm_lock DEBUG_CTX2 );
+	lock( &event_kernel->lock DEBUG_CTX2 );
 	{
-		verify( validate( &systemProcessor->alarms ) );
-		remove( &systemProcessor->alarms, this );
-	}
-	unlock( &systemProcessor->alarm_lock );
+		verify( validate( &event_kernel->alarms ) );
+		remove( &event_kernel->alarms, this );
+	}
+	unlock( &event_kernel->lock );
 	enable_interrupts( DEBUG_CTX );
 	this->set = false;
-	// LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Kernel : unregister %p end\n", this );
-}
+}
+
+// Local Variables: //
+// mode: c //
+// tab-width: 4 //
+// End: //
Index: src/libcfa/concurrency/alarm.h
===================================================================
--- src/libcfa/concurrency/alarm.h	(revision 83a071f98d287fe0dd150376b0c6ea5ff812b18b)
+++ src/libcfa/concurrency/alarm.h	(revision 92360603d942184e66e5f92706ecc75c6b04f121)
@@ -1,3 +1,2 @@
-//                              -*- Mode: CFA -*-
 //
 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
@@ -10,26 +9,76 @@
 // Author           : Thierry Delisle
 // Created On       : Fri Jun 2 11:31:25 2017
-// Last Modified By : Thierry Delisle
-// Last Modified On : --
-// Update Count     : 0
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Sat Jul 22 09:59:27 2017
+// Update Count     : 3
 //
 
-#ifndef ALARM_H
-#define ALARM_H
+#pragma once
 
 #include <stdbool.h>
+#include <stdint.h>
 
-#include "assert"
-
-typedef unsigned long int __cfa_time_t;
+#include <assert.h>
 
 struct thread_desc;
 struct processor;
 
+struct timespec;
+struct itimerval;
+
+//=============================================================================================
+// time type
+//=============================================================================================
+
+struct __cfa_time_t {
+	uint64_t val;
+};
+
+// ctors
+void ?{}( __cfa_time_t & this );
+void ?{}( __cfa_time_t & this, zero_t zero );
+void ?{}( __cfa_time_t & this, timespec * curr );
+void ?{}( itimerval & this, __cfa_time_t * alarm );
+
+__cfa_time_t ?=?( __cfa_time_t & this, zero_t rhs );
+
+// logical ops
+static inline bool ?==?( __cfa_time_t lhs, __cfa_time_t rhs ) { return lhs.val == rhs.val; }
+static inline bool ?!=?( __cfa_time_t lhs, __cfa_time_t rhs ) { return lhs.val != rhs.val; }
+static inline bool ?>? ( __cfa_time_t lhs, __cfa_time_t rhs ) { return lhs.val >  rhs.val; }
+static inline bool ?<? ( __cfa_time_t lhs, __cfa_time_t rhs ) { return lhs.val <  rhs.val; }
+static inline bool ?>=?( __cfa_time_t lhs, __cfa_time_t rhs ) { return lhs.val >= rhs.val; }
+static inline bool ?<=?( __cfa_time_t lhs, __cfa_time_t rhs ) { return lhs.val <= rhs.val; }
+
+static inline bool ?==?( __cfa_time_t lhs, zero_t rhs ) { return lhs.val == rhs; }
+static inline bool ?!=?( __cfa_time_t lhs, zero_t rhs ) { return lhs.val != rhs; }
+static inline bool ?>? ( __cfa_time_t lhs, zero_t rhs ) { return lhs.val >  rhs; }
+static inline bool ?<? ( __cfa_time_t lhs, zero_t rhs ) { return lhs.val <  rhs; }
+static inline bool ?>=?( __cfa_time_t lhs, zero_t rhs ) { return lhs.val >= rhs; }
+static inline bool ?<=?( __cfa_time_t lhs, zero_t rhs ) { return lhs.val <= rhs; }
+
+// addition/substract
+static inline __cfa_time_t ?+?( __cfa_time_t lhs, __cfa_time_t rhs ) {
+	__cfa_time_t ret;
+	ret.val = lhs.val + rhs.val;
+	return ret;
+}
+
+static inline __cfa_time_t ?-?( __cfa_time_t lhs, __cfa_time_t rhs ) {
+	__cfa_time_t ret;
+	ret.val = lhs.val - rhs.val;
+	return ret;
+}
+
+__cfa_time_t from_s ( uint64_t );
+__cfa_time_t from_ms( uint64_t );
+__cfa_time_t from_us( uint64_t );
+__cfa_time_t from_ns( uint64_t );
+
+extern __cfa_time_t zero_time;
+
 //=============================================================================================
 // Clock logic
 //=============================================================================================
-
-#define TIMEGRAN 1_000_000_000L				// nanosecond granularity, except for timeval
 
 __cfa_time_t __kernel_get_time();
@@ -56,6 +105,6 @@
 typedef alarm_node_t ** __alarm_it_t;
 
-void ?{}( alarm_node_t & this, thread_desc * thrd, __cfa_time_t alarm = 0, __cfa_time_t period = 0 );
-void ?{}( alarm_node_t & this, processor   * proc, __cfa_time_t alarm = 0, __cfa_time_t period = 0 );
+void ?{}( alarm_node_t & this, thread_desc * thrd, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time );
+void ?{}( alarm_node_t & this, processor   * proc, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time );
 void ^?{}( alarm_node_t & this );
 
@@ -76,8 +125,6 @@
 void unregister_self( alarm_node_t * this );
 
-#endif
-
 // Local Variables: //
-// mode: CFA //
+// mode: c //
 // tab-width: 6 //
 // End: //
Index: src/libcfa/concurrency/coroutine
===================================================================
--- src/libcfa/concurrency/coroutine	(revision 83a071f98d287fe0dd150376b0c6ea5ff812b18b)
+++ src/libcfa/concurrency/coroutine	(revision 92360603d942184e66e5f92706ecc75c6b04f121)
@@ -10,13 +10,12 @@
 // Author           : Thierry Delisle
 // Created On       : Mon Nov 28 12:27:26 2016
-// Last Modified By : Thierry Delisle
-// Last Modified On : Mon Nov 28 12:27:26 2016
-// Update Count     : 0
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Sat Jul 22 09:57:17 2017
+// Update Count     : 2
 //
 
-#ifndef COROUTINES_H
-#define COROUTINES_H
+#pragma once
 
-#include "assert"
+#include <assert.h>
 #include "invoke.h"
 
@@ -63,5 +62,5 @@
 
 // Get current coroutine
-extern volatile thread_local coroutine_desc * this_coroutine;
+extern thread_local coroutine_desc * volatile this_coroutine;
 
 // Private wrappers for context switch and stack creation
@@ -129,6 +128,4 @@
 }
 
-#endif //COROUTINES_H
-
 // Local Variables: //
 // mode: c //
Index: src/libcfa/concurrency/coroutine.c
===================================================================
--- src/libcfa/concurrency/coroutine.c	(revision 83a071f98d287fe0dd150376b0c6ea5ff812b18b)
+++ src/libcfa/concurrency/coroutine.c	(revision 92360603d942184e66e5f92706ecc75c6b04f121)
@@ -1,3 +1,2 @@
-//                              -*- Mode: CFA -*-
 //
 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
@@ -10,7 +9,7 @@
 // Author           : Thierry Delisle
 // Created On       : Mon Nov 28 12:27:26 2016
-// Last Modified By : Thierry Delisle
-// Last Modified On : Mon Nov 28 12:27:26 2016
-// Update Count     : 0
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Fri Jul 21 22:34:57 2017
+// Update Count     : 1
 //
 
@@ -26,11 +25,9 @@
 }
 
-#include "kernel"
-#include "libhdr.h"
+#include "kernel_private.h"
 
 #define __CFA_INVOKE_PRIVATE__
 #include "invoke.h"
 
-extern volatile thread_local processor * this_processor;
 
 //-----------------------------------------------------------------------------
Index: src/libcfa/concurrency/invoke.c
===================================================================
--- src/libcfa/concurrency/invoke.c	(revision 83a071f98d287fe0dd150376b0c6ea5ff812b18b)
+++ src/libcfa/concurrency/invoke.c	(revision 92360603d942184e66e5f92706ecc75c6b04f121)
@@ -1,3 +1,2 @@
-//                              -*- Mode: C -*-
 //
 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
@@ -10,7 +9,7 @@
 // Author           : Thierry Delisle
 // Created On       : Tue Jan 17 12:27:26 2016
-// Last Modified By : Thierry Delisle
-// Last Modified On : --
-// Update Count     : 0
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Fri Jul 21 22:28:33 2017
+// Update Count     : 1
 //
 
@@ -142,2 +141,7 @@
 #endif
 }
+
+// Local Variables: //
+// mode: c //
+// tab-width: 4 //
+// End: //
Index: src/libcfa/concurrency/invoke.h
===================================================================
--- src/libcfa/concurrency/invoke.h	(revision 83a071f98d287fe0dd150376b0c6ea5ff812b18b)
+++ src/libcfa/concurrency/invoke.h	(revision 92360603d942184e66e5f92706ecc75c6b04f121)
@@ -1,3 +1,2 @@
-//                              -*- Mode: C -*-
 //
 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
@@ -10,7 +9,7 @@
 // Author           : Thierry Delisle
 // Created On       : Tue Jan 17 12:27:26 2016
-// Last Modified By : Thierry Delisle
-// Last Modified On : --
-// Update Count     : 0
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Fri Jul 21 22:28:56 2017
+// Update Count     : 1
 //
 
@@ -130,2 +129,7 @@
 }
 #endif
+
+// Local Variables: //
+// mode: c //
+// tab-width: 4 //
+// End: //
Index: src/libcfa/concurrency/kernel
===================================================================
--- src/libcfa/concurrency/kernel	(revision 83a071f98d287fe0dd150376b0c6ea5ff812b18b)
+++ src/libcfa/concurrency/kernel	(revision 92360603d942184e66e5f92706ecc75c6b04f121)
@@ -1,3 +1,2 @@
-//                              -*- Mode: CFA -*-
 //
 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
@@ -10,11 +9,10 @@
 // Author           : Thierry Delisle
 // Created On       : Tue Jan 17 12:27:26 2017
-// Last Modified By : Thierry Delisle
-// Last Modified On : --
-// Update Count     : 0
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Sat Jul 22 09:58:39 2017
+// Update Count     : 2
 //
 
-#ifndef KERNEL_H
-#define KERNEL_H
+#pragma once
 
 #include <stdbool.h>
@@ -28,8 +26,8 @@
 //-----------------------------------------------------------------------------
 // Locks
-bool try_lock  ( spinlock * DEBUG_CTX_PARAM2 );
-void lock      ( spinlock * DEBUG_CTX_PARAM2 );
-void lock_yield( spinlock * DEBUG_CTX_PARAM2 );
-void unlock    ( spinlock * );
+void lock      ( spinlock * DEBUG_CTX_PARAM2 );       // Lock the spinlock, spin if already acquired
+void lock_yield( spinlock * DEBUG_CTX_PARAM2 );       // Lock the spinlock, yield repeatedly if already acquired
+bool try_lock  ( spinlock * DEBUG_CTX_PARAM2 );       // Lock the spinlock, return false if already acquired
+void unlock    ( spinlock * );                        // Unlock the spinlock
 
 struct semaphore {
@@ -48,6 +46,7 @@
 // Cluster
 struct cluster {
-	__thread_queue_t ready_queue;
-	spinlock lock;
+	spinlock ready_queue_lock;                      // Ready queue locks
+	__thread_queue_t ready_queue;                   // Ready queue for threads
+	unsigned long long int preemption;              // Preemption rate on this cluster
 };
 
@@ -76,20 +75,26 @@
 static inline void ^?{}(FinishAction & this) {}
 
+// Processor
+// Wrapper around kernel threads
 struct processor {
-	struct processorCtx_t * runner;
-	cluster * cltr;
-	pthread_t kernel_thread;
+	// Main state
+	struct processorCtx_t * runner;                 // Coroutine ctx who does keeps the state of the processor
+	cluster * cltr;                                 // Cluster from which to get threads
+	pthread_t kernel_thread;                        // Handle to pthreads
 
-	semaphore terminated;
-	volatile bool is_terminated;
+	// Termination
+	volatile bool do_terminate;                     // Set to true to notify the processor should terminate
+	semaphore terminated;                           // Termination synchronisation
 
-	struct FinishAction finish;
+	// RunThread data
+	struct FinishAction finish;                     // Action to do after a thread is ran
 
-	struct alarm_node_t * preemption_alarm;
-	unsigned int preemption;
+	// Preemption data
+	struct alarm_node_t * preemption_alarm;         // Node which is added in the discrete event simulaiton
+	bool pending_preemption;                        // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
 
-	bool pending_preemption;
-
-	char * last_enable;
+#ifdef __CFA_DEBUG__
+	char * last_enable;                             // Last function to enable preemption on this processor
+#endif
 };
 
@@ -98,8 +103,6 @@
 void ^?{}(processor & this);
 
-#endif //KERNEL_H
-
 // Local Variables: //
-// mode: CFA //
-// tab-width: 6 //
+// mode: c //
+// tab-width: 4 //
 // End: //
Index: src/libcfa/concurrency/kernel.c
===================================================================
--- src/libcfa/concurrency/kernel.c	(revision 83a071f98d287fe0dd150376b0c6ea5ff812b18b)
+++ src/libcfa/concurrency/kernel.c	(revision 92360603d942184e66e5f92706ecc75c6b04f121)
@@ -1,3 +1,2 @@
-//                              -*- Mode: CFA -*-
 //
 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
@@ -10,7 +9,7 @@
 // Author           : Thierry Delisle
 // Created On       : Tue Jan 17 12:27:26 2017
-// Last Modified By : Thierry Delisle
-// Last Modified On : --
-// Update Count     : 0
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Fri Jul 21 22:33:18 2017
+// Update Count     : 2
 //
 
@@ -42,14 +41,12 @@
 //-----------------------------------------------------------------------------
 // Kernel storage
-#define KERNEL_STORAGE(T,X) static char X##Storage[sizeof(T)]
-
-KERNEL_STORAGE(processorCtx_t, systemProcessorCtx);
-KERNEL_STORAGE(cluster, systemCluster);
-KERNEL_STORAGE(system_proc_t, systemProcessor);
-KERNEL_STORAGE(thread_desc, mainThread);
+KERNEL_STORAGE(cluster,           mainCluster);
+KERNEL_STORAGE(processor,         mainProcessor);
+KERNEL_STORAGE(processorCtx_t,    mainProcessorCtx);
+KERNEL_STORAGE(thread_desc,       mainThread);
 KERNEL_STORAGE(machine_context_t, mainThreadCtx);
 
-cluster * systemCluster;
-system_proc_t * systemProcessor;
+cluster *     mainCluster;
+processor *   mainProcessor;
 thread_desc * mainThread;
 
@@ -57,7 +54,8 @@
 // Global state
 
-volatile thread_local processor * this_processor;
-volatile thread_local coroutine_desc * this_coroutine;
-volatile thread_local thread_desc * this_thread;
+thread_local coroutine_desc * volatile this_coroutine;
+thread_local thread_desc *    volatile this_thread;
+thread_local processor *      volatile this_processor;
+
 volatile thread_local bool preemption_in_progress = 0;
 volatile thread_local unsigned short disable_preempt_count = 1;
@@ -85,5 +83,5 @@
 
 	this.limit = (void *)(((intptr_t)this.base) - this.size);
-	this.context = &mainThreadCtxStorage;
+	this.context = &storage_mainThreadCtx;
 	this.top = this.base;
 }
@@ -125,5 +123,5 @@
 
 void ?{}(processor & this) {
-	this{ systemCluster };
+	this{ mainCluster };
 }
 
@@ -131,7 +129,6 @@
 	this.cltr = cltr;
 	(this.terminated){ 0 };
-	this.is_terminated = false;
+	this.do_terminate = false;
 	this.preemption_alarm = NULL;
-	this.preemption = default_preemption();
 	this.pending_preemption = false;
 
@@ -142,31 +139,18 @@
 	this.cltr = cltr;
 	(this.terminated){ 0 };
-	this.is_terminated = false;
+	this.do_terminate = false;
 	this.preemption_alarm = NULL;
-	this.preemption = default_preemption();
 	this.pending_preemption = false;
 	this.kernel_thread = pthread_self();
 
 	this.runner = &runner;
-	LIB_DEBUG_PRINT_SAFE("Kernel : constructing system processor context %p\n", &runner);
+	LIB_DEBUG_PRINT_SAFE("Kernel : constructing main processor context %p\n", &runner);
 	runner{ &this };
 }
 
-LIB_DEBUG_DO( bool validate( alarm_list_t * this ); )
-
-void ?{}(system_proc_t & this, cluster * cltr, processorCtx_t & runner) {
-	(this.alarms){};
-	(this.alarm_lock){};
-	this.pending_alarm = false;
-
-	(this.proc){ cltr, runner };
-
-	verify( validate( &this.alarms ) );
-}
-
 void ^?{}(processor & this) {
-	if( ! this.is_terminated ) {
+	if( ! this.do_terminate ) {
 		LIB_DEBUG_PRINT_SAFE("Kernel : core %p signaling termination\n", &this);
-		this.is_terminated = true;
+		this.do_terminate = true;
 		P( &this.terminated );
 		pthread_join( this.kernel_thread, NULL );
@@ -176,5 +160,7 @@
 void ?{}(cluster & this) {
 	( this.ready_queue ){};
-	( this.lock ){};
+	( this.ready_queue_lock ){};
+
+	this.preemption = default_preemption();
 }
 
@@ -199,5 +185,5 @@
 
 		thread_desc * readyThread = NULL;
-		for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ )
+		for( unsigned int spin_count = 0; ! this->do_terminate; spin_count++ )
 		{
 			readyThread = nextThread( this->cltr );
@@ -343,7 +329,7 @@
 	verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
 
-	lock( &systemProcessor->proc.cltr->lock DEBUG_CTX2 );
-	append( &systemProcessor->proc.cltr->ready_queue, thrd );
-	unlock( &systemProcessor->proc.cltr->lock );
+	lock(   &this_processor->cltr->ready_queue_lock DEBUG_CTX2 );
+	append( &this_processor->cltr->ready_queue, thrd );
+	unlock( &this_processor->cltr->ready_queue_lock );
 
 	verify( disable_preempt_count > 0 );
@@ -352,7 +338,7 @@
 thread_desc * nextThread(cluster * this) {
 	verify( disable_preempt_count > 0 );
-	lock( &this->lock DEBUG_CTX2 );
+	lock( &this->ready_queue_lock DEBUG_CTX2 );
 	thread_desc * head = pop_head( &this->ready_queue );
-	unlock( &this->lock );
+	unlock( &this->ready_queue_lock );
 	verify( disable_preempt_count > 0 );
 	return head;
@@ -452,6 +438,6 @@
 	// Start by initializing the main thread
 	// SKULLDUGGERY: the mainThread steals the process main thread
-	// which will then be scheduled by the systemProcessor normally
-	mainThread = (thread_desc *)&mainThreadStorage;
+	// which will then be scheduled by the mainProcessor normally
+	mainThread = (thread_desc *)&storage_mainThread;
 	current_stack_info_t info;
 	(*mainThread){ &info };
@@ -459,32 +445,31 @@
 	LIB_DEBUG_PRINT_SAFE("Kernel : Main thread ready\n");
 
-	// Initialize the system cluster
-	systemCluster = (cluster *)&systemClusterStorage;
-	(*systemCluster){};
-
-	LIB_DEBUG_PRINT_SAFE("Kernel : System cluster ready\n");
-
-	// Initialize the system processor and the system processor ctx
+	// Initialize the main cluster
+	mainCluster = (cluster *)&storage_mainCluster;
+	(*mainCluster){};
+
+	LIB_DEBUG_PRINT_SAFE("Kernel : main cluster ready\n");
+
+	// Initialize the main processor and the main processor ctx
 	// (the coroutine that contains the processing control flow)
-	systemProcessor = (system_proc_t *)&systemProcessorStorage;
-	(*systemProcessor){ systemCluster, *(processorCtx_t *)&systemProcessorCtxStorage };
-
-	// 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);
+	mainProcessor = (processor *)&storage_mainProcessor;
+	(*mainProcessor){ mainCluster, *(processorCtx_t *)&storage_mainProcessorCtx };
 
 	//initialize the global state variables
-	this_processor = &systemProcessor->proc;
+	this_processor = mainProcessor;
 	this_thread = mainThread;
 	this_coroutine = &mainThread->cor;
-	disable_preempt_count = 1;
 
 	// Enable preemption
 	kernel_start_preemption();
 
-	// SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX
+	// Add the main thread to the ready queue
+	// once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
+	ScheduleThread(mainThread);
+
+	// SKULLDUGGERY: Force a context switch to the main 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.
-	resume( *systemProcessor->proc.runner );
+	resume( *mainProcessor->runner );
 
 
@@ -501,8 +486,8 @@
 	disable_interrupts();
 
-	// SKULLDUGGERY: Notify the systemProcessor it needs to terminates.
+	// SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
 	// When its coroutine terminates, it return control to the mainThread
 	// which is currently here
-	systemProcessor->proc.is_terminated = true;
+	mainProcessor->do_terminate = true;
 	suspend();
 
@@ -512,8 +497,8 @@
 	kernel_stop_preemption();
 
-	// Destroy the system processor and its context in reverse order of construction
+	// Destroy the main processor and its context in reverse order of construction
 	// These were manually constructed so we need manually destroy them
-	^(*systemProcessor->proc.runner){};
-	^(systemProcessor){};
+	^(*mainProcessor->runner){};
+	^(mainProcessor){};
 
 	// Final step, destroy the main thread since it is no longer needed
@@ -699,4 +684,5 @@
 	return top;
 }
+
 // Local Variables: //
 // mode: c //
Index: src/libcfa/concurrency/kernel_private.h
===================================================================
--- src/libcfa/concurrency/kernel_private.h	(revision 83a071f98d287fe0dd150376b0c6ea5ff812b18b)
+++ src/libcfa/concurrency/kernel_private.h	(revision 92360603d942184e66e5f92706ecc75c6b04f121)
@@ -1,3 +1,2 @@
-//                              -*- Mode: CFA -*-
 //
 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
@@ -10,11 +9,10 @@
 // Author           : Thierry Delisle
 // Created On       : Mon Feb 13 12:27:26 2017
-// Last Modified By : Thierry Delisle
-// Last Modified On : --
-// Update Count     : 0
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Sat Jul 22 09:58:09 2017
+// Update Count     : 2
 //
 
-#ifndef KERNEL_PRIVATE_H
-#define KERNEL_PRIVATE_H
+#pragma once
 
 #include "libhdr.h"
@@ -31,5 +29,5 @@
 extern "C" {
 	void disable_interrupts();
-	void enable_interrupts_noRF();
+	void enable_interrupts_noPoll();
 	void enable_interrupts( DEBUG_CTX_PARAM );
 }
@@ -45,4 +43,5 @@
 thread_desc * nextThread(cluster * this);
 
+//Block current thread and release/wake-up the following resources
 void BlockInternal(void);
 void BlockInternal(spinlock * lock);
@@ -65,18 +64,15 @@
 void spin(processor * this, unsigned int * spin_count);
 
-struct system_proc_t {
-	processor proc;
-
+struct event_kernel_t {
 	alarm_list_t alarms;
-	spinlock alarm_lock;
-
-	bool pending_alarm;
+	spinlock lock;
 };
 
-extern cluster * systemCluster;
-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 event_kernel_t * event_kernel;
+
+extern thread_local coroutine_desc * volatile this_coroutine;
+extern thread_local thread_desc *    volatile this_thread;
+extern thread_local processor *      volatile this_processor;
+
 extern volatile thread_local bool preemption_in_progress;
 extern volatile thread_local unsigned short disable_preempt_count;
@@ -91,5 +87,7 @@
 extern void ThreadCtxSwitch(coroutine_desc * src, coroutine_desc * dst);
 
-#endif //KERNEL_PRIVATE_H
+//-----------------------------------------------------------------------------
+// Utils
+#define KERNEL_STORAGE(T,X) static char storage_##X[sizeof(T)]
 
 // Local Variables: //
Index: src/libcfa/concurrency/monitor
===================================================================
--- src/libcfa/concurrency/monitor	(revision 83a071f98d287fe0dd150376b0c6ea5ff812b18b)
+++ src/libcfa/concurrency/monitor	(revision 92360603d942184e66e5f92706ecc75c6b04f121)
@@ -1,3 +1,2 @@
-//                              -*- Mode: CFA -*-
 //
 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
@@ -10,15 +9,14 @@
 // Author           : Thierry Delisle
 // Created On       : Thd Feb 23 12:27:26 2017
-// Last Modified By : Thierry Delisle
-// Last Modified On : --
-// Update Count     : 0
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Sat Jul 22 09:59:01 2017
+// Update Count     : 3
 //
 
-#ifndef MONITOR_H
-#define MONITOR_H
+#pragma once
 
 #include <stddef.h>
 
-#include "assert"
+#include <assert.h>
 #include "invoke.h"
 #include "stdlib"
@@ -99,3 +97,6 @@
 void __accept_internal( unsigned short count, __acceptable_t * acceptables, void (*func)(void) );
 
-#endif //MONITOR_H
+// Local Variables: //
+// mode: c //
+// tab-width: 4 //
+// End: //
Index: src/libcfa/concurrency/monitor.c
===================================================================
--- src/libcfa/concurrency/monitor.c	(revision 83a071f98d287fe0dd150376b0c6ea5ff812b18b)
+++ src/libcfa/concurrency/monitor.c	(revision 92360603d942184e66e5f92706ecc75c6b04f121)
@@ -1,3 +1,2 @@
-//                              -*- Mode: CFA -*-
 //
 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
@@ -10,7 +9,7 @@
 // Author           : Thierry Delisle
 // Created On       : Thd Feb 23 12:27:26 2017
-// Last Modified By : Thierry Delisle
-// Last Modified On : --
-// Update Count     : 0
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Mon Jul 31 14:59:05 2017
+// Update Count     : 3
 //
 
@@ -485,5 +484,5 @@
 	if( !this->monitors ) {
 		// LIB_DEBUG_PRINT_SAFE("Branding\n");
-		assertf( thrd->current_monitors != NULL, "No current monitor to brand condition", thrd->current_monitors );
+		assertf( thrd->current_monitors != NULL, "No current monitor to brand condition %p", thrd->current_monitors );
 		this->monitor_count = thrd->current_monitor_count;
 
@@ -528,2 +527,7 @@
 	return head;
 }
+
+// Local Variables: //
+// mode: c //
+// tab-width: 4 //
+// End: //
Index: src/libcfa/concurrency/preemption.c
===================================================================
--- src/libcfa/concurrency/preemption.c	(revision 83a071f98d287fe0dd150376b0c6ea5ff812b18b)
+++ src/libcfa/concurrency/preemption.c	(revision 92360603d942184e66e5f92706ecc75c6b04f121)
@@ -1,3 +1,2 @@
-//                              -*- Mode: CFA -*-
 //
 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
@@ -10,7 +9,7 @@
 // Author           : Thierry Delisle
 // Created On       : Mon Jun 5 14:20:42 2017
-// Last Modified By : Thierry Delisle
-// Last Modified On : --
-// Update Count     : 0
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Fri Jul 21 22:36:05 2017
+// Update Count     : 2
 //
 
@@ -34,24 +33,32 @@
 #endif
 
+//TODO move to defaults
 #define __CFA_DEFAULT_PREEMPTION__ 10000
 
+//TODO move to defaults
 __attribute__((weak)) unsigned int default_preemption() {
 	return __CFA_DEFAULT_PREEMPTION__;
 }
 
+// Short hands for signal context information
 #define __CFA_SIGCXT__ ucontext_t *
 #define __CFA_SIGPARMS__ __attribute__((unused)) int sig, __attribute__((unused)) siginfo_t *sfp, __attribute__((unused)) __CFA_SIGCXT__ cxt
 
+// FwdDeclarations : timeout handlers
 static void preempt( processor   * this );
 static void timeout( thread_desc * this );
 
+// FwdDeclarations : Signal handlers
 void sigHandler_ctxSwitch( __CFA_SIGPARMS__ );
-void sigHandler_alarm    ( __CFA_SIGPARMS__ );
 void sigHandler_segv     ( __CFA_SIGPARMS__ );
 void sigHandler_abort    ( __CFA_SIGPARMS__ );
 
+// FwdDeclarations : sigaction wrapper
 static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags );
-LIB_DEBUG_DO( bool validate( alarm_list_t * this ); )
-
+
+// FwdDeclarations : alarm thread main
+void * alarm_loop( __attribute__((unused)) void * args );
+
+// Machine specific register name
 #ifdef __x86_64__
 #define CFA_REG_IP REG_RIP
@@ -60,4 +67,12 @@
 #endif
 
+KERNEL_STORAGE(event_kernel_t, event_kernel);         // private storage for event kernel
+event_kernel_t * event_kernel;                        // kernel public handle to even kernel
+static pthread_t alarm_thread;                        // pthread handle to alarm thread
+
+void ?{}(event_kernel_t & this) {
+	(this.alarms){};
+	(this.lock){};
+}
 
 //=============================================================================================
@@ -65,13 +80,21 @@
 //=============================================================================================
 
+// Get next expired node
+static inline alarm_node_t * get_expired( alarm_list_t * alarms, __cfa_time_t currtime ) {
+	if( !alarms->head ) return NULL;                          // If no alarms return null
+	if( alarms->head->alarm >= currtime ) return NULL;        // If alarms head not expired return null
+	return pop(alarms);                                       // Otherwise just pop head
+}
+
+// Tick one frame of the Discrete Event Simulation for alarms
 void tick_preemption() {
-	// LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Ticking preemption\n" );
-
-	alarm_list_t * alarms = &systemProcessor->alarms;
-	__cfa_time_t currtime = __kernel_get_time();
-	while( alarms->head && alarms->head->alarm < currtime ) {
-		alarm_node_t * node = pop(alarms);
-		// LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ticking %p\n", node );
-
+	alarm_node_t * node = NULL;                     // Used in the while loop but cannot be declared in the while condition
+	alarm_list_t * alarms = &event_kernel->alarms;  // Local copy for ease of reading
+	__cfa_time_t currtime = __kernel_get_time();    // Check current time once so we everything "happens at once"
+
+	//Loop throught every thing expired
+	while( node = get_expired( alarms, currtime ) ) {
+
+		// Check if this is a kernel
 		if( node->kernel_alarm ) {
 			preempt( node->proc );
@@ -81,28 +104,22 @@
 		}
 
-		verify( validate( alarms ) );
-
-		if( node->period > 0 ) {
-			node->alarm = currtime + node->period;
-			insert( alarms, node );
+		// Check if this is a periodic alarm
+		__cfa_time_t period = node->period;
+		if( period > 0 ) {
+			node->alarm = currtime + period;    // Alarm is periodic, add currtime to it (used cached current time)
+			insert( alarms, node );             // Reinsert the node for the next time it triggers
 		}
 		else {
-			node->set = false;
-		}
-	}
-
-	if( alarms->head ) {
-		__kernel_set_timer( alarms->head->alarm - currtime );
-	}
-
-	verify( validate( alarms ) );
-	// LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ticking preemption done\n" );
-}
-
+			node->set = false;                  // Node is one-shot, just mark it as not pending
+		}
+	}
+
+	// If there are still alarms pending, reset the timer
+	if( alarms->head ) { __kernel_set_timer( alarms->head->alarm - currtime ); }
+}
+
+// Update the preemption of a processor and notify interested parties
 void update_preemption( processor * this, __cfa_time_t duration ) {
-	LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Processor : %p updating preemption to %lu\n", this, duration );
-
 	alarm_node_t * alarm = this->preemption_alarm;
-	duration *= 1000;
 
 	// Alarms need to be enabled
@@ -134,20 +151,20 @@
 
 extern "C" {
+	// Disable interrupts by incrementing the counter
 	void disable_interrupts() {
 		__attribute__((unused)) unsigned short new_val = __atomic_add_fetch_2( &disable_preempt_count, 1, __ATOMIC_SEQ_CST );
-		verify( new_val < (unsigned short)65_000 );
-		verify( new_val != (unsigned short) 0 );
-	}
-
-	void enable_interrupts_noRF() {
-		__attribute__((unused)) unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST );
-		verify( prev != (unsigned short) 0 );
-	}
-
+		verify( new_val < 65_000u );              // If this triggers someone is disabling interrupts without enabling them
+	}
+
+	// Enable interrupts by decrementing the counter
+	// If counter reaches 0, execute any pending CtxSwitch
 	void enable_interrupts( DEBUG_CTX_PARAM ) {
-		processor * proc   = this_processor;
-		thread_desc * thrd = this_thread;
+		processor * proc   = this_processor;      // Cache the processor now since interrupts can start happening after the atomic add
+		thread_desc * thrd = this_thread;         // Cache the thread now since interrupts can start happening after the atomic add
+
 		unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST );
-		verify( prev != (unsigned short) 0 );
+		verify( prev != 0u );                     // If this triggers someone is enabled already enabled interruptsverify( prev != 0u );
+
+		// Check if we need to prempt the thread because an interrupt was missed
 		if( prev == 1 && proc->pending_preemption ) {
 			proc->pending_preemption = false;
@@ -155,8 +172,17 @@
 		}
 
+		// For debugging purposes : keep track of the last person to enable the interrupts
 		LIB_DEBUG_DO( proc->last_enable = caller; )
 	}
-}
-
+
+	// Disable interrupts by incrementint the counter
+	// Don't execute any pending CtxSwitch even if counter reaches 0
+	void enable_interrupts_noPoll() {
+		__attribute__((unused)) unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST );
+		verify( prev != 0u );                     // If this triggers someone is enabled already enabled interrupts
+	}
+}
+
+// sigprocmask wrapper : unblock a single signal
 static inline void signal_unblock( int sig ) {
 	sigset_t mask;
@@ -169,4 +195,5 @@
 }
 
+// sigprocmask wrapper : block a single signal
 static inline void signal_block( int sig ) {
 	sigset_t mask;
@@ -179,36 +206,44 @@
 }
 
-static inline bool preemption_ready() {
-	return disable_preempt_count == 0 && !preemption_in_progress;
-}
-
-static inline void defer_ctxSwitch() {
-	this_processor->pending_preemption = true;
-}
-
-static inline void defer_alarm() {
-	systemProcessor->pending_alarm = true;
-}
-
+// kill wrapper : signal a processor
 static void preempt( processor * this ) {
 	pthread_kill( this->kernel_thread, SIGUSR1 );
 }
 
+// reserved for future use
 static void timeout( thread_desc * this ) {
 	//TODO : implement waking threads
 }
 
+
+// Check if a CtxSwitch signal handler shoud defer
+// If true  : preemption is safe
+// If false : preemption is unsafe and marked as pending
+static inline bool preemption_ready() {
+	bool ready = disable_preempt_count == 0 && !preemption_in_progress; // Check if preemption is safe
+	this_processor->pending_preemption = !ready;                        // Adjust the pending flag accordingly
+	return ready;
+}
+
 //=============================================================================================
 // Kernel Signal Startup/Shutdown logic
 //=============================================================================================
 
-static pthread_t alarm_thread;
-void * alarm_loop( __attribute__((unused)) void * args );
-
+// Startup routine to activate preemption
+// Called from kernel_startup
 void kernel_start_preemption() {
 	LIB_DEBUG_PRINT_SAFE("Kernel : Starting preemption\n");
-	__kernel_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO );
-	// __kernel_sigaction( SIGSEGV, sigHandler_segv     , SA_SIGINFO );
-	// __kernel_sigaction( SIGBUS , sigHandler_segv     , SA_SIGINFO );
+
+	// Start with preemption disabled until ready
+	disable_preempt_count = 1;
+
+	// Initialize the event kernel
+	event_kernel = (event_kernel_t *)&storage_event_kernel;
+	(*event_kernel){};
+
+	// Setup proper signal handlers
+	__kernel_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO );         // CtxSwitch handler
+	// __kernel_sigaction( SIGSEGV, sigHandler_segv     , SA_SIGINFO );      // Failure handler
+	// __kernel_sigaction( SIGBUS , sigHandler_segv     , SA_SIGINFO );      // Failure handler
 
 	signal_block( SIGALRM );
@@ -217,22 +252,34 @@
 }
 
+// Shutdown routine to deactivate preemption
+// Called from kernel_shutdown
 void kernel_stop_preemption() {
 	LIB_DEBUG_PRINT_SAFE("Kernel : Preemption stopping\n");
 
+	// Block all signals since we are already shutting down
 	sigset_t mask;
 	sigfillset( &mask );
 	sigprocmask( SIG_BLOCK, &mask, NULL );
 
+	// Notify the alarm thread of the shutdown
 	sigval val = { 1 };
 	pthread_sigqueue( alarm_thread, SIGALRM, val );
+
+	// Wait for the preemption thread to finish
 	pthread_join( alarm_thread, NULL );
+
+	// Preemption is now fully stopped
+
 	LIB_DEBUG_PRINT_SAFE("Kernel : Preemption stopped\n");
 }
 
+// Raii ctor/dtor for the preemption_scope
+// Used by thread to control when they want to receive preemption signals
 void ?{}( preemption_scope & this, processor * proc ) {
-	(this.alarm){ proc };
+	(this.alarm){ proc, zero_time, zero_time };
 	this.proc = proc;
 	this.proc->preemption_alarm = &this.alarm;
-	update_preemption( this.proc, this.proc->preemption );
+
+	update_preemption( this.proc, from_us(this.proc->cltr->preemption) );
 }
 
@@ -240,5 +287,5 @@
 	disable_interrupts();
 
-	update_preemption( this.proc, 0 );
+	update_preemption( this.proc, zero_time );
 }
 
@@ -247,19 +294,25 @@
 //=============================================================================================
 
+// Context switch signal handler
+// Receives SIGUSR1 signal and causes the current thread to yield
 void sigHandler_ctxSwitch( __CFA_SIGPARMS__ ) {
 	LIB_DEBUG_DO( last_interrupt = (void *)(cxt->uc_mcontext.gregs[CFA_REG_IP]); )
-	if( preemption_ready() ) {
-		preemption_in_progress = true;
-		signal_unblock( SIGUSR1 );
-		this_processor->pending_preemption = false;
-		preemption_in_progress = false;
-		BlockInternal( (thread_desc*)this_thread );
-	}
-	else {
-		defer_ctxSwitch();
-	}
-}
-
+
+	// Check if it is safe to preempt here
+	if( !preemption_ready() ) { return; }
+
+	preemption_in_progress = true;                      // Sync flag : prevent recursive calls to the signal handler
+	signal_unblock( SIGUSR1 );                          // We are about to CtxSwitch out of the signal handler, let other handlers in
+	preemption_in_progress = false;                     // Clear the in progress flag
+
+	// Preemption can occur here
+
+	BlockInternal( (thread_desc*)this_thread );         // Do the actual CtxSwitch
+}
+
+// Main of the alarm thread
+// Waits on SIGALRM and send SIGUSR1 to whom ever needs it
 void * alarm_loop( __attribute__((unused)) void * args ) {
+	// Block sigalrms to control when they arrive
 	sigset_t mask;
 	sigemptyset( &mask );
@@ -270,35 +323,40 @@
 	}
 
+	// Main loop
 	while( true ) {
+		// Wait for a sigalrm
 		siginfo_t info;
 		int sig = sigwaitinfo( &mask, &info );
-		if( sig < 0 ) {
-			abortf( "internal error, sigwait" );
-		}
-		else if( sig == SIGALRM )
+
+		// If another signal arrived something went wrong
+		assertf(sig == SIGALRM, "Kernel Internal Error, sigwait: Unexpected signal %d (%d : %d)\n", sig, info.si_code, info.si_value.sival_int);
+
+		LIB_DEBUG_PRINT_SAFE("Kernel : Caught alarm from %d with %d\n", info.si_code, info.si_value.sival_int );
+		// Switch on the code (a.k.a. the sender) to
+		switch( info.si_code )
 		{
-			LIB_DEBUG_PRINT_SAFE("Kernel : Caught signal %d (%d)\n", sig, info.si_value.sival_int );
-			if( info.si_value.sival_int == 0 )
-			{
-				LIB_DEBUG_PRINT_SAFE("Kernel : Preemption thread tick\n");
-				lock( &systemProcessor->alarm_lock DEBUG_CTX2 );
-				tick_preemption();
-				unlock( &systemProcessor->alarm_lock );
-			}
-			else if( info.si_value.sival_int == 1 )
-			{
-				break;
-			}
-		}
-		else
-		{
-			LIB_DEBUG_PRINT_SAFE("Kernel : Unexpected signal %d (%d)\n", sig, info.si_value.sival_int);
-		}
-	}
-
+		// Timers can apparently be marked as sent for the kernel
+		// In either case, tick preemption
+		case SI_TIMER:
+		case SI_KERNEL:
+			LIB_DEBUG_PRINT_SAFE("Kernel : Preemption thread tick\n");
+			lock( &event_kernel->lock DEBUG_CTX2 );
+			tick_preemption();
+			unlock( &event_kernel->lock );
+			break;
+		// Signal was not sent by the kernel but by an other thread
+		case SI_QUEUE:
+			// For now, other thread only signal the alarm thread to shut it down
+			// If this needs to change use info.si_value and handle the case here
+			goto EXIT;
+		}
+	}
+
+EXIT:
 	LIB_DEBUG_PRINT_SAFE("Kernel : Preemption thread stopping\n");
 	return NULL;
 }
 
+// Sigaction wrapper : register an signal handler
 static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags ) {
 	struct sigaction act;
@@ -316,10 +374,9 @@
 }
 
-typedef void (*sa_handler_t)(int);
-
+// Sigaction wrapper : restore default handler
 static void __kernel_sigdefault( int sig ) {
 	struct sigaction act;
 
-	// act.sa_handler = SIG_DFL;
+	act.sa_handler = SIG_DFL;
 	act.sa_flags = 0;
 	sigemptyset( &act.sa_mask );
@@ -429,2 +486,7 @@
 // 	raise( SIGABRT );
 // }
+
+// Local Variables: //
+// mode: c //
+// tab-width: 4 //
+// End: //
Index: src/libcfa/concurrency/preemption.h
===================================================================
--- src/libcfa/concurrency/preemption.h	(revision 83a071f98d287fe0dd150376b0c6ea5ff812b18b)
+++ src/libcfa/concurrency/preemption.h	(revision 92360603d942184e66e5f92706ecc75c6b04f121)
@@ -1,3 +1,2 @@
-//                              -*- Mode: CFA -*-
 //
 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
@@ -10,11 +9,10 @@
 // Author           : Thierry Delisle
 // Created On       : Mon Jun 5 14:20:42 2017
-// Last Modified By : Thierry Delisle
-// Last Modified On : --
-// Update Count     : 0
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Fri Jul 21 22:34:25 2017
+// Update Count     : 1
 //
 
-#ifndef PREEMPTION_H
-#define PREEMPTION_H
+#pragma once
 
 #include "alarm.h"
@@ -35,3 +33,6 @@
 void ^?{}( preemption_scope & this );
 
-#endif //PREEMPTION_H
+// Local Variables: //
+// mode: c //
+// tab-width: 4 //
+// End: //
Index: src/libcfa/concurrency/thread
===================================================================
--- src/libcfa/concurrency/thread	(revision 83a071f98d287fe0dd150376b0c6ea5ff812b18b)
+++ src/libcfa/concurrency/thread	(revision 92360603d942184e66e5f92706ecc75c6b04f121)
@@ -1,3 +1,2 @@
-//                              -*- Mode: CFA -*-
 //
 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
@@ -10,13 +9,12 @@
 // Author           : Thierry Delisle
 // Created On       : Tue Jan 17 12:27:26 2017
-// Last Modified By : Thierry Delisle
-// Last Modified On : --
-// Update Count     : 0
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Sat Jul 22 09:59:40 2017
+// Update Count     : 3
 //
 
-#ifndef THREADS_H
-#define THREADS_H
+#pragma once
 
-#include "assert"
+#include <assert.h>
 #include "invoke.h"
 
@@ -54,5 +52,5 @@
 }
 
-extern volatile thread_local thread_desc * this_thread;
+extern thread_local thread_desc * volatile this_thread;
 
 forall( dtype T | is_thread(T) )
@@ -84,6 +82,4 @@
 void yield( unsigned times );
 
-#endif //THREADS_H
-
 // Local Variables: //
 // mode: c //
Index: src/libcfa/concurrency/thread.c
===================================================================
--- src/libcfa/concurrency/thread.c	(revision 83a071f98d287fe0dd150376b0c6ea5ff812b18b)
+++ src/libcfa/concurrency/thread.c	(revision 92360603d942184e66e5f92706ecc75c6b04f121)
@@ -1,3 +1,2 @@
-//                              -*- Mode: CFA -*-
 //
 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
@@ -10,7 +9,7 @@
 // Author           : Thierry Delisle
 // Created On       : Tue Jan 17 12:27:26 2017
-// Last Modified By : Thierry Delisle
-// Last Modified On : --
-// Update Count     : 0
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Fri Jul 21 22:34:46 2017
+// Update Count     : 1
 //
 
@@ -87,5 +86,5 @@
 
 void yield( void ) {
-	BlockInternal( (thread_desc *)this_thread );
+	BlockInternal( this_thread );
 }
 
