[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 |
|
---|
| 43 | static task_event_handler_t task_event_handler = NULL;
|
---|
| 44 |
|
---|
[241f1985] | 45 | static void taskman_task_event(ipc_call_t *icall)
|
---|
[456f7ae] | 46 | {
|
---|
| 47 | task_id_t tid = (task_id_t)
|
---|
[241f1985] | 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);
|
---|
[456f7ae] | 52 |
|
---|
| 53 | task_event_handler(tid, flags, texit, retval);
|
---|
| 54 |
|
---|
[241f1985] | 55 | async_answer_0(icall, EOK);
|
---|
[456f7ae] | 56 | }
|
---|
| 57 |
|
---|
[241f1985] | 58 | static void taskman_event_conn(ipc_call_t *icall, void *arg)
|
---|
[456f7ae] | 59 | {
|
---|
| 60 | /* Accept connection */
|
---|
[241f1985] | 61 | async_answer_0(icall, EOK);
|
---|
[456f7ae] | 62 |
|
---|
| 63 | while (true) {
|
---|
| 64 | ipc_call_t call;
|
---|
[241f1985] | 65 |
|
---|
| 66 | if (!async_get_call(&call) || !ipc_get_imethod(&call)) {
|
---|
[012dd8e] | 67 | /* Hangup, end of game */
|
---|
[456f7ae] | 68 | break;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[241f1985] | 71 | switch (ipc_get_imethod(&call)) {
|
---|
[456f7ae] | 72 | case TASKMAN_EV_TASK:
|
---|
[241f1985] | 73 | taskman_task_event(&call);
|
---|
[456f7ae] | 74 | break;
|
---|
| 75 | default:
|
---|
[241f1985] | 76 | async_answer_0(&call, ENOTSUP);
|
---|
[456f7ae] | 77 | break;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
| 83 | * Blocks, calls handler in another fibril
|
---|
| 84 | */
|
---|
[241f1985] | 85 | errno_t task_register_event_handler(task_event_handler_t handler, bool past_events)
|
---|
[456f7ae] | 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();
|
---|
[4ff66ae] | 97 | aid_t req = async_send_1(exch, TASKMAN_EVENT_CALLBACK, past_events, NULL);
|
---|
[456f7ae] | 98 |
|
---|
[241f1985] | 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);
|
---|
[456f7ae] | 102 | taskman_exchange_end(exch);
|
---|
| 103 |
|
---|
| 104 | if (rc != EOK) {
|
---|
| 105 | return rc;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[241f1985] | 108 | errno_t retval;
|
---|
[456f7ae] | 109 | async_wait_for(req, &retval);
|
---|
| 110 | return retval;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /** @}
|
---|
| 114 | */
|
---|