Subversion Repositories HelenOS

Rev

Rev 4509 | Rev 4618 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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