Subversion Repositories HelenOS

Rev

Rev 4537 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4537 Rev 4668
Line 1... Line 1...
1
/*
1
/*
2
 * Copyright (c) 2009 Martin Decky
2
 * Copyright (c) 2009 Martin Decky
-
 
3
 * Copyright (c) 2009 Jiri Svoboda
3
 * All rights reserved.
4
 * All rights reserved.
4
 *
5
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * are met:
Line 39... Line 40...
39
#include <macros.h>
40
#include <macros.h>
40
#include "task.h"
41
#include "task.h"
41
#include "ns.h"
42
#include "ns.h"
42
 
43
 
43
#define TASK_HASH_TABLE_CHAINS  256
44
#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);
44
 
48
 
45
/* TODO:
49
/* TODO:
46
 *
50
 *
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
51
 * 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.
52
 * for, the NS can leak memory because of the zombie tasks.
59
 *
53
 *
60
 */
54
 */
61
 
55
 
62
/** Task hash table item. */
56
/** Task hash table item. */
63
typedef struct {
57
typedef struct {
64
    link_t link;
58
    link_t link;
65
    task_id_t id;    /**< Task ID. */
59
    task_id_t id;   /**< Task ID. */
66
    bool destroyed;
60
    bool finished;  /**< Task is done. */
-
 
61
    bool have_rval; /**< Task returned a value. */
-
 
62
    int retval; /**< The return value. */
67
} hashed_task_t;
63
} hashed_task_t;
68
 
64
 
69
/** Compute hash index into task hash table.
65
/** Compute hash index into task hash table.
70
 *
66
 *
71
 * @param key Pointer keys. However, only the first key (i.e. truncated task
67
 * @param key Pointer keys. However, only the first key (i.e. truncated task
Line 81... Line 77...
81
}
77
}
82
 
78
 
83
/** Compare a key with hashed item.
79
/** Compare a key with hashed item.
84
 *
80
 *
85
 * @param key  Array of keys.
81
 * @param key  Array of keys.
86
 * @param keys Must be lesser or equal to 2.
82
 * @param keys Must be less than or equal to 2.
87
 * @param item Pointer to a hash table item.
83
 * @param item Pointer to a hash table item.
88
 *
84
 *
89
 * @return Non-zero if the key matches the item, zero otherwise.
85
 * @return Non-zero if the key matches the item, zero otherwise.
90
 *
86
 *
91
 */
87
 */
Line 123... Line 119...
123
};
119
};
124
 
120
 
125
/** Task hash table structure. */
121
/** Task hash table structure. */
126
static hash_table_t task_hash_table;
122
static hash_table_t task_hash_table;
127
 
123
 
-
 
124
typedef struct {
-
 
125
    link_t link;
-
 
126
    ipcarg_t phash;    /**< Task ID. */
-
 
127
    task_id_t id;    /**< Task ID. */
-
 
128
} p2i_entry_t;
-
 
129
 
-
 
130
/** Compute hash index into task hash table.
-
 
131
 *
-
 
132
 * @param key Array of keys.
-
 
133
 * @return Hash index corresponding to key[0].
-
 
134
 *
-
 
135
 */
-
 
136
static hash_index_t p2i_hash(unsigned long *key)
-
 
137
{
-
 
138
    assert(key);
-
 
139
    return (*key % TASK_HASH_TABLE_CHAINS);
-
 
140
}
-
 
141
 
-
 
142
/** Compare a key with hashed item.
-
 
143
 *
-
 
144
 * @param key  Array of keys.
-
 
145
 * @param keys Must be less than or equal to 1.
-
 
146
 * @param item Pointer to a hash table item.
-
 
147
 *
-
 
148
 * @return Non-zero if the key matches the item, zero otherwise.
-
 
149
 *
-
 
150
 */
-
 
151
static int p2i_compare(unsigned long key[], hash_count_t keys, link_t *item)
-
 
152
{
-
 
153
    assert(key);
-
 
154
    assert(keys == 1);
-
 
155
    assert(item);
-
 
156
 
-
 
157
    p2i_entry_t *e = hash_table_get_instance(item, p2i_entry_t, link);
-
 
158
 
-
 
159
    return (key[0] == e->phash);
-
 
160
}
-
 
161
 
-
 
162
/** Perform actions after removal of item from the hash table.
-
 
163
 *
-
 
164
 * @param item Item that was removed from the hash table.
-
 
165
 *
-
 
166
 */
-
 
167
static void p2i_remove(link_t *item)
-
 
168
{
-
 
169
    assert(item);
-
 
170
    free(hash_table_get_instance(item, p2i_entry_t, link));
-
 
171
}
-
 
172
 
-
 
173
/** Operations for task hash table. */
-
 
174
static hash_table_operations_t p2i_ops = {
-
 
175
    .hash = p2i_hash,
-
 
176
    .compare = p2i_compare,
-
 
177
    .remove_callback = p2i_remove
-
 
178
};
-
 
179
 
-
 
180
/** Map phone hash to task ID */
-
 
181
static hash_table_t phone_to_id;
-
 
182
 
128
/** Pending task wait structure. */
183
/** Pending task wait structure. */
129
typedef struct {
184
typedef struct {
130
    link_t link;
185
    link_t link;
131
    task_id_t id;         /**< Task ID. */
186
    task_id_t id;         /**< Task ID. */
132
    ipc_callid_t callid;  /**< Call ID waiting for the connection */
187
    ipc_callid_t callid;  /**< Call ID waiting for the connection */
Line 139... Line 194...
139
    if (!hash_table_create(&task_hash_table, TASK_HASH_TABLE_CHAINS,
194
    if (!hash_table_create(&task_hash_table, TASK_HASH_TABLE_CHAINS,
140
        2, &task_hash_table_ops)) {
195
        2, &task_hash_table_ops)) {
141
        printf(NAME ": No memory available for tasks\n");
196
        printf(NAME ": No memory available for tasks\n");
142
        return ENOMEM;
197
        return ENOMEM;
143
    }
198
    }
144
   
199
 
145
    if (event_subscribe(EVENT_WAIT, 0) != EOK)
200
    if (!hash_table_create(&phone_to_id, P2I_HASH_TABLE_CHAINS,
-
 
201
        1, &p2i_ops)) {
146
        printf(NAME ": Error registering wait notifications\n");
202
        printf(NAME ": No memory available for tasks\n");
-
 
203
        return ENOMEM;
-
 
204
    }
147
   
205
   
148
    list_initialize(&pending_wait);
206
    list_initialize(&pending_wait);
149
   
207
   
150
    return EOK;
208
    return EOK;
151
}
209
}
152
 
210
 
153
/** Process pending wait requests */
211
/** Process pending wait requests */
154
void process_pending_wait(void)
212
void process_pending_wait(void)
155
{
213
{
156
    link_t *cur;
214
    link_t *cur;
-
 
215
    task_exit_t texit;
157
   
216
   
158
loop:
217
loop:
159
    for (cur = pending_wait.next; cur != &pending_wait; cur = cur->next) {
218
    for (cur = pending_wait.next; cur != &pending_wait; cur = cur->next) {
160
        pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link);
219
        pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link);
161
       
220
       
Line 167... Line 226...
167
        link_t *link = hash_table_find(&task_hash_table, keys);
226
        link_t *link = hash_table_find(&task_hash_table, keys);
168
        if (!link)
227
        if (!link)
169
            continue;
228
            continue;
170
       
229
       
171
        hashed_task_t *ht = hash_table_get_instance(link, hashed_task_t, link);
230
        hashed_task_t *ht = hash_table_get_instance(link, hashed_task_t, link);
172
        if (!ht->destroyed)
231
        if (!ht->finished)
173
            continue;
232
            continue;
174
       
233
       
175
        if (!(pr->callid & IPC_CALLID_NOTIFICATION))
234
        if (!(pr->callid & IPC_CALLID_NOTIFICATION)) {
-
 
235
            texit = ht->have_rval ? TASK_EXIT_NORMAL :
-
 
236
                TASK_EXIT_UNEXPECTED;
176
            ipc_answer_0(pr->callid, EOK);
237
            ipc_answer_2(pr->callid, EOK, texit,
-
 
238
                ht->retval);
177
       
239
        }
-
 
240
 
178
        hash_table_remove(&task_hash_table, keys, 2);
241
        hash_table_remove(&task_hash_table, keys, 2);
179
        list_remove(cur);
242
        list_remove(cur);
180
        free(pr);
243
        free(pr);
181
        goto loop;
244
        goto loop;
182
    }
245
    }
183
}
246
}
184
 
247
 
185
static void fail_pending_wait(task_id_t id, int rc)
-
 
186
{
-
 
187
    link_t *cur;
-
 
188
   
-
 
189
loop:
-
 
190
    for (cur = pending_wait.next; cur != &pending_wait; cur = cur->next) {
-
 
191
        pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link);
-
 
192
       
-
 
193
        if (pr->id == id) {
-
 
194
            if (!(pr->callid & IPC_CALLID_NOTIFICATION))
-
 
195
                ipc_answer_0(pr->callid, rc);
-
 
196
       
-
 
197
            list_remove(cur);
-
 
198
            free(pr);
-
 
199
            goto loop;
-
 
200
        }
-
 
201
    }
-
 
202
}
-
 
203
 
-
 
204
void wait_notification(wait_type_t et, task_id_t id)
-
 
205
{
-
 
206
    unsigned long keys[2] = {
-
 
207
        LOWER32(id),
-
 
208
        UPPER32(id)
-
 
209
    };
-
 
210
   
-
 
211
    link_t *link = hash_table_find(&task_hash_table, keys);
-
 
212
   
-
 
213
    if (link == NULL) {
-
 
214
        hashed_task_t *ht =
-
 
215
            (hashed_task_t *) malloc(sizeof(hashed_task_t));
-
 
216
        if (ht == NULL) {
-
 
217
            fail_pending_wait(id, ENOMEM);
-
 
218
            return;
-
 
219
        }
-
 
220
       
-
 
221
        link_initialize(&ht->link);
-
 
222
        ht->id = id;
-
 
223
        ht->destroyed = (et == TASK_CREATE) ? false : true;
-
 
224
        hash_table_insert(&task_hash_table, keys, &ht->link);
-
 
225
    } else {
-
 
226
        hashed_task_t *ht =
-
 
227
            hash_table_get_instance(link, hashed_task_t, link);
-
 
228
        ht->destroyed = (et == TASK_CREATE) ? false : true;
-
 
229
    }
-
 
230
}
-
 
231
 
-
 
232
void wait_for_task(task_id_t id, ipc_call_t *call, ipc_callid_t callid)
248
void wait_for_task(task_id_t id, ipc_call_t *call, ipc_callid_t callid)
233
{
249
{
234
    ipcarg_t retval;
250
    ipcarg_t retval;
-
 
251
    task_exit_t texit;
-
 
252
 
235
    unsigned long keys[2] = {
253
    unsigned long keys[2] = {
236
        LOWER32(id),
254
        LOWER32(id),
237
        UPPER32(id)
255
        UPPER32(id)
238
    };
256
    };
239
   
257
 
240
    link_t *link = hash_table_find(&task_hash_table, keys);
258
    link_t *link = hash_table_find(&task_hash_table, keys);
241
    hashed_task_t *ht = (link != NULL) ?
259
    hashed_task_t *ht = (link != NULL) ?
242
        hash_table_get_instance(link, hashed_task_t, link) : NULL;
260
        hash_table_get_instance(link, hashed_task_t, link) : NULL;
-
 
261
 
-
 
262
    if (ht == NULL) {
-
 
263
        /* No such task exists. */
-
 
264
        retval = ENOENT;
-
 
265
        goto out;
243
   
266
    }
-
 
267
 
244
    if ((ht == NULL) || (!ht->destroyed)) {
268
    if (!ht->finished) {
245
        /* Add to pending list */
269
        /* Add to pending list */
246
        pending_wait_t *pr =
270
        pending_wait_t *pr =
247
            (pending_wait_t *) malloc(sizeof(pending_wait_t));
271
            (pending_wait_t *) malloc(sizeof(pending_wait_t));
248
        if (!pr) {
272
        if (!pr) {
249
            retval = ENOMEM;
273
            retval = ENOMEM;
Line 258... Line 282...
258
   
282
   
259
    hash_table_remove(&task_hash_table, keys, 2);
283
    hash_table_remove(&task_hash_table, keys, 2);
260
    retval = EOK;
284
    retval = EOK;
261
   
285
   
262
out:
286
out:
263
    if (!(callid & IPC_CALLID_NOTIFICATION))
287
    if (!(callid & IPC_CALLID_NOTIFICATION)) {
-
 
288
        texit = ht->have_rval ? TASK_EXIT_NORMAL : TASK_EXIT_UNEXPECTED;
264
        ipc_answer_0(callid, retval);
289
        ipc_answer_2(callid, retval, texit, ht->retval);
-
 
290
    }
-
 
291
}
-
 
292
 
-
 
293
int ns_task_id_intro(ipc_call_t *call)
-
 
294
{
-
 
295
    task_id_t id;
-
 
296
    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
 
-
 
303
    keys[0] = call->in_phone_hash;
-
 
304
 
-
 
305
    link = hash_table_find(&phone_to_id, keys);
-
 
306
    if (link != NULL)
-
 
307
        return EEXISTS;
-
 
308
 
-
 
309
    e = (p2i_entry_t *) malloc(sizeof(p2i_entry_t));
-
 
310
    if (e == NULL)
-
 
311
        return ENOMEM;
-
 
312
 
-
 
313
    ht = (hashed_task_t *) malloc(sizeof(hashed_task_t));
-
 
314
    if (ht == NULL)
-
 
315
        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
 
-
 
326
    keys[0] = LOWER32(id);
-
 
327
    keys[1] = UPPER32(id);
-
 
328
 
-
 
329
    link_initialize(&ht->link);
-
 
330
    ht->id = id;
-
 
331
    ht->finished = false;
-
 
332
    ht->have_rval = false;
-
 
333
    ht->retval = -1;
-
 
334
    hash_table_insert(&task_hash_table, keys, &ht->link);
-
 
335
 
-
 
336
    return EOK;
-
 
337
}
-
 
338
 
-
 
339
int ns_task_retval(ipc_call_t *call)
-
 
340
{
-
 
341
    task_id_t id;
-
 
342
    unsigned long keys[2];
-
 
343
    int rc;
-
 
344
 
-
 
345
    rc = get_id_by_phone(call->in_phone_hash, &id);
-
 
346
    if (rc != EOK)
-
 
347
        return rc;
-
 
348
 
-
 
349
    keys[0] = LOWER32(id);
-
 
350
    keys[1] = UPPER32(id);
-
 
351
   
-
 
352
    link_t *link = hash_table_find(&task_hash_table, keys);
-
 
353
    hashed_task_t *ht = (link != NULL) ?
-
 
354
        hash_table_get_instance(link, hashed_task_t, link) : NULL;
-
 
355
   
-
 
356
    if ((ht == NULL) || ht->finished)
-
 
357
        return EINVAL;
-
 
358
 
-
 
359
    ht->finished = true;
-
 
360
    ht->have_rval = true;
-
 
361
    ht->retval = IPC_GET_ARG1(*call);
-
 
362
 
-
 
363
    return EOK;
-
 
364
}
-
 
365
 
-
 
366
int ns_task_disconnect(ipc_call_t *call)
-
 
367
{
-
 
368
    unsigned long keys[2];
-
 
369
    task_id_t id;
-
 
370
    int rc;
-
 
371
 
-
 
372
    rc = get_id_by_phone(call->in_phone_hash, &id);
-
 
373
    if (rc != EOK)
-
 
374
        return rc;
-
 
375
 
-
 
376
    /* Delete from phone-to-id map. */
-
 
377
    keys[0] = call->in_phone_hash;
-
 
378
    hash_table_remove(&phone_to_id, keys, 1);
-
 
379
 
-
 
380
    /* Mark task as finished. */
-
 
381
    keys[0] = LOWER32(id);
-
 
382
    keys[1] = UPPER32(id);
-
 
383
 
-
 
384
    link_t *link = hash_table_find(&task_hash_table, keys);
-
 
385
    hashed_task_t *ht =
-
 
386
        hash_table_get_instance(link, hashed_task_t, link);
-
 
387
    if (ht == NULL)
-
 
388
        return EOK;
-
 
389
 
-
 
390
    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
 
-
 
409
    return EOK;
265
}
410
}
266
 
411
 
267
/**
412
/**
268
 * @}
413
 * @}
269
 */
414
 */