Subversion Repositories HelenOS

Rev

Rev 4509 | Rev 4618 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2009 Martin Decky
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup ns
  30.  * @{
  31.  */
  32.  
  33. #include <ipc/ipc.h>
  34. #include <adt/hash_table.h>
  35. #include <bool.h>
  36. #include <errno.h>
  37. #include <assert.h>
  38. #include <stdio.h>
  39. #include <macros.h>
  40. #include "task.h"
  41. #include "ns.h"
  42.  
  43. #define TASK_HASH_TABLE_CHAINS  256
  44.  
  45. /* TODO:
  46.  *
  47.  * The current implementation of waiting on a task is not perfect. If somebody
  48.  * wants to wait on a task which has already finished before the NS asked
  49.  * the kernel to receive notifications, it would block indefinitively.
  50.  *
  51.  * A solution to this is to fail immediately on a task for which no creation
  52.  * notification was received yet. However, there is a danger of a race condition
  53.  * in this solution -- the caller has to make sure that it is not trying to wait
  54.  * before the NS has a change to receive the task creation notification. This
  55.  * can be assured by waiting for this event in task_spawn().
  56.  *
  57.  * Finally, as there is currently no convention that each task has to be waited
  58.  * for, the NS can leak memory because of the zombie tasks.
  59.  *
  60.  */
  61.  
  62. /** Task hash table item. */
  63. typedef struct {
  64.     link_t link;
  65.     task_id_t id;    /**< Task ID. */
  66.     int retval;
  67.     bool destroyed;
  68. } hashed_task_t;
  69.  
  70. /** Compute hash index into task hash table.
  71.  *
  72.  * @param key Pointer keys. However, only the first key (i.e. truncated task
  73.  *            number) is used to compute the hash index.
  74.  *
  75.  * @return Hash index corresponding to key[0].
  76.  *
  77.  */
  78. static hash_index_t task_hash(unsigned long *key)
  79. {
  80.     assert(key);
  81.     return (LOWER32(*key) % TASK_HASH_TABLE_CHAINS);
  82. }
  83.  
  84. /** Compare a key with hashed item.
  85.  *
  86.  * @param key  Array of keys.
  87.  * @param keys Must be lesser or equal to 2.
  88.  * @param item Pointer to a hash table item.
  89.  *
  90.  * @return Non-zero if the key matches the item, zero otherwise.
  91.  *
  92.  */
  93. static int task_compare(unsigned long key[], hash_count_t keys, link_t *item)
  94. {
  95.     assert(key);
  96.     assert(keys <= 2);
  97.     assert(item);
  98.    
  99.     hashed_task_t *ht = hash_table_get_instance(item, hashed_task_t, link);
  100.    
  101.     if (keys == 2)
  102.         return ((LOWER32(key[1]) == UPPER32(ht->id))
  103.             && (LOWER32(key[0]) == LOWER32(ht->id)));
  104.     else
  105.         return (LOWER32(key[0]) == LOWER32(ht->id));
  106. }
  107.  
  108. /** Perform actions after removal of item from the hash table.
  109.  *
  110.  * @param item Item that was removed from the hash table.
  111.  *
  112.  */
  113. static void task_remove(link_t *item)
  114. {
  115.     assert(item);
  116.     free(hash_table_get_instance(item, hashed_task_t, link));
  117. }
  118.  
  119. /** Operations for task hash table. */
  120. static hash_table_operations_t task_hash_table_ops = {
  121.     .hash = task_hash,
  122.     .compare = task_compare,
  123.     .remove_callback = task_remove
  124. };
  125.  
  126. /** Task hash table structure. */
  127. static hash_table_t task_hash_table;
  128.  
  129. /** Pending task wait structure. */
  130. typedef struct {
  131.     link_t link;
  132.     task_id_t id;         /**< Task ID. */
  133.     ipc_callid_t callid;  /**< Call ID waiting for the connection */
  134. } pending_wait_t;
  135.  
  136. static link_t pending_wait;
  137.  
  138. int task_init(void)
  139. {
  140.     if (!hash_table_create(&task_hash_table, TASK_HASH_TABLE_CHAINS,
  141.         2, &task_hash_table_ops)) {
  142.         printf(NAME ": No memory available for tasks\n");
  143.         return ENOMEM;
  144.     }
  145.    
  146.     if (event_subscribe(EVENT_WAIT, 0) != EOK)
  147.         printf(NAME ": Error registering wait notifications\n");
  148.    
  149.     list_initialize(&pending_wait);
  150.    
  151.     return EOK;
  152. }
  153.  
  154. /** Process pending wait requests */
  155. void process_pending_wait(void)
  156. {
  157.     link_t *cur;
  158.    
  159. loop:
  160.     for (cur = pending_wait.next; cur != &pending_wait; cur = cur->next) {
  161.         pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link);
  162.        
  163.         unsigned long keys[2] = {
  164.             LOWER32(pr->id),
  165.             UPPER32(pr->id)
  166.         };
  167.        
  168.         link_t *link = hash_table_find(&task_hash_table, keys);
  169.         if (!link)
  170.             continue;
  171.        
  172.         hashed_task_t *ht = hash_table_get_instance(link, hashed_task_t, link);
  173.         if (!ht->destroyed)
  174.             continue;
  175.        
  176.         if (!(pr->callid & IPC_CALLID_NOTIFICATION))
  177.             ipc_answer_1(pr->callid, EOK, ht->retval);
  178.        
  179.         hash_table_remove(&task_hash_table, keys, 2);
  180.         list_remove(cur);
  181.         free(pr);
  182.         goto loop;
  183.     }
  184. }
  185.  
  186. static void fail_pending_wait(task_id_t id, int rc)
  187. {
  188.     link_t *cur;
  189.    
  190. loop:
  191.     for (cur = pending_wait.next; cur != &pending_wait; cur = cur->next) {
  192.         pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link);
  193.        
  194.         if (pr->id == id) {
  195.             if (!(pr->callid & IPC_CALLID_NOTIFICATION))
  196.                 ipc_answer_0(pr->callid, rc);
  197.        
  198.             list_remove(cur);
  199.             free(pr);
  200.             goto loop;
  201.         }
  202.     }
  203. }
  204.  
  205. void wait_notification(wait_type_t et, task_id_t id)
  206. {
  207.     unsigned long keys[2] = {
  208.         LOWER32(id),
  209.         UPPER32(id)
  210.     };
  211.    
  212.     link_t *link = hash_table_find(&task_hash_table, keys);
  213.    
  214.     if (link == NULL) {
  215.         hashed_task_t *ht =
  216.             (hashed_task_t *) malloc(sizeof(hashed_task_t));
  217.         if (ht == NULL) {
  218.             fail_pending_wait(id, ENOMEM);
  219.             return;
  220.         }
  221.        
  222.         link_initialize(&ht->link);
  223.         ht->id = id;
  224.         ht->destroyed = (et == TASK_CREATE) ? false : true;
  225.         ht->retval = -1;
  226.         hash_table_insert(&task_hash_table, keys, &ht->link);
  227.     } else {
  228.         hashed_task_t *ht =
  229.             hash_table_get_instance(link, hashed_task_t, link);
  230.         ht->destroyed = (et == TASK_CREATE) ? false : true;
  231.     }
  232. }
  233.  
  234. void wait_for_task(task_id_t id, ipc_call_t *call, ipc_callid_t callid)
  235. {
  236.     ipcarg_t retval;
  237.     unsigned long keys[2] = {
  238.         LOWER32(id),
  239.         UPPER32(id)
  240.     };
  241.    
  242.     link_t *link = hash_table_find(&task_hash_table, keys);
  243.     hashed_task_t *ht = (link != NULL) ?
  244.         hash_table_get_instance(link, hashed_task_t, link) : NULL;
  245.    
  246.     if ((ht == NULL) || (!ht->destroyed)) {
  247.         /* Add to pending list */
  248.         pending_wait_t *pr =
  249.             (pending_wait_t *) malloc(sizeof(pending_wait_t));
  250.         if (!pr) {
  251.             retval = ENOMEM;
  252.             goto out;
  253.         }
  254.        
  255.         pr->id = id;
  256.         pr->callid = callid;
  257.         list_append(&pr->link, &pending_wait);
  258.         return;
  259.     }
  260.    
  261.     hash_table_remove(&task_hash_table, keys, 2);
  262.     retval = EOK;
  263.    
  264. out:
  265.     if (!(callid & IPC_CALLID_NOTIFICATION))
  266.         ipc_answer_1(callid, retval, ht->retval);
  267. }
  268.  
  269. int ns_task_retval(ipc_call_t *call)
  270. {
  271.     task_id_t id;
  272.     unsigned long keys[2];
  273.  
  274.     id = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call));
  275.  
  276.     keys[0] = LOWER32(id);
  277.     keys[1] = UPPER32(id);
  278.    
  279.     link_t *link = hash_table_find(&task_hash_table, keys);
  280.     hashed_task_t *ht = (link != NULL) ?
  281.         hash_table_get_instance(link, hashed_task_t, link) : NULL;
  282.    
  283.     if ((ht == NULL) || ht->destroyed)
  284.         return EINVAL;
  285.  
  286.     ht->retval = IPC_GET_ARG3(*call);
  287.  
  288.     return EOK;
  289. }
  290.  
  291. /**
  292.  * @}
  293.  */
  294.