Index: arch/ia32/include/atomic.h
===================================================================
--- arch/ia32/include/atomic.h	(revision 253f8590e4181969f613bd6bf8a6f0a74ab038e9)
+++ arch/ia32/include/atomic.h	(revision 05e2a7ad64a4c5a40748a558c64ca76a66934a0c)
@@ -54,6 +54,6 @@
 	atomic_t r;
 	__asm__ volatile (
-		"movl $1,%0;"
-		"lock xaddl %0,%1;"
+		"movl $1, %0\n"
+		"lock xaddl %0, %1\n"
 		: "=r"(r), "=m" (*val)
 	);
@@ -67,6 +67,6 @@
 	atomic_t r;
 	__asm__ volatile (
-		"movl $-1,%0;"
-		"lock xaddl %0,%1;"
+		"movl $-1, %0\n"
+		"lock xaddl %0, %1\n"
 		: "=r"(r), "=m" (*val)
 	);
@@ -76,6 +76,4 @@
 #define atomic_inc_post(val) (atomic_inc_pre(val)+1)
 #define atomic_dec_post(val) (atomic_dec_pre(val)-1)
-
-
 
 static inline int test_and_set(volatile int *val) {
Index: generic/include/proc/thread.h
===================================================================
--- generic/include/proc/thread.h	(revision 253f8590e4181969f613bd6bf8a6f0a74ab038e9)
+++ generic/include/proc/thread.h	(revision 05e2a7ad64a4c5a40748a558c64ca76a66934a0c)
@@ -65,5 +65,10 @@
 	link_t threads_link;			/**< Link to the list of all threads. */
 	
-	/* items below are protected by lock */
+	/** Lock protecting thread structure.
+	 *
+	 * Protects the whole thread structure except list links above.
+	 * Must be acquired before T.lock for each T of type task_t.
+	 * 
+	 */
 	spinlock_t lock;
 
@@ -110,5 +115,12 @@
 };
 
-extern spinlock_t threads_lock;			/**< Lock protecting threads_head list. */
+/** Thread list lock.
+ *
+ * This lock protects all link_t structures chained in threads_head.
+ * Must be acquired before T.lock for each T of type thread_t.
+ *
+ */
+extern spinlock_t threads_lock;
+
 extern link_t threads_head;			/**< List of all threads in the system. */
 
Index: generic/include/synch/waitq.h
===================================================================
--- generic/include/synch/waitq.h	(revision 253f8590e4181969f613bd6bf8a6f0a74ab038e9)
+++ generic/include/synch/waitq.h	(revision 05e2a7ad64a4c5a40748a558c64ca76a66934a0c)
@@ -39,6 +39,13 @@
 #define WAKEUP_ALL	1
 
+/** Wait queue structure. */
 struct waitq {
+
+	/** Lock protecting wait queue structure.
+	 *
+	 * Must be acquired before T.lock for each T of type thread_t.
+	 */
 	spinlock_t lock;
+
 	int missed_wakeups;	/**< Number of waitq_wakeup() calls that didn't find a thread to wake up. */
 	link_t head;		/**< List of sleeping threads for wich there was no missed_wakeup. */
@@ -52,6 +59,6 @@
 extern void waitq_initialize(waitq_t *wq);
 extern int waitq_sleep_timeout(waitq_t *wq, __u32 usec, int nonblocking);
-extern void waitq_wakeup(waitq_t *wq, int all);
-extern void _waitq_wakeup_unsafe(waitq_t *wq, int all);
+extern void waitq_wakeup(waitq_t *wq, bool all);
+extern void _waitq_wakeup_unsafe(waitq_t *wq, bool all);
 
 #endif
Index: generic/src/proc/scheduler.c
===================================================================
--- generic/src/proc/scheduler.c	(revision 253f8590e4181969f613bd6bf8a6f0a74ab038e9)
+++ generic/src/proc/scheduler.c	(revision 05e2a7ad64a4c5a40748a558c64ca76a66934a0c)
@@ -434,5 +434,5 @@
 
 	/*
-	 * Through the 'THE' structure, we keep track of THREAD, TASK, CPU
+	 * Through the 'THE' structure, we keep track of THREAD, TASK, CPU, VM
 	 * and preemption counter. At this point THE could be coming either
 	 * from THREAD's or CPU's stack.
Index: generic/src/proc/thread.c
===================================================================
--- generic/src/proc/thread.c	(revision 253f8590e4181969f613bd6bf8a6f0a74ab038e9)
+++ generic/src/proc/thread.c	(revision 05e2a7ad64a4c5a40748a558c64ca76a66934a0c)
@@ -55,6 +55,6 @@
 char *thread_states[] = {"Invalid", "Running", "Sleeping", "Ready", "Entering", "Exiting"}; /**< Thread states */
 
-spinlock_t threads_lock;
-link_t threads_head;
+spinlock_t threads_lock;	/**< Lock protecting threads_head list. For locking rules, see declaration thereof. */
+link_t threads_head;		/**< List of all threads. */
 
 static spinlock_t tidlock;
Index: generic/src/synch/rwlock.c
===================================================================
--- generic/src/synch/rwlock.c	(revision 253f8590e4181969f613bd6bf8a6f0a74ab038e9)
+++ generic/src/synch/rwlock.c	(revision 05e2a7ad64a4c5a40748a558c64ca76a66934a0c)
@@ -27,7 +27,8 @@
  */
 
-
-/*
- * Reader/Writer locks
+/** Reader/Writer locks
+ *
+ * A reader/writer lock can be held by multiple readers at a time.
+ * Or it can be exclusively held by a sole writer at a time.
  */
 
@@ -76,5 +77,5 @@
  */
 void rwlock_initialize(rwlock_t *rwl) {
-	spinlock_initialize(&rwl->lock, "rwlock");
+	spinlock_initialize(&rwl->lock, "rwlock_t");
 	mutex_initialize(&rwl->exclusive);
 	rwl->readers_in = 0;
@@ -219,8 +220,8 @@
 				break;
 			case ESYNCH_OK_ATOMIC:
-				panic("_mutex_lock_timeout()==ESYNCH_OK_ATOMIC");
+				panic("_mutex_lock_timeout()==ESYNCH_OK_ATOMIC\n");
 				break;
 			dafault:
-				panic("invalid ESYNCH");
+				panic("invalid ESYNCH\n");
 				break;
 		}
@@ -284,5 +285,5 @@
 
 
-/** Direct handoff
+/** Direct handoff of reader/writer lock ownership.
  *
  * Direct handoff of reader/writer lock ownership
@@ -307,5 +308,5 @@
 	rwlock_type_t type = RWLOCK_NONE;
 	thread_t *t = NULL;
-	int one_more = 1;
+	bool one_more = true;
 	
 	spinlock_lock(&rwl->exclusive.sem.wq.lock);
@@ -353,5 +354,5 @@
 				spinlock_lock(&t->lock);
 				if (t->rwlock_holder_type != RWLOCK_READER)
-					one_more = 0;
+					one_more = false;
 				spinlock_unlock(&t->lock);	
 			}
Index: generic/src/synch/spinlock.c
===================================================================
--- generic/src/synch/spinlock.c	(revision 253f8590e4181969f613bd6bf8a6f0a74ab038e9)
+++ generic/src/synch/spinlock.c	(revision 05e2a7ad64a4c5a40748a558c64ca76a66934a0c)
@@ -63,7 +63,8 @@
 void spinlock_lock(spinlock_t *sl)
 {
-	int i = 0;
+	count_t i = 0;
 	__address caller = ((__address *) &sl)[-1];
 	char *symbol;
+	bool deadlock_reported = false;
 
 	preemption_disable();
@@ -77,6 +78,10 @@
 			printf("\n");
 			i = 0;
+			deadlock_reported = true;
 		}
 	}
+
+	if (deadlock_reported)
+		printf("cpu%d: not deadlocked\n", CPU->id);
 
 	/*
Index: generic/src/synch/waitq.c
===================================================================
--- generic/src/synch/waitq.c	(revision 253f8590e4181969f613bd6bf8a6f0a74ab038e9)
+++ generic/src/synch/waitq.c	(revision 05e2a7ad64a4c5a40748a558c64ca76a66934a0c)
@@ -34,4 +34,5 @@
 #include <arch/asm.h>
 #include <arch/types.h>
+#include <typedefs.h>
 #include <time/timeout.h>
 #include <arch.h>
@@ -68,5 +69,5 @@
 	thread_t *t = (thread_t *) data;
 	waitq_t *wq;
-	int do_wakeup = 0;
+	bool do_wakeup = false;
 
 	spinlock_lock(&threads_lock);
@@ -76,13 +77,13 @@
 grab_locks:
 	spinlock_lock(&t->lock);
-	if (wq = t->sleep_queue) {
+	if (wq = t->sleep_queue) {		/* assignment */
 		if (!spinlock_trylock(&wq->lock)) {
 			spinlock_unlock(&t->lock);
-			goto grab_locks; /* avoid deadlock */
+			goto grab_locks;	/* avoid deadlock */
 		}
 
 		list_remove(&t->wq_link);
 		t->saved_context = t->sleep_timeout_context;
-		do_wakeup = 1;
+		do_wakeup = true;
 		
 		spinlock_unlock(&wq->lock);
@@ -90,8 +91,9 @@
 	}
 	
-	t->timeout_pending = 0;
+	t->timeout_pending = false;
 	spinlock_unlock(&t->lock);
 	
-	if (do_wakeup) thread_ready(t);
+	if (do_wakeup)
+		thread_ready(t);
 
 out:
@@ -194,5 +196,5 @@
 			return ESYNCH_TIMEOUT;
 		}
-		THREAD->timeout_pending = 1;
+		THREAD->timeout_pending = true;
 		timeout_register(&THREAD->sleep_timeout, (__u64) usec, waitq_interrupted_sleep, THREAD);
 	}
@@ -228,5 +230,5 @@
  *        will be woken up and missed count will be zeroed.
  */
-void waitq_wakeup(waitq_t *wq, int all)
+void waitq_wakeup(waitq_t *wq, bool all)
 {
 	ipl_t ipl;
@@ -251,5 +253,5 @@
  *        will be woken up and missed count will be zeroed.
  */
-void _waitq_wakeup_unsafe(waitq_t *wq, int all)
+void _waitq_wakeup_unsafe(waitq_t *wq, bool all)
 {
 	thread_t *t;
@@ -258,5 +260,6 @@
 	if (list_empty(&wq->head)) {
 		wq->missed_wakeups++;
-		if (all) wq->missed_wakeups = 0;
+		if (all)
+			wq->missed_wakeups = 0;
 		return;
 	}
@@ -267,5 +270,5 @@
 	spinlock_lock(&t->lock);
 	if (t->timeout_pending && timeout_unregister(&t->sleep_timeout))
-		t->timeout_pending = 0;
+		t->timeout_pending = false;
 	t->sleep_queue = NULL;
 	spinlock_unlock(&t->lock);
@@ -273,4 +276,5 @@
 	thread_ready(t);
 
-	if (all) goto loop;
-}
+	if (all)
+		goto loop;
+}
