Index: libcfa/src/concurrency/pthread.cfa
===================================================================
--- libcfa/src/concurrency/pthread.cfa	(revision 1a452638e119559b489523986faa60a438969248)
+++ libcfa/src/concurrency/pthread.cfa	(revision 8bd886e83ef877a2ab3a69217f7d29b068ccb384)
@@ -33,5 +33,5 @@
 enum { PTHREAD_KEYS_MAX = 1024 };
 
-struct Pthread_values{
+struct pthread_values{
 	inline Seqable;
 	void* value;
@@ -39,24 +39,26 @@
 };
 
-
-static Pthread_values *& Back( Pthread_values * n ) {
-	return (Pthread_values *)Back( (Seqable *)n );
-}
-static Pthread_values *& Next( Pthread_values * n ) {
-	return (Pthread_values *)Next( (Colable *)n );
-}
-
-struct Pthread_keys{
+static inline {
+	pthread_values *& Back( pthread_values * n ) {
+		return (pthread_values *)Back( (Seqable *)n );
+	}
+
+	pthread_values *& Next( pthread_values * n ) {
+		return (pthread_values *)Next( (Colable *)n );
+	}
+}
+
+struct pthread_keys {
 	bool in_use;
 	void (*destructor)( void * );
-	Sequence(Pthread_values) threads;
-};  // Pthread keys
-
-static void ?{}(Pthread_keys& k){
+	Sequence(pthread_values) threads;
+};
+
+static void ?{}(pthread_keys& k){
 	k.threads{};
 }
 
 // Create storage separately to ensure no constructors are called.
-static Pthread_keys cfa_pthread_keys_storage[PTHREAD_KEYS_MAX] __attribute__((aligned (16)));
+static pthread_keys cfa_pthread_keys_storage[PTHREAD_KEYS_MAX] __attribute__((aligned (16)));
 
 static void init_pthread_storage(){
@@ -66,5 +68,5 @@
 }
 
-#define cfa_pthread_keys ((Pthread_keys *)cfa_pthread_keys_storage)
+#define cfa_pthread_keys ((pthread_keys *)cfa_pthread_keys_storage)
 
 /* Controlling the iterations of destructors for thread-specific data.  */
@@ -115,17 +117,20 @@
 /* mutex helper routines */
 static void mutex_check(pthread_mutex_t* t){
-	// Use double check to improve performance. Check is safe on x86; volatile prevents compiler reordering
+	// Use double check to improve performance.
+	// Check is safe on x86; volatile prevents compiler reordering
 	volatile pthread_mutex_t *const mutex_ = t;
+
 	// SKULLDUGGERY: not a portable way to access the kind field, /usr/include/x86_64-linux-gnu/bits/pthreadtypes.h
 	int _lock_val = ((pthread_mutex_t *)mutex_)->__data.__lock;
+
 	// if pthread_mutex_t is initialized by PTHREAD_MUTEX_INITIALIZER, _lock_val should be 0
-	if ( _lock_val == 0 ) {			// static initialized ?
-		lock(magic_mutex_check);	// race
+	if ( _lock_val == 0 ) {
+		lock(magic_mutex_check);
 		_lock_val = ((pthread_mutex_t *)mutex_)->__data.__lock;
-		if ( _lock_val == 0 ) {		// static initialized ?
-		pthread_mutex_init( t, NULL );
-		} // if
-		unlock(magic_mutex_check);	// race
-	} // if
+		if ( _lock_val == 0 ) {
+			pthread_mutex_init( t, NULL );
+		}
+		unlock(magic_mutex_check);
+	}
 } // mutex_check
 
@@ -168,19 +173,4 @@
 };
 
-
-/*
-static const cfaPthread_attr_t default_attrs = {
-	PTHREAD_SCOPE_SYSTEM,
-	PTHREAD_CREATE_JOINABLE,
-	(size_t)DEFAULT_STACK_SIZE,
-	(void *)NULL,
-	0,
-	PTHREAD_EXPLICIT_SCHED,
-	{0}
-};
-*/
-
-
-
 static cfaPthread_attr_t* get(const pthread_attr_t* attr){
 	static_assert(sizeof(pthread_attr_t) >= sizeof(cfaPthread_attr_t),"sizeof(pthread_attr_t) < sizeof(cfaPthread_attr_t)");
@@ -198,10 +188,19 @@
 	cfaPthread_attr_t attr;
 	pthread_t pthreadId;
-	void *joinval;										// pthreads return value
-	pthread_attr_t pthread_attr;						// pthread attributes
-	void *(*start_routine)(void *);                     // routine start
-	void *arg;					                        // thread parameter
-	Pthread_values* pthreadData;
-	bool isTerminated;                                  // flag used for tryjoin
+
+	// pthreads return value
+	void *joinval;
+
+	// pthread attributes
+	pthread_attr_t pthread_attr;
+
+	void *(*start_routine)(void *);
+	void *start_arg;
+
+	// thread local data
+	pthread_values* pthreadData;
+
+	// flag used for tryjoin
+	bool isTerminated;
 };
 
@@ -209,12 +208,6 @@
 //  cfaPthread entry point
 void main(cfaPthread& _thread) with(_thread){
-	joinval =  start_routine(arg);
+	joinval =  start_routine(start_arg);
 	isTerminated = true;
-}
-
-// generate pthread_t by cfaPthread ptr
-static pthread_t create( cfaPthread *p ) {
-	static_assert(sizeof(pthread_t) >= sizeof(cfaPthread*),"sizeof(pthread_t) < sizeof(cfaPthread*)");
-	return (pthread_t)p;
 }
 
@@ -224,7 +217,7 @@
 }
 
-static void pthread_deletespecific_( Pthread_values* values )  { // see uMachContext::invokeTask
-	Pthread_values* value;
-	Pthread_keys* key;
+static void pthread_deletespecific_( pthread_values* values )  { // see uMachContext::invokeTask
+	pthread_values* value;
+	pthread_keys* key;
 	bool destcalled = true;
 	if (values != NULL){
@@ -258,9 +251,10 @@
 static void ^?{}(cfaPthread & mutex t){
 	// delete pthread local storage
-	Pthread_values* values = t.pthreadData;
+	pthread_values * values = t.pthreadData;
 	pthread_deletespecific_(values);
 }
 
 static void ?{}(cfaPthread &t, pthread_t* _thread, const pthread_attr_t * _attr,void *(*start_routine)(void *), void * arg) {
+	static_assert(sizeof(pthread_t) >= sizeof(cfaPthread*), "pthread_t too small to hold a pointer: sizeof(pthread_t) < sizeof(cfaPthread*)");
 
 	// set up user thread stackSize
@@ -269,6 +263,5 @@
 
 	// initialize _thread & cfaPthread id
-	t.pthreadId = create(&t);
-	*_thread = t.pthreadId;
+	*_thread = t.pthreadId = (pthread_t)(&t);
 
 	// if attr null, self attr will be set as default_attrs; else set to attr
@@ -277,7 +270,7 @@
 	// init start routine and arguments
 	t.start_routine = start_routine;
-	t.arg = arg;
+	t.start_arg = arg;
 	t.pthreadData = NULL;
-}   // not used
+}
 
 
@@ -287,6 +280,5 @@
 	int pthread_attr_init(pthread_attr_t *attr) libcfa_public __THROW {
 		cfaPthread_attr_t* _attr = get(attr);
-		?{}(*_attr);
-		*_attr = default_attrs;
+		?{}(*_attr, default_attrs);
 		return 0;
 	}
@@ -360,11 +352,9 @@
 	// and destroyed with pthread_attr_destroy when no longer needed.
 	int pthread_getattr_np( pthread_t threadID, pthread_attr_t *attr ) libcfa_public __THROW { // GNU extension
-		// race condition during copy
-		cfaPthread_attr_t* _attr = get(attr);
-		?{}(*_attr);
-		if (_attr == NULL){
-			return ENOMEM;
-		}   // if
-		*_attr = lookup( threadID )->attr; // copy all fields
+		check_nonnull(attr);
+
+		// copy all fields
+		*get(attr) = lookup( threadID )->attr;
+
 		return 0;
 	} // pthread_getattr_np
@@ -376,36 +366,47 @@
 		cfaPthread *t = alloc();
 		(*t){_thread, attr, start_routine, arg};
-		//init_user_pthread(*t, _thread, attr, start_routine, arg);
-		if (t == NULL) return EAGAIN; //no resource
-		return 0;
-	}   //pthread_create_
+		return 0;
+	}
 
 
 	int pthread_join(pthread_t _thread, void **value_ptr) libcfa_public __THROW {
-		if (_thread == NULL) return EINVAL;   // if thread is invalid
+		// if thread is invalid
+		if (_thread == NULL) return EINVAL;
 		if (_thread == pthread_self()) return EDEADLK;
-		cfaPthread* p = lookup(_thread);    // get user thr pointer
+
+		// get user thr pointer
+		cfaPthread* p = lookup(_thread);
 		try {
 			join(*p);
-		} catchResume (ThreadCancelled(cfaPthread) * cancel) {} // if thread called pthread_exit
-		if (value_ptr != NULL ) *value_ptr = p->joinval;   // fetch result
-		delete(p);
-		return 0;
-	}   //pthread_join_
-
-	int pthread_tryjoin_np(pthread_t _thread, void **value_ptr) libcfa_public __THROW {
-		if (_thread == NULL) return EINVAL;  // if thread is invalid
-		if (_thread == pthread_self()) return EDEADLK;
-		cfaPthread* p = lookup(_thread);
-		if (!p->isTerminated) return EBUSY; // thread not finished ?
-		join( *p );
+		}
+		// if thread called pthread_exit
+		catchResume (ThreadCancelled(cfaPthread) * cancel) {}
+
+		// fetch result
 		if (value_ptr != NULL ) *value_ptr = p->joinval;
 		delete(p);
 		return 0;
-	}   //pthread_join_
+	}
+
+	int pthread_tryjoin_np(pthread_t _thread, void **value_ptr) libcfa_public __THROW {
+		// if thread is invalid
+		if (_thread == NULL) return EINVAL;
+		if (_thread == pthread_self()) return EDEADLK;
+
+		cfaPthread* p = lookup(_thread);
+
+		// thread not finished ?
+		if (!p->isTerminated) return EBUSY;
+
+		join( *p );
+
+		if (value_ptr != NULL ) *value_ptr = p->joinval;
+		delete(p);
+		return 0;
+	}
 
 	pthread_t pthread_self(void) libcfa_public __THROW {
-		return (pthread_t)((char*)active_thread()-(sizeof(cfaPthread)-sizeof(thread$)));
-	}   //pthread_self_
+		return (pthread_t)((uintptr_t)active_thread() - (sizeof(cfaPthread) - sizeof(thread$)));
+	}
 
 	void pthread_exit(void * status) libcfa_public __THROW {
@@ -551,7 +552,7 @@
 
 		// Remove key from all threads with a value.
-		Pthread_values& p;
-		Sequence(Pthread_values)& head = cfa_pthread_keys[key].threads;
-		for ( SeqIter(Pthread_values) iter = { head }; iter | p; ) {
+		pthread_values& p;
+		Sequence(pthread_values)& head = cfa_pthread_keys[key].threads;
+		for ( SeqIter(pthread_values) iter = { head }; iter | p; ) {
 			remove(head, p);
 			p.in_use = false;
@@ -565,5 +566,5 @@
 		cfaPthread* t = lookup(pthread_self());
 		// if current thread's pthreadData is NULL; initialize it
-		Pthread_values* values;
+		pthread_values* values;
 		if (t->pthreadData == NULL){
 			values = anew( PTHREAD_KEYS_MAX);
@@ -582,5 +583,5 @@
 			return EINVAL;
 		} // if
-		Pthread_values &entry = values[key];
+		pthread_values &entry = values[key];
 		if ( ! entry.in_use ) {
 			entry.in_use = true;
@@ -599,5 +600,5 @@
 		if (t->pthreadData == NULL) return NULL;
 		lock(key_lock);
-		Pthread_values &entry = ((Pthread_values *)t->pthreadData)[key];
+		pthread_values &entry = ((pthread_values *)t->pthreadData)[key];
 		if ( ! entry.in_use ) {
 			unlock( key_lock );
@@ -611,5 +612,5 @@
 
 	//######################### Parallelism #########################
-	void pthread_delete_kernel_threads_() libcfa_public __THROW {	// see uMain::~uMain
+	void pthread_delete_kernel_threads_() __THROW {	// see uMain::~uMain
 		Pthread_kernel_threads& p;
 		for ( StackIter(Pthread_kernel_threads) iter = {cfa_pthreads_kernel_threads}; iter | p; ) {
@@ -618,24 +619,24 @@
 	} // pthread_delete_kernel_threads_
 
-	int pthread_getconcurrency( void ) libcfa_public __THROW {	// XOPEN extension
+	int pthread_getconcurrency( void ) __THROW {	// XOPEN extension
 		return cfa_pthreads_kernel_threads_zero ? 0 : cfa_pthreads_no_kernel_threads;
 	} // pthread_getconcurrency
 
 	int pthread_setconcurrency( int new_level ) libcfa_public __THROW { // XOPEN extension
-	  if ( new_level < 0 ) return EINVAL;
-	  if ( new_level == 0 ) {
-		cfa_pthreads_kernel_threads_zero = true;	// remember set to zero, but ignore
-		return 0;					// do not do kernel thread management
-	  } // exit
-	  cfa_pthreads_kernel_threads_zero = false;
-	  lock( concurrency_lock );
-	  for ( ; new_level > cfa_pthreads_no_kernel_threads; cfa_pthreads_no_kernel_threads += 1 ) { // add processors ?
-		push(cfa_pthreads_kernel_threads, *new() );
-	  } // for
-	  for ( ; new_level < cfa_pthreads_no_kernel_threads; cfa_pthreads_no_kernel_threads -= 1 ) { // remove processors ?
-		delete(&pop(cfa_pthreads_kernel_threads));
-	  } // for
-	  unlock( concurrency_lock );
-	  return 0;
+		if ( new_level < 0 ) return EINVAL;
+		if ( new_level == 0 ) {
+			cfa_pthreads_kernel_threads_zero = true;	// remember set to zero, but ignore
+			return 0;					// do not do kernel thread management
+		} // exit
+		cfa_pthreads_kernel_threads_zero = false;
+		lock( concurrency_lock );
+		for ( ; new_level > cfa_pthreads_no_kernel_threads; cfa_pthreads_no_kernel_threads += 1 ) { // add processors ?
+			push(cfa_pthreads_kernel_threads, *new() );
+		} // for
+		for ( ; new_level < cfa_pthreads_no_kernel_threads; cfa_pthreads_no_kernel_threads -= 1 ) { // remove processors ?
+			delete(&pop(cfa_pthreads_kernel_threads));
+		} // for
+		unlock( concurrency_lock );
+		return 0;
 	} // pthread_setconcurrency
 
@@ -644,5 +645,6 @@
 
 	 int pthread_sigmask( int /* how */, const sigset_t * /* set */, sigset_t * /* oset */ ) libcfa_public __THROW {
-		 return 0;
+		abort( "pthread_sigmask : not implemented" );
+		return 0;
 	 } // pthread_sigmask
 
@@ -657,4 +659,5 @@
 
 	int pthread_sigqueue(pthread_t , int sig, const union sigval) libcfa_public __THROW {
+		abort( "pthread_sigqueue : not implemented" );
 		return 0;
 	} // pthread_sigqueue
@@ -662,5 +665,5 @@
 	//######################### Scheduling #########################
 	int pthread_detach( pthread_t threadID ) __THROW {
-		abort( "pthread_detach" );
+		abort( "pthread_detach : not implemented" );
 		return 0;
 	} // pthread_detach
@@ -683,45 +686,45 @@
 
 	int pthread_mutexattr_destroy( pthread_mutexattr_t * /* attr */ ) libcfa_public __THROW {
-	return 0;
+		return 0;
 	} // pthread_mutexattr_destroy
 
 	int pthread_mutexattr_setpshared( pthread_mutexattr_t * /* attr */, int /* pshared */ ) libcfa_public __THROW {
-	return 0;
+		return 0;
 	} // pthread_mutexattr_setpshared
 
 	int pthread_mutexattr_getpshared( const pthread_mutexattr_t * /* attr */, int * /* pshared */ ) libcfa_public __THROW {
-	return 0;
+		return 0;
 	} // pthread_mutexattr_getpshared
 
 	int pthread_mutexattr_setprotocol( pthread_mutexattr_t * /* attr */, int /* protocol */ ) libcfa_public __THROW {
-	return 0;
+		return 0;
 	} // pthread_mutexattr_setprotocol
 
 	int pthread_mutexattr_getprotocol( const pthread_mutexattr_t * /* attr */, int * /* protocol */ ) libcfa_public __THROW {
-	return 0;
+		return 0;
 	} // pthread_mutexattr_getprotocol
 
 	int pthread_mutexattr_setprioceiling( pthread_mutexattr_t * /* attr */, int /* prioceiling */ ) libcfa_public __THROW {
-	return 0;
+		return 0;
 	} // pthread_mutexattr_setprioceiling
 
 	int pthread_mutexattr_getprioceiling( const pthread_mutexattr_t * /* attr */, int * /* ceiling */ ) libcfa_public __THROW {
-	return 0;
+		return 0;
 	} // pthread_mutexattr_getprioceiling
 
 	int pthread_mutex_setprioceiling( pthread_mutex_t * /* mutex */, int /* prioceiling */, int * /* old_ceiling */ ) libcfa_public __THROW {
-	return 0;
+		return 0;
 	} // pthread_mutex_setprioceiling
 
 	int pthread_mutex_getprioceiling( const pthread_mutex_t * /* mutex */, int * /* ceiling */ ) libcfa_public __THROW {
-	return 0;
+		return 0;
 	} // pthread_mutex_getprioceiling
 
 	int pthread_mutexattr_gettype( __const pthread_mutexattr_t * __restrict /* __attr */, int * __restrict /* __kind */ ) libcfa_public __THROW {
-	return 0;
+		return 0;
 	} // pthread_mutexattr_gettype
 
 	int pthread_mutexattr_settype( pthread_mutexattr_t * /* __attr */, int /* __kind */ ) libcfa_public __THROW {
-	return 0;
+		return 0;
 	} // pthread_mutexattr_settype
 
