Subversion Repositories HelenOS

Rev

Rev 4381 | Rev 4475 | 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
4324 decky 33
 */
1351 palkovsky 34
 
1392 palkovsky 35
/**
36
 * Asynchronous library
37
 *
4324 decky 38
 * The aim of this library is to provide a facility for writing programs which
39
 * utilize the asynchronous nature of HelenOS IPC, yet using a normal way of
40
 * programming.
1392 palkovsky 41
 *
2484 jermar 42
 * You should be able to write very simple multithreaded programs, the async
43
 * framework will automatically take care of most synchronization problems.
1392 palkovsky 44
 *
45
 * Default semantics:
4324 decky 46
 * - async_send_*(): Send asynchronously. If the kernel refuses to send
47
 *                   more messages, [ try to get responses from kernel, if
48
 *                   nothing found, might try synchronous ]
1392 palkovsky 49
 *
2484 jermar 50
 * Example of use (pseudo C):
4324 decky 51
 *
1392 palkovsky 52
 * 1) Multithreaded client application
2484 jermar 53
 *
4324 decky 54
 *   fibril_create(fibril1, ...);
55
 *   fibril_create(fibril2, ...);
56
 *   ...
1392 palkovsky 57
 *
4324 decky 58
 *   int fibril1(void *arg)
59
 *   {
60
 *     conn = ipc_connect_me_to();
61
 *     c1 = async_send(conn);
62
 *     c2 = async_send(conn);
63
 *     async_wait_for(c1);
64
 *     async_wait_for(c2);
65
 *     ...
66
 *   }
1392 palkovsky 67
 *
4324 decky 68
 *
1392 palkovsky 69
 * 2) Multithreaded server application
70
 *
4324 decky 71
 *   main()
72
 *   {
73
 *     async_manager();
74
 *   }
1392 palkovsky 75
 *
4324 decky 76
 *   my_client_connection(icallid, *icall)
77
 *   {
78
 *     if (want_refuse) {
79
 *       ipc_answer_0(icallid, ELIMIT);
80
 *       return;
81
 *     }
82
 *     ipc_answer_0(icallid, EOK);
1407 palkovsky 83
 *
4324 decky 84
 *     callid = async_get_call(&call);
85
 *     handle_call(callid, call);
86
 *     ipc_answer_2(callid, 1, 2, 3);
1404 palkovsky 87
 *
4324 decky 88
 *     callid = async_get_call(&call);
89
 *     ...
90
 *   }
91
 *
1392 palkovsky 92
 */
2484 jermar 93
 
1392 palkovsky 94
#include <futex.h>
95
#include <async.h>
2482 jermar 96
#include <fibril.h>
1392 palkovsky 97
#include <stdio.h>
98
#include <libadt/hash_table.h>
99
#include <libadt/list.h>
100
#include <ipc/ipc.h>
101
#include <assert.h>
102
#include <errno.h>
2486 jermar 103
#include <sys/time.h>
1441 palkovsky 104
#include <arch/barrier.h>
2621 jermar 105
#include <bool.h>
1392 palkovsky 106
 
1463 palkovsky 107
atomic_t async_futex = FUTEX_INITIALIZER;
1392 palkovsky 108
 
2488 jermar 109
/** Structures of this type represent a waiting fibril. */
1392 palkovsky 110
typedef struct {
2488 jermar 111
	/** Expiration time. */
4324 decky 112
	struct timeval expires;
113
 
2482 jermar 114
	/** If true, this struct is in the timeout list. */
4324 decky 115
	bool inlist;
116
 
2488 jermar 117
	/** Timeout list link. */
1500 palkovsky 118
	link_t link;
4324 decky 119
 
2490 jermar 120
	/** Identification of and link to the waiting fibril. */
2482 jermar 121
	fid_t fid;
4324 decky 122
 
2488 jermar 123
	/** If true, this fibril is currently active. */
4324 decky 124
	bool active;
125
 
2488 jermar 126
	/** If true, we have timed out. */
4324 decky 127
	bool timedout;
1500 palkovsky 128
} awaiter_t;
129
 
130
typedef struct {
131
	awaiter_t wdata;
2488 jermar 132
 
133
	/** If reply was received. */
4324 decky 134
	bool done;
135
 
2488 jermar 136
	/** Pointer to where the answer data is stored. */
4324 decky 137
	ipc_call_t *dataptr;
138
 
1427 palkovsky 139
	ipcarg_t retval;
140
} amsg_t;
141
 
2490 jermar 142
/**
143
 * Structures of this type are used to group information about a call and a
144
 * message queue link.
145
 */
1427 palkovsky 146
typedef struct {
1392 palkovsky 147
	link_t link;
148
	ipc_callid_t callid;
149
	ipc_call_t call;
150
} msg_t;
151
 
152
typedef struct {
1500 palkovsky 153
	awaiter_t wdata;
4324 decky 154
 
2488 jermar 155
	/** Hash table link. */
156
	link_t link;
4324 decky 157
 
2488 jermar 158
	/** Incoming phone hash. */
4324 decky 159
	ipcarg_t in_phone_hash;
160
 
2488 jermar 161
	/** Messages that should be delivered to this fibril. */
4324 decky 162
	link_t msg_queue;
163
 
2488 jermar 164
	/** Identification of the opening call. */
1392 palkovsky 165
	ipc_callid_t callid;
2488 jermar 166
	/** Call data of the opening call. */
1392 palkovsky 167
	ipc_call_t call;
4324 decky 168
 
2488 jermar 169
	/** Identification of the closing call. */
170
	ipc_callid_t close_callid;
4324 decky 171
 
2488 jermar 172
	/** Fibril function that will be used to handle the connection. */
2482 jermar 173
	void (*cfibril)(ipc_callid_t, ipc_call_t *);
1392 palkovsky 174
} connection_t;
175
 
2482 jermar 176
/** Identifier of the incoming connection handled by the current fibril. */
177
__thread connection_t *FIBRIL_connection;
2488 jermar 178
 
1490 palkovsky 179
static void default_client_connection(ipc_callid_t callid, ipc_call_t *call);
1596 palkovsky 180
static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call);
2490 jermar 181
 
182
/**
183
 * Pointer to a fibril function that will be used to handle connections.
184
 */
1490 palkovsky 185
static async_client_conn_t client_connection = default_client_connection;
4324 decky 186
 
2490 jermar 187
/**
188
 * Pointer to a fibril function that will be used to handle interrupt
189
 * notifications.
190
 */
1596 palkovsky 191
static async_client_conn_t interrupt_received = default_interrupt_received;
1490 palkovsky 192
 
2621 jermar 193
 
4324 decky 194
static hash_table_t conn_hash_table;
195
static LIST_INITIALIZE(timeout_list);
1392 palkovsky 196
 
4324 decky 197
 
198
#define CONN_HASH_TABLE_CHAINS  32
199
 
2488 jermar 200
/** Compute hash into the connection hash table based on the source phone hash.
201
 *
4324 decky 202
 * @param key Pointer to source phone hash.
2488 jermar 203
 *
4324 decky 204
 * @return Index into the connection hash table.
205
 *
2488 jermar 206
 */
1392 palkovsky 207
static hash_index_t conn_hash(unsigned long *key)
1351 palkovsky 208
{
1392 palkovsky 209
	assert(key);
4324 decky 210
	return (((*key) >> 4) % CONN_HASH_TABLE_CHAINS);
1351 palkovsky 211
}
212
 
2488 jermar 213
/** Compare hash table item with a key.
214
 *
4324 decky 215
 * @param key  Array containing the source phone hash as the only item.
216
 * @param keys Expected 1 but ignored.
217
 * @param item Connection hash table item.
2488 jermar 218
 *
4324 decky 219
 * @return True on match, false otherwise.
220
 *
2488 jermar 221
 */
1392 palkovsky 222
static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
1351 palkovsky 223
{
4324 decky 224
	connection_t *hs = hash_table_get_instance(item, connection_t, link);
225
	return (key[0] == hs->in_phone_hash);
1351 palkovsky 226
}
227
 
2488 jermar 228
/** Connection hash table removal callback function.
229
 *
230
 * This function is called whenever a connection is removed from the connection
231
 * hash table.
232
 *
4324 decky 233
 * @param item Connection hash table item being removed.
234
 *
2488 jermar 235
 */
1392 palkovsky 236
static void conn_remove(link_t *item)
1351 palkovsky 237
{
1392 palkovsky 238
	free(hash_table_get_instance(item, connection_t, link));
1351 palkovsky 239
}
240
 
1392 palkovsky 241
 
2488 jermar 242
/** Operations for the connection hash table. */
1392 palkovsky 243
static hash_table_operations_t conn_hash_table_ops = {
244
	.hash = conn_hash,
245
	.compare = conn_compare,
246
	.remove_callback = conn_remove
247
};
248
 
2488 jermar 249
/** Sort in current fibril's timeout request.
1500 palkovsky 250
 *
4324 decky 251
 * @param wd Wait data of the current fibril.
252
 *
1500 palkovsky 253
 */
254
static void insert_timeout(awaiter_t *wd)
255
{
4324 decky 256
	wd->timedout = false;
257
	wd->inlist = true;
258
 
259
	link_t *tmp = timeout_list.next;
1500 palkovsky 260
	while (tmp != &timeout_list) {
4324 decky 261
		awaiter_t *cur = list_get_instance(tmp, awaiter_t, link);
262
 
1500 palkovsky 263
		if (tv_gteq(&cur->expires, &wd->expires))
264
			break;
4324 decky 265
 
1500 palkovsky 266
		tmp = tmp->next;
267
	}
4324 decky 268
 
1500 palkovsky 269
	list_append(&wd->link, tmp);
270
}
271
 
2488 jermar 272
/** Try to route a call to an appropriate connection fibril.
1392 palkovsky 273
 *
2490 jermar 274
 * If the proper connection fibril is found, a message with the call is added to
275
 * its message queue. If the fibril was not active, it is activated and all
276
 * timeouts are unregistered.
277
 *
4324 decky 278
 * @param callid Hash of the incoming call.
279
 * @param call   Data of the incoming call.
2490 jermar 280
 *
4324 decky 281
 * @return False if the call doesn't match any connection.
282
 *         True if the call was passed to the respective connection fibril.
283
 *
1392 palkovsky 284
 */
4324 decky 285
static bool route_call(ipc_callid_t callid, ipc_call_t *call)
1351 palkovsky 286
{
1427 palkovsky 287
	futex_down(&async_futex);
4324 decky 288
 
289
	unsigned long key = call->in_phone_hash;
290
	link_t *hlp = hash_table_find(&conn_hash_table, &key);
291
 
1392 palkovsky 292
	if (!hlp) {
1427 palkovsky 293
		futex_up(&async_futex);
4324 decky 294
		return false;
1351 palkovsky 295
	}
4324 decky 296
 
297
	connection_t *conn = hash_table_get_instance(hlp, connection_t, link);
298
 
299
	msg_t *msg = malloc(sizeof(*msg));
300
	if (!msg) {
301
		futex_up(&async_futex);
302
		return false;
303
	}
304
 
1392 palkovsky 305
	msg->callid = callid;
306
	msg->call = *call;
307
	list_append(&msg->link, &conn->msg_queue);
4324 decky 308
 
1648 palkovsky 309
	if (IPC_GET_METHOD(*call) == IPC_M_PHONE_HUNGUP)
310
		conn->close_callid = callid;
1392 palkovsky 311
 
2490 jermar 312
	/* If the connection fibril is waiting for an event, activate it */
1500 palkovsky 313
	if (!conn->wdata.active) {
4324 decky 314
 
1500 palkovsky 315
		/* If in timeout list, remove it */
316
		if (conn->wdata.inlist) {
4324 decky 317
			conn->wdata.inlist = false;
1500 palkovsky 318
			list_remove(&conn->wdata.link);
319
		}
4324 decky 320
 
321
		conn->wdata.active = true;
2482 jermar 322
		fibril_add_ready(conn->wdata.fid);
1392 palkovsky 323
	}
4324 decky 324
 
1427 palkovsky 325
	futex_up(&async_futex);
4324 decky 326
	return true;
327
}
1392 palkovsky 328
 
4324 decky 329
/** Notification fibril.
330
 *
331
 * When a notification arrives, a fibril with this implementing function is
332
 * created. It calls interrupt_received() and does the final cleanup.
333
 *
334
 * @param arg Message structure pointer.
335
 *
336
 * @return Always zero.
337
 *
338
 */
339
static int notification_fibril(void *arg)
340
{
341
	msg_t *msg = (msg_t *) arg;
342
	interrupt_received(msg->callid, &msg->call);
343
 
344
	free(msg);
345
	return 0;
1392 palkovsky 346
}
347
 
4324 decky 348
/** Process interrupt notification.
349
 *
350
 * A new fibril is created which would process the notification.
351
 *
352
 * @param callid Hash of the incoming call.
353
 * @param call   Data of the incoming call.
354
 *
355
 * @return False if an error occured.
356
 *         True if the call was passed to the notification fibril.
357
 *
358
 */
359
static bool process_notification(ipc_callid_t callid, ipc_call_t *call)
360
{
361
	futex_down(&async_futex);
362
 
363
	msg_t *msg = malloc(sizeof(*msg));
364
	if (!msg) {
365
		futex_up(&async_futex);
366
		return false;
367
	}
368
 
369
	msg->callid = callid;
370
	msg->call = *call;
371
 
372
	fid_t fid = fibril_create(notification_fibril, msg);
373
	fibril_add_ready(fid);
374
 
375
	futex_up(&async_futex);
376
	return true;
377
}
378
 
2488 jermar 379
/** Return new incoming message for the current (fibril-local) connection.
380
 *
4324 decky 381
 * @param call  Storage where the incoming call data will be stored.
382
 * @param usecs Timeout in microseconds. Zero denotes no timeout.
2488 jermar 383
 *
4324 decky 384
 * @return If no timeout was specified, then a hash of the
385
 *         incoming call is returned. If a timeout is specified,
386
 *         then a hash of the incoming call is returned unless
387
 *         the timeout expires prior to receiving a message. In
388
 *         that case zero is returned.
389
 *
2488 jermar 390
 */
1500 palkovsky 391
ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
1392 palkovsky 392
{
4324 decky 393
	assert(FIBRIL_connection);
1392 palkovsky 394
 
4324 decky 395
	/* Why doing this?
396
	 * GCC 4.1.0 coughs on FIBRIL_connection-> dereference.
1536 palkovsky 397
	 * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
4324 decky 398
	 *           I would never expect to find so many errors in
399
	 *           a compiler.
1536 palkovsky 400
	 */
4324 decky 401
	connection_t *conn = FIBRIL_connection;
402
 
1427 palkovsky 403
	futex_down(&async_futex);
4324 decky 404
 
1500 palkovsky 405
	if (usecs) {
1536 palkovsky 406
		gettimeofday(&conn->wdata.expires, NULL);
407
		tv_add(&conn->wdata.expires, usecs);
4324 decky 408
	} else
409
		conn->wdata.inlist = false;
410
 
2488 jermar 411
	/* If nothing in queue, wait until something arrives */
1536 palkovsky 412
	while (list_empty(&conn->msg_queue)) {
1610 palkovsky 413
		if (usecs)
1536 palkovsky 414
			insert_timeout(&conn->wdata);
4324 decky 415
 
416
		conn->wdata.active = false;
417
 
2492 jermar 418
		/*
419
		 * Note: the current fibril will be rescheduled either due to a
420
		 * timeout or due to an arriving message destined to it. In the
421
		 * former case, handle_expired_timeouts() and, in the latter
422
		 * case, route_call() will perform the wakeup.
423
		 */
2568 jermar 424
		fibril_switch(FIBRIL_TO_MANAGER);
4324 decky 425
 
2488 jermar 426
		/*
4324 decky 427
		 * Futex is up after getting back from async_manager.
428
		 * Get it again.
2492 jermar 429
		 */
1500 palkovsky 430
		futex_down(&async_futex);
4324 decky 431
		if ((usecs) && (conn->wdata.timedout)
432
		    && (list_empty(&conn->msg_queue))) {
2488 jermar 433
			/* If we timed out -> exit */
1500 palkovsky 434
			futex_up(&async_futex);
435
			return 0;
436
		}
1351 palkovsky 437
	}
438
 
4324 decky 439
	msg_t *msg = list_get_instance(conn->msg_queue.next, msg_t, link);
1392 palkovsky 440
	list_remove(&msg->link);
4324 decky 441
 
442
	ipc_callid_t callid = msg->callid;
1392 palkovsky 443
	*call = msg->call;
444
	free(msg);
445
 
1427 palkovsky 446
	futex_up(&async_futex);
1392 palkovsky 447
	return callid;
1351 palkovsky 448
}
449
 
2490 jermar 450
/** Default fibril function that gets called to handle new connection.
1404 palkovsky 451
 *
2488 jermar 452
 * This function is defined as a weak symbol - to be redefined in user code.
2490 jermar 453
 *
4324 decky 454
 * @param callid Hash of the incoming call.
455
 * @param call   Data of the incoming call.
456
 *
1404 palkovsky 457
 */
1490 palkovsky 458
static void default_client_connection(ipc_callid_t callid, ipc_call_t *call)
1392 palkovsky 459
{
2619 jermar 460
	ipc_answer_0(callid, ENOENT);
1392 palkovsky 461
}
2490 jermar 462
 
463
/** Default fibril function that gets called to handle interrupt notifications.
464
 *
4324 decky 465
 * This function is defined as a weak symbol - to be redefined in user code.
466
 *
467
 * @param callid Hash of the incoming call.
468
 * @param call   Data of the incoming call.
469
 *
2490 jermar 470
 */
1596 palkovsky 471
static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
1452 palkovsky 472
{
473
}
474
 
2485 jermar 475
/** Wrapper for client connection fibril.
1404 palkovsky 476
 *
2490 jermar 477
 * When a new connection arrives, a fibril with this implementing function is
2485 jermar 478
 * created. It calls client_connection() and does the final cleanup.
1404 palkovsky 479
 *
4324 decky 480
 * @param arg Connection structure pointer.
2485 jermar 481
 *
4324 decky 482
 * @return Always zero.
483
 *
1404 palkovsky 484
 */
4324 decky 485
static int connection_fibril(void *arg)
1351 palkovsky 486
{
4324 decky 487
	/*
488
	 * Setup fibril-local connection pointer and call client_connection().
489
	 *
490
	 */
2482 jermar 491
	FIBRIL_connection = (connection_t *) arg;
492
	FIBRIL_connection->cfibril(FIBRIL_connection->callid,
493
	    &FIBRIL_connection->call);
1719 decky 494
 
2490 jermar 495
	/* Remove myself from the connection hash table */
1427 palkovsky 496
	futex_down(&async_futex);
4324 decky 497
	unsigned long key = FIBRIL_connection->in_phone_hash;
1404 palkovsky 498
	hash_table_remove(&conn_hash_table, &key, 1);
1427 palkovsky 499
	futex_up(&async_futex);
1719 decky 500
 
2490 jermar 501
	/* Answer all remaining messages with EHANGUP */
2482 jermar 502
	while (!list_empty(&FIBRIL_connection->msg_queue)) {
4324 decky 503
		msg_t *msg
504
		    = list_get_instance(FIBRIL_connection->msg_queue.next, msg_t, link);
505
 
1404 palkovsky 506
		list_remove(&msg->link);
2619 jermar 507
		ipc_answer_0(msg->callid, EHANGUP);
1404 palkovsky 508
		free(msg);
509
	}
4324 decky 510
 
2482 jermar 511
	if (FIBRIL_connection->close_callid)
2619 jermar 512
		ipc_answer_0(FIBRIL_connection->close_callid, EOK);
1719 decky 513
 
514
	return 0;
1351 palkovsky 515
}
1392 palkovsky 516
 
2485 jermar 517
/** Create a new fibril for a new connection.
1392 palkovsky 518
 *
4324 decky 519
 * Create new fibril for connection, fill in connection structures and inserts
2485 jermar 520
 * it into the hash table, so that later we can easily do routing of messages to
521
 * particular fibrils.
1407 palkovsky 522
 *
4324 decky 523
 * @param in_phone_hash Identification of the incoming connection.
524
 * @param callid        Hash of the opening IPC_M_CONNECT_ME_TO call.
525
 *                      If callid is zero, the connection was opened by
526
 *                      accepting the IPC_M_CONNECT_TO_ME call and this function
527
 *                      is called directly by the server.
528
 * @param call          Call data of the opening call.
529
 * @param cfibril       Fibril function that should be called upon opening the
530
 *                      connection.
2490 jermar 531
 *
4324 decky 532
 * @return New fibril id or NULL on failure.
533
 *
1392 palkovsky 534
 */
2482 jermar 535
fid_t async_new_connection(ipcarg_t in_phone_hash, ipc_callid_t callid,
536
    ipc_call_t *call, void (*cfibril)(ipc_callid_t, ipc_call_t *))
1392 palkovsky 537
{
4324 decky 538
	connection_t *conn = malloc(sizeof(*conn));
1392 palkovsky 539
	if (!conn) {
2534 jermar 540
		if (callid)
2619 jermar 541
			ipc_answer_0(callid, ENOMEM);
1407 palkovsky 542
		return NULL;
1392 palkovsky 543
	}
4324 decky 544
 
1452 palkovsky 545
	conn->in_phone_hash = in_phone_hash;
1392 palkovsky 546
	list_initialize(&conn->msg_queue);
547
	conn->callid = callid;
4324 decky 548
	conn->close_callid = false;
549
 
1453 palkovsky 550
	if (call)
551
		conn->call = *call;
4324 decky 552
 
553
	/* We will activate the fibril ASAP */
554
	conn->wdata.active = true;
2482 jermar 555
	conn->cfibril = cfibril;
4324 decky 556
	conn->wdata.fid = fibril_create(connection_fibril, conn);
3760 decky 557
 
2482 jermar 558
	if (!conn->wdata.fid) {
1392 palkovsky 559
		free(conn);
2534 jermar 560
		if (callid)
2619 jermar 561
			ipc_answer_0(callid, ENOMEM);
1407 palkovsky 562
		return NULL;
1392 palkovsky 563
	}
3760 decky 564
 
2490 jermar 565
	/* Add connection to the connection hash table */
4324 decky 566
	ipcarg_t key = conn->in_phone_hash;
567
 
1427 palkovsky 568
	futex_down(&async_futex);
1392 palkovsky 569
	hash_table_insert(&conn_hash_table, &key, &conn->link);
1427 palkovsky 570
	futex_up(&async_futex);
3760 decky 571
 
2482 jermar 572
	fibril_add_ready(conn->wdata.fid);
3760 decky 573
 
2482 jermar 574
	return conn->wdata.fid;
1392 palkovsky 575
}
576
 
2490 jermar 577
/** Handle a call that was received.
578
 *
579
 * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
580
 * Otherwise the call is routed to its connection fibril.
581
 *
4324 decky 582
 * @param callid Hash of the incoming call.
583
 * @param call   Data of the incoming call.
3760 decky 584
 *
2490 jermar 585
 */
1392 palkovsky 586
static void handle_call(ipc_callid_t callid, ipc_call_t *call)
587
{
1452 palkovsky 588
	/* Unrouted call - do some default behaviour */
1694 palkovsky 589
	if ((callid & IPC_CALLID_NOTIFICATION)) {
4324 decky 590
		process_notification(callid, call);
1452 palkovsky 591
		return;
3760 decky 592
	}
593
 
1694 palkovsky 594
	switch (IPC_GET_METHOD(*call)) {
4407 jermar 595
	case IPC_M_CONNECT_ME:
1392 palkovsky 596
	case IPC_M_CONNECT_ME_TO:
2485 jermar 597
		/* Open new connection with fibril etc. */
2635 cejka 598
		async_new_connection(IPC_GET_ARG5(*call), callid, call,
2482 jermar 599
		    client_connection);
1452 palkovsky 600
		return;
1392 palkovsky 601
	}
3760 decky 602
 
2490 jermar 603
	/* Try to route the call through the connection hash table */
1452 palkovsky 604
	if (route_call(callid, call))
605
		return;
3760 decky 606
 
1452 palkovsky 607
	/* Unknown call from unknown phone - hang it up */
2619 jermar 608
	ipc_answer_0(callid, EHANGUP);
1392 palkovsky 609
}
610
 
2485 jermar 611
/** Fire all timeouts that expired. */
1441 palkovsky 612
static void handle_expired_timeouts(void)
613
{
614
	struct timeval tv;
2490 jermar 615
	gettimeofday(&tv, NULL);
4324 decky 616
 
1441 palkovsky 617
	futex_down(&async_futex);
4324 decky 618
 
619
	link_t *cur = timeout_list.next;
1441 palkovsky 620
	while (cur != &timeout_list) {
4324 decky 621
		awaiter_t *waiter = list_get_instance(cur, awaiter_t, link);
622
 
1500 palkovsky 623
		if (tv_gt(&waiter->expires, &tv))
1441 palkovsky 624
			break;
4324 decky 625
 
1441 palkovsky 626
		cur = cur->next;
4324 decky 627
 
1500 palkovsky 628
		list_remove(&waiter->link);
4324 decky 629
		waiter->inlist = false;
630
		waiter->timedout = true;
631
 
2490 jermar 632
		/*
4324 decky 633
		 * Redundant condition?
634
		 * The fibril should not be active when it gets here.
1441 palkovsky 635
		 */
1500 palkovsky 636
		if (!waiter->active) {
4324 decky 637
			waiter->active = true;
2482 jermar 638
			fibril_add_ready(waiter->fid);
1441 palkovsky 639
		}
640
	}
4324 decky 641
 
1441 palkovsky 642
	futex_up(&async_futex);
643
}
644
 
2490 jermar 645
/** Endless loop dispatching incoming calls and answers.
646
 *
4324 decky 647
 * @return Never returns.
648
 *
2490 jermar 649
 */
1610 palkovsky 650
static int async_manager_worker(void)
1392 palkovsky 651
{
4324 decky 652
	while (true) {
2568 jermar 653
		if (fibril_switch(FIBRIL_FROM_MANAGER)) {
1719 decky 654
			futex_up(&async_futex); 
2490 jermar 655
			/*
656
			 * async_futex is always held when entering a manager
657
			 * fibril.
1719 decky 658
			 */
1392 palkovsky 659
			continue;
660
		}
4324 decky 661
 
1441 palkovsky 662
		futex_down(&async_futex);
4324 decky 663
 
664
		suseconds_t timeout;
1441 palkovsky 665
		if (!list_empty(&timeout_list)) {
4324 decky 666
			awaiter_t *waiter
667
			    = list_get_instance(timeout_list.next, awaiter_t, link);
668
 
669
			struct timeval tv;
2482 jermar 670
			gettimeofday(&tv, NULL);
4324 decky 671
 
1500 palkovsky 672
			if (tv_gteq(&tv, &waiter->expires)) {
1536 palkovsky 673
				futex_up(&async_futex);
1441 palkovsky 674
				handle_expired_timeouts();
675
				continue;
676
			} else
1500 palkovsky 677
				timeout = tv_sub(&waiter->expires, &tv);
1441 palkovsky 678
		} else
1435 palkovsky 679
			timeout = SYNCH_NO_TIMEOUT;
4324 decky 680
 
1441 palkovsky 681
		futex_up(&async_futex);
4324 decky 682
 
683
		ipc_call_t call;
684
		ipc_callid_t callid
685
		    = ipc_wait_cycle(&call, timeout, SYNCH_FLAGS_NONE);
686
 
1435 palkovsky 687
		if (!callid) {
1441 palkovsky 688
			handle_expired_timeouts();
1435 palkovsky 689
			continue;
690
		}
4324 decky 691
 
692
		if (callid & IPC_CALLID_ANSWERED)
1392 palkovsky 693
			continue;
4324 decky 694
 
1392 palkovsky 695
		handle_call(callid, &call);
696
	}
1719 decky 697
 
698
	return 0;
1392 palkovsky 699
}
700
 
2490 jermar 701
/** Function to start async_manager as a standalone fibril.
4324 decky 702
 *
2490 jermar 703
 * When more kernel threads are used, one async manager should exist per thread.
704
 *
4324 decky 705
 * @param arg Unused.
706
 * @return Never returns.
2490 jermar 707
 *
1404 palkovsky 708
 */
2484 jermar 709
static int async_manager_fibril(void *arg)
1392 palkovsky 710
{
1719 decky 711
	futex_up(&async_futex);
4324 decky 712
 
2490 jermar 713
	/*
714
	 * async_futex is always locked when entering manager
715
	 */
1610 palkovsky 716
	async_manager_worker();
1719 decky 717
 
718
	return 0;
1392 palkovsky 719
}
720
 
2490 jermar 721
/** Add one manager to manager list. */
1392 palkovsky 722
void async_create_manager(void)
723
{
4324 decky 724
	fid_t fid = fibril_create(async_manager_fibril, NULL);
2482 jermar 725
	fibril_add_manager(fid);
1392 palkovsky 726
}
727
 
728
/** Remove one manager from manager list */
729
void async_destroy_manager(void)
730
{
2482 jermar 731
	fibril_remove_manager();
1392 palkovsky 732
}
733
 
2490 jermar 734
/** Initialize the async framework.
735
 *
4324 decky 736
 * @return Zero on success or an error code.
2490 jermar 737
 */
1392 palkovsky 738
int _async_init(void)
739
{
2482 jermar 740
	if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1,
741
	    &conn_hash_table_ops)) {
1392 palkovsky 742
		printf("%s: cannot create hash table\n", "async");
743
		return ENOMEM;
744
	}
745
 
1719 decky 746
	return 0;
1392 palkovsky 747
}
1427 palkovsky 748
 
2490 jermar 749
/** Reply received callback.
1427 palkovsky 750
 *
2490 jermar 751
 * This function is called whenever a reply for an asynchronous message sent out
752
 * by the asynchronous framework is received.
753
 *
754
 * Notify the fibril which is waiting for this message that it has arrived.
755
 *
4324 decky 756
 * @param arg    Pointer to the asynchronous message record.
757
 * @param retval Value returned in the answer.
758
 * @param data   Call data of the answer.
1427 palkovsky 759
 */
4324 decky 760
static void reply_received(void *arg, int retval, ipc_call_t *data)
1427 palkovsky 761
{
4324 decky 762
	amsg_t *msg = (amsg_t *) arg;
1427 palkovsky 763
	msg->retval = retval;
4324 decky 764
 
1427 palkovsky 765
	futex_down(&async_futex);
4324 decky 766
 
2490 jermar 767
	/* Copy data after futex_down, just in case the call was detached */
1427 palkovsky 768
	if (msg->dataptr)
4324 decky 769
		*msg->dataptr = *data;
770
 
1441 palkovsky 771
	write_barrier();
4324 decky 772
 
1441 palkovsky 773
	/* Remove message from timeout list */
1500 palkovsky 774
	if (msg->wdata.inlist)
775
		list_remove(&msg->wdata.link);
4324 decky 776
 
777
	msg->done = true;
2490 jermar 778
	if (!msg->wdata.active) {
4324 decky 779
		msg->wdata.active = true;
2482 jermar 780
		fibril_add_ready(msg->wdata.fid);
1427 palkovsky 781
	}
4324 decky 782
 
1427 palkovsky 783
	futex_up(&async_futex);
784
}
785
 
2490 jermar 786
/** Send message and return id of the sent message.
1427 palkovsky 787
 *
2490 jermar 788
 * The return value can be used as input for async_wait() to wait for
789
 * completion.
790
 *
4324 decky 791
 * @param phoneid Handle of the phone that will be used for the send.
792
 * @param method  Service-defined method.
793
 * @param arg1    Service-defined payload argument.
794
 * @param arg2    Service-defined payload argument.
795
 * @param arg3    Service-defined payload argument.
796
 * @param arg4    Service-defined payload argument.
797
 * @param dataptr If non-NULL, storage where the reply data will be
798
 *                stored.
2490 jermar 799
 *
4324 decky 800
 * @return Hash of the sent message or 0 on error.
801
 *
1427 palkovsky 802
 */
2621 jermar 803
aid_t async_send_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
804
    ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipc_call_t *dataptr)
1427 palkovsky 805
{
4324 decky 806
	amsg_t *msg = malloc(sizeof(*msg));
3760 decky 807
 
4324 decky 808
	if (!msg)
809
		return 0;
810
 
811
	msg->done = false;
1427 palkovsky 812
	msg->dataptr = dataptr;
3760 decky 813
 
4381 jermar 814
	msg->wdata.inlist = false;
2490 jermar 815
	/* We may sleep in the next method, but it will use its own mechanism */
4324 decky 816
	msg->wdata.active = true;
817
 
2621 jermar 818
	ipc_call_async_4(phoneid, method, arg1, arg2, arg3, arg4, msg,
4324 decky 819
	    reply_received, true);
3760 decky 820
 
1427 palkovsky 821
	return (aid_t) msg;
822
}
823
 
1547 palkovsky 824
/** Send message and return id of the sent message
825
 *
2490 jermar 826
 * The return value can be used as input for async_wait() to wait for
827
 * completion.
828
 *
4324 decky 829
 * @param phoneid Handle of the phone that will be used for the send.
830
 * @param method  Service-defined method.
831
 * @param arg1    Service-defined payload argument.
832
 * @param arg2    Service-defined payload argument.
833
 * @param arg3    Service-defined payload argument.
834
 * @param arg4    Service-defined payload argument.
835
 * @param arg5    Service-defined payload argument.
836
 * @param dataptr If non-NULL, storage where the reply data will be
837
 *                stored.
2490 jermar 838
 *
4324 decky 839
 * @return Hash of the sent message or 0 on error.
840
 *
1547 palkovsky 841
 */
2621 jermar 842
aid_t async_send_slow(int phoneid, ipcarg_t method, ipcarg_t arg1,
843
    ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5,
844
    ipc_call_t *dataptr)
1547 palkovsky 845
{
4324 decky 846
	amsg_t *msg = malloc(sizeof(*msg));
3760 decky 847
 
4324 decky 848
	if (!msg)
849
		return 0;
850
 
851
	msg->done = false;
1547 palkovsky 852
	msg->dataptr = dataptr;
3760 decky 853
 
4381 jermar 854
	msg->wdata.inlist = false;
2490 jermar 855
	/* We may sleep in next method, but it will use its own mechanism */
4324 decky 856
	msg->wdata.active = true;
3760 decky 857
 
2621 jermar 858
	ipc_call_async_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, msg,
4324 decky 859
	    reply_received, true);
3760 decky 860
 
1547 palkovsky 861
	return (aid_t) msg;
862
}
863
 
2490 jermar 864
/** Wait for a message sent by the async framework.
1427 palkovsky 865
 *
4324 decky 866
 * @param amsgid Hash of the message to wait for.
867
 * @param retval Pointer to storage where the retval of the answer will
868
 *               be stored.
869
 *
1427 palkovsky 870
 */
871
void async_wait_for(aid_t amsgid, ipcarg_t *retval)
872
{
873
	amsg_t *msg = (amsg_t *) amsgid;
4324 decky 874
 
1427 palkovsky 875
	futex_down(&async_futex);
876
	if (msg->done) {
877
		futex_up(&async_futex);
878
		goto done;
879
	}
4324 decky 880
 
2482 jermar 881
	msg->wdata.fid = fibril_get_id();
4324 decky 882
	msg->wdata.active = false;
883
	msg->wdata.inlist = false;
884
 
2490 jermar 885
	/* Leave the async_futex locked when entering this function */
2568 jermar 886
	fibril_switch(FIBRIL_TO_MANAGER);
4324 decky 887
 
888
	/* Futex is up automatically after fibril_switch */
889
 
1427 palkovsky 890
done:
891
	if (retval)
892
		*retval = msg->retval;
4324 decky 893
 
1427 palkovsky 894
	free(msg);
895
}
1435 palkovsky 896
 
2490 jermar 897
/** Wait for a message sent by the async framework, timeout variant.
1441 palkovsky 898
 *
4324 decky 899
 * @param amsgid  Hash of the message to wait for.
900
 * @param retval  Pointer to storage where the retval of the answer will
901
 *                be stored.
902
 * @param timeout Timeout in microseconds.
1441 palkovsky 903
 *
4324 decky 904
 * @return Zero on success, ETIMEOUT if the timeout has expired.
905
 *
1441 palkovsky 906
 */
907
int async_wait_timeout(aid_t amsgid, ipcarg_t *retval, suseconds_t timeout)
908
{
909
	amsg_t *msg = (amsg_t *) amsgid;
4324 decky 910
 
1532 palkovsky 911
	/* TODO: Let it go through the event read at least once */
912
	if (timeout < 0)
913
		return ETIMEOUT;
4324 decky 914
 
1441 palkovsky 915
	futex_down(&async_futex);
916
	if (msg->done) {
917
		futex_up(&async_futex);
918
		goto done;
919
	}
4324 decky 920
 
1500 palkovsky 921
	gettimeofday(&msg->wdata.expires, NULL);
922
	tv_add(&msg->wdata.expires, timeout);
4324 decky 923
 
2482 jermar 924
	msg->wdata.fid = fibril_get_id();
4324 decky 925
	msg->wdata.active = false;
1500 palkovsky 926
	insert_timeout(&msg->wdata);
4324 decky 927
 
2490 jermar 928
	/* Leave the async_futex locked when entering this function */
2568 jermar 929
	fibril_switch(FIBRIL_TO_MANAGER);
4324 decky 930
 
931
	/* Futex is up automatically after fibril_switch */
932
 
1441 palkovsky 933
	if (!msg->done)
934
		return ETIMEOUT;
4324 decky 935
 
1441 palkovsky 936
done:
937
	if (retval)
938
		*retval = msg->retval;
4324 decky 939
 
1441 palkovsky 940
	free(msg);
4324 decky 941
 
1441 palkovsky 942
	return 0;
943
}
944
 
2490 jermar 945
/** Wait for specified time.
1452 palkovsky 946
 *
2490 jermar 947
 * The current fibril is suspended but the thread continues to execute.
948
 *
4324 decky 949
 * @param timeout Duration of the wait in microseconds.
950
 *
1452 palkovsky 951
 */
952
void async_usleep(suseconds_t timeout)
953
{
4324 decky 954
	amsg_t *msg = malloc(sizeof(*msg));
1452 palkovsky 955
 
956
	if (!msg)
957
		return;
3760 decky 958
 
2482 jermar 959
	msg->wdata.fid = fibril_get_id();
4324 decky 960
	msg->wdata.active = false;
3760 decky 961
 
1500 palkovsky 962
	gettimeofday(&msg->wdata.expires, NULL);
963
	tv_add(&msg->wdata.expires, timeout);
3760 decky 964
 
1452 palkovsky 965
	futex_down(&async_futex);
4324 decky 966
 
1500 palkovsky 967
	insert_timeout(&msg->wdata);
4324 decky 968
 
2490 jermar 969
	/* Leave the async_futex locked when entering this function */
2568 jermar 970
	fibril_switch(FIBRIL_TO_MANAGER);
4324 decky 971
 
972
	/* Futex is up automatically after fibril_switch() */
973
 
1452 palkovsky 974
	free(msg);
975
}
1490 palkovsky 976
 
2490 jermar 977
/** Setter for client_connection function pointer.
1490 palkovsky 978
 *
4324 decky 979
 * @param conn Function that will implement a new connection fibril.
980
 *
1490 palkovsky 981
 */
982
void async_set_client_connection(async_client_conn_t conn)
983
{
984
	client_connection = conn;
985
}
2490 jermar 986
 
987
/** Setter for interrupt_received function pointer.
988
 *
4324 decky 989
 * @param intr Function that will implement a new interrupt
990
 *             notification fibril.
2490 jermar 991
 */
4324 decky 992
void async_set_interrupt_received(async_client_conn_t intr)
1596 palkovsky 993
{
4324 decky 994
	interrupt_received = intr;
1596 palkovsky 995
}
1610 palkovsky 996
 
2621 jermar 997
/** Pseudo-synchronous message sending - fast version.
998
 *
999
 * Send message asynchronously and return only after the reply arrives.
1000
 *
1001
 * This function can only transfer 4 register payload arguments. For
1002
 * transferring more arguments, see the slower async_req_slow().
1003
 *
4324 decky 1004
 * @param phoneid Hash of the phone through which to make the call.
1005
 * @param method  Method of the call.
1006
 * @param arg1    Service-defined payload argument.
1007
 * @param arg2    Service-defined payload argument.
1008
 * @param arg3    Service-defined payload argument.
1009
 * @param arg4    Service-defined payload argument.
1010
 * @param r1      If non-NULL, storage for the 1st reply argument.
1011
 * @param r2      If non-NULL, storage for the 2nd reply argument.
1012
 * @param r3      If non-NULL, storage for the 3rd reply argument.
1013
 * @param r4      If non-NULL, storage for the 4th reply argument.
1014
 * @param r5      If non-NULL, storage for the 5th reply argument.
1015
 *
1016
 * @return Return code of the reply or a negative error code.
1017
 *
2621 jermar 1018
 */
1019
ipcarg_t async_req_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
1020
    ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t *r1, ipcarg_t *r2,
1021
    ipcarg_t *r3, ipcarg_t *r4, ipcarg_t *r5)
1610 palkovsky 1022
{
2621 jermar 1023
	ipc_call_t result;
1024
	aid_t eid = async_send_4(phoneid, method, arg1, arg2, arg3, arg4,
1025
	    &result);
4324 decky 1026
 
1027
	ipcarg_t rc;
2621 jermar 1028
	async_wait_for(eid, &rc);
4324 decky 1029
 
1030
	if (r1)
2621 jermar 1031
		*r1 = IPC_GET_ARG1(result);
4324 decky 1032
 
2621 jermar 1033
	if (r2)
1034
		*r2 = IPC_GET_ARG2(result);
4324 decky 1035
 
2621 jermar 1036
	if (r3)
1037
		*r3 = IPC_GET_ARG3(result);
4324 decky 1038
 
2621 jermar 1039
	if (r4)
1040
		*r4 = IPC_GET_ARG4(result);
4324 decky 1041
 
2621 jermar 1042
	if (r5)
1043
		*r5 = IPC_GET_ARG5(result);
4324 decky 1044
 
2621 jermar 1045
	return rc;
1610 palkovsky 1046
}
1047
 
2621 jermar 1048
/** Pseudo-synchronous message sending - slow version.
1049
 *
1050
 * Send message asynchronously and return only after the reply arrives.
1051
 *
4324 decky 1052
 * @param phoneid Hash of the phone through which to make the call.
1053
 * @param method  Method of the call.
1054
 * @param arg1    Service-defined payload argument.
1055
 * @param arg2    Service-defined payload argument.
1056
 * @param arg3    Service-defined payload argument.
1057
 * @param arg4    Service-defined payload argument.
1058
 * @param arg5    Service-defined payload argument.
1059
 * @param r1      If non-NULL, storage for the 1st reply argument.
1060
 * @param r2      If non-NULL, storage for the 2nd reply argument.
1061
 * @param r3      If non-NULL, storage for the 3rd reply argument.
1062
 * @param r4      If non-NULL, storage for the 4th reply argument.
1063
 * @param r5      If non-NULL, storage for the 5th reply argument.
1064
 *
1065
 * @return Return code of the reply or a negative error code.
1066
 *
2621 jermar 1067
 */
1068
ipcarg_t async_req_slow(int phoneid, ipcarg_t method, ipcarg_t arg1,
1069
    ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, ipcarg_t *r1,
1070
    ipcarg_t *r2, ipcarg_t *r3, ipcarg_t *r4, ipcarg_t *r5)
1610 palkovsky 1071
{
2621 jermar 1072
	ipc_call_t result;
1073
	aid_t eid = async_send_5(phoneid, method, arg1, arg2, arg3, arg4, arg5,
1074
	    &result);
4324 decky 1075
 
1076
	ipcarg_t rc;
2621 jermar 1077
	async_wait_for(eid, &rc);
4324 decky 1078
 
1079
	if (r1)
2621 jermar 1080
		*r1 = IPC_GET_ARG1(result);
4324 decky 1081
 
2621 jermar 1082
	if (r2)
1083
		*r2 = IPC_GET_ARG2(result);
4324 decky 1084
 
2621 jermar 1085
	if (r3)
1086
		*r3 = IPC_GET_ARG3(result);
4324 decky 1087
 
2621 jermar 1088
	if (r4)
1089
		*r4 = IPC_GET_ARG4(result);
4324 decky 1090
 
2621 jermar 1091
	if (r5)
1092
		*r5 = IPC_GET_ARG5(result);
4324 decky 1093
 
2621 jermar 1094
	return rc;
1610 palkovsky 1095
}
1653 cejka 1096
 
1719 decky 1097
/** @}
1653 cejka 1098
 */