aboutsummaryrefslogtreecommitdiffstats
path: root/os/kernel/include/chbsem.h
blob: 2d8815c3c145f3ade51875ea7e71e6c18665659a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
                 2011 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file    chbsem.h
 * @brief   Binary semaphores structures and macros.
 *
 * @addtogroup binary_semaphores
 * @details Binary semaphores related APIs and services.
 *
 *          <h2>Operation mode</h2>
 *          Binary semaphores are implemented as a set of macros that use the
 *          existing counting semaphores primitives. The difference between
 *          counting and binary semaphores is that the counter of binary
 *          semaphores is not allowed to grow above the value 1. Repeated
 *          signal operation are ignored. A binary semaphore can thus have
 *          only two defined states:
 *          - <b>Taken</b>, when its counter has a value of zero or lower
 *            than zero. A negative number represent the number of threads
 *            queued on the binary semaphore.
 *          - <b>Not taken</b>, when its counter has a value of one.
 *          .
 *          Binary semaphores are different from mutexes because there is no
 *          the concept of ownership, a binary semaphore can be taken by a
 *          thread and signaled by another thread or an interrupt handler,
 *          mutexes can only be taken and released by the same thread. Another
 *          difference is that binary semaphores, unlike mutexes, do not
 *          implement the priority inheritance protocol.<br>
 *          In order to use the binary semaphores APIs the @p CH_USE_SEMAPHORES
 *          option must be enabled in @p chconf.h.
 * @{
 */

#ifndef _CHBSEM_H_
#define _CHBSEM_H_

#if CH_USE_SEMAPHORES || defined(__DOXYGEN__)

/**
 * @extends Semaphore
 *
 * @brief   Binary semaphore type.
 */
typedef struct  {
  Semaphore             bs_sem;
} BinarySemaphore;

/**
 * @brief   Data part of a static semaphore initializer.
 * @details This macro should be used when statically initializing a semaphore
 *          that is part of a bigger structure.
 *
 * @param[in] name      the name of the semaphore variable
 * @param[in] taken     the semaphore initial state
 */
#define _BSEMAPHORE_DATA(name, taken)                                       \
  {_SEMAPHORE_DATA(name.bs_sem, ((taken) ? 0 : 1))}

/**
 * @brief   Static semaphore initializer.
 * @details Statically initialized semaphores require no explicit
 *          initialization using @p chSemInit().
 *
 * @param[in] name      the name of the semaphore variable
 * @param[in] taken     the semaphore initial state
 */
#define BSEMAPHORE_DECL(name, taken)                                        \
  BinarySemaphore name = _BSEMAPHORE_DATA(name, taken)

/**
 * @name    Macro Functions
 * @{
 */
/**
 * @brief   Initializes a binary semaphore.
 *
 * @param[out] bsp      pointer to a @p BinarySemaphore structure
 * @param[in] taken     initial state of the binary semaphore:
 *                      - @a FALSE, the initial state is not taken.
 *                      - @a TRUE, the initial state is taken.
 *                      .
 *
 * @init
 */
#define chBSemInit(bsp, taken) chSemInit(&(bsp)->bs_sem, (taken) ? 0 : 1)

/**
 * @brief   Wait operation on the binary semaphore.
 *
 * @param[in] bsp       pointer to a @p BinarySemaphore structure
 * @return              A message specifying how the invoking thread has been
 *                      released from the semaphore.
 * @retval RDY_OK       if the binary semaphore has been successfully taken.
 * @retval RDY_RESET    if the binary semaphore has been reset using
 *                      @p bsemReset().
 *
 * @api
 */
#define chBSemWait(bsp) chSemWait(&(bsp)->bs_sem)

/**
 * @brief   Wait operation on the binary semaphore.
 *
 * @param[in] bsp       pointer to a @p BinarySemaphore structure
 * @return              A message specifying how the invoking thread has been
 *                      released from the semaphore.
 * @retval RDY_OK       if the binary semaphore has been successfully taken.
 * @retval RDY_RESET    if the binary semaphore has been reset using
 *                      @p bsemReset().
 *
 * @sclass
 */
#define chBSemWaitS(bsp) chSemWaitS(&(bsp)->bs_sem)

/**
 * @brief   Wait operation on the binary semaphore.
 *
 * @param[in] bsp       pointer to a @p BinarySemaphore structure
 * @param[in] time      the number of ticks before the operation timeouts,
 *                      the following special values are allowed:
 *                      - @a TIME_IMMEDIATE immediate timeout.
 *                      - @a TIME_INFINITE no timeout.
 *                      .
 * @return              A message specifying how the invoking thread has been
 *                      released from the semaphore.
 * @retval RDY_OK       if the binary semaphore has been successfully taken.
 * @retval RDY_RESET    if the binary semaphore has been reset using
 *                      @p bsemReset().
 * @retval RDY_TIMEOUT  if the binary semaphore has not been signaled or reset
 *                      within the specified timeout.
 *
 * @api
 */
#define chBSemWaitTimeout(bsp, time) chSemWaitTimeout(&(bsp)->bs_sem, (time))

/**
 * @brief   Wait operation on the binary semaphore.
 *
 * @param[in] bsp       pointer to a @p BinarySemaphore structure
 * @param[in] time      the number of ticks before the operation timeouts,
 *                      the following special values are allowed:
 *                      - @a TIME_IMMEDIATE immediate timeout.
 *                      - @a TIME_INFINITE no timeout.
 *                      .
 * @return              A message specifying how the invoking thread has been
 *                      released from the semaphore.
 * @retval RDY_OK       if the binary semaphore has been successfully taken.
 * @retval RDY_RESET    if the binary semaphore has been reset using
 *                      @p bsemReset().
 * @retval RDY_TIMEOUT  if the binary semaphore has not been signaled or reset
 *                      within the specified timeout.
 *
 * @sclass
 */
#define chBSemWaitTimeoutS(bsp, time) chSemWaitTimeoutS(&(bsp)->bs_sem, (time))

/**
 * @brief   Reset operation on the binary semaphore.
 * @note    The released threads can recognize they were waked up by a reset
 *          rather than a signal because the @p bsemWait() will return
 *          @p RDY_RESET instead of @p RDY_OK.
 *
 * @param[in] bsp       pointer to a @p BinarySemaphore structure
 * @param[in] taken     new state of the binary semaphore
 *                      - @a FALSE, the new state is not taken.
 *                      - @a TRUE, the new state is taken.
 *                      .
 *
 * @api
 */
#define chBSemReset(bsp, taken) chSemReset(&(bsp)->bs_sem, (taken) ? 0 : 1)

/**
 * @brief   Reset operation on the binary semaphore.
 * @note    The released threads can recognize they were waked up by a reset
 *          rather than a signal because the @p bsemWait() will return
 *          @p RDY_RESET instead of @p RDY_OK.
 * @note    This function does not reschedule.
 *
 * @param[in] bsp       pointer to a @p BinarySemaphore structure
 * @param[in] taken     new state of the binary semaphore
 *                      - @a FALSE, the new state is not taken.
 *                      - @a TRUE, the new state is taken.
 *                      .
 *
 * @iclass
 */
#define chBSemResetI(bsp, taken) chSemResetI(&(bsp)->bs_sem, (taken) ? 0 : 1)

/**
 * @brief   Performs a signal operation on a binary semaphore.
 *
 * @param[in] bsp       pointer to a @p BinarySemaphore structure
 *
 * @api
 */
#define chBSemSignal(bsp) {                                                 \
  chSysLock();                                                              \
  chBSemSignalI((bsp));                                                     \
  chSchRescheduleS();                                                       \
  chSysUnlock();                                                            \
}

/**
 * @brief   Performs a signal operation on a binary semaphore.
 * @note    This function does not reschedule.
 *
 * @param[in] bsp       pointer to a @p BinarySemaphore structure
 *
 * @iclass
 */
#define chBSemSignalI(bsp) {                                                \
  if ((bsp)->bs_sem.s_cnt < 1)                                              \
    chSemSignalI(&(bsp)->bs_sem);                                           \
}

/**
 * @brief   Returns the binary semaphore current state.
 *
 * @param[in] bsp       pointer to a @p BinarySemaphore structure
 * @return              The binary semaphore current state.
 * @retval FALSE        if the binary semaphore is not taken.
 * @retval TRUE         if the binary semaphore is taken.
 *
 * @iclass
 */
#define chBSemGetStateI(bsp) ((bsp)->bs_sem.s_cnt > 0 ? FALSE : TRUE)
/** @} */

#endif /* CH_USE_SEMAPHORES */

#endif /* _CHBSEM_H_ */

/** @} */
class="cm"> * . * @file testmtx.c * @brief Mutexes and CondVars test source file * @file testmtx.h * @brief Mutexes and CondVars test header file */ #if CH_CFG_USE_MUTEXES || defined(__DOXYGEN__) #define ALLOWED_DELAY 5 /* * Note, the static initializers are not really required because the * variables are explicitly initialized in each test case. It is done in order * to test the macros. */ static MUTEX_DECL(m1); static MUTEX_DECL(m2); #if CH_CFG_USE_CONDVARS || defined(__DOXYGEN__) static CONDVAR_DECL(c1); #endif /** * @page test_mtx_001 Priority enqueuing test * * <h2>Description</h2> * Five threads, with increasing priority, are enqueued on a locked mutex then * the mutex is unlocked.<br> * The test expects the threads to perform their operations in increasing * priority order regardless of the initial order. */ static void mtx1_setup(void) { chMtxObjectInit(&m1); } static msg_t thread1(void *p) { chMtxLock(&m1); test_emit_token(*(char *)p); chMtxUnlock(); return 0; } static void mtx1_execute(void) { tprio_t prio = chThdGetPriorityX(); /* Because priority inheritance.*/ chMtxLock(&m1); threads[0] = chThdCreateStatic(wa[0], WA_SIZE, prio+1, thread1, "E"); threads[1] = chThdCreateStatic(wa[1], WA_SIZE, prio+2, thread1, "D"); threads[2] = chThdCreateStatic(wa[2], WA_SIZE, prio+3, thread1, "C"); threads[3] = chThdCreateStatic(wa[3], WA_SIZE, prio+4, thread1, "B"); threads[4] = chThdCreateStatic(wa[4], WA_SIZE, prio+5, thread1, "A"); chMtxUnlock(); test_wait_threads(); test_assert(1, prio == chThdGetPriorityX(), "wrong priority level"); test_assert_sequence(2, "ABCDE"); } ROMCONST struct testcase testmtx1 = { "Mutexes, priority enqueuing test", mtx1_setup, NULL, mtx1_execute }; #if CH_DBG_THREADS_PROFILING || defined(__DOXYGEN__) /** * @page test_mtx_002 Priority inheritance, simple case * * <h2>Description</h2> * Three threads are involved in the classic priority inversion scenario, a * medium priority thread tries to starve an high priority thread by * blocking a low priority thread into a mutex lock zone.<br> * The test expects the threads to reach their goal in increasing priority * order by rearranging their priorities in order to avoid the priority * inversion trap. * * <h2>Scenario</h2> * This weird looking diagram should explain what happens in the test case: * @code * Time ----> 0 10 20 30 40 50 60 70 80 90 100 * 0 ......AL++++++++++............2+++++++++++AU0---------------++++++G... * 1 ..................++++++++++++------------------++++++++++++G......... * 2 .............................AL..........++++++AUG................... * ^ ^ * Legend: * 0..2 - Priority levels * +++ - Running * --- - Ready * ... - Waiting or Terminated * xL - Lock operation on mutex 'x' * xUn - Unlock operation on mutex 'x' with priority returning to level 'n' * G - Goal * ^ - Priority transition (boost or return). * @endcode */ static void mtx2_setup(void) { chMtxObjectInit(&m1); } /* Low priority thread */ static msg_t thread2L(void *p) { (void)p; chMtxLock(&m1); test_cpu_pulse(40); chMtxUnlock(); test_cpu_pulse(10); test_emit_token('C'); return 0; } /* Medium priority thread */ static msg_t thread2M(void *p) { (void)p; chThdSleepMilliseconds(20); test_cpu_pulse(40); test_emit_token('B'); return 0; } /* High priority thread */ static msg_t thread2H(void *p) { (void)p; chThdSleepMilliseconds(40); chMtxLock(&m1); test_cpu_pulse(10); chMtxUnlock(); test_emit_token('A'); return 0; } static void mtx2_execute(void) { systime_t time; test_wait_tick(); time = chVTGetSystemTime(); threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriorityX()-1, thread2H, 0); threads[1] = chThdCreateStatic(wa[1], WA_SIZE, chThdGetPriorityX()-2, thread2M, 0); threads[2] = chThdCreateStatic(wa[2], WA_SIZE, chThdGetPriorityX()-3, thread2L, 0); test_wait_threads(); test_assert_sequence(1, "ABC"); test_assert_time_window(2, time + MS2ST(100), time + MS2ST(100) + ALLOWED_DELAY); } ROMCONST struct testcase testmtx2 = { "Mutexes, priority inheritance, simple case", mtx2_setup, NULL, mtx2_execute }; /** * @page test_mtx_003 Priority inheritance, complex case * * <h2>Description</h2> * Five threads are involved in the complex priority inversion scenario, * please refer to the diagram below for the complete scenario.<br> * The test expects the threads to perform their operations in increasing * priority order by rearranging their priorities in order to avoid the * priority inversion trap. * * <h2>Scenario</h2> * This weird looking diagram should explain what happens in the test case: * @code * Time ----> 0 10 20 30 40 50 60 70 80 90 100 110 * 0 ......BL++++------------2+++++------4+++++BU0---------------------------G..... * 1 ............AL++++2+++++BL----------4-----++++++BU4+++AU1---------------G..... * 2 ..................AL----------------------------------------------++++++AUG... * 3 ..............................+++++++-----------------------++++++G........... * 4 ....................................AL................++++++AUG............... * ^ ^ ^ ^ ^ ^ * Legend: * 0..4 - Priority levels * +++ - Running * --- - Ready * ... - Waiting or Terminated * xL - Lock operation on mutex 'x' * xUn - Unlock operation on mutex 'x' with priority returning to level 'n' * ^ - Priority transition (boost or return). * @endcode */ static void mtx3_setup(void) { chMtxObjectInit(&m1); /* Mutex B.*/ chMtxObjectInit(&m2); /* Mutex A.*/ } /* Lowest priority thread */ static msg_t thread3LL(void *p) { (void)p; chMtxLock(&m1); test_cpu_pulse(30); chMtxUnlock(); test_emit_token('E'); return 0; } /* Low priority thread */ static msg_t thread3L(void *p) { (void)p; chThdSleepMilliseconds(10); chMtxLock(&m2); test_cpu_pulse(20); chMtxLock(&m1); test_cpu_pulse(10); chMtxUnlock(); test_cpu_pulse(10); chMtxUnlock(); test_emit_token('D'); return 0; } /* Medium priority thread */ static msg_t thread3M(void *p) { (void)p; chThdSleepMilliseconds(20); chMtxLock(&m2); test_cpu_pulse(10); chMtxUnlock(); test_emit_token('C'); return 0; } /* High priority thread */ static msg_t thread3H(void *p) { (void)p; chThdSleepMilliseconds(40); test_cpu_pulse(20); test_emit_token('B'); return 0; } /* Highest priority thread */ static msg_t thread3HH(void *p) { (void)p; chThdSleepMilliseconds(50); chMtxLock(&m2); test_cpu_pulse(10); chMtxUnlock(); test_emit_token('A'); return 0; } static void mtx3_execute(void) { systime_t time; test_wait_tick(); time = chVTGetSystemTime(); threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriorityX()-5, thread3LL, 0); threads[1] = chThdCreateStatic(wa[1], WA_SIZE, chThdGetPriorityX()-4, thread3L, 0); threads[2] = chThdCreateStatic(wa[2], WA_SIZE, chThdGetPriorityX()-3, thread3M, 0); threads[3] = chThdCreateStatic(wa[3], WA_SIZE, chThdGetPriorityX()-2, thread3H, 0); threads[4] = chThdCreateStatic(wa[4], WA_SIZE, chThdGetPriorityX()-1, thread3HH, 0); test_wait_threads(); test_assert_sequence(1, "ABCDE"); test_assert_time_window(2, time + MS2ST(110), time + MS2ST(110) + ALLOWED_DELAY); } ROMCONST struct testcase testmtx3 = { "Mutexes, priority inheritance, complex case", mtx3_setup, NULL, mtx3_execute }; #endif /* CH_DBG_THREADS_PROFILING */ /** * @page test_mtx_004 Priority return verification * * <h2>Description</h2> * Two threads are spawned that try to lock the mutexes locked by the tester * thread with precise timing.<br> * The test expects that the priority changes caused by the priority * inheritance algorithm happen at the right moment and with the right values. */ static void mtx4_setup(void) { chMtxObjectInit(&m1); chMtxObjectInit(&m2); } static msg_t thread4a(void *p) { (void)p; chThdSleepMilliseconds(50); chMtxLock(&m2); chMtxUnlock(); return 0; } static msg_t thread4b(void *p) { (void)p; chThdSleepMilliseconds(150); chMtxLock(&m1); chMtxUnlock(); return 0; } static void mtx4_execute(void) { tprio_t p, p1, p2; p = chThdGetPriorityX(); p1 = p + 1; p2 = p + 2; threads[0] = chThdCreateStatic(wa[0], WA_SIZE, p1, thread4a, "B"); threads[1] = chThdCreateStatic(wa[1], WA_SIZE, p2, thread4b, "A"); chMtxLock(&m2); test_assert(1, chThdGetPriorityX() == p, "wrong priority level"); chThdSleepMilliseconds(100); test_assert(2, chThdGetPriorityX() == p1, "wrong priority level"); chMtxLock(&m1); test_assert(3, chThdGetPriorityX() == p1, "wrong priority level"); chThdSleepMilliseconds(100); test_assert(4, chThdGetPriorityX() == p2, "wrong priority level"); chMtxUnlock(); test_assert(5, chThdGetPriorityX() == p1, "wrong priority level"); chThdSleepMilliseconds(100); test_assert(6, chThdGetPriorityX() == p1, "wrong priority level"); chMtxUnlockAll(); test_assert(7, chThdGetPriorityX() == p, "wrong priority level"); test_wait_threads(); /* Test repeated in order to cover chMtxUnlockS().*/ threads[0] = chThdCreateStatic(wa[0], WA_SIZE, p1, thread4a, "D"); threads[1] = chThdCreateStatic(wa[1], WA_SIZE, p2, thread4b, "C"); chMtxLock(&m2); test_assert(8, chThdGetPriorityX() == p, "wrong priority level"); chThdSleepMilliseconds(100); test_assert(9, chThdGetPriorityX() == p1, "wrong priority level"); chMtxLock(&m1); test_assert(10, chThdGetPriorityX() == p1, "wrong priority level"); chThdSleepMilliseconds(100); test_assert(11, chThdGetPriorityX() == p2, "wrong priority level"); chSysLock(); chMtxUnlockS(); chSysUnlock(); test_assert(12, chThdGetPriorityX() == p1, "wrong priority level"); chThdSleepMilliseconds(100); test_assert(13, chThdGetPriorityX() == p1, "wrong priority level"); chMtxUnlockAll(); test_assert(14, chThdGetPriorityX() == p, "wrong priority level"); test_wait_threads(); } ROMCONST struct testcase testmtx4 = { "Mutexes, priority return", mtx4_setup, NULL, mtx4_execute }; /** * @page test_mtx_005 Mutex status * * <h2>Description</h2> * Various tests on the mutex structure status after performing some lock and * unlock operations.<br> * The test expects that the internal mutex status is consistent after each * operation. */ static void mtx5_setup(void) { chMtxObjectInit(&m1); } static void mtx5_execute(void) { bool_t b; tprio_t prio; prio = chThdGetPriorityX(); b = chMtxTryLock(&m1); test_assert(1, b, "already locked"); b = chMtxTryLock(&m1); test_assert(2, !b, "not locked"); chSysLock(); chMtxUnlockS(); chSysUnlock(); test_assert(3, queue_isempty(&m1.m_queue), "queue not empty"); test_assert(4, m1.m_owner == NULL, "still owned"); test_assert(5, chThdGetPriorityX() == prio, "wrong priority level"); chMtxLock(&m1); chMtxUnlockAll(); test_assert(6, queue_isempty(&m1.m_queue), "queue not empty"); test_assert(7, m1.m_owner == NULL, "still owned"); } ROMCONST struct testcase testmtx5 = { "Mutexes, status", mtx5_setup, NULL, mtx5_execute }; #if CH_CFG_USE_CONDVARS || defined(__DOXYGEN__) /** * @page test_mtx_006 Condition Variable signal test * * <h2>Description</h2> * Five threads take a mutex and then enter a conditional variable queue, the * tester thread then proceeds to signal the conditional variable five times * atomically.<br> * The test expects the threads to reach their goal in increasing priority * order regardless of the initial order. */ static void mtx6_setup(void) { chCondObjectInit(&c1); chMtxObjectInit(&m1); } static msg_t thread10(void *p) { chMtxLock(&m1); chCondWait(&c1); test_emit_token(*(char *)p); chMtxUnlock(); return 0; } static void mtx6_execute(void) { tprio_t prio = chThdGetPriorityX(); threads[0] = chThdCreateStatic(wa[0], WA_SIZE, prio+1, thread10, "E"); threads[1] = chThdCreateStatic(wa[1], WA_SIZE, prio+2, thread10, "D"); threads[2] = chThdCreateStatic(wa[2], WA_SIZE, prio+3, thread10, "C"); threads[3] = chThdCreateStatic(wa[3], WA_SIZE, prio+4, thread10, "B"); threads[4] = chThdCreateStatic(wa[4], WA_SIZE, prio+5, thread10, "A"); chSysLock(); chCondSignalI(&c1); chCondSignalI(&c1); chCondSignalI(&c1); chCondSignalI(&c1); chCondSignalI(&c1); chSchRescheduleS(); chSysUnlock(); test_wait_threads(); test_assert_sequence(1, "ABCDE"); } ROMCONST struct testcase testmtx6 = { "CondVar, signal test", mtx6_setup, NULL, mtx6_execute }; /** * @page test_mtx_007 Condition Variable broadcast test * * <h2>Description</h2> * Five threads take a mutex and then enter a conditional variable queue, the * tester thread then proceeds to broadcast the conditional variable.<br> * The test expects the threads to reach their goal in increasing priority * order regardless of the initial order. */ static void mtx7_setup(void) { chCondObjectInit(&c1); chMtxObjectInit(&m1); } static void mtx7_execute(void) { tprio_t prio = chThdGetPriorityX(); threads[0] = chThdCreateStatic(wa[0], WA_SIZE, prio+1, thread10, "E"); threads[1] = chThdCreateStatic(wa[1], WA_SIZE, prio+2, thread10, "D"); threads[2] = chThdCreateStatic(wa[2], WA_SIZE, prio+3, thread10, "C"); threads[3] = chThdCreateStatic(wa[3], WA_SIZE, prio+4, thread10, "B"); threads[4] = chThdCreateStatic(wa[4], WA_SIZE, prio+5, thread10, "A"); chCondBroadcast(&c1); test_wait_threads(); test_assert_sequence(1, "ABCDE"); } ROMCONST struct testcase testmtx7 = { "CondVar, broadcast test", mtx7_setup, NULL, mtx7_execute }; /** * @page test_mtx_008 Condition Variable priority boost test * * <h2>Description</h2> * This test case verifies the priority boost of a thread waiting on a * conditional variable queue. It tests this very specific situation in order * to complete the code coverage. */ static void mtx8_setup(void) { chCondObjectInit(&c1); chMtxObjectInit(&m1); chMtxObjectInit(&m2); } static msg_t thread11(void *p) { chMtxLock(&m2); chMtxLock(&m1); #if CH_CFG_USE_CONDVARS_TIMEOUT || defined(__DOXYGEN__) chCondWaitTimeout(&c1, TIME_INFINITE); #else chCondWait(&c1); #endif test_emit_token(*(char *)p); chMtxUnlock(); chMtxUnlock(); return 0; } static msg_t thread12(void *p) { chMtxLock(&m2); test_emit_token(*(char *)p); chMtxUnlock(); return 0; } static void mtx8_execute(void) { tprio_t prio = chThdGetPriorityX(); threads[0] = chThdCreateStatic(wa[0], WA_SIZE, prio+1, thread11, "A"); threads[1] = chThdCreateStatic(wa[1], WA_SIZE, prio+2, thread10, "C"); threads[2] = chThdCreateStatic(wa[2], WA_SIZE, prio+3, thread12, "B"); chCondSignal(&c1); chCondSignal(&c1); test_wait_threads(); test_assert_sequence(1, "ABC"); } ROMCONST struct testcase testmtx8 = { "CondVar, boost test", mtx8_setup, NULL, mtx8_execute }; #endif /* CH_CFG_USE_CONDVARS */ #endif /* CH_CFG_USE_MUTEXES */ /** * @brief Test sequence for mutexes. */ ROMCONST struct testcase * ROMCONST patternmtx[] = { #if CH_CFG_USE_MUTEXES || defined(__DOXYGEN__) &testmtx1, #if CH_DBG_THREADS_PROFILING || defined(__DOXYGEN__) &testmtx2, &testmtx3, #endif &testmtx4, &testmtx5, #if CH_CFG_USE_CONDVARS || defined(__DOXYGEN__) &testmtx6, &testmtx7, &testmtx8, #endif #endif NULL };