[ab415f9] | 1 | /* |
---|
[7100108] | 2 | * Copyright (c) 2013-2014 embedded brains GmbH. All rights reserved. |
---|
[ab415f9] | 3 | * |
---|
| 4 | * embedded brains GmbH |
---|
| 5 | * Dornierstr. 4 |
---|
| 6 | * 82178 Puchheim |
---|
| 7 | * Germany |
---|
| 8 | * <rtems@embedded-brains.de> |
---|
| 9 | * |
---|
| 10 | * Redistribution and use in source and binary forms, with or without |
---|
| 11 | * modification, are permitted provided that the following conditions |
---|
| 12 | * are met: |
---|
| 13 | * 1. Redistributions of source code must retain the above copyright |
---|
| 14 | * notice, this list of conditions and the following disclaimer. |
---|
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
| 16 | * notice, this list of conditions and the following disclaimer in the |
---|
| 17 | * documentation and/or other materials provided with the distribution. |
---|
| 18 | * |
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
| 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
| 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
| 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
| 29 | * SUCH DAMAGE. |
---|
| 30 | */ |
---|
| 31 | |
---|
[f244de9] | 32 | #include <machine/rtems-bsd-kernel-space.h> |
---|
[7100108] | 33 | #include <machine/rtems-bsd-thread.h> |
---|
[ab415f9] | 34 | |
---|
| 35 | #include <stdlib.h> |
---|
| 36 | #include <stdio.h> |
---|
| 37 | #include <string.h> |
---|
| 38 | #include <assert.h> |
---|
| 39 | |
---|
[3d1e767] | 40 | #include <sys/types.h> |
---|
[0237319] | 41 | #include <sys/param.h> |
---|
[ab415f9] | 42 | #include <sys/proc.h> |
---|
| 43 | #include <sys/kthread.h> |
---|
| 44 | #include <sys/errno.h> |
---|
| 45 | |
---|
[33a15c3] | 46 | #include <rtems/bsd/bsd.h> |
---|
| 47 | |
---|
[ab415f9] | 48 | #include <rtems.h> |
---|
| 49 | #include <rtems/libcsupport.h> |
---|
| 50 | #include <rtems/score/threaddispatch.h> |
---|
| 51 | #include <rtems/score/wkspace.h> |
---|
| 52 | |
---|
| 53 | #define TEST_NAME "LIBBSD THREAD 1" |
---|
| 54 | |
---|
| 55 | #define TEST_KTHREAD_ADD ((void *) 0xdeadbeef) |
---|
| 56 | |
---|
| 57 | static rtems_id main_task_id; |
---|
| 58 | |
---|
| 59 | static char test_kproc_name[] = "kproc"; |
---|
| 60 | |
---|
| 61 | static struct kproc_desc test_kproc_start_desc; |
---|
| 62 | |
---|
| 63 | static char test_kthread_name[] = "kthread"; |
---|
| 64 | |
---|
| 65 | static struct kthread_desc test_kthread_start_desc; |
---|
| 66 | |
---|
| 67 | static void |
---|
| 68 | test_curthread(const char *name) |
---|
| 69 | { |
---|
| 70 | struct thread *td_0 = rtems_bsd_get_curthread_or_null(); |
---|
| 71 | struct thread *td_1 = rtems_bsd_get_curthread_or_wait_forever(); |
---|
| 72 | struct thread *td_2 = curthread; |
---|
| 73 | |
---|
| 74 | assert(td_0 != NULL); |
---|
| 75 | assert(td_0 == td_1); |
---|
| 76 | assert(td_0 == td_2); |
---|
[172f2ac] | 77 | assert(strcmp(td_0->td_thread->Join_queue.Queue.name, name) == 0); |
---|
[ab415f9] | 78 | } |
---|
| 79 | |
---|
| 80 | static void |
---|
| 81 | wake_up_main_thread(void) |
---|
| 82 | { |
---|
| 83 | rtems_status_code sc; |
---|
| 84 | |
---|
| 85 | sc = rtems_event_transient_send(main_task_id); |
---|
| 86 | assert(sc == RTEMS_SUCCESSFUL); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | static void |
---|
| 90 | wait_for_worker_thread(void) |
---|
| 91 | { |
---|
| 92 | rtems_status_code sc; |
---|
| 93 | |
---|
| 94 | sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
| 95 | assert(sc == RTEMS_SUCCESSFUL); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | static void |
---|
| 99 | non_bsd_thread(rtems_task_argument arg) |
---|
| 100 | { |
---|
| 101 | rtems_status_code sc; |
---|
| 102 | |
---|
| 103 | test_curthread(""); |
---|
| 104 | wake_up_main_thread(); |
---|
| 105 | |
---|
| 106 | sc = rtems_task_delete(RTEMS_SELF); |
---|
| 107 | assert(sc == RTEMS_SUCCESSFUL); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | static void |
---|
| 111 | test_non_bsd_thread(void) |
---|
| 112 | { |
---|
| 113 | rtems_status_code sc; |
---|
| 114 | rtems_id task_id; |
---|
| 115 | rtems_resource_snapshot snapshot; |
---|
| 116 | |
---|
| 117 | rtems_resource_snapshot_take(&snapshot); |
---|
| 118 | |
---|
| 119 | sc = rtems_task_create( |
---|
| 120 | rtems_build_name('T', 'A', 'S', 'K'), |
---|
| 121 | RTEMS_MINIMUM_PRIORITY, |
---|
| 122 | RTEMS_MINIMUM_STACK_SIZE, |
---|
| 123 | RTEMS_DEFAULT_MODES, |
---|
[4b8bc5c] | 124 | RTEMS_FLOATING_POINT, |
---|
[ab415f9] | 125 | &task_id |
---|
| 126 | ); |
---|
| 127 | assert(sc == RTEMS_SUCCESSFUL); |
---|
| 128 | |
---|
| 129 | sc = rtems_task_start(task_id, non_bsd_thread, 0); |
---|
| 130 | assert(sc == RTEMS_SUCCESSFUL); |
---|
| 131 | |
---|
| 132 | wait_for_worker_thread(); |
---|
| 133 | |
---|
| 134 | assert(rtems_resource_snapshot_check(&snapshot)); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | static void |
---|
| 138 | test_kproc_start_proc(void) |
---|
| 139 | { |
---|
| 140 | test_curthread(&test_kproc_name[0]); |
---|
| 141 | wake_up_main_thread(); |
---|
| 142 | kproc_exit(0); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | static void |
---|
| 146 | test_kproc_start(void) |
---|
| 147 | { |
---|
| 148 | rtems_resource_snapshot snapshot; |
---|
| 149 | struct proc *pr = NULL; |
---|
| 150 | struct kproc_desc *kpd = &test_kproc_start_desc; |
---|
| 151 | |
---|
| 152 | puts("test kproc_start()"); |
---|
| 153 | |
---|
| 154 | rtems_resource_snapshot_take(&snapshot); |
---|
| 155 | |
---|
| 156 | kpd->arg0 = &test_kproc_name[0]; |
---|
| 157 | kpd->func = test_kproc_start_proc, |
---|
| 158 | kpd->global_procpp = ≺ |
---|
| 159 | |
---|
| 160 | kproc_start(kpd); |
---|
| 161 | wait_for_worker_thread(); |
---|
| 162 | |
---|
| 163 | assert(pr != NULL); |
---|
| 164 | |
---|
| 165 | assert(rtems_resource_snapshot_check(&snapshot)); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | static void |
---|
| 169 | test_kthread_start_thread(void) |
---|
| 170 | { |
---|
| 171 | test_curthread(&test_kthread_name[0]); |
---|
| 172 | wake_up_main_thread(); |
---|
| 173 | kthread_exit(); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | static void |
---|
| 177 | test_kthread_start(void) |
---|
| 178 | { |
---|
| 179 | rtems_resource_snapshot snapshot; |
---|
| 180 | struct thread *td = NULL; |
---|
| 181 | struct kthread_desc *ktd = &test_kthread_start_desc; |
---|
| 182 | |
---|
| 183 | puts("test kthread_start()"); |
---|
| 184 | |
---|
| 185 | rtems_resource_snapshot_take(&snapshot); |
---|
| 186 | |
---|
| 187 | ktd->arg0 = &test_kthread_name[0]; |
---|
| 188 | ktd->func = test_kthread_start_thread, |
---|
| 189 | ktd->global_threadpp = &td; |
---|
| 190 | |
---|
| 191 | kthread_start(ktd); |
---|
| 192 | wait_for_worker_thread(); |
---|
| 193 | |
---|
| 194 | assert(td != NULL); |
---|
| 195 | |
---|
| 196 | assert(rtems_resource_snapshot_check(&snapshot)); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | static void |
---|
| 200 | test_kthread_add_thread(void *arg) |
---|
| 201 | { |
---|
| 202 | test_curthread(&test_kthread_name[0]); |
---|
| 203 | |
---|
| 204 | assert(arg == TEST_KTHREAD_ADD); |
---|
| 205 | |
---|
| 206 | wake_up_main_thread(); |
---|
| 207 | kthread_exit(); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | static void |
---|
| 211 | test_kthread_add(void) |
---|
| 212 | { |
---|
| 213 | rtems_resource_snapshot snapshot; |
---|
| 214 | void *greedy; |
---|
[33a15c3] | 215 | uintptr_t take_away = 2 * rtems_bsd_get_task_stack_size(""); |
---|
[ab415f9] | 216 | |
---|
| 217 | puts("test kthread_add()"); |
---|
| 218 | |
---|
[7100108] | 219 | greedy = rtems_workspace_greedy_allocate(&take_away, 1); |
---|
[ab415f9] | 220 | |
---|
| 221 | rtems_resource_snapshot_take(&snapshot); |
---|
| 222 | |
---|
| 223 | assert(rtems_configuration_get_unified_work_area()); |
---|
| 224 | |
---|
| 225 | while (take_away > 0) { |
---|
| 226 | void *away; |
---|
[7100108] | 227 | bool ok; |
---|
| 228 | |
---|
| 229 | ok = rtems_workspace_allocate(take_away, &away); |
---|
| 230 | if (ok) { |
---|
| 231 | struct thread *td = NULL; |
---|
| 232 | int eno; |
---|
| 233 | |
---|
| 234 | eno = kthread_add( |
---|
| 235 | test_kthread_add_thread, |
---|
| 236 | TEST_KTHREAD_ADD, |
---|
| 237 | NULL, |
---|
| 238 | &td, |
---|
| 239 | 0, |
---|
| 240 | 0, |
---|
| 241 | "%s", |
---|
| 242 | &test_kthread_name[0] |
---|
| 243 | ); |
---|
| 244 | |
---|
| 245 | ok = rtems_workspace_free(away); |
---|
| 246 | assert(ok); |
---|
| 247 | |
---|
| 248 | if (eno == 0) { |
---|
| 249 | wait_for_worker_thread(); |
---|
| 250 | assert(td != NULL); |
---|
| 251 | |
---|
| 252 | break; |
---|
| 253 | } else { |
---|
| 254 | assert(eno == ENOMEM); |
---|
| 255 | assert(rtems_resource_snapshot_check(&snapshot)); |
---|
| 256 | } |
---|
[ab415f9] | 257 | } |
---|
[7100108] | 258 | |
---|
| 259 | --take_away; |
---|
[ab415f9] | 260 | } |
---|
| 261 | |
---|
[7100108] | 262 | assert(take_away > 0); |
---|
| 263 | |
---|
[ab415f9] | 264 | rtems_workspace_greedy_free(greedy); |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | static void |
---|
| 268 | test_rtems_bsd_get_curthread_or_null(void) |
---|
| 269 | { |
---|
| 270 | rtems_resource_snapshot snapshot; |
---|
| 271 | void *greedy; |
---|
| 272 | |
---|
| 273 | puts("test rtems_bsd_get_curthread_or_null()"); |
---|
| 274 | |
---|
| 275 | rtems_resource_snapshot_take(&snapshot); |
---|
| 276 | |
---|
| 277 | greedy = rtems_workspace_greedy_allocate(NULL, 0); |
---|
| 278 | assert(rtems_bsd_get_curthread_or_null() == NULL); |
---|
| 279 | rtems_workspace_greedy_free(greedy); |
---|
| 280 | |
---|
| 281 | rtems_resource_snapshot_take(&snapshot); |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | static void |
---|
| 285 | test_main(void) |
---|
| 286 | { |
---|
| 287 | main_task_id = rtems_task_self(); |
---|
| 288 | |
---|
| 289 | test_non_bsd_thread(); |
---|
| 290 | test_kproc_start(); |
---|
| 291 | test_kthread_start(); |
---|
| 292 | test_kthread_add(); |
---|
| 293 | test_rtems_bsd_get_curthread_or_null(); |
---|
| 294 | |
---|
| 295 | exit(0); |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | #include <rtems/bsd/test/default-init.h> |
---|