1 | /*
|
---|
2 | * Copyright (c) 2015 Michal Koutny
|
---|
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 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <errno.h>
|
---|
36 | #include <ipc/taskman.h>
|
---|
37 | #include <macros.h>
|
---|
38 | #include <task.h>
|
---|
39 |
|
---|
40 | #include "private/taskman.h"
|
---|
41 |
|
---|
42 | static task_event_handler_t task_event_handler = NULL;
|
---|
43 |
|
---|
44 | static void taskman_task_event(ipc_callid_t iid, ipc_call_t *icall)
|
---|
45 | {
|
---|
46 | task_id_t tid = (task_id_t)
|
---|
47 | MERGE_LOUP32(IPC_GET_ARG1(*icall), IPC_GET_ARG2(*icall));
|
---|
48 | int flags = IPC_GET_ARG3(*icall);
|
---|
49 | task_exit_t texit = IPC_GET_ARG4(*icall);
|
---|
50 | int retval = IPC_GET_ARG5(*icall);
|
---|
51 |
|
---|
52 | task_event_handler(tid, flags, texit, retval);
|
---|
53 |
|
---|
54 | async_answer_0(iid, EOK);
|
---|
55 | }
|
---|
56 |
|
---|
57 | static void taskman_event_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
58 | {
|
---|
59 | /* Accept connection */
|
---|
60 | async_answer_0(iid, EOK);
|
---|
61 |
|
---|
62 | while (true) {
|
---|
63 | ipc_call_t call;
|
---|
64 | ipc_callid_t callid = async_get_call(&call);
|
---|
65 |
|
---|
66 | if (!IPC_GET_IMETHOD(call)) {
|
---|
67 | /* Hangup, end of game */
|
---|
68 | break;
|
---|
69 | }
|
---|
70 |
|
---|
71 | switch (IPC_GET_IMETHOD(call)) {
|
---|
72 | case TASKMAN_EV_TASK:
|
---|
73 | taskman_task_event(callid, &call);
|
---|
74 | break;
|
---|
75 | default:
|
---|
76 | async_answer_0(callid, ENOTSUP);
|
---|
77 | break;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Blocks, calls handler in another fibril
|
---|
84 | */
|
---|
85 | int task_register_event_handler(task_event_handler_t handler)
|
---|
86 | {
|
---|
87 | /*
|
---|
88 | * so far support assign once, modification cannot be naïve due to
|
---|
89 | * races
|
---|
90 | */
|
---|
91 | assert(task_event_handler == NULL);
|
---|
92 | assert(handler != NULL); /* no support for "unregistration" */
|
---|
93 |
|
---|
94 | task_event_handler = handler;
|
---|
95 |
|
---|
96 | async_exch_t *exch = taskman_exchange_begin();
|
---|
97 | aid_t req = async_send_0(exch, TASKMAN_EVENT_CALLBACK, NULL);
|
---|
98 |
|
---|
99 | int rc = async_connect_to_me(exch, 0, 0, 0, taskman_event_conn, NULL);
|
---|
100 | taskman_exchange_end(exch);
|
---|
101 |
|
---|
102 | if (rc != EOK) {
|
---|
103 | return rc;
|
---|
104 | }
|
---|
105 |
|
---|
106 | sysarg_t retval;
|
---|
107 | async_wait_for(req, &retval);
|
---|
108 | return retval;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /** @}
|
---|
112 | */
|
---|