Index: uspace/lib/c/Makefile
===================================================================
--- uspace/lib/c/Makefile	(revision e8b8b74f0fc4c26998c0858909f446f86b3642ea)
+++ uspace/lib/c/Makefile	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -47,4 +47,7 @@
 LSONAME = libc.so0
 
+LIBS = $(LIBURCU_PREFIX)/liburcu.a
+EXTRA_CFLAGS += -I$(LIBURCU_PREFIX)
+
 -include $(CONFIG_MAKEFILE)
 -include arch/$(UARCH)/Makefile.inc
@@ -88,4 +91,5 @@
 	generic/pcb.c \
 	generic/smc.c \
+	generic/smp_memory_barrier.c \
 	generic/thread.c \
 	generic/tls.c \
Index: uspace/lib/c/generic/adt/hash_table.c
===================================================================
--- uspace/lib/c/generic/adt/hash_table.c	(revision e8b8b74f0fc4c26998c0858909f446f86b3642ea)
+++ uspace/lib/c/generic/adt/hash_table.c	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -134,5 +134,5 @@
 	free(h->bucket);
 
-	h->bucket = 0;
+	h->bucket = NULL;
 	h->bucket_cnt = 0;
 }
Index: uspace/lib/c/generic/async.c
===================================================================
--- uspace/lib/c/generic/async.c	(revision e8b8b74f0fc4c26998c0858909f446f86b3642ea)
+++ uspace/lib/c/generic/async.c	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -167,5 +167,5 @@
 
 /** Async framework global futex */
-atomic_t async_futex = FUTEX_INITIALIZER;
+futex_t async_futex = FUTEX_INITIALIZER;
 
 /** Number of threads waiting for IPC in the kernel. */
Index: uspace/lib/c/generic/fibril.c
===================================================================
--- uspace/lib/c/generic/fibril.c	(revision e8b8b74f0fc4c26998c0858909f446f86b3642ea)
+++ uspace/lib/c/generic/fibril.c	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -49,4 +49,9 @@
 #include <assert.h>
 #include <async.h>
+#include <futex.h>
+
+#ifdef FUTEX_UPGRADABLE
+#include <rcu.h>
+#endif
 
 /**
@@ -54,5 +59,5 @@
  * serialized_list and manager_list.
  */
-static atomic_t fibril_futex = FUTEX_INITIALIZER;
+static futex_t fibril_futex = FUTEX_INITIALIZER;
 
 static LIST_INITIALIZE(ready_list);
@@ -83,4 +88,8 @@
 {
 	fibril_t *fibril = __tcb_get()->fibril_data;
+
+#ifdef FUTEX_UPGRADABLE
+	rcu_register_fibril();
+#endif
 	
 	/* Call the implementing function. */
@@ -146,5 +155,5 @@
 	int retval = 0;
 	
-	futex_down(&fibril_futex);
+	futex_lock(&fibril_futex);
 	
 	if (stype == FIBRIL_PREEMPT && list_empty(&ready_list))
@@ -168,7 +177,7 @@
 	if ((stype == FIBRIL_TO_MANAGER) || (stype == FIBRIL_FROM_DEAD)) {
 		while (list_empty(&manager_list)) {
-			futex_up(&fibril_futex);
+			futex_unlock(&fibril_futex);
 			async_create_manager();
-			futex_down(&fibril_futex);
+			futex_lock(&fibril_futex);
 		}
 	}
@@ -203,5 +212,5 @@
 			}
 			
-			return 1;	/* futex_up already done here */
+			return 1;	/* futex_unlock already done here */
 		}
 		
@@ -246,10 +255,17 @@
 	list_remove(&dstf->link);
 	
-	futex_up(&fibril_futex);
+	futex_unlock(&fibril_futex);
+	
+#ifdef FUTEX_UPGRADABLE
+	if (stype == FIBRIL_FROM_DEAD) {
+		rcu_deregister_fibril();
+	}
+#endif
+	
 	context_restore(&dstf->ctx);
 	/* not reached */
 	
 ret_0:
-	futex_up(&fibril_futex);
+	futex_unlock(&fibril_futex);
 	return retval;
 }
@@ -318,5 +334,5 @@
 	fibril_t *fibril = (fibril_t *) fid;
 	
-	futex_down(&fibril_futex);
+	futex_lock(&fibril_futex);
 	
 	if ((fibril->flags & FIBRIL_SERIALIZED))
@@ -325,5 +341,5 @@
 		list_append(&fibril->link, &ready_list);
 	
-	futex_up(&fibril_futex);
+	futex_unlock(&fibril_futex);
 }
 
@@ -338,7 +354,7 @@
 	fibril_t *fibril = (fibril_t *) fid;
 	
-	futex_down(&fibril_futex);
+	futex_lock(&fibril_futex);
 	list_append(&fibril->link, &manager_list);
-	futex_up(&fibril_futex);
+	futex_unlock(&fibril_futex);
 }
 
@@ -346,10 +362,10 @@
 void fibril_remove_manager(void)
 {
-	futex_down(&fibril_futex);
+	futex_lock(&fibril_futex);
 	
 	if (!list_empty(&manager_list))
 		list_remove(list_first(&manager_list));
 	
-	futex_up(&fibril_futex);
+	futex_unlock(&fibril_futex);
 }
 
Index: uspace/lib/c/generic/futex.c
===================================================================
--- uspace/lib/c/generic/futex.c	(revision e8b8b74f0fc4c26998c0858909f446f86b3642ea)
+++ uspace/lib/c/generic/futex.c	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -35,7 +35,4 @@
 #include <futex.h>
 #include <atomic.h>
-#include <libarch/barrier.h>
-#include <libc.h>
-#include <sys/types.h>
 
 /** Initialize futex counter.
@@ -47,63 +44,26 @@
 void futex_initialize(futex_t *futex, int val)
 {
-	atomic_set(futex, val);
+	atomic_set(&futex->val, val);
 }
 
-/** Try to down the futex.
- *
- * @param futex Futex.
- *
- * @return Non-zero if the futex was acquired.
- * @return Zero if the futex was not acquired.
- *
- */
-int futex_trydown(futex_t *futex)
+
+#ifdef FUTEX_UPGRADABLE
+
+int _upgrade_futexes = 0;
+static futex_t upg_and_wait_futex = FUTEX_INITIALIZER;
+
+void futex_upgrade_all_and_wait(void)
 {
-	int rc;
-
-	rc = cas(futex, 1, 0);
-	CS_ENTER_BARRIER();
-
-	return rc;
+	futex_down(&upg_and_wait_futex);
+	
+	if (!_upgrade_futexes) {
+		rcu_assign(_upgrade_futexes, 1);
+		_rcu_synchronize(BM_BLOCK_THREAD);
+	}
+	
+	futex_up(&upg_and_wait_futex);
 }
 
-/** Down the futex.
- *
- * @param futex Futex.
- *
- * @return ENOENT if there is no such virtual address.
- * @return Zero in the uncontended case.
- * @return Otherwise one of ESYNCH_OK_ATOMIC or ESYNCH_OK_BLOCKED.
- *
- */
-int futex_down(futex_t *futex)
-{
-	atomic_signed_t nv;
-
-	nv = (atomic_signed_t) atomic_predec(futex);
-	CS_ENTER_BARRIER();
-	if (nv < 0)
-		return __SYSCALL1(SYS_FUTEX_SLEEP, (sysarg_t) &futex->count);
-	
-	return 0;
-}
-
-/** Up the futex.
- *
- * @param futex Futex.
- *
- * @return ENOENT if there is no such virtual address.
- * @return Zero in the uncontended case.
- *
- */
-int futex_up(futex_t *futex)
-{
-	CS_LEAVE_BARRIER();
-
-	if ((atomic_signed_t) atomic_postinc(futex) < 0)
-		return __SYSCALL1(SYS_FUTEX_WAKEUP, (sysarg_t) &futex->count);
-	
-	return 0;
-}
+#endif
 
 /** @}
Index: uspace/lib/c/generic/ipc.c
===================================================================
--- uspace/lib/c/generic/ipc.c	(revision e8b8b74f0fc4c26998c0858909f446f86b3642ea)
+++ uspace/lib/c/generic/ipc.c	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -81,5 +81,5 @@
 LIST_INITIALIZE(queued_calls);
 
-static atomic_t ipc_futex = FUTEX_INITIALIZER;
+static futex_t ipc_futex = FUTEX_INITIALIZER;
 
 /** Send asynchronous message via syscall.
@@ -136,10 +136,10 @@
 	if (!call) {
 		/* Nothing to do regardless if failed or not */
-		futex_up(&ipc_futex);
+		futex_unlock(&ipc_futex);
 		return;
 	}
 	
 	if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
-		futex_up(&ipc_futex);
+		futex_unlock(&ipc_futex);
 		
 		/* Call asynchronous handler with error code */
@@ -152,5 +152,5 @@
 	
 	if (callid == (ipc_callid_t) IPC_CALLRET_TEMPORARY) {
-		futex_up(&ipc_futex);
+		futex_unlock(&ipc_futex);
 		
 		call->u.msg.phoneid = phoneid;
@@ -175,5 +175,5 @@
 	/* Add call to the list of dispatched calls */
 	list_append(&call->list, &dispatched_calls);
-	futex_up(&ipc_futex);
+	futex_unlock(&ipc_futex);
 }
 
@@ -219,5 +219,5 @@
 	 */
 	
-	futex_down(&ipc_futex);
+	futex_lock(&ipc_futex);
 	ipc_callid_t callid = __SYSCALL6(SYS_IPC_CALL_ASYNC_FAST, phoneid,
 	    imethod, arg1, arg2, arg3, arg4);
@@ -226,6 +226,8 @@
 		if (!call) {
 			call = ipc_prepare_async(private, callback);
-			if (!call)
+			if (!call) {
+				futex_unlock(&ipc_futex);
 				return;
+			}
 		}
 		
@@ -289,5 +291,5 @@
 	 */
 	
-	futex_down(&ipc_futex);
+	futex_lock(&ipc_futex);
 	ipc_callid_t callid =
 	    ipc_call_async_internal(phoneid, &call->u.msg.data);
@@ -384,7 +386,7 @@
 			call->u.callid = callid;
 			
-			futex_down(&ipc_futex);
+			futex_lock(&ipc_futex);
 			list_append(&call->list, &dispatched_calls);
-			futex_up(&ipc_futex);
+			futex_unlock(&ipc_futex);
 		}
 		
@@ -412,5 +414,5 @@
 	callid &= ~IPC_CALLID_ANSWERED;
 	
-	futex_down(&ipc_futex);
+	futex_lock(&ipc_futex);
 	
 	link_t *item;
@@ -423,5 +425,5 @@
 			list_remove(&call->list);
 			
-			futex_up(&ipc_futex);
+			futex_unlock(&ipc_futex);
 			
 			if (call->callback)
@@ -434,5 +436,5 @@
 	}
 	
-	futex_up(&ipc_futex);
+	futex_unlock(&ipc_futex);
 }
 
Index: uspace/lib/c/generic/libc.c
===================================================================
--- uspace/lib/c/generic/libc.c	(revision e8b8b74f0fc4c26998c0858909f446f86b3642ea)
+++ uspace/lib/c/generic/libc.c	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -52,7 +52,12 @@
 #include "private/io.h"
 
+#ifdef FUTEX_UPGRADABLE
+#include <rcu.h>
+#endif
+
 #ifdef CONFIG_RTLD
 #include <rtld/rtld.h>
 #endif
+
 
 static bool env_setup = false;
@@ -62,5 +67,4 @@
 	/* Initialize user task run-time environment */
 	__malloc_init();
-	__async_init();
 	
 	fibril_t *fibril = fibril_setup();
@@ -72,4 +76,10 @@
 	/* Save the PCB pointer */
 	__pcb = (pcb_t *) pcb_ptr;
+	
+#ifdef FUTEX_UPGRADABLE
+	rcu_register_fibril();
+#endif
+	
+	__async_init();
 	
 	/* The basic run-time environment is setup */
Index: uspace/lib/c/generic/malloc.c
===================================================================
--- uspace/lib/c/generic/malloc.c	(revision e8b8b74f0fc4c26998c0858909f446f86b3642ea)
+++ uspace/lib/c/generic/malloc.c	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -67,7 +67,7 @@
 /** Heap shrink granularity
  *
- * Try not to pump and stress the heap to much
+ * Try not to pump and stress the heap too much
  * by shrinking and enlarging it too often.
- * A heap area won't shrunk if it the released
+ * A heap area won't shrink if the released
  * free block is smaller than this constant.
  *
@@ -200,5 +200,5 @@
 	do { \
 		if (!(expr)) {\
-			futex_up(&malloc_futex); \
+			heap_unlock(); \
 			assert_abort(#expr, __FILE__, __LINE__); \
 		} \
@@ -210,4 +210,69 @@
 
 #endif /* NDEBUG */
+
+
+#ifdef FUTEX_UPGRADABLE
+/** True if the heap may be accessed from multiple threads. */
+static bool multithreaded = false;
+
+/** Makes accesses to the heap thread safe. */
+void malloc_enable_multithreaded(void)
+{
+	multithreaded = true;
+}
+
+/** Serializes access to the heap from multiple threads. */
+static inline void heap_lock(void)
+{
+	if (multithreaded) {
+		futex_down(&malloc_futex);
+	} else {
+		/*
+		 * Malloc never switches fibrils while the heap is locked.
+		 * Similarly, it never creates new threads from within the 
+		 * locked region. Therefore, if there are no other threads 
+		 * except this one, the whole operation will complete without 
+		 * any interruptions.
+		 */
+	}
+}
+
+/** Serializes access to the heap from multiple threads. */
+static inline void heap_unlock(void)
+{
+	if (multithreaded) {
+		futex_up(&malloc_futex);
+	} else {
+		/*
+		 * Malloc never switches fibrils while the heap is locked.
+		 * Similarly, it never creates new threads from within the 
+		 * locked region. Therefore, if there are no other threads 
+		 * except this one, the whole operation will complete without 
+		 * any interruptions.
+		 */
+	}
+}
+
+#else
+
+/** Makes accesses to the heap thread safe. */
+void malloc_enable_multithreaded(void)
+{
+	/* No-op. Already using thread-safe heap locking operations. */
+}
+
+/** Serializes access to the heap from multiple threads. */
+static inline void heap_lock(void)
+{
+	futex_down(&malloc_futex);
+}
+
+/** Serializes access to the heap from multiple threads. */
+static inline void heap_unlock(void)
+{
+	futex_up(&malloc_futex);
+}
+#endif
+
 
 /** Initialize a heap block
@@ -785,8 +850,8 @@
 void *malloc(const size_t size)
 {
-	futex_down(&malloc_futex);
+	heap_lock();
 	void *block = malloc_internal(size, BASE_ALIGN);
-	futex_up(&malloc_futex);
-	
+	heap_unlock();
+
 	return block;
 }
@@ -807,9 +872,9 @@
 	size_t palign =
 	    1 << (fnzb(max(sizeof(void *), align) - 1) + 1);
-	
-	futex_down(&malloc_futex);
+
+	heap_lock();
 	void *block = malloc_internal(size, palign);
-	futex_up(&malloc_futex);
-	
+	heap_unlock();
+
 	return block;
 }
@@ -828,5 +893,5 @@
 		return malloc(size);
 	
-	futex_down(&malloc_futex);
+	heap_lock();
 	
 	/* Calculate the position of the header. */
@@ -885,5 +950,5 @@
 	}
 	
-	futex_up(&malloc_futex);
+	heap_unlock();
 	
 	if (reloc) {
@@ -908,5 +973,5 @@
 		return;
 	
-	futex_down(&malloc_futex);
+	heap_lock();
 	
 	/* Calculate the position of the header. */
@@ -953,13 +1018,13 @@
 	heap_shrink(area);
 	
-	futex_up(&malloc_futex);
+	heap_unlock();
 }
 
 void *heap_check(void)
 {
-	futex_down(&malloc_futex);
+	heap_lock();
 	
 	if (first_heap_area == NULL) {
-		futex_up(&malloc_futex);
+		heap_unlock();
 		return (void *) -1;
 	}
@@ -975,5 +1040,5 @@
 		    (((uintptr_t) area->start % PAGE_SIZE) != 0) ||
 		    (((uintptr_t) area->end % PAGE_SIZE) != 0)) {
-			futex_up(&malloc_futex);
+			heap_unlock();
 			return (void *) area;
 		}
@@ -986,5 +1051,5 @@
 			/* Check heap block consistency */
 			if (head->magic != HEAP_BLOCK_HEAD_MAGIC) {
-				futex_up(&malloc_futex);
+				heap_unlock();
 				return (void *) head;
 			}
@@ -994,5 +1059,5 @@
 			if ((foot->magic != HEAP_BLOCK_FOOT_MAGIC) ||
 			    (head->size != foot->size)) {
-				futex_up(&malloc_futex);
+				heap_unlock();
 				return (void *) foot;
 			}
@@ -1000,5 +1065,5 @@
 	}
 	
-	futex_up(&malloc_futex);
+	heap_unlock();
 	
 	return NULL;
Index: uspace/lib/c/generic/smp_memory_barrier.c
===================================================================
--- uspace/lib/c/generic/smp_memory_barrier.c	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
+++ uspace/lib/c/generic/smp_memory_barrier.c	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2012 Adam Hraska
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libc
+ * @{
+ */
+/** @file
+ */
+
+#include <smp_memory_barrier.h>
+#include <libc.h>
+
+void smp_memory_barrier(void)
+{
+	__SYSCALL0(SYS_SMP_MEMORY_BARRIER);
+}
+
+/** @}
+ */
Index: uspace/lib/c/generic/thread.c
===================================================================
--- uspace/lib/c/generic/thread.c	(revision e8b8b74f0fc4c26998c0858909f446f86b3642ea)
+++ uspace/lib/c/generic/thread.c	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -46,4 +46,9 @@
 #include "private/thread.h"
 
+#ifdef FUTEX_UPGRADABLE
+#include <rcu.h>
+#endif
+
+
 /** Main thread function.
  *
@@ -63,4 +68,9 @@
 	__tcb_set(fibril->tcb);
 	
+#ifdef FUTEX_UPGRADABLE
+	rcu_register_fibril();
+	futex_upgrade_all_and_wait();
+#endif
+	
 	uarg->uspace_thread_function(uarg->uspace_thread_arg);
 	/*
@@ -73,4 +83,9 @@
 	/* If there is a manager, destroy it */
 	async_destroy_manager();
+
+#ifdef FUTEX_UPGRADABLE
+	rcu_deregister_fibril();
+#endif
+	
 	fibril_teardown(fibril);
 	
@@ -106,4 +121,7 @@
 		return ENOMEM;
 	}
+	
+	/* Make heap thread safe. */
+	malloc_enable_multithreaded();
 	
 	uarg->uspace_entry = (void *) FADDR(__thread_entry);
Index: uspace/lib/c/include/adt/list.h
===================================================================
--- uspace/lib/c/include/adt/list.h	(revision e8b8b74f0fc4c26998c0858909f446f86b3642ea)
+++ uspace/lib/c/include/adt/list.h	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -58,5 +58,23 @@
  */
 #define LIST_INITIALIZE(name) \
-	list_t name = { \
+	list_t name = LIST_INITIALIZER(name)
+
+/** Initializer for statically allocated list.
+ * 
+ * @code
+ * struct named_list {
+ *     const char *name;
+ *     list_t list;
+ * } var = { 
+ *     .name = "default name", 
+ *     .list = LIST_INITIALIZER(name_list.list) 
+ * };
+ * @endcode
+ *
+ * @param name Name of the new statically allocated list.
+ *
+ */
+#define LIST_INITIALIZER(name) \
+	{ \
 		.head = { \
 			.prev = &(name).head, \
Index: uspace/lib/c/include/compiler/barrier.h
===================================================================
--- uspace/lib/c/include/compiler/barrier.h	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
+++ uspace/lib/c/include/compiler/barrier.h	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2012 Adam Hraska
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libc
+ * @{
+ */
+/** @file
+ */
+
+#ifndef LIBC_COMPILER_BARRIER_H_
+#define LIBC_COMPILER_BARRIER_H_
+
+#define compiler_barrier() asm volatile ("" ::: "memory")
+
+/** Forces the compiler to access (ie load/store) the variable only once. */
+#define ACCESS_ONCE(var) (*((volatile typeof(var)*)&(var)))
+
+#endif /* LIBC_COMPILER_BARRIER_H_ */
Index: uspace/lib/c/include/futex.h
===================================================================
--- uspace/lib/c/include/futex.h	(revision e8b8b74f0fc4c26998c0858909f446f86b3642ea)
+++ uspace/lib/c/include/futex.h	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -38,13 +38,115 @@
 #include <atomic.h>
 #include <sys/types.h>
+#include <libc.h>
 
-#define FUTEX_INITIALIZER  {1}
+typedef struct futex {
+	atomic_t val;
+#ifdef FUTEX_UPGRADABLE
+	int upgraded;
+#endif
+} futex_t;
 
-typedef atomic_t futex_t;
 
 extern void futex_initialize(futex_t *futex, int value);
-extern int futex_down(futex_t *futex);
-extern int futex_trydown(futex_t *futex);
-extern int futex_up(futex_t *futex);
+
+#ifdef FUTEX_UPGRADABLE
+#include <rcu.h>
+
+#define FUTEX_INITIALIZE(val) {{ (val) }, 0}
+
+#define futex_lock(fut) \
+({ \
+	rcu_read_lock(); \
+	(fut)->upgraded = rcu_access(_upgrade_futexes); \
+	if ((fut)->upgraded) \
+		(void) futex_down((fut)); \
+})
+
+#define futex_trylock(fut) \
+({ \
+	rcu_read_lock(); \
+	int _upgraded = rcu_access(_upgrade_futexes); \
+	if (_upgraded) { \
+		int _acquired = futex_trydown((fut)); \
+		if (!_acquired) { \
+			rcu_read_unlock(); \
+		} else { \
+			(fut)->upgraded = true; \
+		} \
+		_acquired; \
+	} else { \
+		(fut)->upgraded = false; \
+		1; \
+	} \
+})
+		
+#define futex_unlock(fut) \
+({ \
+	if ((fut)->upgraded) \
+		(void) futex_up((fut)); \
+	rcu_read_unlock(); \
+})
+
+extern int _upgrade_futexes;
+
+extern void futex_upgrade_all_and_wait(void);
+		
+#else
+
+#define FUTEX_INITIALIZE(val) {{ (val) }}
+
+#define futex_lock(fut)     (void) futex_down((fut))
+#define futex_trylock(fut)  futex_trydown((fut))
+#define futex_unlock(fut)   (void) futex_up((fut))
+		
+#endif
+
+#define FUTEX_INITIALIZER     FUTEX_INITIALIZE(1)
+
+/** Try to down the futex.
+ *
+ * @param futex Futex.
+ *
+ * @return Non-zero if the futex was acquired.
+ * @return Zero if the futex was not acquired.
+ *
+ */
+static inline int futex_trydown(futex_t *futex)
+{
+	return cas(&futex->val, 1, 0);
+}
+
+/** Down the futex.
+ *
+ * @param futex Futex.
+ *
+ * @return ENOENT if there is no such virtual address.
+ * @return Zero in the uncontended case.
+ * @return Otherwise one of ESYNCH_OK_ATOMIC or ESYNCH_OK_BLOCKED.
+ *
+ */
+static inline int futex_down(futex_t *futex)
+{
+	if ((atomic_signed_t) atomic_predec(&futex->val) < 0)
+		return __SYSCALL1(SYS_FUTEX_SLEEP, (sysarg_t) &futex->val.count);
+	
+	return 0;
+}
+
+/** Up the futex.
+ *
+ * @param futex Futex.
+ *
+ * @return ENOENT if there is no such virtual address.
+ * @return Zero in the uncontended case.
+ *
+ */
+static inline int futex_up(futex_t *futex)
+{
+	if ((atomic_signed_t) atomic_postinc(&futex->val) < 0)
+		return __SYSCALL1(SYS_FUTEX_WAKEUP, (sysarg_t) &futex->val.count);
+	
+	return 0;
+}
 
 #endif
Index: uspace/lib/c/include/ipc/common.h
===================================================================
--- uspace/lib/c/include/ipc/common.h	(revision e8b8b74f0fc4c26998c0858909f446f86b3642ea)
+++ uspace/lib/c/include/ipc/common.h	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -40,4 +40,5 @@
 #include <atomic.h>
 #include <abi/proc/task.h>
+#include <futex.h>
 
 #define IPC_FLAG_BLOCKING  0x01
@@ -51,5 +52,5 @@
 typedef sysarg_t ipc_callid_t;
 
-extern atomic_t async_futex;
+extern futex_t async_futex;
 
 #endif
Index: uspace/lib/c/include/malloc.h
===================================================================
--- uspace/lib/c/include/malloc.h	(revision e8b8b74f0fc4c26998c0858909f446f86b3642ea)
+++ uspace/lib/c/include/malloc.h	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -48,4 +48,5 @@
 extern void *heap_check(void);
 
+extern void malloc_enable_multithreaded(void);
 #endif
 
Index: uspace/lib/c/include/rwlock.h
===================================================================
--- uspace/lib/c/include/rwlock.h	(revision e8b8b74f0fc4c26998c0858909f446f86b3642ea)
+++ uspace/lib/c/include/rwlock.h	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -49,8 +49,8 @@
 
 #define rwlock_initialize(rwlock)	futex_initialize((rwlock), 1)
-#define rwlock_read_lock(rwlock)	futex_down((rwlock))
-#define rwlock_write_lock(rwlock)	futex_down((rwlock))
-#define rwlock_read_unlock(rwlock)	futex_up((rwlock))
-#define rwlock_write_unlock(rwlock)	futex_up((rwlock))
+#define rwlock_read_lock(rwlock)	futex_lock((rwlock))
+#define rwlock_write_lock(rwlock)	futex_lock((rwlock))
+#define rwlock_read_unlock(rwlock)	futex_unlock((rwlock))
+#define rwlock_write_unlock(rwlock)	futex_unlock((rwlock))
 
 #endif
Index: uspace/lib/c/include/smp_memory_barrier.h
===================================================================
--- uspace/lib/c/include/smp_memory_barrier.h	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
+++ uspace/lib/c/include/smp_memory_barrier.h	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2012 Adam Hraska
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libc
+ * @{
+ */
+/** @file
+ */
+
+#ifndef LIBC_SMP_MEM_BAR_H_
+#define LIBC_SMP_MEM_BAR_H_
+
+extern void smp_memory_barrier(void);
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/c/include/sys/time.h
===================================================================
--- uspace/lib/c/include/sys/time.h	(revision e8b8b74f0fc4c26998c0858909f446f86b3642ea)
+++ uspace/lib/c/include/sys/time.h	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -79,4 +79,5 @@
 
 extern void udelay(useconds_t);
+extern int usleep(useconds_t);
 
 extern time_t mktime(struct tm *);
Index: uspace/lib/urcu/Makefile
===================================================================
--- uspace/lib/urcu/Makefile	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
+++ uspace/lib/urcu/Makefile	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -0,0 +1,40 @@
+#
+# Copyright (c) 2012 Adam Hraska
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# - Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+# - Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+# - The name of the author may not be used to endorse or promote products
+#   derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+USPACE_PREFIX = ../..
+
+LIBS = $(LIBC_PREFIX)/libc.a 
+
+EXTRA_CFLAGS = -I. -I$(LIBC_PREFIX)/include
+
+LIBRARY = liburcu
+
+SOURCES = \
+	rcu.c
+
+include $(USPACE_PREFIX)/Makefile.common
Index: uspace/lib/urcu/rcu.c
===================================================================
--- uspace/lib/urcu/rcu.c	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
+++ uspace/lib/urcu/rcu.c	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -0,0 +1,465 @@
+/*
+ * Copyright (c) 2012 Adam Hraska
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup liburcu
+ * @{
+ */
+/**
+ * @file
+ * 
+ * User space RCU is based on URCU utilizing signals [1]. This 
+ * implementation does not however signal each thread of the process 
+ * to issue a memory barrier. Instead, we introduced a syscall that
+ * issues memory barriers (via IPIs) on cpus that are running threads
+ * of the current process. First, it does not require us to schedule
+ * and run every thread of the process. Second, IPIs are less intrusive 
+ * than switching contexts and entering user space.
+ * 
+ * This algorithm is further modified to require a single instead of
+ * two reader group changes per grace period. Signal-URCU flips
+ * the reader group and waits for readers of the previous group 
+ * twice in succession in order to wait for new readers that were
+ * delayed and mistakenly associated with the previous reader group. 
+ * The modified algorithm ensures that the new reader group is
+ * always empty (by explicitly waiting for it to become empty).
+ * Only then does it flip the reader group and wait for preexisting
+ * readers of the old reader group (invariant of SRCU [2, 3]).
+ * 
+ * 
+ * [1] User-level implementations of read-copy update,
+ *     2012, appendix
+ *     http://www.rdrop.com/users/paulmck/RCU/urcu-supp-accepted.2011.08.30a.pdf
+ * 
+ * [2] linux/kernel/srcu.c in Linux 3.5-rc2,
+ *     2012
+ *     http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/kernel/srcu.c?v=linux-3.5-rc2-ccs-1.8.3
+ *
+ * [3] [RFC PATCH 5/5 single-thread-version] implement 
+ *     per-domain single-thread state machine,
+ *     2012, Lai
+ *     https://lkml.org/lkml/2012/3/6/586
+ */
+
+#include "rcu.h"
+#include <fibril_synch.h>
+#include <fibril.h>
+#include <stdio.h>
+#include <compiler/barrier.h>
+#include <libarch/barrier.h>
+#include <futex.h>
+#include <macros.h>
+#include <async.h>
+#include <adt/list.h>
+#include <smp_memory_barrier.h>
+#include <assert.h>
+#include <time.h>
+
+
+/** RCU sleeps for RCU_SLEEP_MS before polling an active RCU reader again. */
+#define RCU_SLEEP_MS        10
+
+#define RCU_NESTING_SHIFT   1
+#define RCU_NESTING_INC     (1 << RCU_NESTING_SHIFT)
+#define RCU_GROUP_BIT_MASK  (size_t)(RCU_NESTING_INC - 1)
+#define RCU_GROUP_A         (size_t)(0 | RCU_NESTING_INC)
+#define RCU_GROUP_B         (size_t)(1 | RCU_NESTING_INC)
+
+
+/** Fibril local RCU data. */
+typedef struct fibril_rcu_data {
+	size_t nesting_cnt;
+	link_t link;
+	bool registered;
+} fibril_rcu_data_t;
+
+/** Process global RCU data. */
+typedef struct rcu_data {
+	size_t cur_gp;
+	size_t reader_group;
+	futex_t list_futex;
+	list_t fibrils_list;
+	struct {
+		futex_t futex;
+		bool locked;
+		list_t blocked_fibrils;
+		size_t blocked_thread_cnt;
+		futex_t futex_blocking_threads;
+	} sync_lock;
+} rcu_data_t;
+
+typedef struct blocked_fibril {
+	fid_t id;
+	link_t link;
+	bool is_ready;
+} blocked_fibril_t;
+
+
+/** Fibril local RCU data. */
+static fibril_local fibril_rcu_data_t fibril_rcu = {
+	.nesting_cnt = 0,
+	.link = {
+		.next = NULL,
+		.prev = NULL
+	},
+	.registered = false
+};
+
+/** Process global RCU data. */
+static rcu_data_t rcu = {
+	.cur_gp = 0,
+	.reader_group = RCU_GROUP_A,
+	.list_futex = FUTEX_INITIALIZER,
+	.fibrils_list = LIST_INITIALIZER(rcu.fibrils_list),
+	.sync_lock = {
+		.futex = FUTEX_INITIALIZER,
+		.locked = false,
+		.blocked_fibrils = LIST_INITIALIZER(rcu.sync_lock.blocked_fibrils),
+		.blocked_thread_cnt = 0,
+		.futex_blocking_threads = FUTEX_INITIALIZE(0),
+	},
+};
+
+
+static void wait_for_readers(size_t reader_group, blocking_mode_t blocking_mode);
+static void force_mb_in_all_threads(void);
+static bool is_preexisting_reader(const fibril_rcu_data_t *fib, size_t group);
+
+static void lock_sync(blocking_mode_t blocking_mode);
+static void unlock_sync(void);
+static void sync_sleep(blocking_mode_t blocking_mode);
+
+static bool is_in_group(size_t nesting_cnt, size_t group);
+static bool is_in_reader_section(size_t nesting_cnt);
+static size_t get_other_group(size_t group);
+
+
+/** Registers a fibril so it may start using RCU read sections.
+ * 
+ * A fibril must be registered with rcu before it can enter RCU critical
+ * sections delineated by rcu_read_lock() and rcu_read_unlock().
+ */
+void rcu_register_fibril(void)
+{
+	assert(!fibril_rcu.registered);
+	
+	futex_down(&rcu.list_futex);
+	list_append(&fibril_rcu.link, &rcu.fibrils_list);
+	futex_up(&rcu.list_futex);
+	
+	fibril_rcu.registered = true;
+}
+
+/** Deregisters a fibril that had been using RCU read sections.
+ * 
+ * A fibril must be deregistered before it exits if it had
+ * been registered with rcu via rcu_register_fibril().
+ */
+void rcu_deregister_fibril(void)
+{
+	assert(fibril_rcu.registered);
+	
+	/* 
+	 * Forcefully unlock any reader sections. The fibril is exiting
+	 * so it is not holding any references to data protected by the
+	 * rcu section. Therefore, it is safe to unlock. Otherwise, 
+	 * rcu_synchronize() would wait indefinitely.
+	 */
+	memory_barrier();
+	fibril_rcu.nesting_cnt = 0;
+	
+	futex_down(&rcu.list_futex);
+	list_remove(&fibril_rcu.link);
+	futex_up(&rcu.list_futex);
+
+	fibril_rcu.registered = false;
+}
+
+/** Delimits the start of an RCU reader critical section. 
+ * 
+ * RCU reader sections may be nested.  
+ */
+void rcu_read_lock(void)
+{
+	assert(fibril_rcu.registered);
+	
+	size_t nesting_cnt = ACCESS_ONCE(fibril_rcu.nesting_cnt);
+	
+	if (0 == (nesting_cnt >> RCU_NESTING_SHIFT)) {
+		ACCESS_ONCE(fibril_rcu.nesting_cnt) = ACCESS_ONCE(rcu.reader_group);
+		/* Required by MB_FORCE_L */
+		compiler_barrier(); /* CC_BAR_L */
+	} else {
+		ACCESS_ONCE(fibril_rcu.nesting_cnt) = nesting_cnt + RCU_NESTING_INC;
+	}
+}
+
+/** Delimits the start of an RCU reader critical section. */
+void rcu_read_unlock(void)
+{
+	assert(fibril_rcu.registered);
+	assert(rcu_read_locked());
+	
+	/* Required by MB_FORCE_U */
+	compiler_barrier(); /* CC_BAR_U */
+	/* todo: ACCESS_ONCE(nesting_cnt) ? */
+	fibril_rcu.nesting_cnt -= RCU_NESTING_INC;
+}
+
+/** Returns true if the current fibril is in an RCU reader section. */
+bool rcu_read_locked(void)
+{
+	return 0 != (fibril_rcu.nesting_cnt >> RCU_NESTING_SHIFT);
+}
+
+/** Blocks until all preexisting readers exit their critical sections. */
+void _rcu_synchronize(blocking_mode_t blocking_mode)
+{
+	assert(!rcu_read_locked());
+	
+	/* Contain load of rcu.cur_gp. */
+	memory_barrier();
+
+	/* Approximately the number of the GP in progress. */
+	size_t gp_in_progress = ACCESS_ONCE(rcu.cur_gp);
+	
+	lock_sync(blocking_mode);
+	
+	/* 
+	 * Exit early if we were stuck waiting for the mutex for a full grace 
+	 * period. Started waiting during gp_in_progress (or gp_in_progress + 1
+	 * if the value propagated to this cpu too late) so wait for the next
+	 * full GP, gp_in_progress + 1, to finish. Ie don't wait if the GP
+	 * after that, gp_in_progress + 2, already started.
+	 */
+	/* rcu.cur_gp >= gp_in_progress + 2, but tolerates overflows. */
+	if (rcu.cur_gp != gp_in_progress && rcu.cur_gp + 1 != gp_in_progress) {
+		unlock_sync();
+		return;
+	}
+	
+	++ACCESS_ONCE(rcu.cur_gp);
+	
+	/* 
+	 * Pairs up with MB_FORCE_L (ie CC_BAR_L). Makes changes prior 
+	 * to rcu_synchronize() visible to new readers. 
+	 */
+	memory_barrier(); /* MB_A */
+	
+	/* 
+	 * Pairs up with MB_A. 
+	 * 
+	 * If the memory barrier is issued before CC_BAR_L in the target
+	 * thread, it pairs up with MB_A and the thread sees all changes
+	 * prior to rcu_synchronize(). Ie any reader sections are new
+	 * rcu readers.  
+	 * 
+	 * If the memory barrier is issued after CC_BAR_L, it pairs up
+	 * with MB_B and it will make the most recent nesting_cnt visible
+	 * in this thread. Since the reader may have already accessed
+	 * memory protected by RCU (it ran instructions passed CC_BAR_L),
+	 * it is a preexisting reader. Seeing the most recent nesting_cnt 
+	 * ensures the thread will be identified as a preexisting reader
+	 * and we will wait for it in wait_for_readers(old_reader_group).
+	 */
+	force_mb_in_all_threads(); /* MB_FORCE_L */
+	
+	/* 
+	 * Pairs with MB_FORCE_L (ie CC_BAR_L, CC_BAR_U) and makes the most
+	 * current fibril.nesting_cnt visible to this cpu.
+	 */
+	read_barrier(); /* MB_B */
+	
+	size_t new_reader_group = get_other_group(rcu.reader_group);
+	wait_for_readers(new_reader_group, blocking_mode);
+	
+	/* Separates waiting for readers in new_reader_group from group flip. */
+	memory_barrier();
+	
+	/* Flip the group new readers should associate with. */
+	size_t old_reader_group = rcu.reader_group;
+	rcu.reader_group = new_reader_group;
+
+	/* Flip the group before waiting for preexisting readers in the old group.*/
+	memory_barrier();
+	
+	wait_for_readers(old_reader_group, blocking_mode);
+	
+	/* MB_FORCE_U  */
+	force_mb_in_all_threads(); /* MB_FORCE_U */
+	
+	unlock_sync();
+}
+
+/** Issues a memory barrier in each thread of this process. */
+static void force_mb_in_all_threads(void)
+{
+	/* 
+	 * Only issue barriers in running threads. The scheduler will 
+	 * execute additional memory barriers when switching to threads
+	 * of the process that are currently not running.
+	 */
+	smp_memory_barrier();
+}
+
+/** Waits for readers of reader_group to exit their readers sections. */
+static void wait_for_readers(size_t reader_group, blocking_mode_t blocking_mode)
+{
+	futex_down(&rcu.list_futex);
+	
+	list_t quiescent_fibrils;
+	list_initialize(&quiescent_fibrils);
+	
+	while (!list_empty(&rcu.fibrils_list)) {
+		list_foreach_safe(rcu.fibrils_list, fibril_it, next_fibril) {
+			fibril_rcu_data_t *fib = member_to_inst(fibril_it, 
+				fibril_rcu_data_t, link);
+			
+			if (is_preexisting_reader(fib, reader_group)) {
+				futex_up(&rcu.list_futex);
+				sync_sleep(blocking_mode);
+				futex_down(&rcu.list_futex);
+				/* Break to while loop. */
+				break;
+			} else {
+				list_remove(fibril_it);
+				list_append(fibril_it, &quiescent_fibrils);
+			}
+		}
+	}
+	
+	list_concat(&rcu.fibrils_list, &quiescent_fibrils);
+	futex_up(&rcu.list_futex);
+}
+
+static void lock_sync(blocking_mode_t blocking_mode)
+{
+	futex_down(&rcu.sync_lock.futex);
+	if (rcu.sync_lock.locked) {
+		if (blocking_mode == BM_BLOCK_FIBRIL) {
+			blocked_fibril_t blocked_fib;
+			blocked_fib.id = fibril_get_id();
+				
+			list_append(&blocked_fib.link, &rcu.sync_lock.blocked_fibrils);
+			
+			do {
+				blocked_fib.is_ready = false;
+				futex_up(&rcu.sync_lock.futex);
+				fibril_switch(FIBRIL_TO_MANAGER);
+				futex_down(&rcu.sync_lock.futex);
+			} while (rcu.sync_lock.locked);
+			
+			list_remove(&blocked_fib.link);
+			rcu.sync_lock.locked = true;
+		} else {
+			assert(blocking_mode == BM_BLOCK_THREAD);
+			rcu.sync_lock.blocked_thread_cnt++;
+			futex_up(&rcu.sync_lock.futex);
+			futex_down(&rcu.sync_lock.futex_blocking_threads);
+		}
+	} else {
+		rcu.sync_lock.locked = true;
+	}
+}
+
+static void unlock_sync(void)
+{
+	assert(rcu.sync_lock.locked);
+	
+	/* 
+	 * Blocked threads have a priority over fibrils when accessing sync().
+	 * Pass the lock onto a waiting thread.
+	 */
+	if (0 < rcu.sync_lock.blocked_thread_cnt) {
+		--rcu.sync_lock.blocked_thread_cnt;
+		futex_up(&rcu.sync_lock.futex_blocking_threads);
+	} else {
+		/* Unlock but wake up any fibrils waiting for the lock. */
+		
+		if (!list_empty(&rcu.sync_lock.blocked_fibrils)) {
+			blocked_fibril_t *blocked_fib = member_to_inst(
+				list_first(&rcu.sync_lock.blocked_fibrils), blocked_fibril_t, link);
+	
+			if (!blocked_fib->is_ready) {
+				blocked_fib->is_ready = true;
+				fibril_add_ready(blocked_fib->id);
+			}
+		}
+		
+		rcu.sync_lock.locked = false;
+		futex_up(&rcu.sync_lock.futex);
+	}
+}
+
+static void sync_sleep(blocking_mode_t blocking_mode)
+{
+	assert(rcu.sync_lock.locked);
+	/* 
+	 * Release the futex to avoid deadlocks in singlethreaded apps 
+	 * but keep sync locked. 
+	 */
+	futex_up(&rcu.sync_lock.futex);
+
+	if (blocking_mode == BM_BLOCK_FIBRIL) {
+		async_usleep(RCU_SLEEP_MS * 1000);
+	} else {
+		usleep(RCU_SLEEP_MS * 1000);
+	}
+		
+	futex_down(&rcu.sync_lock.futex);
+}
+
+
+static bool is_preexisting_reader(const fibril_rcu_data_t *fib, size_t group)
+{
+	size_t nesting_cnt = ACCESS_ONCE(fib->nesting_cnt);
+	
+	return is_in_group(nesting_cnt, group) && is_in_reader_section(nesting_cnt);
+}
+
+static size_t get_other_group(size_t group)
+{
+	if (group == RCU_GROUP_A) 
+		return RCU_GROUP_B;
+	else
+		return RCU_GROUP_A;
+}
+
+static bool is_in_reader_section(size_t nesting_cnt)
+{
+	return RCU_NESTING_INC <= nesting_cnt;
+}
+
+static bool is_in_group(size_t nesting_cnt, size_t group)
+{
+	return (nesting_cnt & RCU_GROUP_BIT_MASK) == (group & RCU_GROUP_BIT_MASK);
+}
+
+
+
+/** @}
+ */
Index: uspace/lib/urcu/rcu.h
===================================================================
--- uspace/lib/urcu/rcu.h	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
+++ uspace/lib/urcu/rcu.h	(revision 01784d20ebe77098db8f9fe743d50de2c8c9b646)
@@ -0,0 +1,114 @@
+/*
+ * Copyright (c) 2012 Adam Hraska
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup liburcu
+ * @{
+ */
+/**
+ * @file
+ */
+
+#ifndef LIBURCU_RCU_H_
+#define LIBURCU_RCU_H_
+
+#include <compiler/barrier.h>
+#include <libarch/barrier.h>
+#include <stdbool.h>
+
+/** Use to assign a pointer to newly initialized data to a rcu reader 
+ * accessible pointer.
+ * 
+ * Example:
+ * @code
+ * typedef struct exam {
+ *     struct exam *next;
+ *     int grade;
+ * } exam_t;
+ * 
+ * exam_t *exam_list;
+ * // ..
+ * 
+ * // Insert at the beginning of the list.
+ * exam_t *my_exam = malloc(sizeof(exam_t), 0);
+ * my_exam->grade = 5;
+ * my_exam->next = exam_list;
+ * rcu_assign(exam_list, my_exam);
+ * 
+ * // Changes properly propagate. Every reader either sees
+ * // the old version of exam_list or the new version with
+ * // the fully initialized my_exam.
+ * rcu_synchronize();
+ * // Now we can be sure every reader sees my_exam.
+ * 
+ * @endcode
+ */
+#define rcu_assign(ptr, value) \
+	do { \
+		memory_barrier(); \
+		(ptr) = (value); \
+	} while (0)
+
+/** Use to access RCU protected data in a reader section.
+ * 
+ * Example:
+ * @code
+ * exam_t *exam_list;
+ * // ...
+ * 
+ * rcu_read_lock();
+ * exam_t *first_exam = rcu_access(exam_list);
+ * // We can now safely use first_exam, it won't change 
+ * // under us while we're using it.
+ *
+ * // ..
+ * rcu_read_unlock();
+ * @endcode
+ */
+#define rcu_access(ptr) ACCESS_ONCE(ptr)
+
+typedef enum blocking_mode {
+	BM_BLOCK_FIBRIL,
+	BM_BLOCK_THREAD
+} blocking_mode_t;
+
+extern void rcu_register_fibril(void);
+extern void rcu_deregister_fibril(void);
+
+extern void rcu_read_lock(void);
+extern void rcu_read_unlock(void);
+
+extern bool rcu_read_locked(void);
+
+#define rcu_synchronize() _rcu_synchronize(BM_BLOCK_FIBRIL)
+
+extern void _rcu_synchronize(blocking_mode_t);
+
+#endif
+
+/** @}
+ */
