Subversion Repositories HelenOS

Rev

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