Subversion Repositories HelenOS-historic

Rev

Rev 1614 | Rev 1653 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1351 palkovsky 1
/*
2
 * Copyright (C) 2006 Ondrej Palkovsky
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
 
1392 palkovsky 29
/**
30
 * Asynchronous library
31
 *
32
 * The aim of this library is facilitating writing programs utilizing
33
 * the asynchronous nature of Helenos IPC, yet using a normal way
34
 * of programming.
35
 *
36
 * You should be able to write very simple multithreaded programs,
37
 * the async framework will automatically take care of most synchronization
38
 * problems.
39
 *
40
 * Default semantics:
41
 * - send() - send asynchronously. If the kernel refuses to send more
42
 *            messages, [ try to get responses from kernel, if nothing
43
 *            found, might try synchronous ]
44
 *
45
 * Example of use:
46
 *
47
 * 1) Multithreaded client application
48
 *  create_thread(thread1);
49
 *  create_thread(thread2);
50
 *  ...
51
 *  
52
 *  thread1() {
53
 *        conn = ipc_connect_me_to();
54
 *        c1 = send(conn);
55
 *        c2 = send(conn);
56
 *        wait_for(c1);
57
 *        wait_for(c2);
58
 *  }
59
 *
60
 *
61
 * 2) Multithreaded server application
62
 * main() {
1407 palkovsky 63
 *      async_manager();
1392 palkovsky 64
 * }
65
 *
66
 *
1407 palkovsky 67
 * client_connection(icallid, *icall) {
68
 *       if (want_refuse) {
69
 *           ipc_answer_fast(icallid, ELIMIT, 0, 0);
70
 *           return;
71
 *       }
72
 *       ipc_answer_fast(icallid, 0, 0, 0);
1392 palkovsky 73
 *
1407 palkovsky 74
 *       callid = async_get_call(&call);
75
 *       handle(callid, call);
76
 *       ipc_answer_fast(callid, 1,2,3);
77
 *
78
 *       callid = async_get_call(&call);
1392 palkovsky 79
 *       ....
80
 * }
1404 palkovsky 81
 *
1405 decky 82
 * TODO: Detaching/joining dead psthreads?
1392 palkovsky 83
 */
84
#include <futex.h>
85
#include <async.h>
86
#include <psthread.h>
87
#include <stdio.h>
88
#include <libadt/hash_table.h>
89
#include <libadt/list.h>
90
#include <ipc/ipc.h>
91
#include <assert.h>
92
#include <errno.h>
1441 palkovsky 93
#include <time.h>
94
#include <arch/barrier.h>
1392 palkovsky 95
 
1463 palkovsky 96
atomic_t async_futex = FUTEX_INITIALIZER;
1392 palkovsky 97
static hash_table_t conn_hash_table;
1441 palkovsky 98
static LIST_INITIALIZE(timeout_list);
1392 palkovsky 99
 
100
typedef struct {
1500 palkovsky 101
    struct timeval expires;      /**< Expiration time for waiting thread */
102
    int inlist;             /**< If true, this struct is in timeout list */
103
    link_t link;
104
 
1427 palkovsky 105
    pstid_t ptid;                /**< Thread waiting for this message */
106
    int active;                  /**< If this thread is currently active */
1500 palkovsky 107
    int timedout;                /**< If true, we timed out */
108
} awaiter_t;
109
 
110
typedef struct {
111
    awaiter_t wdata;
112
 
1427 palkovsky 113
    int done;                    /**< If reply was received */
114
    ipc_call_t *dataptr;         /**< Pointer where the answer data
1500 palkovsky 115
                      *   is stored */
1427 palkovsky 116
    ipcarg_t retval;
117
} amsg_t;
118
 
119
typedef struct {
1392 palkovsky 120
    link_t link;
121
    ipc_callid_t callid;
122
    ipc_call_t call;
123
} msg_t;
124
 
125
typedef struct {
1500 palkovsky 126
    awaiter_t wdata;
127
 
128
    link_t link;             /**< Hash table link */
129
    ipcarg_t in_phone_hash;  /**< Incoming phone hash. */
130
    link_t msg_queue;        /**< Messages that should be delivered to this thread */
1392 palkovsky 131
    /* Structures for connection opening packet */
132
    ipc_callid_t callid;
133
    ipc_call_t call;
1648 palkovsky 134
    ipc_callid_t close_callid; /* Identification of closing packet */
1407 palkovsky 135
    void (*cthread)(ipc_callid_t,ipc_call_t *);
1392 palkovsky 136
} connection_t;
137
 
1610 palkovsky 138
/** Identifier of incoming connection handled by current thread */
1392 palkovsky 139
__thread connection_t *PS_connection;
1610 palkovsky 140
/** If true, it is forbidden to use async_req functions and
141
 *  all preemption is disabled */
142
__thread int in_interrupt_handler;
1392 palkovsky 143
 
1490 palkovsky 144
static void default_client_connection(ipc_callid_t callid, ipc_call_t *call);
1596 palkovsky 145
static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call);
1490 palkovsky 146
static async_client_conn_t client_connection = default_client_connection;
1596 palkovsky 147
static async_client_conn_t interrupt_received = default_interrupt_received;
1490 palkovsky 148
 
1441 palkovsky 149
/** Add microseconds to give timeval */
150
static void tv_add(struct timeval *tv, suseconds_t usecs)
151
{
152
    tv->tv_sec += usecs / 1000000;
153
    tv->tv_usec += usecs % 1000000;
154
    if (tv->tv_usec > 1000000) {
155
        tv->tv_sec++;
156
        tv->tv_usec -= 1000000;
157
    }
158
}
159
 
160
/** Subtract 2 timevals, return microseconds difference */
161
static suseconds_t tv_sub(struct timeval *tv1, struct timeval *tv2)
162
{
163
    suseconds_t result;
164
 
165
    result = tv1->tv_usec - tv2->tv_usec;
166
    result += (tv1->tv_sec - tv2->tv_sec) * 1000000;
167
 
168
    return result;
169
}
170
 
171
/** Compare timeval
172
 *
173
 * @return 1 if tv1 > tv2, otherwise 0
174
 */
175
static int tv_gt(struct timeval *tv1, struct timeval *tv2)
176
{
177
    if (tv1->tv_sec > tv2->tv_sec)
178
        return 1;
179
    if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec > tv2->tv_usec)
180
        return 1;
181
    return 0;
182
}
1466 palkovsky 183
static int tv_gteq(struct timeval *tv1, struct timeval *tv2)
184
{
185
    if (tv1->tv_sec > tv2->tv_sec)
186
        return 1;
187
    if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec >= tv2->tv_usec)
188
        return 1;
189
    return 0;
190
}
1441 palkovsky 191
 
1392 palkovsky 192
/* Hash table functions */
1404 palkovsky 193
#define CONN_HASH_TABLE_CHAINS  32
1392 palkovsky 194
 
195
static hash_index_t conn_hash(unsigned long *key)
1351 palkovsky 196
{
1392 palkovsky 197
    assert(key);
1404 palkovsky 198
    return ((*key) >> 4) % CONN_HASH_TABLE_CHAINS;
1351 palkovsky 199
}
200
 
1392 palkovsky 201
static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
1351 palkovsky 202
{
1392 palkovsky 203
    connection_t *hs;
204
 
205
    hs = hash_table_get_instance(item, connection_t, link);
206
 
207
    return key[0] == hs->in_phone_hash;
1351 palkovsky 208
}
209
 
1392 palkovsky 210
static void conn_remove(link_t *item)
1351 palkovsky 211
{
1392 palkovsky 212
    free(hash_table_get_instance(item, connection_t, link));
1351 palkovsky 213
}
214
 
1392 palkovsky 215
 
216
/** Operations for NS hash table. */
217
static hash_table_operations_t conn_hash_table_ops = {
218
    .hash = conn_hash,
219
    .compare = conn_compare,
220
    .remove_callback = conn_remove
221
};
222
 
1500 palkovsky 223
/** Insert sort timeout msg into timeouts list
224
 *
225
 */
226
static void insert_timeout(awaiter_t *wd)
227
{
228
    link_t *tmp;
229
    awaiter_t *cur;
230
 
231
    wd->timedout = 0;
1610 palkovsky 232
    wd->inlist = 1;
1500 palkovsky 233
 
234
    tmp = timeout_list.next;
235
    while (tmp != &timeout_list) {
236
        cur = list_get_instance(tmp, awaiter_t, link);
237
        if (tv_gteq(&cur->expires, &wd->expires))
238
            break;
239
        tmp = tmp->next;
240
    }
241
    list_append(&wd->link, tmp);
242
}
243
 
1427 palkovsky 244
/*************************************************/
245
 
1392 palkovsky 246
/** Try to route a call to an appropriate connection thread
247
 *
248
 */
249
static int route_call(ipc_callid_t callid, ipc_call_t *call)
1351 palkovsky 250
{
1392 palkovsky 251
    connection_t *conn;
252
    msg_t *msg;
253
    link_t *hlp;
254
    unsigned long key;
255
 
1427 palkovsky 256
    futex_down(&async_futex);
1392 palkovsky 257
 
258
    key = call->in_phone_hash;
259
    hlp = hash_table_find(&conn_hash_table, &key);
260
    if (!hlp) {
1427 palkovsky 261
        futex_up(&async_futex);
1392 palkovsky 262
        return 0;
1351 palkovsky 263
    }
1392 palkovsky 264
    conn = hash_table_get_instance(hlp, connection_t, link);
1351 palkovsky 265
 
1392 palkovsky 266
    msg = malloc(sizeof(*msg));
267
    msg->callid = callid;
268
    msg->call = *call;
269
    list_append(&msg->link, &conn->msg_queue);
1648 palkovsky 270
 
271
    if (IPC_GET_METHOD(*call) == IPC_M_PHONE_HUNGUP)
272
        conn->close_callid = callid;
1392 palkovsky 273
 
1500 palkovsky 274
    /* If the call is waiting for event, run it */
275
    if (!conn->wdata.active) {
276
        /* If in timeout list, remove it */
277
        if (conn->wdata.inlist) {
278
            conn->wdata.inlist = 0;
279
            list_remove(&conn->wdata.link);
280
        }
281
        conn->wdata.active = 1;
282
        psthread_add_ready(conn->wdata.ptid);
1392 palkovsky 283
    }
1351 palkovsky 284
 
1427 palkovsky 285
    futex_up(&async_futex);
1392 palkovsky 286
 
287
    return 1;
288
}
289
 
1404 palkovsky 290
/** Return new incoming message for current(thread-local) connection */
1500 palkovsky 291
ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
1392 palkovsky 292
{
293
    msg_t *msg;
294
    ipc_callid_t callid;
1536 palkovsky 295
    connection_t *conn;
1392 palkovsky 296
 
1466 palkovsky 297
    assert(PS_connection);
1536 palkovsky 298
    /* GCC 4.1.0 coughs on PS_connection-> dereference,
299
     * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
300
     *           I would never expect to find so many errors in
301
     *           compiler *($&$(*&$
302
     */
303
    conn = PS_connection;
1466 palkovsky 304
 
1427 palkovsky 305
    futex_down(&async_futex);
1392 palkovsky 306
 
1500 palkovsky 307
    if (usecs) {
1536 palkovsky 308
        gettimeofday(&conn->wdata.expires, NULL);
309
        tv_add(&conn->wdata.expires, usecs);
1500 palkovsky 310
    } else {
1536 palkovsky 311
        conn->wdata.inlist = 0;
1500 palkovsky 312
    }
1392 palkovsky 313
    /* If nothing in queue, wait until something appears */
1536 palkovsky 314
    while (list_empty(&conn->msg_queue)) {
1610 palkovsky 315
        if (usecs)
1536 palkovsky 316
            insert_timeout(&conn->wdata);
1610 palkovsky 317
 
1536 palkovsky 318
        conn->wdata.active = 0;
1392 palkovsky 319
        psthread_schedule_next_adv(PS_TO_MANAGER);
1500 palkovsky 320
        /* Futex is up after getting back from async_manager
321
         * get it again */
322
        futex_down(&async_futex);
1536 palkovsky 323
        if (usecs && conn->wdata.timedout && \
324
            list_empty(&conn->msg_queue)) {
1500 palkovsky 325
            /* If we timed out-> exit */
326
            futex_up(&async_futex);
327
            return 0;
328
        }
1351 palkovsky 329
    }
330
 
1536 palkovsky 331
    msg = list_get_instance(conn->msg_queue.next, msg_t, link);
1392 palkovsky 332
    list_remove(&msg->link);
333
    callid = msg->callid;
334
    *call = msg->call;
335
    free(msg);
336
 
1427 palkovsky 337
    futex_up(&async_futex);
1392 palkovsky 338
    return callid;
1351 palkovsky 339
}
340
 
1404 palkovsky 341
/** Thread function that gets created on new connection
342
 *
343
 * This function is defined as a weak symbol - to be redefined in
344
 * user code.
345
 */
1490 palkovsky 346
static void default_client_connection(ipc_callid_t callid, ipc_call_t *call)
1392 palkovsky 347
{
1404 palkovsky 348
    ipc_answer_fast(callid, ENOENT, 0, 0);
1392 palkovsky 349
}
1596 palkovsky 350
static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
1452 palkovsky 351
{
352
}
353
 
1404 palkovsky 354
/** Wrapper for client connection thread
355
 *
356
 * When new connection arrives, thread with this function is created.
357
 * It calls client_connection and does final cleanup.
358
 *
359
 * @parameter arg Connection structure pointer
360
 */
1392 palkovsky 361
static int connection_thread(void  *arg)
1351 palkovsky 362
{
1404 palkovsky 363
    unsigned long key;
364
    msg_t *msg;
1648 palkovsky 365
    int close_answered = 0;
1404 palkovsky 366
 
1392 palkovsky 367
    /* Setup thread local connection pointer */
368
    PS_connection = (connection_t *)arg;
1470 palkovsky 369
    PS_connection->cthread(PS_connection->callid, &PS_connection->call);
1404 palkovsky 370
    /* Remove myself from connection hash table */
1427 palkovsky 371
    futex_down(&async_futex);
1470 palkovsky 372
    key = PS_connection->in_phone_hash;
1404 palkovsky 373
    hash_table_remove(&conn_hash_table, &key, 1);
1427 palkovsky 374
    futex_up(&async_futex);
1404 palkovsky 375
    /* Answer all remaining messages with ehangup */
1470 palkovsky 376
    while (!list_empty(&PS_connection->msg_queue)) {
377
        msg = list_get_instance(PS_connection->msg_queue.next, msg_t, link);
1404 palkovsky 378
        list_remove(&msg->link);
1648 palkovsky 379
        if (msg->callid == PS_connection->close_callid)
380
            close_answered = 1;
1404 palkovsky 381
        ipc_answer_fast(msg->callid, EHANGUP, 0, 0);
382
        free(msg);
383
    }
1648 palkovsky 384
    if (PS_connection->close_callid)
385
        ipc_answer_fast(PS_connection->close_callid, 0, 0, 0);
1351 palkovsky 386
}
1392 palkovsky 387
 
388
/** Create new thread for a new connection
389
 *
390
 * Creates new thread for connection, fills in connection
391
 * structures and inserts it into the hash table, so that
392
 * later we can easily do routing of messages to particular
393
 * threads.
1407 palkovsky 394
 *
1452 palkovsky 395
 * @param in_phone_hash Identification of the incoming connection
1407 palkovsky 396
 * @param callid Callid of the IPC_M_CONNECT_ME_TO packet
397
 * @param call Call data of the opening packet
398
 * @param cthread Thread function that should be called upon
399
 *                opening the connection
400
 * @return New thread id
1392 palkovsky 401
 */
1452 palkovsky 402
pstid_t async_new_connection(ipcarg_t in_phone_hash,ipc_callid_t callid,
403
                 ipc_call_t *call,
1407 palkovsky 404
                 void (*cthread)(ipc_callid_t,ipc_call_t *))
1392 palkovsky 405
{
406
    pstid_t ptid;
407
    connection_t *conn;
408
    unsigned long key;
409
 
410
    conn = malloc(sizeof(*conn));
411
    if (!conn) {
412
        ipc_answer_fast(callid, ENOMEM, 0, 0);
1407 palkovsky 413
        return NULL;
1392 palkovsky 414
    }
1452 palkovsky 415
    conn->in_phone_hash = in_phone_hash;
1392 palkovsky 416
    list_initialize(&conn->msg_queue);
417
    conn->callid = callid;
1648 palkovsky 418
    conn->close_callid = 0;
1453 palkovsky 419
    if (call)
420
        conn->call = *call;
1500 palkovsky 421
    conn->wdata.active = 1; /* We will activate it asap */
1407 palkovsky 422
    conn->cthread = cthread;
1500 palkovsky 423
 
424
    conn->wdata.ptid = psthread_create(connection_thread, conn);
425
    if (!conn->wdata.ptid) {
1392 palkovsky 426
        free(conn);
427
        ipc_answer_fast(callid, ENOMEM, 0, 0);
1407 palkovsky 428
        return NULL;
1392 palkovsky 429
    }
1500 palkovsky 430
    /* Add connection to hash table */
1392 palkovsky 431
    key = conn->in_phone_hash;
1427 palkovsky 432
    futex_down(&async_futex);
1392 palkovsky 433
    hash_table_insert(&conn_hash_table, &key, &conn->link);
1427 palkovsky 434
    futex_up(&async_futex);
1392 palkovsky 435
 
1500 palkovsky 436
    psthread_add_ready(conn->wdata.ptid);
1407 palkovsky 437
 
1500 palkovsky 438
    return conn->wdata.ptid;
1392 palkovsky 439
}
440
 
1427 palkovsky 441
/** Handle call that was received */
1392 palkovsky 442
static void handle_call(ipc_callid_t callid, ipc_call_t *call)
443
{
1452 palkovsky 444
    /* Unrouted call - do some default behaviour */
1392 palkovsky 445
    switch (IPC_GET_METHOD(*call)) {
446
    case IPC_M_INTERRUPT:
1610 palkovsky 447
        in_interrupt_handler = 1;
1596 palkovsky 448
        (*interrupt_received)(callid,call);
1610 palkovsky 449
        in_interrupt_handler = 0;
1452 palkovsky 450
        return;
1392 palkovsky 451
    case IPC_M_CONNECT_ME_TO:
452
        /* Open new connection with thread etc. */
1452 palkovsky 453
        async_new_connection(IPC_GET_ARG3(*call), callid, call, client_connection);
454
        return;
1392 palkovsky 455
    }
1452 palkovsky 456
 
457
    /* Try to route call through connection tables */
458
    if (route_call(callid, call))
459
        return;
460
 
461
    /* Unknown call from unknown phone - hang it up */
462
    ipc_answer_fast(callid, EHANGUP, 0, 0);
1392 palkovsky 463
}
464
 
1536 palkovsky 465
/** Fire all timeouts that expired
466
 *
467
 */
1441 palkovsky 468
static void handle_expired_timeouts(void)
469
{
470
    struct timeval tv;
1500 palkovsky 471
    awaiter_t *waiter;
1441 palkovsky 472
    link_t *cur;
473
 
474
    gettimeofday(&tv,NULL);
475
    futex_down(&async_futex);
476
 
477
    cur = timeout_list.next;
478
    while (cur != &timeout_list) {
1500 palkovsky 479
        waiter = list_get_instance(cur,awaiter_t,link);
480
        if (tv_gt(&waiter->expires, &tv))
1441 palkovsky 481
            break;
482
        cur = cur->next;
1500 palkovsky 483
        list_remove(&waiter->link);
484
        waiter->inlist = 0;
485
        waiter->timedout = 1;
1441 palkovsky 486
        /* Redundant condition? The thread should not
487
         * be active when it gets here.
488
         */
1500 palkovsky 489
        if (!waiter->active) {
490
            waiter->active = 1;
491
            psthread_add_ready(waiter->ptid);
1441 palkovsky 492
        }
493
    }
494
 
495
    futex_up(&async_futex);
496
}
497
 
1392 palkovsky 498
/** Endless loop dispatching incoming calls and answers */
1610 palkovsky 499
static int async_manager_worker(void)
1392 palkovsky 500
{
501
    ipc_call_t call;
502
    ipc_callid_t callid;
1435 palkovsky 503
    int timeout;
1500 palkovsky 504
    awaiter_t *waiter;
1441 palkovsky 505
    struct timeval tv;
1392 palkovsky 506
 
507
    while (1) {
508
        if (psthread_schedule_next_adv(PS_FROM_MANAGER)) {
1427 palkovsky 509
            futex_up(&async_futex); /* async_futex is always held
1392 palkovsky 510
                        * when entering manager thread
511
                        */
512
            continue;
513
        }
1441 palkovsky 514
        futex_down(&async_futex);
515
        if (!list_empty(&timeout_list)) {
1500 palkovsky 516
            waiter = list_get_instance(timeout_list.next,awaiter_t,link);
1441 palkovsky 517
            gettimeofday(&tv,NULL);
1500 palkovsky 518
            if (tv_gteq(&tv, &waiter->expires)) {
1536 palkovsky 519
                futex_up(&async_futex);
1441 palkovsky 520
                handle_expired_timeouts();
521
                continue;
522
            } else
1500 palkovsky 523
                timeout = tv_sub(&waiter->expires, &tv);
1441 palkovsky 524
        } else
1435 palkovsky 525
            timeout = SYNCH_NO_TIMEOUT;
1441 palkovsky 526
        futex_up(&async_futex);
527
 
1503 jermar 528
        callid = ipc_wait_cycle(&call, timeout, SYNCH_FLAGS_NONE);
1392 palkovsky 529
 
1435 palkovsky 530
        if (!callid) {
1441 palkovsky 531
            handle_expired_timeouts();
1435 palkovsky 532
            continue;
533
        }
534
 
1610 palkovsky 535
        if (callid & IPC_CALLID_ANSWERED) {
1392 palkovsky 536
            continue;
1610 palkovsky 537
        }
1427 palkovsky 538
 
1392 palkovsky 539
        handle_call(callid, &call);
540
    }
541
}
542
 
1404 palkovsky 543
/** Function to start async_manager as a standalone thread
544
 *
545
 * When more kernel threads are used, one async manager should
546
 * exist per thread. The particular implementation may change,
547
 * currently one async_manager is started automatically per kernel
548
 * thread except main thread.
549
 */
1392 palkovsky 550
static int async_manager_thread(void *arg)
551
{
1427 palkovsky 552
    futex_up(&async_futex); /* async_futex is always locked when entering
1392 palkovsky 553
                * manager */
1610 palkovsky 554
    async_manager_worker();
1392 palkovsky 555
}
556
 
557
/** Add one manager to manager list */
558
void async_create_manager(void)
559
{
560
    pstid_t ptid;
561
 
562
    ptid = psthread_create(async_manager_thread, NULL);
563
    psthread_add_manager(ptid);
564
}
565
 
566
/** Remove one manager from manager list */
567
void async_destroy_manager(void)
568
{
569
    psthread_remove_manager();
570
}
571
 
572
/** Initialize internal structures needed for async manager */
573
int _async_init(void)
574
{
1404 palkovsky 575
    if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1, &conn_hash_table_ops)) {
1392 palkovsky 576
        printf("%s: cannot create hash table\n", "async");
577
        return ENOMEM;
578
    }
579
 
580
}
1427 palkovsky 581
 
582
/** IPC handler for messages in async framework
583
 *
584
 * Notify thread that is waiting for this message, that it arrived
585
 */
586
static void reply_received(void *private, int retval,
587
               ipc_call_t *data)
588
{
589
    amsg_t *msg = (amsg_t *) private;
590
 
591
    msg->retval = retval;
592
 
593
    futex_down(&async_futex);
594
    /* Copy data after futex_down, just in case the
595
     * call was detached
596
     */
597
    if (msg->dataptr)
598
        *msg->dataptr = *data;
1435 palkovsky 599
 
1441 palkovsky 600
    write_barrier();
601
    /* Remove message from timeout list */
1500 palkovsky 602
    if (msg->wdata.inlist)
603
        list_remove(&msg->wdata.link);
1427 palkovsky 604
    msg->done = 1;
1500 palkovsky 605
    if (! msg->wdata.active) {
606
        msg->wdata.active = 1;
607
        psthread_add_ready(msg->wdata.ptid);
1427 palkovsky 608
    }
609
    futex_up(&async_futex);
610
}
611
 
612
/** Send message and return id of the sent message
613
 *
614
 * The return value can be used as input for async_wait() to wait
615
 * for completion.
616
 */
617
aid_t async_send_2(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2,
618
           ipc_call_t *dataptr)
619
{
620
    amsg_t *msg;
621
 
1610 palkovsky 622
    if (in_interrupt_handler) {
623
        printf("Cannot send asynchronou request in interrupt handler.\n");
624
        _exit(1);
625
    }
626
 
1427 palkovsky 627
    msg = malloc(sizeof(*msg));
628
    msg->done = 0;
629
    msg->dataptr = dataptr;
1500 palkovsky 630
 
631
    msg->wdata.active = 1; /* We may sleep in next method, but it
632
                * will use it's own mechanism */
1518 palkovsky 633
    ipc_call_async_2(phoneid,method,arg1,arg2,msg,reply_received,1);
1427 palkovsky 634
 
635
    return (aid_t) msg;
636
}
637
 
1547 palkovsky 638
/** Send message and return id of the sent message
639
 *
640
 * The return value can be used as input for async_wait() to wait
641
 * for completion.
642
 */
643
aid_t async_send_3(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2,
644
           ipcarg_t arg3, ipc_call_t *dataptr)
645
{
646
    amsg_t *msg;
647
 
1610 palkovsky 648
    if (in_interrupt_handler) {
649
        printf("Cannot send asynchronou request in interrupt handler.\n");
650
        _exit(1);
651
    }
652
 
1547 palkovsky 653
    msg = malloc(sizeof(*msg));
654
    msg->done = 0;
655
    msg->dataptr = dataptr;
656
 
657
    msg->wdata.active = 1; /* We may sleep in next method, but it
658
                * will use it's own mechanism */
659
    ipc_call_async_3(phoneid,method,arg1,arg2,arg3, msg,reply_received,1);
660
 
661
    return (aid_t) msg;
662
}
663
 
1427 palkovsky 664
/** Wait for a message sent by async framework
665
 *
666
 * @param amsgid Message ID to wait for
667
 * @param retval Pointer to variable where will be stored retval
668
 *               of the answered message. If NULL, it is ignored.
669
 *
670
 */
671
void async_wait_for(aid_t amsgid, ipcarg_t *retval)
672
{
673
    amsg_t *msg = (amsg_t *) amsgid;
674
    connection_t *conn;
675
 
676
    futex_down(&async_futex);
677
    if (msg->done) {
678
        futex_up(&async_futex);
679
        goto done;
680
    }
681
 
1500 palkovsky 682
    msg->wdata.ptid = psthread_get_id();
683
    msg->wdata.active = 0;
684
    msg->wdata.inlist = 0;
1427 palkovsky 685
    /* Leave locked async_futex when entering this function */
686
    psthread_schedule_next_adv(PS_TO_MANAGER);
687
    /* futex is up automatically after psthread_schedule_next...*/
688
done:
689
    if (retval)
690
        *retval = msg->retval;
691
    free(msg);
692
}
1435 palkovsky 693
 
1441 palkovsky 694
/** Wait for a message sent by async framework with timeout
695
 *
696
 * @param amsgid Message ID to wait for
697
 * @param retval Pointer to variable where will be stored retval
698
 *               of the answered message. If NULL, it is ignored.
699
 * @param timeout Timeout in usecs
700
 * @return 0 on success, ETIMEOUT if timeout expired
701
 *
702
 */
703
int async_wait_timeout(aid_t amsgid, ipcarg_t *retval, suseconds_t timeout)
704
{
705
    amsg_t *msg = (amsg_t *) amsgid;
706
    connection_t *conn;
1435 palkovsky 707
 
1532 palkovsky 708
    /* TODO: Let it go through the event read at least once */
709
    if (timeout < 0)
710
        return ETIMEOUT;
711
 
1441 palkovsky 712
    futex_down(&async_futex);
713
    if (msg->done) {
714
        futex_up(&async_futex);
715
        goto done;
716
    }
1435 palkovsky 717
 
1500 palkovsky 718
    gettimeofday(&msg->wdata.expires, NULL);
719
    tv_add(&msg->wdata.expires, timeout);
1435 palkovsky 720
 
1500 palkovsky 721
    msg->wdata.ptid = psthread_get_id();
722
    msg->wdata.active = 0;
723
    insert_timeout(&msg->wdata);
724
 
1441 palkovsky 725
    /* Leave locked async_futex when entering this function */
726
    psthread_schedule_next_adv(PS_TO_MANAGER);
727
    /* futex is up automatically after psthread_schedule_next...*/
1435 palkovsky 728
 
1441 palkovsky 729
    if (!msg->done)
730
        return ETIMEOUT;
731
 
732
done:
733
    if (retval)
734
        *retval = msg->retval;
735
    free(msg);
736
 
737
    return 0;
738
}
739
 
1452 palkovsky 740
/** Wait specified time, but in the meantime handle incoming events
741
 *
742
 * @param timeout Time in microseconds to wait
743
 */
744
void async_usleep(suseconds_t timeout)
745
{
746
    amsg_t *msg;
747
 
1610 palkovsky 748
    if (in_interrupt_handler) {
749
        printf("Cannot call async_usleep in interrupt handler.\n");
750
        _exit(1);
751
    }
752
 
1452 palkovsky 753
    msg = malloc(sizeof(*msg));
754
    if (!msg)
755
        return;
756
 
1500 palkovsky 757
    msg->wdata.ptid = psthread_get_id();
758
    msg->wdata.active = 0;
1452 palkovsky 759
 
1500 palkovsky 760
    gettimeofday(&msg->wdata.expires, NULL);
761
    tv_add(&msg->wdata.expires, timeout);
1452 palkovsky 762
 
763
    futex_down(&async_futex);
1500 palkovsky 764
    insert_timeout(&msg->wdata);
1452 palkovsky 765
    /* Leave locked async_futex when entering this function */
766
    psthread_schedule_next_adv(PS_TO_MANAGER);
767
    /* futex is up automatically after psthread_schedule_next...*/
768
    free(msg);
769
}
1490 palkovsky 770
 
771
/** Set function that is called, IPC_M_CONNECT_ME_TO is received
772
 *
773
 * @param conn Function that will form new psthread.
774
 */
775
void async_set_client_connection(async_client_conn_t conn)
776
{
777
    client_connection = conn;
778
}
1596 palkovsky 779
void async_set_interrupt_received(async_client_conn_t conn)
780
{
781
    interrupt_received = conn;
782
}
1610 palkovsky 783
 
784
/* Primitive functions for simple communication */
785
void async_msg_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
786
         ipcarg_t arg2, ipcarg_t arg3)
787
{
788
    ipc_call_async_3(phoneid, method, arg1, arg2, arg3, NULL, NULL, !in_interrupt_handler);
789
}
790
 
791
void async_msg_2(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2)
792
{
793
    ipc_call_async_2(phoneid, method, arg1, arg2, NULL, NULL, !in_interrupt_handler);
794
}