Changeset bc216a0 in mainline for uspace/srv/ns
- Timestamp:
- 2012-08-07T22:13:44Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- da68871a
- Parents:
- b17518e
- Location:
- uspace/srv/ns
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/ns/service.c
rb17518e rbc216a0 43 43 /** Service hash table item. */ 44 44 typedef struct { 45 link_t link;45 ht_link_t link; 46 46 sysarg_t service; /**< Service ID. */ 47 47 sysarg_t phone; /**< Phone registered with the service. */ … … 49 49 } hashed_service_t; 50 50 51 /** Compute hash index into service hash table. 52 * 53 * @param key Pointer keys. However, only the first key (i.e. service number) 54 * is used to compute the hash index. 55 * 56 * @return Hash index corresponding to key[0]. 57 * 58 */ 59 static size_t service_key_hash(unsigned long key[]) 51 52 static size_t service_key_hash(void *key) 60 53 { 61 assert(key); 62 return key[0]; 54 return *(sysarg_t*)key; 63 55 } 64 56 65 static size_t service_hash(const link_t *item)57 static size_t service_hash(const ht_link_t *item) 66 58 { 67 hashed_service_t *hs = hash_table_get_instance(item, hashed_service_t, link); 68 unsigned long key = hs->service; 69 return service_key_hash(&key); 59 hashed_service_t *hs = hash_table_get_inst(item, hashed_service_t, link); 60 return hs->service; 70 61 } 71 62 72 /** Compare a key with hashed item. 73 * 74 * This compare function always ignores the third key. 75 * It exists only to make it possible to remove records 76 * originating from connection with key[1] in_phone_hash 77 * value. Note that this is close to being classified 78 * as a nasty hack. 79 * 80 * @param key Array of keys. 81 * @param keys Must be lesser or equal to 3. 82 * @param item Pointer to a hash table item. 83 * 84 * @return Non-zero if the key matches the item, zero otherwise. 85 * 86 */ 87 static bool service_match(unsigned long key[], size_t keys, const link_t *item) 63 static bool service_key_equal(void *key, const ht_link_t *item) 88 64 { 89 assert(key); 90 assert(keys <= 3); 91 assert(item); 92 93 hashed_service_t *hs = hash_table_get_instance(item, hashed_service_t, link); 94 95 if (keys == 2) 96 return ((key[0] == hs->service) && (key[1] == hs->in_phone_hash)); 97 else 98 return (key[0] == hs->service); 99 } 100 101 /** Perform actions after removal of item from the hash table. 102 * 103 * @param item Item that was removed from the hash table. 104 * 105 */ 106 static void service_remove(link_t *item) 107 { 108 assert(item); 109 free(hash_table_get_instance(item, hashed_service_t, link)); 65 hashed_service_t *hs = hash_table_get_inst(item, hashed_service_t, link); 66 return hs->service == *(sysarg_t*)key; 110 67 } 111 68 … … 114 71 .hash = service_hash, 115 72 .key_hash = service_key_hash, 116 . match = service_match,73 .key_equal = service_key_equal, 117 74 .equal = 0, 118 .remove_callback = service_remove75 .remove_callback = 0 119 76 }; 120 77 … … 135 92 int service_init(void) 136 93 { 137 if (!hash_table_create(&service_hash_table, 0, 3, &service_hash_table_ops)) {94 if (!hash_table_create(&service_hash_table, 0, 0, &service_hash_table_ops)) { 138 95 printf(NAME ": No memory available for services\n"); 139 96 return ENOMEM; … … 152 109 pending_conn_t *pr = list_get_instance(cur, pending_conn_t, link); 153 110 154 unsigned long keys[3] = { 155 pr->service, 156 0, 157 0 158 }; 159 160 link_t *link = hash_table_find(&service_hash_table, keys); 111 ht_link_t *link = hash_table_find(&service_hash_table, &pr->service); 161 112 if (!link) 162 113 continue; 163 114 164 hashed_service_t *hs = hash_table_get_inst ance(link, hashed_service_t, link);115 hashed_service_t *hs = hash_table_get_inst(link, hashed_service_t, link); 165 116 (void) ipc_forward_fast(pr->callid, hs->phone, pr->arg2, 166 117 pr->arg3, 0, IPC_FF_NONE); … … 183 134 int register_service(sysarg_t service, sysarg_t phone, ipc_call_t *call) 184 135 { 185 unsigned long keys[3] = { 186 service, 187 call->in_phone_hash, 188 0 189 }; 190 191 if (hash_table_find(&service_hash_table, keys)) 136 if (hash_table_find(&service_hash_table, &service)) 192 137 return EEXISTS; 193 138 … … 196 141 return ENOMEM; 197 142 198 link_initialize(&hs->link);199 143 hs->service = service; 200 144 hs->phone = phone; … … 217 161 { 218 162 sysarg_t retval; 219 unsigned long keys[3] = {220 service,221 0,222 0223 };224 163 225 link_t *link = hash_table_find(&service_hash_table, keys);164 ht_link_t *link = hash_table_find(&service_hash_table, &service); 226 165 if (!link) { 227 166 if (IPC_GET_ARG4(*call) & IPC_FLAG_BLOCKING) { … … 246 185 } 247 186 248 hashed_service_t *hs = hash_table_get_inst ance(link, hashed_service_t, link);187 hashed_service_t *hs = hash_table_get_inst(link, hashed_service_t, link); 249 188 (void) ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call), 250 189 IPC_GET_ARG3(*call), 0, IPC_FF_NONE); -
uspace/srv/ns/task.c
rb17518e rbc216a0 53 53 /** Task hash table item. */ 54 54 typedef struct { 55 link_t link;55 ht_link_t link; 56 56 57 57 task_id_t id; /**< Task ID. */ … … 61 61 } hashed_task_t; 62 62 63 /** Compute hash index into task hash table. 64 * 65 * @param key Pointer keys. However, only the first key (i.e. truncated task 66 * number) is used to compute the hash index. 67 * 68 * @return Hash index corresponding to key[0]. 69 * 70 */ 71 static size_t task_key_hash(unsigned long key[]) 72 { 73 size_t hash = 17; 74 hash = 37 * hash + key[1]; 75 hash = 37 * hash + key[0]; 76 return hash; 77 } 78 79 static size_t task_hash(const link_t *item) 80 { 81 hashed_task_t *ht = hash_table_get_instance(item, hashed_task_t, link); 82 83 unsigned long key[] = { 84 LOWER32(ht->id), 85 UPPER32(ht->id) 86 }; 87 88 return task_key_hash(key); 89 } 90 91 /** Compare a key with hashed item. 92 * 93 * @param key Array of keys. 94 * @param keys Must be less than or equal to 2. 95 * @param item Pointer to a hash table item. 96 * 97 * @return Non-zero if the key matches the item, zero otherwise. 98 * 99 */ 100 static bool task_match(unsigned long key[], size_t keys, const link_t *item) 101 { 102 assert(key); 103 assert(keys == 2); 104 assert(item); 105 106 hashed_task_t *ht = hash_table_get_instance(item, hashed_task_t, link); 107 108 return (key[0] == LOWER32(ht->id)) 109 && (key[1] == UPPER32(ht->id)); 110 } 111 112 /** Perform actions after removal of item from the hash table. 113 * 114 * @param item Item that was removed from the hash table. 115 * 116 */ 117 static void task_remove(link_t *item) 118 { 119 assert(item); 120 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)); 121 85 } 122 86 … … 125 89 .hash = task_hash, 126 90 .key_hash = task_key_hash, 127 . match = task_match,91 .key_equal = task_key_equal, 128 92 .equal = 0, 129 93 .remove_callback = task_remove … … 134 98 135 99 typedef struct { 136 link_t link;100 ht_link_t link; 137 101 sysarg_t in_phone_hash; /**< Incoming phone hash. */ 138 102 task_id_t id; /**< Task ID. */ 139 103 } p2i_entry_t; 140 104 141 /** Compute hash index into task hash table. 142 * 143 * @param key Array of keys. 144 * 145 * @return Hash index corresponding to key[0]. 146 * 147 */ 148 static size_t p2i_key_hash(unsigned long key[]) 149 { 150 assert(key); 151 return key[0]; 152 } 153 154 static size_t p2i_hash(const link_t *item) 155 { 156 p2i_entry_t *entry = hash_table_get_instance(item, p2i_entry_t, link); 157 unsigned long key = entry->in_phone_hash; 158 return p2i_key_hash(&key); 159 } 160 161 /** Compare a key with hashed item. 162 * 163 * @param key Array of keys. 164 * @param keys Must be less than or equal to 1. 165 * @param item Pointer to a hash table item. 166 * 167 * @return Non-zero if the key matches the item, zero otherwise. 168 * 169 */ 170 static bool p2i_match(unsigned long key[], size_t keys, const link_t *item) 171 { 172 assert(key); 173 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 { 174 134 assert(item); 175 176 p2i_entry_t *entry = hash_table_get_instance(item, p2i_entry_t, link); 177 178 return (key[0] == entry->in_phone_hash); 179 } 180 181 /** Perform actions after removal of item from the hash table. 182 * 183 * @param item Item that was removed from the hash table. 184 * 185 */ 186 static void p2i_remove(link_t *item) 187 { 188 assert(item); 189 free(hash_table_get_instance(item, p2i_entry_t, link)); 135 free(hash_table_get_inst(item, p2i_entry_t, link)); 190 136 } 191 137 … … 194 140 .hash = p2i_hash, 195 141 .key_hash = p2i_key_hash, 196 . match = p2i_match,142 .key_equal = p2i_key_equal, 197 143 .equal = 0, 198 144 .remove_callback = p2i_remove … … 213 159 int task_init(void) 214 160 { 215 if (!hash_table_create(&task_hash_table, 0, 2, &task_hash_table_ops)) {161 if (!hash_table_create(&task_hash_table, 0, 0, &task_hash_table_ops)) { 216 162 printf(NAME ": No memory available for tasks\n"); 217 163 return ENOMEM; 218 164 } 219 165 220 if (!hash_table_create(&phone_to_id, 0, 1, &p2i_ops)) {166 if (!hash_table_create(&phone_to_id, 0, 0, &p2i_ops)) { 221 167 printf(NAME ": No memory available for tasks\n"); 222 168 return ENOMEM; … … 236 182 pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link); 237 183 238 unsigned long keys[2] = { 239 LOWER32(pr->id), 240 UPPER32(pr->id) 241 }; 242 243 link_t *link = hash_table_find(&task_hash_table, keys); 184 ht_link_t *link = hash_table_find(&task_hash_table, &pr->id); 244 185 if (!link) 245 186 continue; 246 187 247 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); 248 189 if (!ht->finished) 249 190 continue; … … 256 197 } 257 198 258 hash_table_remove(&task_hash_table, keys, 2);199 hash_table_remove(&task_hash_table, &pr->id); 259 200 list_remove(cur); 260 201 free(pr); … … 268 209 task_exit_t texit; 269 210 270 unsigned long keys[2] = { 271 LOWER32(id), 272 UPPER32(id) 273 }; 274 275 link_t *link = hash_table_find(&task_hash_table, keys); 211 ht_link_t *link = hash_table_find(&task_hash_table, &id); 276 212 hashed_task_t *ht = (link != NULL) ? 277 hash_table_get_inst ance(link, hashed_task_t, link) : NULL;213 hash_table_get_inst(link, hashed_task_t, link) : NULL; 278 214 279 215 if (ht == NULL) { … … 299 235 } 300 236 301 hash_table_remove (&task_hash_table, keys, 2);237 hash_table_remove_item(&task_hash_table, link); 302 238 retval = EOK; 303 239 … … 314 250 task_id_t id = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call)); 315 251 316 unsigned long keys[] = { call->in_phone_hash }; 317 318 link_t *link = hash_table_find(&phone_to_id, keys); 252 ht_link_t *link = hash_table_find(&phone_to_id, &call->in_phone_hash); 319 253 if (link != NULL) 320 254 return EEXISTS; … … 332 266 */ 333 267 334 link_initialize(&entry->link);335 268 entry->in_phone_hash = call->in_phone_hash; 336 269 entry->id = id; … … 341 274 */ 342 275 343 link_initialize(&ht->link);344 276 ht->id = id; 345 277 ht->finished = false; … … 353 285 static int get_id_by_phone(sysarg_t phone_hash, task_id_t *id) 354 286 { 355 unsigned long keys[1] = {phone_hash}; 356 357 link_t *link = hash_table_find(&phone_to_id, keys); 287 ht_link_t *link = hash_table_find(&phone_to_id, &phone_hash); 358 288 if (link == NULL) 359 289 return ENOENT; 360 290 361 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); 362 292 *id = entry->id; 363 293 … … 372 302 return rc; 373 303 374 unsigned long keys[2] = { 375 LOWER32(id), 376 UPPER32(id) 377 }; 378 379 link_t *link = hash_table_find(&task_hash_table, keys); 304 ht_link_t *link = hash_table_find(&task_hash_table, &id); 380 305 hashed_task_t *ht = (link != NULL) ? 381 hash_table_get_inst ance(link, hashed_task_t, link) : NULL;306 hash_table_get_inst(link, hashed_task_t, link) : NULL; 382 307 383 308 if ((ht == NULL) || (ht->finished)) … … 393 318 int ns_task_disconnect(ipc_call_t *call) 394 319 { 395 unsigned long keys[2];396 397 320 task_id_t id; 398 321 int rc = get_id_by_phone(call->in_phone_hash, &id); … … 401 324 402 325 /* Delete from phone-to-id map. */ 403 keys[0] = call->in_phone_hash; 404 hash_table_remove(&phone_to_id, keys, 1); 326 hash_table_remove(&phone_to_id, &call->in_phone_hash); 405 327 406 328 /* Mark task as finished. */ 407 keys[0] = LOWER32(id); 408 keys[1] = UPPER32(id); 409 410 link_t *link = hash_table_find(&task_hash_table, keys); 329 ht_link_t *link = hash_table_find(&task_hash_table, &id); 411 330 if (link == NULL) 412 331 return EOK; 413 332 414 hashed_task_t *ht = 415 hash_table_get_instance(link, hashed_task_t, link); 333 hashed_task_t *ht = hash_table_get_inst(link, hashed_task_t, link); 416 334 417 335 ht->finished = true;
Note:
See TracChangeset
for help on using the changeset viewer.
