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 | #include <assert.h>
|
---|
40 |
|
---|
41 | #include "private/taskman.h"
|
---|
42 |
|
---|
43 | static task_event_handler_t task_event_handler = NULL;
|
---|
44 |
|
---|
45 | static void taskman_task_event(ipc_call_t *icall)
|
---|
46 | {
|
---|
47 | task_id_t tid = (task_id_t)
|
---|
48 | MERGE_LOUP32(ipc_get_arg1(icall), ipc_get_arg2(icall));
|
---|
49 | int flags = ipc_get_arg3(icall);
|
---|
50 | task_exit_t texit = ipc_get_arg4(icall);
|
---|
51 | int retval = ipc_get_arg5(icall);
|
---|
52 |
|
---|
53 | task_event_handler(tid, flags, texit, retval);
|
---|
54 |
|
---|
55 | async_answer_0(icall, EOK);
|
---|
56 | }
|
---|
57 |
|
---|
58 | static void taskman_event_conn(ipc_call_t *icall, void *arg)
|
---|
59 | {
|
---|
60 | /* Accept connection */
|
---|
61 | async_answer_0(icall, EOK);
|
---|
62 |
|
---|
63 | while (true) {
|
---|
64 | ipc_call_t call;
|
---|
65 |
|
---|
66 | if (!async_get_call(&call) || !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(&call);
|
---|
74 | break;
|
---|
75 | default:
|
---|
76 | async_answer_0(&call, ENOTSUP);
|
---|
77 | break;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Blocks, calls handler in another fibril
|
---|
84 | */
|
---|
85 | errno_t task_register_event_handler(task_event_handler_t handler, bool past_events)
|
---|
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_1(exch, TASKMAN_EVENT_CALLBACK, past_events, NULL);
|
---|
98 |
|
---|
99 | port_id_t port;
|
---|
100 | errno_t rc = async_create_callback_port(exch, INTERFACE_TASKMAN_CB, 0, 0,
|
---|
101 | taskman_event_conn, NULL, &port);
|
---|
102 | taskman_exchange_end(exch);
|
---|
103 |
|
---|
104 | if (rc != EOK) {
|
---|
105 | return rc;
|
---|
106 | }
|
---|
107 |
|
---|
108 | errno_t retval;
|
---|
109 | async_wait_for(req, &retval);
|
---|
110 | return retval;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /** @}
|
---|
114 | */
|
---|