Changeset 2dfd9fa in mainline for uspace/srv/fs/devfs/devfs_ops.c


Ignore:
Timestamp:
2009-06-22T13:39:46Z (15 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e2dccb0
Parents:
12956e57
Message:

avoid races during access to the shared hash table

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/fs/devfs/devfs_ops.c

    r12956e57 r2dfd9fa  
    4242#include <string.h>
    4343#include <libfs.h>
     44#include <fibril_sync.h>
    4445#include <adt/hash_table.h>
    4546#include "devfs.h"
     
    5960static hash_table_t devices;
    6061
     62/** Hash table mutex */
     63static FIBRIL_MUTEX_INITIALIZE(devices_mutex);
     64
    6165#define DEVICES_KEYS        1
    6266#define DEVICES_KEY_HANDLE  0
     
    196200                                };
    197201                               
     202                                fibril_mutex_lock(&devices_mutex);
    198203                                link_t *lnk = hash_table_find(&devices, key);
    199204                                if (lnk == NULL) {
    200205                                        int phone = devmap_device_connect(handle, 0);
    201206                                        if (phone < 0) {
     207                                                fibril_mutex_unlock(&devices_mutex);
    202208                                                free(name);
    203209                                                ipc_answer_0(rid, ENOENT);
     
    207213                                        device_t *dev = (device_t *) malloc(sizeof(device_t));
    208214                                        if (dev == NULL) {
     215                                                fibril_mutex_unlock(&devices_mutex);
    209216                                                free(name);
    210217                                                ipc_answer_0(rid, ENOMEM);
     
    221228                                        dev->refcount++;
    222229                                }
     230                                fibril_mutex_unlock(&devices_mutex);
    223231                        }
    224232                       
     
    239247        };
    240248       
     249        fibril_mutex_lock(&devices_mutex);
    241250        link_t *lnk = hash_table_find(&devices, key);
    242251        if (lnk == NULL) {
    243252                int phone = devmap_device_connect(handle, 0);
    244253                if (phone < 0) {
     254                        fibril_mutex_unlock(&devices_mutex);
    245255                        ipc_answer_0(rid, ENOENT);
    246256                        return;
     
    249259                device_t *dev = (device_t *) malloc(sizeof(device_t));
    250260                if (dev == NULL) {
     261                        fibril_mutex_unlock(&devices_mutex);
    251262                        ipc_answer_0(rid, ENOMEM);
    252263                        return;
     
    262273                dev->refcount++;
    263274        }
     275        fibril_mutex_unlock(&devices_mutex);
    264276       
    265277        ipc_answer_3(rid, EOK, 0, 1, L_FILE);
     
    275287                };
    276288               
     289                fibril_mutex_lock(&devices_mutex);
    277290                link_t *lnk = hash_table_find(&devices, key);
    278291                if (lnk == NULL) {
    279                         ipc_answer_0(rid, ENOENT);
    280                         return;
    281                 }
     292                        fibril_mutex_unlock(&devices_mutex);
     293                        ipc_answer_0(rid, ENOENT);
     294                        return;
     295                }
     296                fibril_mutex_unlock(&devices_mutex);
    282297               
    283298                ipc_answer_1(rid, EOK, (ipcarg_t) index);
     
    296311                };
    297312               
     313                fibril_mutex_lock(&devices_mutex);
    298314                link_t *lnk = hash_table_find(&devices, key);
    299315                if (lnk == NULL) {
     316                        fibril_mutex_unlock(&devices_mutex);
    300317                        ipc_answer_0(rid, ENOENT);
    301318                        return;
     
    306323                ipc_callid_t callid;
    307324                if (!ipc_data_read_receive(&callid, NULL)) {
     325                        fibril_mutex_unlock(&devices_mutex);
    308326                        ipc_answer_0(callid, EINVAL);
    309327                        ipc_answer_0(rid, EINVAL);
     
    319337                /* Forward the IPC_M_DATA_READ request to the driver */
    320338                ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
     339                fibril_mutex_unlock(&devices_mutex);
    321340               
    322341                /* Wait for reply from the driver. */
     
    370389                };
    371390               
     391                fibril_mutex_lock(&devices_mutex);
    372392                link_t *lnk = hash_table_find(&devices, key);
    373393                if (lnk == NULL) {
     394                        fibril_mutex_unlock(&devices_mutex);
    374395                        ipc_answer_0(rid, ENOENT);
    375396                        return;
     
    380401                ipc_callid_t callid;
    381402                if (!ipc_data_write_receive(&callid, NULL)) {
     403                        fibril_mutex_unlock(&devices_mutex);
    382404                        ipc_answer_0(callid, EINVAL);
    383405                        ipc_answer_0(rid, EINVAL);
     
    394416                ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
    395417               
     418                fibril_mutex_unlock(&devices_mutex);
     419               
    396420                /* Wait for reply from the driver. */
    397421                ipcarg_t rc;
     
    421445                };
    422446               
     447                fibril_mutex_lock(&devices_mutex);
    423448                link_t *lnk = hash_table_find(&devices, key);
    424449                if (lnk == NULL) {
     450                        fibril_mutex_unlock(&devices_mutex);
    425451                        ipc_answer_0(rid, ENOENT);
    426452                        return;
     
    434460                        hash_table_remove(&devices, key, DEVICES_KEYS);
    435461                }
     462               
     463                fibril_mutex_unlock(&devices_mutex);
    436464               
    437465                ipc_answer_0(rid, EOK);
     
    449477                };
    450478               
     479                fibril_mutex_lock(&devices_mutex);
    451480                link_t *lnk = hash_table_find(&devices, key);
    452481                if (lnk == NULL) {
     482                        fibril_mutex_unlock(&devices_mutex);
    453483                        ipc_answer_0(rid, ENOENT);
    454484                        return;
     
    462492                    IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), &answer);
    463493               
     494                fibril_mutex_unlock(&devices_mutex);
     495               
    464496                /* Wait for reply from the driver */
    465497                ipcarg_t rc;
Note: See TracChangeset for help on using the changeset viewer.