1 | /* |
---|
2 | * Copyright (c) 2013 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #ifdef HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <tmacros.h> |
---|
20 | #include <intrcritical.h> |
---|
21 | #include <rtems/score/threadimpl.h> |
---|
22 | #include <rtems/score/threadqimpl.h> |
---|
23 | #include <rtems/rtems/semimpl.h> |
---|
24 | |
---|
25 | const char rtems_test_name[] = "SPINTRCRITICAL 20"; |
---|
26 | |
---|
27 | #define PRIORITY_MASTER 2 |
---|
28 | |
---|
29 | #define PRIORITY_SEMAPHORE 1 |
---|
30 | |
---|
31 | typedef struct { |
---|
32 | rtems_id master_task; |
---|
33 | rtems_id semaphore_task; |
---|
34 | rtems_id semaphore_id; |
---|
35 | Thread_Control *semaphore_task_tcb; |
---|
36 | bool thread_queue_was_null; |
---|
37 | bool status_was_successful; |
---|
38 | bool status_was_timeout; |
---|
39 | } test_context; |
---|
40 | |
---|
41 | static test_context ctx_instance; |
---|
42 | |
---|
43 | static void semaphore_task(rtems_task_argument arg) |
---|
44 | { |
---|
45 | test_context *ctx = (test_context *) arg; |
---|
46 | |
---|
47 | ctx->semaphore_task_tcb = _Thread_Get_executing(); |
---|
48 | |
---|
49 | while (true) { |
---|
50 | rtems_status_code sc = rtems_semaphore_obtain( |
---|
51 | ctx->semaphore_id, |
---|
52 | RTEMS_WAIT, |
---|
53 | RTEMS_NO_TIMEOUT |
---|
54 | ); |
---|
55 | rtems_test_assert(sc == RTEMS_SUCCESSFUL || sc == RTEMS_TIMEOUT); |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | static void release_semaphore(rtems_id timer, void *arg) |
---|
60 | { |
---|
61 | /* The arg is NULL */ |
---|
62 | test_context *ctx = &ctx_instance; |
---|
63 | rtems_status_code sc = rtems_semaphore_release(ctx->semaphore_id); |
---|
64 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
65 | } |
---|
66 | |
---|
67 | static bool test_body(void *arg) |
---|
68 | { |
---|
69 | test_context *ctx = arg; |
---|
70 | int busy; |
---|
71 | Per_CPU_Control *cpu_self; |
---|
72 | |
---|
73 | cpu_self = _Thread_Dispatch_disable(); |
---|
74 | |
---|
75 | rtems_test_assert( |
---|
76 | _Thread_Wait_get_status( ctx->semaphore_task_tcb ) == STATUS_SUCCESSFUL |
---|
77 | ); |
---|
78 | |
---|
79 | /* |
---|
80 | * Spend some time to make it more likely that we hit the test condition |
---|
81 | * below. |
---|
82 | */ |
---|
83 | for (busy = 0; busy < 1000; ++busy) { |
---|
84 | __asm__ volatile (""); |
---|
85 | } |
---|
86 | |
---|
87 | if (ctx->semaphore_task_tcb->Wait.queue == NULL) { |
---|
88 | ctx->thread_queue_was_null = true; |
---|
89 | } |
---|
90 | |
---|
91 | _Thread_Timeout(&ctx->semaphore_task_tcb->Timer.Watchdog); |
---|
92 | |
---|
93 | switch (_Thread_Wait_get_status(ctx->semaphore_task_tcb)) { |
---|
94 | case STATUS_SUCCESSFUL: |
---|
95 | ctx->status_was_successful = true; |
---|
96 | break; |
---|
97 | case STATUS_TIMEOUT: |
---|
98 | ctx->status_was_timeout = true; |
---|
99 | break; |
---|
100 | default: |
---|
101 | rtems_test_assert(0); |
---|
102 | break; |
---|
103 | } |
---|
104 | |
---|
105 | _Thread_Dispatch_enable(cpu_self); |
---|
106 | |
---|
107 | return ctx->thread_queue_was_null |
---|
108 | && ctx->status_was_successful |
---|
109 | && ctx->status_was_timeout; |
---|
110 | } |
---|
111 | |
---|
112 | static void Init(rtems_task_argument ignored) |
---|
113 | { |
---|
114 | test_context *ctx = &ctx_instance; |
---|
115 | rtems_status_code sc; |
---|
116 | |
---|
117 | TEST_BEGIN(); |
---|
118 | |
---|
119 | ctx->master_task = rtems_task_self(); |
---|
120 | |
---|
121 | sc = rtems_semaphore_create( |
---|
122 | rtems_build_name('S', 'E', 'M', 'A'), |
---|
123 | 1, |
---|
124 | RTEMS_COUNTING_SEMAPHORE, |
---|
125 | 0, |
---|
126 | &ctx->semaphore_id |
---|
127 | ); |
---|
128 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
129 | |
---|
130 | sc = rtems_task_create( |
---|
131 | rtems_build_name('S', 'E', 'M', 'A'), |
---|
132 | PRIORITY_SEMAPHORE, |
---|
133 | RTEMS_MINIMUM_STACK_SIZE, |
---|
134 | RTEMS_DEFAULT_MODES, |
---|
135 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
136 | &ctx->semaphore_task |
---|
137 | ); |
---|
138 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
139 | |
---|
140 | sc = rtems_task_start( |
---|
141 | ctx->semaphore_task, |
---|
142 | semaphore_task, |
---|
143 | (rtems_task_argument) ctx |
---|
144 | ); |
---|
145 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
146 | |
---|
147 | interrupt_critical_section_test(test_body, ctx, release_semaphore); |
---|
148 | |
---|
149 | rtems_test_assert(ctx->thread_queue_was_null); |
---|
150 | rtems_test_assert(ctx->status_was_successful); |
---|
151 | rtems_test_assert(ctx->status_was_timeout); |
---|
152 | |
---|
153 | TEST_END(); |
---|
154 | |
---|
155 | rtems_test_exit(0); |
---|
156 | } |
---|
157 | |
---|
158 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
159 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
160 | |
---|
161 | #define CONFIGURE_MICROSECONDS_PER_TICK 1000 |
---|
162 | |
---|
163 | #define CONFIGURE_MAXIMUM_SEMAPHORES 1 |
---|
164 | #define CONFIGURE_MAXIMUM_TASKS 2 |
---|
165 | #define CONFIGURE_MAXIMUM_TIMERS 1 |
---|
166 | #define CONFIGURE_MAXIMUM_USER_EXTENSIONS 1 |
---|
167 | |
---|
168 | #define CONFIGURE_INIT_TASK_PRIORITY PRIORITY_MASTER |
---|
169 | #define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_DEFAULT_ATTRIBUTES |
---|
170 | #define CONFIGURE_INIT_TASK_INITIAL_MODES RTEMS_DEFAULT_MODES |
---|
171 | |
---|
172 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
173 | |
---|
174 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
175 | |
---|
176 | #define CONFIGURE_INIT |
---|
177 | |
---|
178 | #include <rtems/confdefs.h> |
---|