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