Subversion Repositories HelenOS-historic

Rev

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