[456f7ae] | 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>
|
---|
[241f1985] | 39 | #include <assert.h>
|
---|
[456f7ae] | 40 |
|
---|
[012dd8e] | 41 | #include "private/taskman.h"
|
---|
[456f7ae] | 42 |
|
---|
[338a1a76] | 43 | static void taskman_task_event(ipc_call_t *icall, task_event_handler_t handler)
|
---|
[456f7ae] | 44 | {
|
---|
| 45 | task_id_t tid = (task_id_t)
|
---|
[241f1985] | 46 | MERGE_LOUP32(ipc_get_arg1(icall), ipc_get_arg2(icall));
|
---|
| 47 | int flags = ipc_get_arg3(icall);
|
---|
| 48 | task_exit_t texit = ipc_get_arg4(icall);
|
---|
| 49 | int retval = ipc_get_arg5(icall);
|
---|
[456f7ae] | 50 |
|
---|
[338a1a76] | 51 | handler(tid, flags, texit, retval);
|
---|
[456f7ae] | 52 |
|
---|
[241f1985] | 53 | async_answer_0(icall, EOK);
|
---|
[456f7ae] | 54 | }
|
---|
| 55 |
|
---|
[241f1985] | 56 | static void taskman_event_conn(ipc_call_t *icall, void *arg)
|
---|
[456f7ae] | 57 | {
|
---|
| 58 | while (true) {
|
---|
| 59 | ipc_call_t call;
|
---|
[102f641] | 60 |
|
---|
[241f1985] | 61 | if (!async_get_call(&call) || !ipc_get_imethod(&call)) {
|
---|
[012dd8e] | 62 | /* Hangup, end of game */
|
---|
[338a1a76] | 63 | async_answer_0(&call, EOK);
|
---|
| 64 | return;
|
---|
[456f7ae] | 65 | }
|
---|
| 66 |
|
---|
[241f1985] | 67 | switch (ipc_get_imethod(&call)) {
|
---|
[456f7ae] | 68 | case TASKMAN_EV_TASK:
|
---|
[338a1a76] | 69 | taskman_task_event(&call, (task_event_handler_t)arg);
|
---|
[456f7ae] | 70 | break;
|
---|
| 71 | default:
|
---|
[241f1985] | 72 | async_answer_0(&call, ENOTSUP);
|
---|
[456f7ae] | 73 | break;
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * Blocks, calls handler in another fibril
|
---|
| 80 | */
|
---|
[241f1985] | 81 | errno_t task_register_event_handler(task_event_handler_t handler, bool past_events)
|
---|
[456f7ae] | 82 | {
|
---|
| 83 | assert(handler != NULL); /* no support for "unregistration" */
|
---|
| 84 |
|
---|
| 85 | async_exch_t *exch = taskman_exchange_begin();
|
---|
[4ff66ae] | 86 | aid_t req = async_send_1(exch, TASKMAN_EVENT_CALLBACK, past_events, NULL);
|
---|
[456f7ae] | 87 |
|
---|
[241f1985] | 88 | port_id_t port;
|
---|
| 89 | errno_t rc = async_create_callback_port(exch, INTERFACE_TASKMAN_CB, 0, 0,
|
---|
[338a1a76] | 90 | taskman_event_conn, handler, &port);
|
---|
[456f7ae] | 91 | taskman_exchange_end(exch);
|
---|
| 92 |
|
---|
| 93 | if (rc != EOK) {
|
---|
| 94 | return rc;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[241f1985] | 97 | errno_t retval;
|
---|
[456f7ae] | 98 | async_wait_for(req, &retval);
|
---|
| 99 | return retval;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /** @}
|
---|
| 103 | */
|
---|