41,7 → 41,7 |
#include <stdio.h> |
#include <errno.h> |
#include <bool.h> |
#include <fibril_sync.h> |
#include <futex.h> |
#include <stdlib.h> |
#include <string.h> |
#include <ipc/devmap.h> |
62,8 → 62,8 |
ipcarg_t phone; |
/** Device driver name */ |
char *name; |
/** Fibril mutex for list of devices owned by this driver */ |
fibril_mutex_t devices_mutex; |
/** Futex for list of devices owned by this driver */ |
atomic_t devices_futex; |
} devmap_driver_t; |
|
/** Info about registered device |
95,15 → 95,15 |
LIST_INITIALIZE(pending_req); |
|
/* Locking order: |
* drivers_list_mutex |
* devices_list_mutex |
* (devmap_driver_t *)->devices_mutex |
* create_handle_mutex |
* drivers_list_futex |
* devices_list_futex |
* (devmap_driver_t *)->devices_futex |
* create_handle_futex |
**/ |
|
static FIBRIL_MUTEX_INITIALIZE(devices_list_mutex); |
static FIBRIL_MUTEX_INITIALIZE(drivers_list_mutex); |
static FIBRIL_MUTEX_INITIALIZE(create_handle_mutex); |
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; |
|
110,12 → 110,13 |
static dev_handle_t devmap_create_handle(void) |
{ |
/* TODO: allow reusing old handles after their unregistration |
* and implement some version of LRU algorithm, avoid overflow |
* and implement some version of LRU algorithm |
*/ |
|
fibril_mutex_lock(&create_handle_mutex); |
/* FIXME: overflow */ |
futex_down(&create_handle_futex); |
last_handle++; |
fibril_mutex_unlock(&create_handle_mutex); |
futex_up(&create_handle_futex); |
|
return last_handle; |
} |
130,7 → 131,7 |
|
while (item != &devices_list) { |
device = list_get_instance(item, devmap_device_t, devices); |
if (str_cmp(device->name, name) == 0) |
if (0 == str_cmp(device->name, name)) |
break; |
item = item->next; |
} |
149,7 → 150,7 |
*/ |
static devmap_device_t *devmap_device_find_handle(dev_handle_t handle) |
{ |
fibril_mutex_lock(&devices_list_mutex); |
futex_down(&devices_list_futex); |
|
link_t *item = (&devices_list)->next; |
devmap_device_t *device = NULL; |
162,13 → 163,13 |
} |
|
if (item == &devices_list) { |
fibril_mutex_unlock(&devices_list_mutex); |
futex_up(&devices_list_futex); |
return NULL; |
} |
|
device = list_get_instance(item, devmap_device_t, devices); |
|
fibril_mutex_unlock(&devices_list_mutex); |
futex_up(&devices_list_futex); |
|
return device; |
} |
248,7 → 249,7 |
/* |
* Send confirmation to sender and get data into buffer. |
*/ |
if (ipc_data_write_finalize(callid, driver->name, name_size) != EOK) { |
if (EOK != ipc_data_write_finalize(callid, driver->name, name_size)) { |
free(driver->name); |
free(driver); |
ipc_answer_0(iid, EREFUSED); |
257,21 → 258,21 |
|
driver->name[name_size] = 0; |
|
/* Initialize mutex for list of devices owned by this driver */ |
fibril_mutex_initialize(&driver->devices_mutex); |
/* Initialize futex for list of devices owned by this driver */ |
futex_initialize(&(driver->devices_futex), 1); |
|
/* |
* Initialize list of asociated devices |
*/ |
list_initialize(&driver->devices); |
list_initialize(&(driver->devices)); |
|
/* |
* Create connection to the driver |
* Create connection to the driver |
*/ |
ipc_call_t call; |
callid = async_get_call(&call); |
|
if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) { |
if (IPC_M_CONNECT_TO_ME != IPC_GET_METHOD(call)) { |
ipc_answer_0(callid, ENOTSUP); |
|
free(driver->name); |
286,7 → 287,7 |
|
list_initialize(&(driver->drivers)); |
|
fibril_mutex_lock(&drivers_list_mutex); |
futex_down(&drivers_list_futex); |
|
/* TODO: |
* check that no driver with name equal to driver->name is registered |
296,7 → 297,7 |
* Insert new driver into list of registered drivers |
*/ |
list_append(&(driver->drivers), &drivers_list); |
fibril_mutex_unlock(&drivers_list_mutex); |
futex_up(&drivers_list_futex); |
|
ipc_answer_0(iid, EOK); |
|
313,7 → 314,7 |
if (driver == NULL) |
return EEXISTS; |
|
fibril_mutex_lock(&drivers_list_mutex); |
futex_down(&drivers_list_futex); |
|
if (driver->phone != 0) |
ipc_hangup(driver->phone); |
322,8 → 323,8 |
list_remove(&(driver->drivers)); |
|
/* Unregister all its devices */ |
fibril_mutex_lock(&devices_list_mutex); |
fibril_mutex_lock(&driver->devices_mutex); |
futex_down(&devices_list_futex); |
futex_down(&(driver->devices_futex)); |
|
while (!list_empty(&(driver->devices))) { |
devmap_device_t *device = list_get_instance(driver->devices.next, |
331,9 → 332,9 |
devmap_device_unregister_core(device); |
} |
|
fibril_mutex_unlock(&driver->devices_mutex); |
fibril_mutex_unlock(&devices_list_mutex); |
fibril_mutex_unlock(&drivers_list_mutex); |
futex_up(&(driver->devices_futex)); |
futex_up(&devices_list_futex); |
futex_up(&drivers_list_futex); |
|
/* free name and driver */ |
if (driver->name != NULL) |
346,7 → 347,7 |
|
|
/** Process pending lookup requests */ |
static void process_pending_lookup(void) |
static void process_pending_lookup() |
{ |
link_t *cur; |
|
363,7 → 364,6 |
free(pr->name); |
list_remove(cur); |
free(pr); |
|
goto loop; |
} |
} |
419,12 → 419,12 |
list_initialize(&(device->devices)); |
list_initialize(&(device->driver_devices)); |
|
fibril_mutex_lock(&devices_list_mutex); |
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); |
fibril_mutex_unlock(&devices_list_mutex); |
futex_up(&devices_list_futex); |
free(device->name); |
free(device); |
ipc_answer_0(iid, EEXISTS); |
440,14 → 440,16 |
list_append(&device->devices, &devices_list); |
|
/* Insert device into list of devices that belog to one driver */ |
fibril_mutex_lock(&device->driver->devices_mutex); |
futex_down(&device->driver->devices_futex); |
|
list_append(&device->driver_devices, &device->driver->devices); |
|
fibril_mutex_unlock(&device->driver->devices_mutex); |
fibril_mutex_unlock(&devices_list_mutex); |
futex_up(&device->driver->devices_futex); |
futex_up(&devices_list_futex); |
|
ipc_answer_1(iid, EOK, device->handle); |
|
process_pending_lookup(); |
} |
|
/** |
598,14 → 600,14 |
|
static void devmap_get_count(ipc_callid_t iid, ipc_call_t *icall) |
{ |
fibril_mutex_lock(&devices_list_mutex); |
futex_down(&devices_list_futex); |
ipc_answer_1(iid, EOK, list_count(&devices_list)); |
fibril_mutex_unlock(&devices_list_mutex); |
futex_up(&devices_list_futex); |
} |
|
static void devmap_get_devices(ipc_callid_t iid, ipc_call_t *icall) |
{ |
fibril_mutex_lock(&devices_list_mutex); |
futex_down(&devices_list_futex); |
|
ipc_callid_t callid; |
size_t size; |
621,7 → 623,7 |
return; |
} |
|
size_t count = size / sizeof(dev_desc_t); |
count_t count = size / sizeof(dev_desc_t); |
dev_desc_t *desc = (dev_desc_t *) malloc(size); |
if (desc == NULL) { |
ipc_answer_0(callid, ENOMEM); |
629,7 → 631,7 |
return; |
} |
|
size_t pos = 0; |
count_t pos = 0; |
link_t *item = devices_list.next; |
|
while ((item != &devices_list) && (pos < count)) { |
650,7 → 652,7 |
|
free(desc); |
|
fibril_mutex_unlock(&devices_list_mutex); |
futex_up(&devices_list_futex); |
|
ipc_answer_1(iid, EOK, pos); |
} |
675,7 → 677,7 |
list_initialize(&(device->devices)); |
list_initialize(&(device->driver_devices)); |
|
fibril_mutex_lock(&devices_list_mutex); |
futex_down(&devices_list_futex); |
|
/* Get unique device handle */ |
device->handle = devmap_create_handle(); |
684,7 → 686,7 |
/* Insert device into list of all devices */ |
list_append(&device->devices, &devices_list); |
|
fibril_mutex_unlock(&devices_list_mutex); |
futex_up(&devices_list_futex); |
|
return true; |
} |
697,7 → 699,7 |
/* Accept connection */ |
ipc_answer_0(iid, EOK); |
|
devmap_driver_t *driver = NULL; |
devmap_driver_t *driver = NULL; |
devmap_driver_register(&driver); |
|
if (NULL == driver) |
711,6 → 713,7 |
switch (IPC_GET_METHOD(call)) { |
case IPC_M_PHONE_HUNGUP: |
cont = false; |
/* Exit thread */ |
continue; |
case DEVMAP_DRIVER_UNREGISTER: |
if (NULL == driver) |
763,6 → 766,7 |
switch (IPC_GET_METHOD(call)) { |
case IPC_M_PHONE_HUNGUP: |
cont = false; |
/* Exit thread */ |
continue; |
case DEVMAP_DEVICE_GET_HANDLE: |
devmap_get_handle(callid, &call); |
802,7 → 806,7 |
break; |
default: |
/* No such interface */ |
ipc_answer_0(iid, ENOENT); |
ipc_answer_0(iid, ENOENT); |
} |
} |
|
818,9 → 822,7 |
return -1; |
} |
|
/* Set a handler of incomming connections and |
pending operations */ |
async_set_pending(process_pending_lookup); |
/* Set a handler of incomming connections */ |
async_set_client_connection(devmap_connection); |
|
/* Register device mapper at naming service */ |