Index: uspace/srv/devmap/devmap.c
===================================================================
--- uspace/srv/devmap/devmap.c	(revision cb41a5e8d354c38c6d53f55fadf65c3d3dc83c6f)
+++ uspace/srv/devmap/devmap.c	(revision 5d0e461de5dcfd3de23361b514bc638db7526b4d)
@@ -42,5 +42,4 @@
 #include <errno.h>
 #include <bool.h>
-#include <futex.h>
 #include <stdlib.h>
 #include <string.h>
@@ -63,6 +62,4 @@
 	/** Device driver name */
 	char *name;
-	/** Futex for list of devices owned by this driver */
-	atomic_t devices_futex;
 } devmap_driver_t;
 
@@ -95,15 +92,4 @@
 LIST_INITIALIZE(pending_req);
 
-/* Locking order:
- *  drivers_list_futex
- *  devices_list_futex
- *  (devmap_driver_t *)->devices_futex
- *  create_handle_futex
- **/
-
-static atomic_t devices_list_futex = FUTEX_INITIALIZER;
-static atomic_t drivers_list_futex = FUTEX_INITIALIZER;
-static atomic_t create_handle_futex = FUTEX_INITIALIZER;
-
 static dev_handle_t last_handle = 0;
 
@@ -111,11 +97,8 @@
 {
 	/* TODO: allow reusing old handles after their unregistration
-	 * and implement some version of LRU algorithm
-	 */
-	
-	/* FIXME: overflow */
-	futex_down(&create_handle_futex);
+	 * and implement some version of LRU algorithm, avoid overflow
+	 */
+	
 	last_handle++;
-	futex_up(&create_handle_futex);
 	
 	return last_handle;
@@ -132,5 +115,5 @@
 	while (item != &devices_list) {
 		device = list_get_instance(item, devmap_device_t, devices);
-		if (0 == str_cmp(device->name, name))
+		if (str_cmp(device->name, name) == 0)
 			break;
 		item = item->next;
@@ -151,6 +134,4 @@
 static devmap_device_t *devmap_device_find_handle(dev_handle_t handle)
 {
-	futex_down(&devices_list_futex);
-	
 	link_t *item = (&devices_list)->next;
 	devmap_device_t *device = NULL;
@@ -163,12 +144,8 @@
 	}
 	
-	if (item == &devices_list) {
-		futex_up(&devices_list_futex);
+	if (item == &devices_list)
 		return NULL;
-	}
 	
 	device = list_get_instance(item, devmap_device_t, devices);
-	
-	futex_up(&devices_list_futex);
 	
 	return device;
@@ -250,5 +227,5 @@
 	 * Send confirmation to sender and get data into buffer.
 	 */
-	if (EOK != ipc_data_write_finalize(callid, driver->name, name_size)) {
+	if (ipc_data_write_finalize(callid, driver->name, name_size) != EOK) {
 		free(driver->name);
 		free(driver);
@@ -259,7 +236,4 @@
 	driver->name[name_size] = 0;
 	
-	/* Initialize futex for list of devices owned by this driver */
-	futex_initialize(&(driver->devices_futex), 1);
-	
 	/*
 	 * Initialize list of asociated devices
@@ -268,10 +242,10 @@
 	
 	/*
-	 * Create connection to the driver 
+	 * Create connection to the driver
 	 */
 	ipc_call_t call;
 	callid = async_get_call(&call);
 	
-	if (IPC_M_CONNECT_TO_ME != IPC_GET_METHOD(call)) {
+	if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
 		ipc_answer_0(callid, ENOTSUP);
 		
@@ -288,6 +262,4 @@
 	list_initialize(&(driver->drivers));
 	
-	futex_down(&drivers_list_futex);
-	
 	/* TODO:
 	 * check that no driver with name equal to driver->name is registered
@@ -298,5 +270,4 @@
 	 */
 	list_append(&(driver->drivers), &drivers_list);
-	futex_up(&drivers_list_futex);
 	
 	ipc_answer_0(iid, EOK);
@@ -315,6 +286,4 @@
 		return EEXISTS;
 	
-	futex_down(&drivers_list_futex);
-	
 	if (driver->phone != 0)
 		ipc_hangup(driver->phone);
@@ -322,8 +291,4 @@
 	/* Remove it from list of drivers */
 	list_remove(&(driver->drivers));
-	
-	/* Unregister all its devices */
-	futex_down(&devices_list_futex);
-	futex_down(&(driver->devices_futex));
 	
 	while (!list_empty(&(driver->devices))) {
@@ -333,8 +298,4 @@
 	}
 	
-	futex_up(&(driver->devices_futex));
-	futex_up(&devices_list_futex);
-	futex_up(&drivers_list_futex);
-	
 	/* free name and driver */
 	if (driver->name != NULL)
@@ -348,6 +309,8 @@
 
 /** Process pending lookup requests */
-static void process_pending_lookup()
-{
+static void process_pending_lookup(void)
+{
+	async_serialize_start();
+	
 	link_t *cur;
 	
@@ -365,6 +328,9 @@
 		list_remove(cur);
 		free(pr);
+		
 		goto loop;
 	}
+	
+	async_serialize_end();
 }
 
@@ -420,10 +386,7 @@
 	list_initialize(&(device->driver_devices));
 	
-	futex_down(&devices_list_futex);
-	
 	/* Check that device with such name is not already registered */
 	if (NULL != devmap_device_find_name(device->name)) {
 		printf(NAME ": Device '%s' already registered\n", device->name);
-		futex_up(&devices_list_futex);	
 		free(device->name);
 		free(device);
@@ -440,15 +403,7 @@
 	list_append(&device->devices, &devices_list);
 	
-	/* Insert device into list of devices that belog to one driver */
-	futex_down(&device->driver->devices_futex);	
-	
 	list_append(&device->driver_devices, &device->driver->devices);
 	
-	futex_up(&device->driver->devices_futex);
-	futex_up(&devices_list_futex);
-	
 	ipc_answer_1(iid, EOK, device->handle);
-	
-	process_pending_lookup();
 }
 
@@ -601,13 +556,9 @@
 static void devmap_get_count(ipc_callid_t iid, ipc_call_t *icall)
 {
-	futex_down(&devices_list_futex);
 	ipc_answer_1(iid, EOK, list_count(&devices_list));
-	futex_up(&devices_list_futex);
 }
 
 static void devmap_get_devices(ipc_callid_t iid, ipc_call_t *icall)
 {
-	futex_down(&devices_list_futex);
-	
 	ipc_callid_t callid;
 	size_t size;
@@ -624,5 +575,5 @@
 	}
 	
-	count_t count = size / sizeof(dev_desc_t);
+	size_t count = size / sizeof(dev_desc_t);
 	dev_desc_t *desc = (dev_desc_t *) malloc(size);
 	if (desc == NULL) {
@@ -632,5 +583,5 @@
 	}
 	
-	count_t pos = 0;
+	size_t pos = 0;
 	link_t *item = devices_list.next;
 	
@@ -653,6 +604,4 @@
 	free(desc);
 	
-	futex_up(&devices_list_futex);
-	
 	ipc_answer_1(iid, EOK, pos);
 }
@@ -678,6 +627,4 @@
 	list_initialize(&(device->driver_devices));
 	
-	futex_down(&devices_list_futex);
-	
 	/* Get unique device handle */
 	device->handle = devmap_create_handle();
@@ -687,6 +634,4 @@
 	list_append(&device->devices, &devices_list);
 	
-	futex_up(&devices_list_futex);
-	
 	return true;
 }
@@ -700,5 +645,5 @@
 	ipc_answer_0(iid, EOK);
 	
-	devmap_driver_t *driver = NULL; 
+	devmap_driver_t *driver = NULL;
 	devmap_driver_register(&driver);
 	
@@ -711,8 +656,10 @@
 		ipc_callid_t callid = async_get_call(&call);
 		
+		async_serialize_start();
+		
 		switch (IPC_GET_METHOD(call)) {
 		case IPC_M_PHONE_HUNGUP:
 			cont = false;
-			/* Exit thread */
+			async_serialize_end();
 			continue;
 		case DEVMAP_DRIVER_UNREGISTER:
@@ -740,4 +687,6 @@
 				ipc_answer_0(callid, ENOENT);
 		}
+		
+		async_serialize_end();
 	}
 	
@@ -746,6 +695,11 @@
 		 * Unregister the device driver and all its devices.
 		 */
+		
+		async_serialize_start();
+		
 		devmap_driver_unregister(driver);
 		driver = NULL;
+		
+		async_serialize_end();
 	}
 }
@@ -764,8 +718,10 @@
 		ipc_callid_t callid = async_get_call(&call);
 		
+		async_serialize_start();
+		
 		switch (IPC_GET_METHOD(call)) {
 		case IPC_M_PHONE_HUNGUP:
 			cont = false;
-			/* Exit thread */
+			async_serialize_end();
 			continue;
 		case DEVMAP_DEVICE_GET_HANDLE:
@@ -785,4 +741,6 @@
 				ipc_answer_0(callid, ENOENT);
 		}
+		
+		async_serialize_end();
 	}
 }
@@ -807,5 +765,5 @@
 	default:
 		/* No such interface */
-		ipc_answer_0(iid, ENOENT); 
+		ipc_answer_0(iid, ENOENT);
 	}
 }
@@ -823,5 +781,7 @@
 	}
 	
-	/* Set a handler of incomming connections */
+	/* Set a handler of incomming connections and
+	   pending operations */
+	async_set_pending(process_pending_lookup);
 	async_set_client_connection(devmap_connection);
 	
