1 | /* |
---|
2 | * Copyright (c) 2014 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/rtems/semimpl.h> |
---|
22 | |
---|
23 | const char rtems_test_name[] = "SPINTRCRITICAL 22"; |
---|
24 | |
---|
25 | typedef struct { |
---|
26 | rtems_id semaphore_id; |
---|
27 | Semaphore_Control *semaphore_control; |
---|
28 | Thread_Control *main_task_control; |
---|
29 | volatile bool done; |
---|
30 | } test_context; |
---|
31 | |
---|
32 | static test_context ctx_instance; |
---|
33 | |
---|
34 | static Semaphore_Control *get_semaphore_control(rtems_id id) |
---|
35 | { |
---|
36 | Objects_Locations location; |
---|
37 | ISR_lock_Context lock_context; |
---|
38 | Semaphore_Control *sem; |
---|
39 | |
---|
40 | sem = _Semaphore_Get_interrupt_disable(id, &location, &lock_context); |
---|
41 | rtems_test_assert(sem != NULL); |
---|
42 | _ISR_lock_ISR_enable(&lock_context); |
---|
43 | |
---|
44 | return sem; |
---|
45 | } |
---|
46 | |
---|
47 | static void release_semaphore(rtems_id timer, void *arg) |
---|
48 | { |
---|
49 | /* The arg is NULL */ |
---|
50 | test_context *ctx = &ctx_instance; |
---|
51 | rtems_status_code sc; |
---|
52 | CORE_mutex_Control *mtx = &ctx->semaphore_control->Core_control.mutex; |
---|
53 | |
---|
54 | if ( |
---|
55 | _Thread_Wait_flags_get(ctx->main_task_control) |
---|
56 | == (THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_INTEND_TO_BLOCK) |
---|
57 | ) { |
---|
58 | ctx->done = true; |
---|
59 | |
---|
60 | sc = rtems_semaphore_release(ctx->semaphore_id); |
---|
61 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
62 | |
---|
63 | rtems_test_assert( |
---|
64 | _Thread_Wait_flags_get(ctx->main_task_control) |
---|
65 | == (THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_READY_AGAIN) |
---|
66 | ); |
---|
67 | rtems_test_assert(mtx->nest_count == 1); |
---|
68 | rtems_test_assert(mtx->holder == ctx->main_task_control); |
---|
69 | } else { |
---|
70 | sc = rtems_semaphore_release(ctx->semaphore_id); |
---|
71 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | static bool test_body(void *arg) |
---|
76 | { |
---|
77 | test_context *ctx = arg; |
---|
78 | rtems_status_code sc; |
---|
79 | |
---|
80 | sc = rtems_semaphore_obtain( |
---|
81 | ctx->semaphore_id, |
---|
82 | RTEMS_NO_WAIT, |
---|
83 | 0 |
---|
84 | ); |
---|
85 | rtems_test_assert(sc == RTEMS_SUCCESSFUL || sc == RTEMS_UNSATISFIED); |
---|
86 | |
---|
87 | sc = rtems_semaphore_obtain( |
---|
88 | ctx->semaphore_id, |
---|
89 | RTEMS_WAIT, |
---|
90 | 2 |
---|
91 | ); |
---|
92 | rtems_test_assert(sc == RTEMS_SUCCESSFUL || sc == RTEMS_TIMEOUT); |
---|
93 | |
---|
94 | return ctx->done; |
---|
95 | } |
---|
96 | |
---|
97 | static void Init(rtems_task_argument ignored) |
---|
98 | { |
---|
99 | test_context *ctx = &ctx_instance; |
---|
100 | rtems_status_code sc; |
---|
101 | |
---|
102 | TEST_BEGIN(); |
---|
103 | |
---|
104 | ctx->main_task_control = _Thread_Get_executing(); |
---|
105 | |
---|
106 | sc = rtems_semaphore_create( |
---|
107 | rtems_build_name('S', 'E', 'M', 'A'), |
---|
108 | 1, |
---|
109 | RTEMS_SIMPLE_BINARY_SEMAPHORE, |
---|
110 | 0, |
---|
111 | &ctx->semaphore_id |
---|
112 | ); |
---|
113 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
114 | |
---|
115 | ctx->semaphore_control = get_semaphore_control(ctx->semaphore_id); |
---|
116 | |
---|
117 | interrupt_critical_section_test(test_body, ctx, release_semaphore); |
---|
118 | rtems_test_assert(ctx->done); |
---|
119 | |
---|
120 | TEST_END(); |
---|
121 | |
---|
122 | rtems_test_exit(0); |
---|
123 | } |
---|
124 | |
---|
125 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
126 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
127 | |
---|
128 | #define CONFIGURE_MICROSECONDS_PER_TICK 1000 |
---|
129 | |
---|
130 | #define CONFIGURE_MAXIMUM_SEMAPHORES 1 |
---|
131 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
132 | #define CONFIGURE_MAXIMUM_TIMERS 1 |
---|
133 | #define CONFIGURE_MAXIMUM_USER_EXTENSIONS 1 |
---|
134 | |
---|
135 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
136 | |
---|
137 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
138 | |
---|
139 | #define CONFIGURE_INIT |
---|
140 | |
---|
141 | #include <rtems/confdefs.h> |
---|