Changes in uspace/srv/ns/task.c [adb49f58:c7bbf029] in mainline


Ignore:
File:
1 edited

Legend:

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

    radb49f58 rc7bbf029  
    3939#include <stdio.h>
    4040#include <macros.h>
     41#include <malloc.h>
    4142#include "task.h"
    4243#include "ns.h"
    4344
    4445#define TASK_HASH_TABLE_CHAINS  256
    45 #define P2I_HASH_TABLE_CHAINS  256
    46 
    47 static int get_id_by_phone(ipcarg_t phone_hash, task_id_t *id);
     46#define P2I_HASH_TABLE_CHAINS   256
    4847
    4948/* TODO:
     
    5756typedef struct {
    5857        link_t link;
    59         task_id_t id;   /**< Task ID. */
    60         bool finished;  /**< Task is done. */
    61         bool have_rval; /**< Task returned a value. */
    62         int retval;     /**< The return value. */
     58       
     59        task_id_t id;    /**< Task ID. */
     60        bool finished;   /**< Task is done. */
     61        bool have_rval;  /**< Task returned a value. */
     62        int retval;      /**< The return value. */
    6363} hashed_task_t;
    6464
     
    7171 *
    7272 */
    73 static hash_index_t task_hash(unsigned long *key)
     73static hash_index_t task_hash(unsigned long key[])
    7474{
    7575        assert(key);
    76         return (LOWER32(*key) % TASK_HASH_TABLE_CHAINS);
     76        return (LOWER32(key[0]) % TASK_HASH_TABLE_CHAINS);
    7777}
    7878
     
    124124typedef struct {
    125125        link_t link;
    126         ipcarg_t phash;    /**< Task ID. */
    127         task_id_t id;    /**< Task ID. */
     126        sysarg_t in_phone_hash;  /**< Incoming phone hash. */
     127        task_id_t id;            /**< Task ID. */
    128128} p2i_entry_t;
    129129
     
    131131 *
    132132 * @param key Array of keys.
     133 *
    133134 * @return Hash index corresponding to key[0].
    134135 *
    135136 */
    136 static hash_index_t p2i_hash(unsigned long *key)
     137static hash_index_t p2i_hash(unsigned long key[])
    137138{
    138139        assert(key);
    139         return (*key % TASK_HASH_TABLE_CHAINS);
     140        return (key[0] % TASK_HASH_TABLE_CHAINS);
    140141}
    141142
     
    154155        assert(keys == 1);
    155156        assert(item);
    156 
    157         p2i_entry_t *e = hash_table_get_instance(item, p2i_entry_t, link);
    158 
    159         return (key[0] == e->phash);
     157       
     158        p2i_entry_t *entry = hash_table_get_instance(item, p2i_entry_t, link);
     159       
     160        return (key[0] == entry->in_phone_hash);
    160161}
    161162
     
    197198                return ENOMEM;
    198199        }
    199 
     200       
    200201        if (!hash_table_create(&phone_to_id, P2I_HASH_TABLE_CHAINS,
    201202            1, &p2i_ops)) {
     
    205206       
    206207        list_initialize(&pending_wait);
    207        
    208208        return EOK;
    209209}
     
    238238                            ht->retval);
    239239                }
    240 
     240               
    241241                hash_table_remove(&task_hash_table, keys, 2);
    242242                list_remove(cur);
     
    248248void wait_for_task(task_id_t id, ipc_call_t *call, ipc_callid_t callid)
    249249{
    250         ipcarg_t retval;
     250        sysarg_t retval;
    251251        task_exit_t texit;
    252 
     252       
    253253        unsigned long keys[2] = {
    254254                LOWER32(id),
    255255                UPPER32(id)
    256256        };
    257 
     257       
    258258        link_t *link = hash_table_find(&task_hash_table, keys);
    259259        hashed_task_t *ht = (link != NULL) ?
    260260            hash_table_get_instance(link, hashed_task_t, link) : NULL;
    261 
     261       
    262262        if (ht == NULL) {
    263263                /* No such task exists. */
    264                 retval = ENOENT;
    265                 goto out;
    266         }
    267 
     264                ipc_answer_0(callid, ENOENT);
     265                return;
     266        }
     267       
    268268        if (!ht->finished) {
    269269                /* Add to pending list */
     
    275275                }
    276276               
     277                link_initialize(&pr->link);
    277278                pr->id = id;
    278279                pr->callid = callid;
     
    293294int ns_task_id_intro(ipc_call_t *call)
    294295{
    295         task_id_t id;
    296296        unsigned long keys[2];
    297         link_t *link;
    298         p2i_entry_t *e;
    299         hashed_task_t *ht;
    300 
    301         id = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call));
    302 
     297       
     298        task_id_t id = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call));
    303299        keys[0] = call->in_phone_hash;
    304 
    305         link = hash_table_find(&phone_to_id, keys);
     300       
     301        link_t *link = hash_table_find(&phone_to_id, keys);
    306302        if (link != NULL)
    307303                return EEXISTS;
    308 
    309         e = (p2i_entry_t *) malloc(sizeof(p2i_entry_t));
    310         if (e == NULL)
     304       
     305        p2i_entry_t *entry = (p2i_entry_t *) malloc(sizeof(p2i_entry_t));
     306        if (entry == NULL)
    311307                return ENOMEM;
    312 
    313         ht = (hashed_task_t *) malloc(sizeof(hashed_task_t));
     308       
     309        hashed_task_t *ht = (hashed_task_t *) malloc(sizeof(hashed_task_t));
    314310        if (ht == NULL)
    315311                return ENOMEM;
    316 
    317         /* Insert to phone-to-id map. */
    318 
    319         link_initialize(&e->link);
    320         e->phash = call->in_phone_hash;
    321         e->id = id;
    322         hash_table_insert(&phone_to_id, keys, &e->link);
    323 
    324         /* Insert to main table. */
    325 
     312       
     313        /*
     314         * Insert into the phone-to-id map.
     315         */
     316       
     317        link_initialize(&entry->link);
     318        entry->in_phone_hash = call->in_phone_hash;
     319        entry->id = id;
     320        hash_table_insert(&phone_to_id, keys, &entry->link);
     321       
     322        /*
     323         * Insert into the main table.
     324         */
     325       
    326326        keys[0] = LOWER32(id);
    327327        keys[1] = UPPER32(id);
    328 
     328       
    329329        link_initialize(&ht->link);
    330330        ht->id = id;
     
    333333        ht->retval = -1;
    334334        hash_table_insert(&task_hash_table, keys, &ht->link);
    335 
     335       
    336336        return EOK;
    337337}
    338338
     339static int get_id_by_phone(sysarg_t phone_hash, task_id_t *id)
     340{
     341        unsigned long keys[1] = {phone_hash};
     342       
     343        link_t *link = hash_table_find(&phone_to_id, keys);
     344        if (link == NULL)
     345                return ENOENT;
     346       
     347        p2i_entry_t *entry = hash_table_get_instance(link, p2i_entry_t, link);
     348        *id = entry->id;
     349       
     350        return EOK;
     351}
     352
    339353int ns_task_retval(ipc_call_t *call)
    340354{
    341355        task_id_t id;
    342         unsigned long keys[2];
    343         int rc;
    344 
    345         rc = get_id_by_phone(call->in_phone_hash, &id);
     356        int rc = get_id_by_phone(call->in_phone_hash, &id);
    346357        if (rc != EOK)
    347358                return rc;
    348 
    349         keys[0] = LOWER32(id);
    350         keys[1] = UPPER32(id);
     359       
     360        unsigned long keys[2] = {
     361                LOWER32(id),
     362                UPPER32(id)
     363        };
    351364       
    352365        link_t *link = hash_table_find(&task_hash_table, keys);
     
    354367            hash_table_get_instance(link, hashed_task_t, link) : NULL;
    355368       
    356         if ((ht == NULL) || ht->finished)
     369        if ((ht == NULL) || (ht->finished))
    357370                return EINVAL;
    358 
     371       
    359372        ht->finished = true;
    360373        ht->have_rval = true;
    361374        ht->retval = IPC_GET_ARG1(*call);
    362 
     375       
    363376        return EOK;
    364377}
     
    367380{
    368381        unsigned long keys[2];
     382       
    369383        task_id_t id;
    370         int rc;
    371 
    372         rc = get_id_by_phone(call->in_phone_hash, &id);
     384        int rc = get_id_by_phone(call->in_phone_hash, &id);
    373385        if (rc != EOK)
    374386                return rc;
    375 
     387       
    376388        /* Delete from phone-to-id map. */
    377389        keys[0] = call->in_phone_hash;
    378390        hash_table_remove(&phone_to_id, keys, 1);
    379 
     391       
    380392        /* Mark task as finished. */
    381393        keys[0] = LOWER32(id);
    382394        keys[1] = UPPER32(id);
    383 
     395       
    384396        link_t *link = hash_table_find(&task_hash_table, keys);
    385397        hashed_task_t *ht =
     
    387399        if (ht == NULL)
    388400                return EOK;
    389 
     401       
    390402        ht->finished = true;
    391 
    392         return EOK;
    393 }
    394 
    395 static int get_id_by_phone(ipcarg_t phone_hash, task_id_t *id)
    396 {
    397         unsigned long keys[1];
    398         link_t *link;
    399         p2i_entry_t *e;
    400 
    401         keys[0] = phone_hash;
    402         link = hash_table_find(&phone_to_id, keys);
    403         if (link == NULL)
    404                 return ENOENT;
    405 
    406         e = hash_table_get_instance(link, p2i_entry_t, link);
    407         *id = e->id;
    408 
     403       
    409404        return EOK;
    410405}
Note: See TracChangeset for help on using the changeset viewer.