Index: libcfa/src/concurrency/clib/cfathread.cfa
===================================================================
--- libcfa/src/concurrency/clib/cfathread.cfa	(revision b1d83ba11e9818e08439f842783270fdaa5a987b)
+++ libcfa/src/concurrency/clib/cfathread.cfa	(revision a1538cd646f1e032f1abd29e7abb8e6ece8a787a)
@@ -14,17 +14,53 @@
 //
 
+#include "fstream.hfa"
+#include "locks.hfa"
 #include "kernel.hfa"
 #include "thread.hfa"
-
-thread CRunner {
-	void (*themain)( CRunner * );
+#include "time.hfa"
+
+#include "cfathread.h"
+
+struct cfathread_object {
+	$thread self;
+	void * (*themain)( void * );
+	void * arg;
+	void * ret;
 };
-
-static void ?{}( CRunner & this, void (*themain)( CRunner * ) ) {
+void main(cfathread_object & this);
+void ^?{}(cfathread_object & mutex this);
+
+static inline $thread * get_thread( cfathread_object & this ) { return &this.self; }
+
+typedef ThreadCancelled(cfathread_object) cfathread_exception;
+typedef ThreadCancelled_vtable(cfathread_object) cfathread_vtable;
+
+void defaultResumptionHandler(ThreadCancelled(cfathread_object) & except) {
+	abort | "A thread was cancelled";
+}
+
+cfathread_vtable _cfathread_vtable_instance;
+
+cfathread_vtable const & get_exception_vtable(cfathread_exception *) {
+	return _cfathread_vtable_instance;
+}
+
+static void ?{}( cfathread_object & this, cluster & cl, void *(*themain)( void * ), void * arg ) {
 	this.themain = themain;
-}
-
-void main( CRunner & this ) {
-	this.themain( &this );
+	this.arg = arg;
+	((thread&)this){"C-thread", cl};
+	__thrd_start(this, main);
+}
+
+void ^?{}(cfathread_object & mutex this) {
+	^(this.self){};
+}
+
+void main( cfathread_object & this ) {
+	__attribute__((unused)) void * const thrd_obj = (void*)&this;
+	__attribute__((unused)) void * const thrd_hdl = (void*)active_thread();
+	/* paranoid */ verify( thrd_obj == thrd_hdl );
+
+	this.ret = this.themain( this.arg );
 }
 
@@ -33,12 +69,69 @@
 
 extern "C" {
-	//--------------------
-	// Basic thread management
-	CRunner * cfathread_create( void (*main)( CRunner * ) ) {
-		return new( main );
-	}
-
-	void cfathread_join( CRunner * thrd ) {
+	int cfathread_cluster_create(cfathread_cluster_t * cl) __attribute__((nonnull(1))) {
+		*cl = new();
+		return 0;
+	}
+
+	cfathread_cluster_t cfathread_cluster_self(void) {
+		return active_cluster();
+	}
+
+	int cfathread_cluster_add_worker(cfathread_cluster_t cl, pthread_t* tid, void (*init_routine) (void *), void * arg) {
+		// processor * proc = new("C-processor", *cl, init_routine, arg);
+		processor * proc = alloc();
+		(*proc){ "C-processor", *cl, init_routine, arg };
+		if(tid) *tid = proc->kernel_thread;
+		return 0;
+	}
+
+	int cfathread_cluster_pause (cfathread_cluster_t) {
+		abort | "Pausing clusters is not supported";
+		exit(1);
+	}
+
+	int cfathread_cluster_resume(cfathread_cluster_t) {
+		abort | "Resuming clusters is not supported";
+		exit(1);
+	}
+
+	//--------------------
+	// Thread attributes
+	int cfathread_attr_init(cfathread_attr_t *attr) __attribute__((nonnull (1))) {
+		attr->cl = active_cluster();
+		return 0;
+	}
+
+	//--------------------
+	// Thread
+	int cfathread_create( cfathread_t * handle, cfathread_attr_t * attr, void *(*main)( void * ), void * arg ) __attribute__((nonnull (1))) {
+		cluster * cl = attr ? attr->cl : active_cluster();
+		cfathread_t thrd = alloc();
+		(*thrd){ *cl, main, arg };
+		*handle = thrd;
+		return 0;
+	}
+
+	int cfathread_join( cfathread_t thrd, void ** retval ) {
+		void * ret = join( *thrd ).ret;
 		delete( thrd );
+		if(retval) {
+			*retval = ret;
+		}
+		return 0;
+	}
+
+	cfathread_t cfathread_self(void) {
+		return (cfathread_t)active_thread();
+	}
+
+	int cfathread_usleep(useconds_t usecs) {
+		sleep(usecs`us);
+		return 0;
+	}
+
+	int cfathread_sleep(unsigned int secs) {
+		sleep(secs`s);
+		return 0;
 	}
 
@@ -47,5 +140,5 @@
 	}
 
-	void cfathread_unpark( CRunner * thrd ) {
+	void cfathread_unpark( cfathread_t thrd ) {
 		unpark( *thrd );
 	}
@@ -55,12 +148,109 @@
 	}
 
-	//--------------------
-	// Basic kernel features
-	void cfathread_setproccnt( int ncnt ) {
-		assert( ncnt >= 1 );
-		adelete( procs );
-
-		proc_cnt = ncnt - 1;
-		procs = anew(proc_cnt);
-	}
-}
+	typedef struct cfathread_mutex * cfathread_mutex_t;
+
+	//--------------------
+	// Mutex
+	struct cfathread_mutex {
+		single_acquisition_lock impl;
+	};
+	int cfathread_mutex_init(cfathread_mutex_t *restrict mut, const cfathread_mutexattr_t *restrict) __attribute__((nonnull (1))) { *mut = new(); return 0; }
+	int cfathread_mutex_destroy(cfathread_mutex_t *mut) __attribute__((nonnull (1))) { delete( *mut ); return 0; }
+	int cfathread_mutex_lock   (cfathread_mutex_t *mut) __attribute__((nonnull (1))) { lock    ( (*mut)->impl ); return 0; }
+	int cfathread_mutex_trylock(cfathread_mutex_t *mut) __attribute__((nonnull (1))) { try_lock( (*mut)->impl ); return 0; }
+	int cfathread_mutex_unlock (cfathread_mutex_t *mut) __attribute__((nonnull (1))) { unlock  ( (*mut)->impl ); return 0; }
+
+	//--------------------
+	// Condition
+	struct cfathread_condition {
+		condition_variable(single_acquisition_lock) impl;
+	};
+	int cfathread_cond_init(cfathread_cond_t *restrict cond, const cfathread_condattr_t *restrict) __attribute__((nonnull (1))) { *cond = new(); return 0; }
+	int cfathread_cond_signal(cfathread_cond_t *cond) __attribute__((nonnull (1)))  { notify_one( (*cond)->impl ); return 0; }
+	int cfathread_cond_wait(cfathread_cond_t *restrict cond, cfathread_mutex_t *restrict mut) __attribute__((nonnull (1,2))) { wait( (*cond)->impl, (*mut)->impl ); return 0; }
+	int cfathread_cond_timedwait(cfathread_cond_t *restrict cond, cfathread_mutex_t *restrict mut, const struct timespec *restrict abstime) __attribute__((nonnull (1,2,3))) {
+		Time t = { *abstime };
+		if( wait( (*cond)->impl, (*mut)->impl, t ) ) {
+			return 0;
+		}
+		errno = ETIMEDOUT;
+		return ETIMEDOUT;
+	}
+}
+
+#include <iofwd.hfa>
+
+extern "C" {
+	#include <unistd.h>
+	#include <sys/types.h>
+	#include <sys/socket.h>
+
+	//--------------------
+	// IO operations
+	int cfathread_socket(int domain, int type, int protocol) {
+		return socket(domain, type, protocol);
+	}
+	int cfathread_bind(int socket, const struct sockaddr *address, socklen_t address_len) {
+		return bind(socket, address, address_len);
+	}
+
+	int cfathread_listen(int socket, int backlog) {
+		return listen(socket, backlog);
+	}
+
+	int cfathread_accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len) {
+		return cfa_accept4(socket, address, address_len, 0, CFA_IO_LAZY);
+	}
+
+	int cfathread_connect(int socket, const struct sockaddr *address, socklen_t address_len) {
+		return cfa_connect(socket, address, address_len, CFA_IO_LAZY);
+	}
+
+	int cfathread_dup(int fildes) {
+		return dup(fildes);
+	}
+
+	int cfathread_close(int fildes) {
+		return cfa_close(fildes, CFA_IO_LAZY);
+	}
+
+	ssize_t cfathread_sendmsg(int socket, const struct msghdr *message, int flags) {
+		return cfa_sendmsg(socket, message, flags, CFA_IO_LAZY);
+	}
+
+	ssize_t cfathread_write(int fildes, const void *buf, size_t nbyte) {
+		return cfa_write(fildes, buf, nbyte, CFA_IO_LAZY);
+	}
+
+	ssize_t cfathread_recvfrom(int socket, void *restrict buffer, size_t length, int flags, struct sockaddr *restrict address, socklen_t *restrict address_len)  {
+		struct iovec iov;
+		iov.iov_base = buffer;
+		iov.iov_len = length;
+
+		struct msghdr msg;
+		msg.msg_name = address;
+		msg.msg_namelen = address_len ? (socklen_t)*address_len : (socklen_t)0;
+		msg.msg_iov = &iov;
+		msg.msg_iovlen = 1;
+		msg.msg_control = 0p;
+		msg.msg_controllen = 0;
+
+		ssize_t ret = cfa_recvmsg(socket, &msg, flags, CFA_IO_LAZY);
+
+		if(address_len) *address_len = msg.msg_namelen;
+		return ret;
+	}
+
+	ssize_t cfathread_read(int fildes, void *buf, size_t nbyte) {
+		return cfa_read(fildes, buf, nbyte, CFA_IO_LAZY);
+	}
+
+	void cfathread_suspendFD(int) {
+		abort | "Suspending File Descriptors is not supported";
+	}
+
+	void cfathread_resumeFD (int) {
+		abort | "Resuming File Descriptors is not supported";
+	}
+
+}
Index: libcfa/src/concurrency/clib/cfathread.h
===================================================================
--- libcfa/src/concurrency/clib/cfathread.h	(revision b1d83ba11e9818e08439f842783270fdaa5a987b)
+++ libcfa/src/concurrency/clib/cfathread.h	(revision a1538cd646f1e032f1abd29e7abb8e6ece8a787a)
@@ -20,13 +20,44 @@
 extern "C" {
 #endif
+	#include <asm/types.h>
+	#include <errno.h>
+	#include <unistd.h>
+
+
 	//--------------------
 	// Basic types
-	struct cfathread_CRunner_t;
-	typedef struct cfathread_CRunner_t * cfathread_t;
+
+	typedef struct cluster * cfathread_cluster_t;
+
+	int cfathread_cluster_create(cfathread_cluster_t * cluster) __attribute__((nonnull(1)));
+	cfathread_cluster_t cfathread_cluster_self(void);
+	int cfathread_cluster_add_worker(cfathread_cluster_t cluster, pthread_t* tid, void (*init_routine) (void *), void * arg);
+	int cfathread_cluster_pause (cfathread_cluster_t cluster);
+	int cfathread_cluster_resume(cfathread_cluster_t cluster);
 
 	//--------------------
-	// Basic thread support
-	cfathread_t cfathread_create( void (*main)( cfathread_t ) );
-	void cfathread_join( cfathread_t );
+	// thread attribute
+	typedef struct cfathread_attr {
+		cfathread_cluster_t cl;
+	} cfathread_attr_t;
+
+	int cfathread_attr_init(cfathread_attr_t * attr) __attribute__((nonnull (1)));
+	static inline int cfathread_attr_destroy(cfathread_attr_t * attr) __attribute__((nonnull (1))) { return 0; }
+	static inline int cfathread_attr_setbackground(cfathread_attr_t * attr, int background) __attribute__((nonnull (1))) { return 0; }
+	static inline int cfathread_attr_setcluster(cfathread_attr_t * attr, cfathread_cluster_t cl) __attribute__((nonnull (1))) { attr->cl = cl; return 0; }
+
+	//--------------------
+	// thread type
+	struct cfathread_object;
+	typedef struct cfathread_object * cfathread_t;
+
+	int cfathread_create( cfathread_t * h, cfathread_attr_t * a, void *(*main)( void * ), void * arg ) __attribute__((nonnull (1)));
+	int cfathread_join( cfathread_t, void ** retval );
+
+	int cfathread_get_errno(void);
+	cfathread_t cfathread_self(void);
+
+	int cfathread_usleep(useconds_t usecs);
+	int cfathread_sleep(unsigned int secs);
 
 	void cfathread_park( void );
@@ -35,7 +66,42 @@
 
 	//--------------------
-	// Basic kernel features
-	void cfathread_setproccnt( int );
+	// mutex and condition
+	struct timespec;
 
+	typedef struct cfathread_mutex_attr {
+	} cfathread_mutexattr_t;
+	typedef struct cfathread_mutex * cfathread_mutex_t;
+	int cfathread_mutex_init(cfathread_mutex_t *restrict mut, const cfathread_mutexattr_t *restrict attr) __attribute__((nonnull (1)));
+	int cfathread_mutex_destroy(cfathread_mutex_t *mut) __attribute__((nonnull (1)));
+	int cfathread_mutex_lock(cfathread_mutex_t *mut) __attribute__((nonnull (1)));
+	int cfathread_mutex_trylock(cfathread_mutex_t *mut) __attribute__((nonnull (1)));
+	int cfathread_mutex_unlock(cfathread_mutex_t *mut) __attribute__((nonnull (1)));
+
+	typedef struct cfathread_cond_attr {
+	} cfathread_condattr_t;
+	typedef struct cfathread_condition * cfathread_cond_t;
+	int cfathread_cond_init(cfathread_cond_t *restrict cond, const cfathread_condattr_t *restrict attr) __attribute__((nonnull (1)));
+	int cfathread_cond_wait(cfathread_cond_t *restrict cond, cfathread_mutex_t *restrict mut) __attribute__((nonnull (1,2)));
+	int cfathread_cond_timedwait(cfathread_cond_t *restrict cond, cfathread_mutex_t *restrict mut, const struct timespec *restrict abstime) __attribute__((nonnull (1,2,3)));
+	int cfathread_cond_signal(cfathread_cond_t *cond) __attribute__((nonnull (1)));
+
+	//--------------------
+	// IO operations
+	struct sockaddr;
+	struct msghdr;
+	int cfathread_socket(int domain, int type, int protocol);
+	int cfathread_bind(int socket, const struct sockaddr *address, socklen_t address_len);
+	int cfathread_listen(int socket, int backlog);
+	int cfathread_accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len);
+	int cfathread_connect(int socket, const struct sockaddr *address, socklen_t address_len);
+	int cfathread_dup(int fildes);
+	int cfathread_close(int fildes);
+	ssize_t cfathread_sendmsg(int socket, const struct msghdr *message, int flags);
+	ssize_t cfathread_write(int fildes, const void *buf, size_t nbyte);
+	ssize_t cfathread_recvfrom(int socket, void *restrict buffer, size_t length, int flags, struct sockaddr *restrict address, socklen_t *restrict address_len);
+	ssize_t cfathread_read(int fildes, void *buf, size_t nbyte);
+
+	void cfathread_suspendFD(int fd);
+	void cfathread_resumeFD (int fd);
 
 #if defined(__cforall) || defined(__cplusplus)
Index: libcfa/src/concurrency/kernel.cfa
===================================================================
--- libcfa/src/concurrency/kernel.cfa	(revision b1d83ba11e9818e08439f842783270fdaa5a987b)
+++ libcfa/src/concurrency/kernel.cfa	(revision a1538cd646f1e032f1abd29e7abb8e6ece8a787a)
@@ -149,4 +149,9 @@
 	#endif
 
+	// if we need to run some special setup, now is the time to do it.
+	if(this->init.fnc) {
+		this->init.fnc(this->init.arg);
+	}
+
 	{
 		// Setup preemption data
Index: libcfa/src/concurrency/kernel.hfa
===================================================================
--- libcfa/src/concurrency/kernel.hfa	(revision b1d83ba11e9818e08439f842783270fdaa5a987b)
+++ libcfa/src/concurrency/kernel.hfa	(revision a1538cd646f1e032f1abd29e7abb8e6ece8a787a)
@@ -107,4 +107,13 @@
 	DLISTED_MGD_IMPL_IN(processor)
 
+	// special init fields
+	// This is needed for memcached integration
+	// once memcached experiments are done this should probably be removed
+	// it is not a particularly safe scheme as it can make processors less homogeneous
+	struct {
+		void (*fnc) (void *);
+		void * arg;
+	} init;
+
 	#if !defined(__CFA_NO_STATISTICS__)
 		int print_stats;
@@ -118,10 +127,10 @@
 };
 
-void  ?{}(processor & this, const char name[], struct cluster & cltr);
+void  ?{}(processor & this, const char name[], struct cluster & cltr, void (*init) (void *), void * arg);
 void ^?{}(processor & this);
 
-static inline void  ?{}(processor & this)                    { this{ "Anonymous Processor", *mainCluster}; }
-static inline void  ?{}(processor & this, struct cluster & cltr)    { this{ "Anonymous Processor", cltr}; }
-static inline void  ?{}(processor & this, const char name[]) { this{name, *mainCluster }; }
+static inline void  ?{}(processor & this)                        { this{ "Anonymous Processor", *mainCluster, 0p, 0p}; }
+static inline void  ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr, 0p, 0p}; }
+static inline void  ?{}(processor & this, const char name[])     { this{name, *mainCluster, 0p, 0p }; }
 
 DLISTED_MGD_IMPL_OUT(processor)
Index: libcfa/src/concurrency/kernel/startup.cfa
===================================================================
--- libcfa/src/concurrency/kernel/startup.cfa	(revision b1d83ba11e9818e08439f842783270fdaa5a987b)
+++ libcfa/src/concurrency/kernel/startup.cfa	(revision a1538cd646f1e032f1abd29e7abb8e6ece8a787a)
@@ -73,5 +73,5 @@
 static void __kernel_first_resume( processor * this );
 static void __kernel_last_resume ( processor * this );
-static void init(processor & this, const char name[], cluster & _cltr);
+static void init(processor & this, const char name[], cluster & _cltr, void (*fnc) (void *), void * arg);
 static void deinit(processor & this);
 static void doregister( struct cluster & cltr );
@@ -198,5 +198,5 @@
 		( this.terminated ){};
 		( this.runner ){};
-		init( this, "Main Processor", *mainCluster );
+		init( this, "Main Processor", *mainCluster, 0p, 0p );
 		kernel_thread = pthread_self();
 
@@ -452,5 +452,5 @@
 }
 
-static void init(processor & this, const char name[], cluster & _cltr) with( this ) {
+static void init(processor & this, const char name[], cluster & _cltr, void (*fnc) (void *), void * arg) with( this ) {
 	this.name = name;
 	this.cltr = &_cltr;
@@ -464,4 +464,7 @@
 	this.io.dirty   = false;
 
+	this.init.fnc = fnc;
+	this.init.arg = arg;
+
 	this.idle = eventfd(0, 0);
 	if (idle < 0) {
@@ -513,10 +516,10 @@
 }
 
-void ?{}(processor & this, const char name[], cluster & _cltr) {
+void ?{}(processor & this, const char name[], cluster & _cltr, void (*fnc) (void *), void * arg) {
 	( this.terminated ){};
 	( this.runner ){};
 
 	disable_interrupts();
-		init( this, name, _cltr );
+		init( this, name, _cltr, fnc, arg );
 	enable_interrupts( __cfaabi_dbg_ctx );
 
