Changeset efdfebc in mainline for uspace/srv/ns/task.c


Ignore:
Timestamp:
2012-11-06T21:03:44Z (11 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
338810f
Parents:
de73242 (diff), 94795812 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/ns/task.c

    rde73242 refdfebc  
    4343#include "ns.h"
    4444
    45 #define TASK_HASH_TABLE_CHAINS  256
    46 #define P2I_HASH_TABLE_CHAINS   256
    4745
    4846/* TODO:
     
    5553/** Task hash table item. */
    5654typedef struct {
    57         link_t link;
     55        ht_link_t link;
    5856       
    5957        task_id_t id;    /**< Task ID. */
     
    6361} hashed_task_t;
    6462
    65 /** Compute hash index into task hash table.
    66  *
    67  * @param key Pointer keys. However, only the first key (i.e. truncated task
    68  *            number) is used to compute the hash index.
    69  *
    70  * @return Hash index corresponding to key[0].
    71  *
    72  */
    73 static hash_index_t task_hash(unsigned long key[])
    74 {
    75         assert(key);
    76         return (LOWER32(key[0]) % TASK_HASH_TABLE_CHAINS);
    77 }
    78 
    79 /** Compare a key with hashed item.
    80  *
    81  * @param key  Array of keys.
    82  * @param keys Must be less than or equal to 2.
    83  * @param item Pointer to a hash table item.
    84  *
    85  * @return Non-zero if the key matches the item, zero otherwise.
    86  *
    87  */
    88 static int task_compare(unsigned long key[], hash_count_t keys, link_t *item)
    89 {
    90         assert(key);
    91         assert(keys <= 2);
    92         assert(item);
    93        
    94         hashed_task_t *ht = hash_table_get_instance(item, hashed_task_t, link);
    95        
    96         if (keys == 2)
    97                 return ((LOWER32(key[1]) == UPPER32(ht->id))
    98                     && (LOWER32(key[0]) == LOWER32(ht->id)));
    99         else
    100                 return (LOWER32(key[0]) == LOWER32(ht->id));
    101 }
    102 
    103 /** Perform actions after removal of item from the hash table.
    104  *
    105  * @param item Item that was removed from the hash table.
    106  *
    107  */
    108 static void task_remove(link_t *item)
    109 {
    110         assert(item);
    111         free(hash_table_get_instance(item, hashed_task_t, link));
     63
     64static size_t task_key_hash(void *key)
     65{
     66        return *(task_id_t*)key;
     67}
     68
     69static size_t task_hash(const ht_link_t  *item)
     70{
     71        hashed_task_t *ht = hash_table_get_inst(item, hashed_task_t, link);
     72        return ht->id;
     73}
     74
     75static bool task_key_equal(void *key, const ht_link_t *item)
     76{
     77        hashed_task_t *ht = hash_table_get_inst(item, hashed_task_t, link);
     78        return ht->id == *(task_id_t*)key;
     79}
     80
     81/** Perform actions after removal of item from the hash table. */
     82static void task_remove(ht_link_t *item)
     83{
     84        free(hash_table_get_inst(item, hashed_task_t, link));
    11285}
    11386
    11487/** Operations for task hash table. */
    115 static hash_table_operations_t task_hash_table_ops = {
     88static hash_table_ops_t task_hash_table_ops = {
    11689        .hash = task_hash,
    117         .compare = task_compare,
     90        .key_hash = task_key_hash,
     91        .key_equal = task_key_equal,
     92        .equal = NULL,
    11893        .remove_callback = task_remove
    11994};
     
    12398
    12499typedef struct {
    125         link_t link;
     100        ht_link_t link;
    126101        sysarg_t in_phone_hash;  /**< Incoming phone hash. */
    127102        task_id_t id;            /**< Task ID. */
    128103} p2i_entry_t;
    129104
    130 /** Compute hash index into task hash table.
    131  *
    132  * @param key Array of keys.
    133  *
    134  * @return Hash index corresponding to key[0].
    135  *
    136  */
    137 static hash_index_t p2i_hash(unsigned long key[])
    138 {
    139         assert(key);
    140         return (key[0] % TASK_HASH_TABLE_CHAINS);
    141 }
    142 
    143 /** Compare a key with hashed item.
    144  *
    145  * @param key  Array of keys.
    146  * @param keys Must be less than or equal to 1.
    147  * @param item Pointer to a hash table item.
    148  *
    149  * @return Non-zero if the key matches the item, zero otherwise.
    150  *
    151  */
    152 static int p2i_compare(unsigned long key[], hash_count_t keys, link_t *item)
    153 {
    154         assert(key);
    155         assert(keys == 1);
     105/* phone-to-id hash table operations */
     106
     107static size_t p2i_key_hash(void *key)
     108{
     109        sysarg_t in_phone_hash = *(sysarg_t*)key;
     110        return in_phone_hash;
     111}
     112
     113static size_t p2i_hash(const ht_link_t *item)
     114{
     115        p2i_entry_t *entry = hash_table_get_inst(item, p2i_entry_t, link);
     116        return entry->in_phone_hash;
     117}
     118
     119static bool p2i_key_equal(void *key, const ht_link_t *item)
     120{
     121        sysarg_t in_phone_hash = *(sysarg_t*)key;
     122        p2i_entry_t *entry = hash_table_get_inst(item, p2i_entry_t, link);
     123       
     124        return (in_phone_hash == entry->in_phone_hash);
     125}
     126
     127/** Perform actions after removal of item from the hash table.
     128 *
     129 * @param item Item that was removed from the hash table.
     130 *
     131 */
     132static void p2i_remove(ht_link_t *item)
     133{
    156134        assert(item);
    157        
    158         p2i_entry_t *entry = hash_table_get_instance(item, p2i_entry_t, link);
    159        
    160         return (key[0] == entry->in_phone_hash);
    161 }
    162 
    163 /** Perform actions after removal of item from the hash table.
    164  *
    165  * @param item Item that was removed from the hash table.
    166  *
    167  */
    168 static void p2i_remove(link_t *item)
    169 {
    170         assert(item);
    171         free(hash_table_get_instance(item, p2i_entry_t, link));
     135        free(hash_table_get_inst(item, p2i_entry_t, link));
    172136}
    173137
    174138/** Operations for task hash table. */
    175 static hash_table_operations_t p2i_ops = {
     139static hash_table_ops_t p2i_ops = {
    176140        .hash = p2i_hash,
    177         .compare = p2i_compare,
     141        .key_hash = p2i_key_hash,
     142        .key_equal = p2i_key_equal,
     143        .equal = NULL,
    178144        .remove_callback = p2i_remove
    179145};
     
    193159int task_init(void)
    194160{
    195         if (!hash_table_create(&task_hash_table, TASK_HASH_TABLE_CHAINS,
    196             2, &task_hash_table_ops)) {
     161        if (!hash_table_create(&task_hash_table, 0, 0, &task_hash_table_ops)) {
    197162                printf(NAME ": No memory available for tasks\n");
    198163                return ENOMEM;
    199164        }
    200165       
    201         if (!hash_table_create(&phone_to_id, P2I_HASH_TABLE_CHAINS,
    202             1, &p2i_ops)) {
     166        if (!hash_table_create(&phone_to_id, 0, 0, &p2i_ops)) {
    203167                printf(NAME ": No memory available for tasks\n");
    204168                return ENOMEM;
     
    218182                pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link);
    219183               
    220                 unsigned long keys[2] = {
    221                         LOWER32(pr->id),
    222                         UPPER32(pr->id)
    223                 };
    224                
    225                 link_t *link = hash_table_find(&task_hash_table, keys);
     184                ht_link_t *link = hash_table_find(&task_hash_table, &pr->id);
    226185                if (!link)
    227186                        continue;
    228187               
    229                 hashed_task_t *ht = hash_table_get_instance(link, hashed_task_t, link);
     188                hashed_task_t *ht = hash_table_get_inst(link, hashed_task_t, link);
    230189                if (!ht->finished)
    231190                        continue;
     
    238197                }
    239198               
    240                 hash_table_remove(&task_hash_table, keys, 2);
     199                hash_table_remove(&task_hash_table, &pr->id);
    241200                list_remove(cur);
    242201                free(pr);
     
    250209        task_exit_t texit;
    251210       
    252         unsigned long keys[2] = {
    253                 LOWER32(id),
    254                 UPPER32(id)
    255         };
    256        
    257         link_t *link = hash_table_find(&task_hash_table, keys);
     211        ht_link_t *link = hash_table_find(&task_hash_table, &id);
    258212        hashed_task_t *ht = (link != NULL) ?
    259             hash_table_get_instance(link, hashed_task_t, link) : NULL;
     213            hash_table_get_inst(link, hashed_task_t, link) : NULL;
    260214       
    261215        if (ht == NULL) {
     
    281235        }
    282236       
    283         hash_table_remove(&task_hash_table, keys, 2);
     237        hash_table_remove_item(&task_hash_table, link);
    284238        retval = EOK;
    285239       
     
    293247int ns_task_id_intro(ipc_call_t *call)
    294248{
    295         unsigned long keys[2];
    296249       
    297250        task_id_t id = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call));
    298         keys[0] = call->in_phone_hash;
    299        
    300         link_t *link = hash_table_find(&phone_to_id, keys);
     251
     252        ht_link_t *link = hash_table_find(&phone_to_id, &call->in_phone_hash);
    301253        if (link != NULL)
    302254                return EEXISTS;
     
    314266         */
    315267       
    316         link_initialize(&entry->link);
    317268        entry->in_phone_hash = call->in_phone_hash;
    318269        entry->id = id;
    319         hash_table_insert(&phone_to_id, keys, &entry->link);
     270        hash_table_insert(&phone_to_id, &entry->link);
    320271       
    321272        /*
     
    323274         */
    324275       
    325         keys[0] = LOWER32(id);
    326         keys[1] = UPPER32(id);
    327        
    328         link_initialize(&ht->link);
    329276        ht->id = id;
    330277        ht->finished = false;
    331278        ht->have_rval = false;
    332279        ht->retval = -1;
    333         hash_table_insert(&task_hash_table, keys, &ht->link);
     280        hash_table_insert(&task_hash_table, &ht->link);
    334281       
    335282        return EOK;
     
    338285static int get_id_by_phone(sysarg_t phone_hash, task_id_t *id)
    339286{
    340         unsigned long keys[1] = {phone_hash};
    341        
    342         link_t *link = hash_table_find(&phone_to_id, keys);
     287        ht_link_t *link = hash_table_find(&phone_to_id, &phone_hash);
    343288        if (link == NULL)
    344289                return ENOENT;
    345290       
    346         p2i_entry_t *entry = hash_table_get_instance(link, p2i_entry_t, link);
     291        p2i_entry_t *entry = hash_table_get_inst(link, p2i_entry_t, link);
    347292        *id = entry->id;
    348293       
     
    357302                return rc;
    358303       
    359         unsigned long keys[2] = {
    360                 LOWER32(id),
    361                 UPPER32(id)
    362         };
    363        
    364         link_t *link = hash_table_find(&task_hash_table, keys);
     304        ht_link_t *link = hash_table_find(&task_hash_table, &id);
    365305        hashed_task_t *ht = (link != NULL) ?
    366             hash_table_get_instance(link, hashed_task_t, link) : NULL;
     306            hash_table_get_inst(link, hashed_task_t, link) : NULL;
    367307       
    368308        if ((ht == NULL) || (ht->finished))
     
    378318int ns_task_disconnect(ipc_call_t *call)
    379319{
    380         unsigned long keys[2];
    381        
    382320        task_id_t id;
    383321        int rc = get_id_by_phone(call->in_phone_hash, &id);
     
    386324       
    387325        /* Delete from phone-to-id map. */
    388         keys[0] = call->in_phone_hash;
    389         hash_table_remove(&phone_to_id, keys, 1);
     326        hash_table_remove(&phone_to_id, &call->in_phone_hash);
    390327       
    391328        /* Mark task as finished. */
    392         keys[0] = LOWER32(id);
    393         keys[1] = UPPER32(id);
    394        
    395         link_t *link = hash_table_find(&task_hash_table, keys);
    396         hashed_task_t *ht =
    397             hash_table_get_instance(link, hashed_task_t, link);
    398         if (ht == NULL)
     329        ht_link_t *link = hash_table_find(&task_hash_table, &id);
     330        if (link == NULL)
    399331                return EOK;
     332
     333        hashed_task_t *ht = hash_table_get_inst(link, hashed_task_t, link);
    400334       
    401335        ht->finished = true;
Note: See TracChangeset for help on using the changeset viewer.