Changeset bc216a0 in mainline for uspace/lib/c/generic/async.c


Ignore:
Timestamp:
2012-08-07T22:13:44Z (13 years ago)
Author:
Adam Hraska <adam.hraska+hos@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
da68871a
Parents:
b17518e
Message:

Refactored any users of hash_table to use opaque void* keys instead of the cumbersome unsigned long[] keys. Switched from the ad hoc computations of hashes of multiple values to hash_combine().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/async.c

    rb17518e rbc216a0  
    202202/* Client connection data */
    203203typedef struct {
    204         link_t link;
     204        ht_link_t link;
    205205       
    206206        task_id_t in_task_id;
     
    214214       
    215215        /** Hash table link. */
    216         link_t link;
     216        ht_link_t link;
    217217       
    218218        /** Incoming client task ID. */
     
    390390static LIST_INITIALIZE(timeout_list);
    391391
    392 static size_t client_key_hash(unsigned long key[])
    393 {
    394         assert(key);
    395         /* LOWER32(in_task_id) */
    396         return key[0] >> 4;
    397 }
    398 
    399 static size_t client_hash(const link_t *item)
    400 {
    401         client_t *client = hash_table_get_instance(item, client_t, link);
    402        
    403         unsigned long key[2] = {
    404                 LOWER32(client->in_task_id),
    405                 UPPER32(client->in_task_id),
    406         };
    407 
    408         return client_key_hash(key);
    409 }
    410 
    411 static bool client_match(unsigned long key[], size_t keys, const link_t *item)
    412 {
    413         assert(key);
    414         assert(keys == 2);
    415         assert(item);
    416        
    417         client_t *client = hash_table_get_instance(item, client_t, link);
    418         return (key[0] == LOWER32(client->in_task_id) &&
    419             (key[1] == UPPER32(client->in_task_id)));
     392static size_t client_key_hash(void *k)
     393{
     394        task_id_t key = *(task_id_t*)k;
     395        return key;
     396}
     397
     398static size_t client_hash(const ht_link_t *item)
     399{
     400        client_t *client = hash_table_get_inst(item, client_t, link);
     401        return client_key_hash(&client->in_task_id);
     402}
     403
     404static bool client_key_equal(void *k, const ht_link_t *item)
     405{
     406        task_id_t key = *(task_id_t*)k;
     407        client_t *client = hash_table_get_inst(item, client_t, link);
     408        return key == client->in_task_id;
    420409}
    421410
     
    425414        .hash = client_hash,
    426415        .key_hash = client_key_hash,
    427         .match = client_match,
     416        .key_equal = client_key_equal,
    428417        .equal = 0,
    429418        .remove_callback = 0
     
    437426 *
    438427 */
    439 static size_t conn_key_hash(unsigned long key[])
    440 {
    441         assert(key);
    442         return key[0] >> 4;
    443 }
    444 
    445 static size_t conn_hash(const link_t *item)
    446 {
    447         connection_t *conn = hash_table_get_instance(item, connection_t, link);
    448         unsigned long key = conn->in_phone_hash;
    449         return conn_key_hash(&key);
    450 }
    451 
    452 /** Compare hash table item with a key.
    453  *
    454  * @param key  Array containing the source phone hash as the only item.
    455  * @param keys Expected 1 but ignored.
    456  * @param item Connection hash table item.
    457  *
    458  * @return True on match, false otherwise.
    459  *
    460  */
    461 static bool conn_match(unsigned long key[], size_t keys, const link_t *item)
    462 {
    463         assert(key);
    464         assert(item);
    465        
    466         connection_t *conn = hash_table_get_instance(item, connection_t, link);
    467         return (key[0] == conn->in_phone_hash);
    468 }
    469 
    470 static bool conn_equal(const link_t *item1, const link_t *item2)
    471 {
    472         connection_t *c1 = hash_table_get_instance(item1, connection_t, link);
    473         connection_t *c2 = hash_table_get_instance(item2, connection_t, link);
    474        
    475         return c1->in_phone_hash == c2->in_phone_hash;
    476 }
    477 
    478 static void conn_remove(link_t *item)
    479 {
    480 }
     428static size_t conn_key_hash(void *key)
     429{
     430        sysarg_t in_phone_hash  = *(sysarg_t*)key;
     431        return in_phone_hash ;
     432}
     433
     434static size_t conn_hash(const ht_link_t *item)
     435{
     436        connection_t *conn = hash_table_get_inst(item, connection_t, link);
     437        return conn_key_hash(&conn->in_phone_hash);
     438}
     439
     440static bool conn_key_equal(void *key, const ht_link_t *item)
     441{
     442        sysarg_t in_phone_hash = *(sysarg_t*)key;
     443        connection_t *conn = hash_table_get_inst(item, connection_t, link);
     444        return (in_phone_hash == conn->in_phone_hash);
     445}
     446
    481447
    482448/** Operations for the connection hash table. */
     
    484450        .hash = conn_hash,
    485451        .key_hash = conn_key_hash,
    486         .match = conn_match,
    487         .equal = conn_equal,
    488         .remove_callback = conn_remove
     452        .key_equal = conn_key_equal,
     453        .equal = 0,
     454        .remove_callback = 0
    489455};
    490456
     
    534500        futex_down(&async_futex);
    535501       
    536         unsigned long key = call->in_phone_hash;
    537         link_t *hlp = hash_table_find(&conn_hash_table, &key);
     502        ht_link_t *hlp = hash_table_find(&conn_hash_table, &call->in_phone_hash);
    538503       
    539504        if (!hlp) {
     
    542507        }
    543508       
    544         connection_t *conn = hash_table_get_instance(hlp, connection_t, link);
     509        connection_t *conn = hash_table_get_inst(hlp, connection_t, link);
    545510       
    546511        msg_t *msg = malloc(sizeof(*msg));
     
    722687static client_t *async_client_get(task_id_t client_id, bool create)
    723688{
    724         unsigned long key[2] = {
    725                 LOWER32(client_id),
    726                 UPPER32(client_id),
    727         };
    728689        client_t *client = NULL;
    729690
    730691        futex_down(&async_futex);
    731         link_t *lnk = hash_table_find(&client_hash_table, key);
     692        ht_link_t *lnk = hash_table_find(&client_hash_table, &client_id);
    732693        if (lnk) {
    733                 client = hash_table_get_instance(lnk, client_t, link);
     694                client = hash_table_get_inst(lnk, client_t, link);
    734695                atomic_inc(&client->refcnt);
    735696        } else if (create) {
     
    751712{
    752713        bool destroy;
    753         unsigned long key[2] = {
    754                 LOWER32(client->in_task_id),
    755                 UPPER32(client->in_task_id)
    756         };
    757        
     714
    758715        futex_down(&async_futex);
    759716       
    760717        if (atomic_predec(&client->refcnt) == 0) {
    761                 hash_table_remove(&client_hash_table, key, 2);
     718                hash_table_remove(&client_hash_table, &client->in_task_id);
    762719                destroy = true;
    763720        } else
     
    855812         */
    856813        futex_down(&async_futex);
    857         unsigned long key = fibril_connection->in_phone_hash;
    858         hash_table_remove(&conn_hash_table, &key, 1);
     814        hash_table_remove(&conn_hash_table, &fibril_connection->in_phone_hash);
    859815        futex_up(&async_futex);
    860816       
     
    11341090void __async_init(void)
    11351091{
    1136         if (!hash_table_create(&client_hash_table, 0, 2, &client_hash_table_ops))
     1092        if (!hash_table_create(&client_hash_table, 0, 0, &client_hash_table_ops))
    11371093                abort();
    11381094       
    1139         if (!hash_table_create(&conn_hash_table, 0, 1, &conn_hash_table_ops))
     1095        if (!hash_table_create(&conn_hash_table, 0, 0, &conn_hash_table_ops))
    11401096                abort();
    11411097       
Note: See TracChangeset for help on using the changeset viewer.