Subversion Repositories HelenOS

Rev

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

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