| 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 | #include <async.h>
|
|---|
| 30 | #include <errno.h>
|
|---|
| 31 | #include <fibril_synch.h>
|
|---|
| 32 | #include <stdio.h>
|
|---|
| 33 | #include <stdlib.h>
|
|---|
| 34 | #include <task.h>
|
|---|
| 35 |
|
|---|
| 36 | #include "../tester.h"
|
|---|
| 37 | #include "common.h"
|
|---|
| 38 |
|
|---|
| 39 | static task_id_t task_id;
|
|---|
| 40 | static task_exit_t last_texit;
|
|---|
| 41 | static task_wait_flag_t last_flags;
|
|---|
| 42 | static int last_retval;
|
|---|
| 43 | static bool handler_hit;
|
|---|
| 44 |
|
|---|
| 45 | /* Locks are listed in their locking order */
|
|---|
| 46 | static FIBRIL_RWLOCK_INITIALIZE(tid_lck);
|
|---|
| 47 | static FIBRIL_MUTEX_INITIALIZE(sync_mtx);
|
|---|
| 48 |
|
|---|
| 49 | static FIBRIL_CONDVAR_INITIALIZE(sync_cv);
|
|---|
| 50 |
|
|---|
| 51 | static void task_event_handler(task_id_t tid, task_wait_flag_t flags, task_exit_t texit,
|
|---|
| 52 | int retval)
|
|---|
| 53 | {
|
|---|
| 54 | fibril_rwlock_read_lock(&tid_lck);
|
|---|
| 55 | fibril_mutex_lock(&sync_mtx);
|
|---|
| 56 |
|
|---|
| 57 | if (task_id != tid) {
|
|---|
| 58 | goto finish;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | handler_hit = true;
|
|---|
| 62 | if (flags & TASK_WAIT_EXIT) {
|
|---|
| 63 | last_texit = texit;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | last_flags = flags;
|
|---|
| 67 | if (last_flags & TASK_WAIT_RETVAL) {
|
|---|
| 68 | last_retval = retval;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | finish:
|
|---|
| 72 | fibril_condvar_signal(&sync_cv);
|
|---|
| 73 | fibril_mutex_unlock(&sync_mtx);
|
|---|
| 74 | fibril_rwlock_read_unlock(&tid_lck);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | static void reset_wait(bool purge)
|
|---|
| 78 | {
|
|---|
| 79 | fibril_mutex_lock(&sync_mtx);
|
|---|
| 80 | handler_hit = false;
|
|---|
| 81 | if (purge) {
|
|---|
| 82 | last_texit = TASK_EXIT_RUNNING;
|
|---|
| 83 | last_flags = 0;
|
|---|
| 84 | last_retval = 255;
|
|---|
| 85 | }
|
|---|
| 86 | fibril_mutex_unlock(&sync_mtx);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | static void wait_for_handler(void)
|
|---|
| 90 | {
|
|---|
| 91 | fibril_mutex_lock(&sync_mtx);
|
|---|
| 92 | while (!handler_hit) {
|
|---|
| 93 | fibril_condvar_wait(&sync_cv, &sync_mtx);
|
|---|
| 94 | }
|
|---|
| 95 | fibril_mutex_unlock(&sync_mtx);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | static inline int safe_dummy_task_spawn(task_id_t *task_id, task_wait_t *wait,
|
|---|
| 99 | const char *behavior)
|
|---|
| 100 | {
|
|---|
| 101 | fibril_rwlock_write_lock(&tid_lck);
|
|---|
| 102 | int rc = dummy_task_spawn(task_id, wait, behavior);
|
|---|
| 103 | fibril_rwlock_write_unlock(&tid_lck);
|
|---|
| 104 | return rc;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | const char *test_proc_task_anywait(void)
|
|---|
| 108 | {
|
|---|
| 109 | const char *err = NULL;
|
|---|
| 110 |
|
|---|
| 111 | int rc;
|
|---|
| 112 |
|
|---|
| 113 | rc = task_register_event_handler(task_event_handler, false);
|
|---|
| 114 | TASSERT(rc == EOK);
|
|---|
| 115 |
|
|---|
| 116 | TPRINTF("1 exit only\n");
|
|---|
| 117 |
|
|---|
| 118 | reset_wait(true);
|
|---|
| 119 | rc = safe_dummy_task_spawn(&task_id, NULL, STR_FAIL);
|
|---|
| 120 | TASSERT(rc == EOK);
|
|---|
| 121 | wait_for_handler();
|
|---|
| 122 | TASSERT(last_flags == (TASK_WAIT_EXIT));
|
|---|
| 123 | TASSERT(last_texit == TASK_EXIT_UNEXPECTED);
|
|---|
| 124 | /* --- */
|
|---|
| 125 |
|
|---|
| 126 | TPRINTF("2 daemon + kill\n");
|
|---|
| 127 |
|
|---|
| 128 | reset_wait(true);
|
|---|
| 129 | rc = safe_dummy_task_spawn(&task_id, NULL, STR_DAEMON);
|
|---|
| 130 | TASSERT(rc == EOK);
|
|---|
| 131 | wait_for_handler();
|
|---|
| 132 | TASSERT(last_flags == (TASK_WAIT_RETVAL));
|
|---|
| 133 | TASSERT(last_retval == EOK);
|
|---|
| 134 | TASSERT(last_texit == TASK_EXIT_RUNNING);
|
|---|
| 135 |
|
|---|
| 136 | reset_wait(false);
|
|---|
| 137 | task_kill(task_id);
|
|---|
| 138 | wait_for_handler();
|
|---|
| 139 | TASSERT(last_flags == (TASK_WAIT_EXIT));
|
|---|
| 140 | TASSERT(last_texit == TASK_EXIT_UNEXPECTED);
|
|---|
| 141 | /* --- */
|
|---|
| 142 |
|
|---|
| 143 | TPRINTF("3 successful job\n");
|
|---|
| 144 |
|
|---|
| 145 | reset_wait(true);
|
|---|
| 146 | rc = safe_dummy_task_spawn(&task_id, NULL, STR_JOB_OK);
|
|---|
| 147 | TASSERT(rc == EOK);
|
|---|
| 148 | wait_for_handler(); /* job is notified in a single handler call */
|
|---|
| 149 | TASSERT(last_flags == (TASK_WAIT_RETVAL | TASK_WAIT_EXIT));
|
|---|
| 150 | TASSERT(last_retval == EOK);
|
|---|
| 151 | TASSERT(last_texit == TASK_EXIT_NORMAL);
|
|---|
| 152 | /* --- */
|
|---|
| 153 |
|
|---|
| 154 | TPRINTF("3 successful job with discrimination\n");
|
|---|
| 155 |
|
|---|
| 156 | reset_wait(true);
|
|---|
| 157 | rc = safe_dummy_task_spawn(&task_id, NULL, STR_JOB_OK);
|
|---|
| 158 | TASSERT(rc == EOK);
|
|---|
| 159 | /* spoil it with another task events */
|
|---|
| 160 | rc = dummy_task_spawn(NULL, NULL, STR_JOB_OK);
|
|---|
| 161 | TASSERT(rc == EOK);
|
|---|
| 162 | wait_for_handler();
|
|---|
| 163 | TASSERT(last_flags == (TASK_WAIT_RETVAL | TASK_WAIT_EXIT));
|
|---|
| 164 | TASSERT(last_retval == EOK);
|
|---|
| 165 | TASSERT(last_texit == TASK_EXIT_NORMAL);
|
|---|
| 166 | /* --- */
|
|---|
| 167 |
|
|---|
| 168 | TPRINTF("All task waiting tests finished");
|
|---|
| 169 |
|
|---|
| 170 | return err;
|
|---|
| 171 | }
|
|---|