Changes in uspace/srv/ns/task.c [9d58539:3e6a98c5] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/ns/task.c
r9d58539 r3e6a98c5 34 34 #include <ipc/ipc.h> 35 35 #include <adt/hash_table.h> 36 #include < bool.h>36 #include <stdbool.h> 37 37 #include <errno.h> 38 38 #include <assert.h> … … 43 43 #include "ns.h" 44 44 45 #define TASK_HASH_TABLE_CHAINS 25646 #define P2I_HASH_TABLE_CHAINS 25647 45 48 46 /* TODO: … … 55 53 /** Task hash table item. */ 56 54 typedef struct { 57 link_t link;55 ht_link_t link; 58 56 59 57 task_id_t id; /**< Task ID. */ … … 63 61 } hashed_task_t; 64 62 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 64 static size_t task_key_hash(void *key) 65 { 66 return *(task_id_t*)key; 67 } 68 69 static 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 75 static 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. */ 82 static void task_remove(ht_link_t *item) 83 { 84 free(hash_table_get_inst(item, hashed_task_t, link)); 112 85 } 113 86 114 87 /** Operations for task hash table. */ 115 static hash_table_op erations_t task_hash_table_ops = {88 static hash_table_ops_t task_hash_table_ops = { 116 89 .hash = task_hash, 117 .compare = task_compare, 90 .key_hash = task_key_hash, 91 .key_equal = task_key_equal, 92 .equal = NULL, 118 93 .remove_callback = task_remove 119 94 }; … … 123 98 124 99 typedef struct { 125 link_t link;100 ht_link_t link; 126 101 sysarg_t in_phone_hash; /**< Incoming phone hash. */ 127 102 task_id_t id; /**< Task ID. */ 128 103 } p2i_entry_t; 129 104 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 107 static size_t p2i_key_hash(void *key) 108 { 109 sysarg_t in_phone_hash = *(sysarg_t*)key; 110 return in_phone_hash; 111 } 112 113 static 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 119 static 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 */ 132 static void p2i_remove(ht_link_t *item) 133 { 156 134 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)); 172 136 } 173 137 174 138 /** Operations for task hash table. */ 175 static hash_table_op erations_t p2i_ops = {139 static hash_table_ops_t p2i_ops = { 176 140 .hash = p2i_hash, 177 .compare = p2i_compare, 141 .key_hash = p2i_key_hash, 142 .key_equal = p2i_key_equal, 143 .equal = NULL, 178 144 .remove_callback = p2i_remove 179 145 }; … … 193 159 int task_init(void) 194 160 { 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)) { 197 162 printf(NAME ": No memory available for tasks\n"); 198 163 return ENOMEM; 199 164 } 200 165 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)) { 203 167 printf(NAME ": No memory available for tasks\n"); 204 168 return ENOMEM; … … 218 182 pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link); 219 183 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); 226 185 if (!link) 227 186 continue; 228 187 229 hashed_task_t *ht = hash_table_get_inst ance(link, hashed_task_t, link);188 hashed_task_t *ht = hash_table_get_inst(link, hashed_task_t, link); 230 189 if (!ht->finished) 231 190 continue; … … 238 197 } 239 198 240 hash_table_remove(&task_hash_table, keys, 2);199 hash_table_remove(&task_hash_table, &pr->id); 241 200 list_remove(cur); 242 201 free(pr); … … 250 209 task_exit_t texit; 251 210 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); 258 212 hashed_task_t *ht = (link != NULL) ? 259 hash_table_get_inst ance(link, hashed_task_t, link) : NULL;213 hash_table_get_inst(link, hashed_task_t, link) : NULL; 260 214 261 215 if (ht == NULL) { … … 281 235 } 282 236 283 hash_table_remove (&task_hash_table, keys, 2);237 hash_table_remove_item(&task_hash_table, link); 284 238 retval = EOK; 285 239 … … 293 247 int ns_task_id_intro(ipc_call_t *call) 294 248 { 295 unsigned long keys[2];296 249 297 250 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); 301 253 if (link != NULL) 302 254 return EEXISTS; … … 314 266 */ 315 267 316 link_initialize(&entry->link);317 268 entry->in_phone_hash = call->in_phone_hash; 318 269 entry->id = id; 319 hash_table_insert(&phone_to_id, keys,&entry->link);270 hash_table_insert(&phone_to_id, &entry->link); 320 271 321 272 /* … … 323 274 */ 324 275 325 keys[0] = LOWER32(id);326 keys[1] = UPPER32(id);327 328 link_initialize(&ht->link);329 276 ht->id = id; 330 277 ht->finished = false; 331 278 ht->have_rval = false; 332 279 ht->retval = -1; 333 hash_table_insert(&task_hash_table, keys,&ht->link);280 hash_table_insert(&task_hash_table, &ht->link); 334 281 335 282 return EOK; … … 338 285 static int get_id_by_phone(sysarg_t phone_hash, task_id_t *id) 339 286 { 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); 343 288 if (link == NULL) 344 289 return ENOENT; 345 290 346 p2i_entry_t *entry = hash_table_get_inst ance(link, p2i_entry_t, link);291 p2i_entry_t *entry = hash_table_get_inst(link, p2i_entry_t, link); 347 292 *id = entry->id; 348 293 … … 357 302 return rc; 358 303 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); 365 305 hashed_task_t *ht = (link != NULL) ? 366 hash_table_get_inst ance(link, hashed_task_t, link) : NULL;306 hash_table_get_inst(link, hashed_task_t, link) : NULL; 367 307 368 308 if ((ht == NULL) || (ht->finished)) … … 378 318 int ns_task_disconnect(ipc_call_t *call) 379 319 { 380 unsigned long keys[2];381 382 320 task_id_t id; 383 321 int rc = get_id_by_phone(call->in_phone_hash, &id); … … 386 324 387 325 /* 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); 390 327 391 328 /* 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) 399 331 return EOK; 332 333 hashed_task_t *ht = hash_table_get_inst(link, hashed_task_t, link); 400 334 401 335 ht->finished = true;
Note:
See TracChangeset
for help on using the changeset viewer.