Index: kernel/generic/src/time/clock.c
===================================================================
--- kernel/generic/src/time/clock.c	(revision 2e4e706a8704952b76f052d1fa9f7a72e9f6d471)
+++ kernel/generic/src/time/clock.c	(revision b7398c034409791e32e89439b6f1e7e895d99e10)
@@ -33,11 +33,12 @@
 /**
  * @file
- * @brief	High-level clock interrupt handler.
+ * @brief High-level clock interrupt handler.
  *
  * This file contains the clock() function which is the source
  * of preemption. It is also responsible for executing expired
  * timeouts.
- */
- 
+ *
+ */
+
 #include <time/clock.h>
 #include <time/timeout.h>
@@ -63,6 +64,8 @@
 static parea_t clock_parea;
 
-/* Variable holding fragment of second, so that we would update
- * seconds correctly
+/** Fragment of second
+ *
+ * For updating  seconds correctly.
+ *
  */
 static unative_t secfrag = 0;
@@ -73,10 +76,9 @@
  * information about realtime data. We allocate 1 page with these 
  * data and update it periodically.
+ *
  */
 void clock_counter_init(void)
 {
-	void *faddr;
-
-	faddr = frame_alloc(ONE_FRAME, FRAME_ATOMIC);
+	void *faddr = frame_alloc(ONE_FRAME, FRAME_ATOMIC);
 	if (!faddr)
 		panic("Cannot allocate page for clock.");
@@ -87,12 +89,13 @@
 	uptime->seconds2 = 0;
 	uptime->useconds = 0;
-
+	
 	clock_parea.pbase = (uintptr_t) faddr;
 	clock_parea.frames = 1;
 	ddi_parea_register(&clock_parea);
-
+	
 	/*
 	 * Prepare information for the userspace so that it can successfully
 	 * physmem_map() the clock_parea.
+	 *
 	 */
 	sysinfo_set_item_val("clock.cacheable", NULL, (unative_t) true);
@@ -100,9 +103,9 @@
 }
 
-
 /** Update public counters
  *
  * Update it only on first processor
- * TODO: Do we really need so many write barriers? 
+ * TODO: Do we really need so many write barriers?
+ *
  */
 static void clock_update_counters(void)
@@ -131,60 +134,64 @@
 void clock(void)
 {
-	link_t *l;
-	timeout_t *h;
-	timeout_handler_t f;
-	void *arg;
 	size_t missed_clock_ticks = CPU->missed_clock_ticks;
-	unsigned int i;
-
+	
 	/* Account lost ticks to CPU usage */
-	if (CPU->idle) {
+	if (CPU->idle)
 		CPU->idle_ticks += missed_clock_ticks + 1;
-	} else {
+	else
 		CPU->busy_ticks += missed_clock_ticks + 1;
-	}
+	
 	CPU->idle = false;
-
+	
 	/*
 	 * To avoid lock ordering problems,
 	 * run all expired timeouts as you visit them.
+	 *
 	 */
+	size_t i;
 	for (i = 0; i <= missed_clock_ticks; i++) {
 		clock_update_counters();
-		spinlock_lock(&CPU->timeoutlock);
-		while ((l = CPU->timeout_active_head.next) != &CPU->timeout_active_head) {
-			h = list_get_instance(l, timeout_t, link);
-			spinlock_lock(&h->lock);
-			if (h->ticks-- != 0) {
-				spinlock_unlock(&h->lock);
+		irq_spinlock_lock(&CPU->timeoutlock, false);
+		
+		link_t *cur;
+		while ((cur = CPU->timeout_active_head.next) != &CPU->timeout_active_head) {
+			timeout_t *timeout = list_get_instance(cur, timeout_t, link);
+			
+			irq_spinlock_lock(&timeout->lock, false);
+			if (timeout->ticks-- != 0) {
+				irq_spinlock_unlock(&timeout->lock, false);
 				break;
 			}
-			list_remove(l);
-			f = h->handler;
-			arg = h->arg;
-			timeout_reinitialize(h);
-			spinlock_unlock(&h->lock);	
-			spinlock_unlock(&CPU->timeoutlock);
-
-			f(arg);
-
-			spinlock_lock(&CPU->timeoutlock);
+			
+			list_remove(cur);
+			timeout_handler_t handler = timeout->handler;
+			void *arg = timeout->arg;
+			timeout_reinitialize(timeout);
+			
+			irq_spinlock_unlock(&timeout->lock, false);
+			irq_spinlock_unlock(&CPU->timeoutlock, false);
+			
+			handler(arg);
+			
+			irq_spinlock_lock(&CPU->timeoutlock, false);
 		}
-		spinlock_unlock(&CPU->timeoutlock);
+		
+		irq_spinlock_unlock(&CPU->timeoutlock, false);
 	}
 	CPU->missed_clock_ticks = 0;
-
+	
 	/*
 	 * Do CPU usage accounting and find out whether to preempt THREAD.
+	 *
 	 */
-
+	
 	if (THREAD) {
 		uint64_t ticks;
 		
-		spinlock_lock(&CPU->lock);
+		irq_spinlock_lock(&CPU->lock, false);
 		CPU->needs_relink += 1 + missed_clock_ticks;
-		spinlock_unlock(&CPU->lock);	
-	
-		spinlock_lock(&THREAD->lock);
+		irq_spinlock_unlock(&CPU->lock, false);
+		
+		irq_spinlock_lock(&THREAD->lock, false);
 		if ((ticks = THREAD->ticks)) {
 			if (ticks >= 1 + missed_clock_ticks)
@@ -193,10 +200,7 @@
 				THREAD->ticks = 0;
 		}
-		spinlock_unlock(&THREAD->lock);
+		irq_spinlock_unlock(&THREAD->lock, false);
 		
 		if ((!ticks) && (!PREEMPTION_DISABLED)) {
-#ifdef CONFIG_UDEBUG
-			istate_t *istate;
-#endif
 			scheduler();
 #ifdef CONFIG_UDEBUG
@@ -205,11 +209,10 @@
 			 * before it begins executing userspace code.
 			 */
-			istate = THREAD->udebug.uspace_state;
-			if (istate && istate_from_uspace(istate))
+			istate_t *istate = THREAD->udebug.uspace_state;
+			if ((istate) && (istate_from_uspace(istate)))
 				udebug_before_thread_runs();
 #endif
 		}
 	}
-
 }
 
Index: kernel/generic/src/time/timeout.c
===================================================================
--- kernel/generic/src/time/timeout.c	(revision 2e4e706a8704952b76f052d1fa9f7a72e9f6d471)
+++ kernel/generic/src/time/timeout.c	(revision b7398c034409791e32e89439b6f1e7e895d99e10)
@@ -33,5 +33,5 @@
 /**
  * @file
- * @brief		Timeout management functions.
+ * @brief Timeout management functions.
  */
 
@@ -53,25 +53,23 @@
 void timeout_init(void)
 {
-	spinlock_initialize(&CPU->timeoutlock, "timeout_lock");
+	irq_spinlock_initialize(&CPU->timeoutlock, "cpu.timeoutlock");
 	list_initialize(&CPU->timeout_active_head);
 }
 
-
-/** Reinitialize timeout 
+/** Reinitialize timeout
  *
  * Initialize all members except the lock.
  *
- * @param t		Timeout to be initialized.
- *
- */
-void timeout_reinitialize(timeout_t *t)
-{
-	t->cpu = NULL;
-	t->ticks = 0;
-	t->handler = NULL;
-	t->arg = NULL;
-	link_initialize(&t->link);
-}
-
+ * @param timeout Timeout to be initialized.
+ *
+ */
+void timeout_reinitialize(timeout_t *timeout)
+{
+	timeout->cpu = NULL;
+	timeout->ticks = 0;
+	timeout->handler = NULL;
+	timeout->arg = NULL;
+	link_initialize(&timeout->link);
+}
 
 /** Initialize timeout
@@ -79,13 +77,12 @@
  * Initialize all members including the lock.
  *
- * @param t		Timeout to be initialized.
- *
- */
-void timeout_initialize(timeout_t *t)
-{
-	spinlock_initialize(&t->lock, "timeout_t_lock");
-	timeout_reinitialize(t);
-}
-
+ * @param timeout Timeout to be initialized.
+ *
+ */
+void timeout_initialize(timeout_t *timeout)
+{
+	irq_spinlock_initialize(&timeout->lock, "timeout_t_lock");
+	timeout_reinitialize(timeout);
+}
 
 /** Register timeout
@@ -95,70 +92,67 @@
  * time microseconds (or slightly more).
  *
- * @param t		Timeout structure.
- * @param time		Number of usec in the future to execute the handler.
- * @param f		Timeout handler function.
- * @param arg		Timeout handler argument.
- *
- */
-void
-timeout_register(timeout_t *t, uint64_t time, timeout_handler_t f, void *arg)
-{
-	timeout_t *hlp = NULL;
-	link_t *l, *m;
-	ipl_t ipl;
-	uint64_t sum;
-
-	ipl = interrupts_disable();
-	spinlock_lock(&CPU->timeoutlock);
-	spinlock_lock(&t->lock);
-
-	if (t->cpu)
-		panic("Unexpected: t->cpu != 0.");
-
-	t->cpu = CPU;
-	t->ticks = us2ticks(time);
-	
-	t->handler = f;
-	t->arg = arg;
-
-	/*
-	 * Insert t into the active timeouts list according to t->ticks.
-	 */
-	sum = 0;
-	l = CPU->timeout_active_head.next;
-	while (l != &CPU->timeout_active_head) {
-		hlp = list_get_instance(l, timeout_t, link);
-		spinlock_lock(&hlp->lock);
-		if (t->ticks < sum + hlp->ticks) {
-			spinlock_unlock(&hlp->lock);
+ * @param timeout Timeout structure.
+ * @param time    Number of usec in the future to execute the handler.
+ * @param handler Timeout handler function.
+ * @param arg     Timeout handler argument.
+ *
+ */
+void timeout_register(timeout_t *timeout, uint64_t time,
+    timeout_handler_t handler, void *arg)
+{
+	irq_spinlock_lock(&CPU->timeoutlock, true);
+	irq_spinlock_lock(&timeout->lock, false);
+	
+	if (timeout->cpu)
+		panic("Unexpected: timeout->cpu != 0.");
+	
+	timeout->cpu = CPU;
+	timeout->ticks = us2ticks(time);
+	
+	timeout->handler = handler;
+	timeout->arg = arg;
+	
+	/*
+	 * Insert timeout into the active timeouts list according to timeout->ticks.
+	 */
+	uint64_t sum = 0;
+	timeout_t *target = NULL;
+	link_t *cur;
+	for (cur = CPU->timeout_active_head.next;
+	    cur != &CPU->timeout_active_head; cur = cur->next) {
+		target = list_get_instance(cur, timeout_t, link);
+		irq_spinlock_lock(&target->lock, false);
+		
+		if (timeout->ticks < sum + target->ticks) {
+			irq_spinlock_unlock(&target->lock, false);
 			break;
 		}
-		sum += hlp->ticks;
-		spinlock_unlock(&hlp->lock);
-		l = l->next;
-	}
-
-	m = l->prev;
-	list_prepend(&t->link, m); /* avoid using l->prev */
-
-	/*
-	 * Adjust t->ticks according to ticks accumulated in h's predecessors.
-	 */
-	t->ticks -= sum;
-
-	/*
-	 * Decrease ticks of t's immediate succesor by t->ticks.
-	 */
-	if (l != &CPU->timeout_active_head) {
-		spinlock_lock(&hlp->lock);
-		hlp->ticks -= t->ticks;
-		spinlock_unlock(&hlp->lock);
-	}
-
-	spinlock_unlock(&t->lock);
-	spinlock_unlock(&CPU->timeoutlock);
-	interrupts_restore(ipl);
-}
-
+		
+		sum += target->ticks;
+		irq_spinlock_unlock(&target->lock, false);
+	}
+	
+	/* Avoid using cur->prev directly */
+	link_t *prev = cur->prev;
+	list_prepend(&timeout->link, prev);
+	
+	/*
+	 * Adjust timeout->ticks according to ticks
+	 * accumulated in target's predecessors.
+	 */
+	timeout->ticks -= sum;
+	
+	/*
+	 * Decrease ticks of timeout's immediate succesor by timeout->ticks.
+	 */
+	if (cur != &CPU->timeout_active_head) {
+		irq_spinlock_lock(&target->lock, false);
+		target->ticks -= timeout->ticks;
+		irq_spinlock_unlock(&target->lock, false);
+	}
+	
+	irq_spinlock_unlock(&timeout->lock, false);
+	irq_spinlock_unlock(&CPU->timeoutlock, true);
+}
 
 /** Unregister timeout
@@ -166,26 +160,22 @@
  * Remove timeout from timeout list.
  *
- * @param t		Timeout to unregister.
- *
- * @return		True on success, false on failure.
- */
-bool timeout_unregister(timeout_t *t)
-{
-	timeout_t *hlp;
-	link_t *l;
-	ipl_t ipl;
+ * @param timeout Timeout to unregister.
+ *
+ * @return True on success, false on failure.
+ *
+ */
+bool timeout_unregister(timeout_t *timeout)
+{
 	DEADLOCK_PROBE_INIT(p_tolock);
-
+	
 grab_locks:
-	ipl = interrupts_disable();
-	spinlock_lock(&t->lock);
-	if (!t->cpu) {
-		spinlock_unlock(&t->lock);
-		interrupts_restore(ipl);
+	irq_spinlock_lock(&timeout->lock, true);
+	if (!timeout->cpu) {
+		irq_spinlock_unlock(&timeout->lock, true);
 		return false;
 	}
-	if (!spinlock_trylock(&t->cpu->timeoutlock)) {
-		spinlock_unlock(&t->lock);
-		interrupts_restore(ipl);
+	
+	if (!irq_spinlock_trylock(&timeout->cpu->timeoutlock)) {
+		irq_spinlock_unlock(&timeout->lock, true);
 		DEADLOCK_PROBE(p_tolock, DEADLOCK_THRESHOLD);
 		goto grab_locks;
@@ -193,23 +183,22 @@
 	
 	/*
-	 * Now we know for sure that t hasn't been activated yet
-	 * and is lurking in t->cpu->timeout_active_head queue.
-	 */
-
-	l = t->link.next;
-	if (l != &t->cpu->timeout_active_head) {
-		hlp = list_get_instance(l, timeout_t, link);
-		spinlock_lock(&hlp->lock);
-		hlp->ticks += t->ticks;
-		spinlock_unlock(&hlp->lock);
-	}
-	
-	list_remove(&t->link);
-	spinlock_unlock(&t->cpu->timeoutlock);
-
-	timeout_reinitialize(t);
-	spinlock_unlock(&t->lock);
-
-	interrupts_restore(ipl);
+	 * Now we know for sure that timeout hasn't been activated yet
+	 * and is lurking in timeout->cpu->timeout_active_head queue.
+	 */
+	
+	link_t *cur = timeout->link.next;
+	if (cur != &timeout->cpu->timeout_active_head) {
+		timeout_t *tmp = list_get_instance(cur, timeout_t, link);
+		irq_spinlock_lock(&tmp->lock, false);
+		tmp->ticks += timeout->ticks;
+		irq_spinlock_unlock(&tmp->lock, false);
+	}
+	
+	list_remove(&timeout->link);
+	irq_spinlock_unlock(&timeout->cpu->timeoutlock, false);
+	
+	timeout_reinitialize(timeout);
+	irq_spinlock_unlock(&timeout->lock, true);
+	
 	return true;
 }
