14
14
* See the License for the specific language governing permissions and
15
15
* limitations under the License.
16
16
*/
17
- #if defined(MBED_RTOS_SINGLE_THREAD) || !defined(MBED_CONF_RTOS_PRESENT)
18
- #error [NOT_SUPPORTED] Semaphore test cases require RTOS with multithread to run
19
- #else
20
-
21
17
#if !DEVICE_USTICKER
22
18
#error [NOT_SUPPORTED] UsTicker need to be enabled for this test.
23
19
#else
30
26
31
27
using namespace utest ::v1;
32
28
29
+ struct test_data {
30
+ Semaphore *sem;
31
+ uint32_t data;
32
+ };
33
+
34
+ #if defined(MBED_CONF_RTOS_PRESENT)
33
35
#define THREAD_DELAY 30
34
36
#define SEMAPHORE_SLOTS 2
35
37
#define SEM_CHANGES 100
@@ -90,12 +92,7 @@ void test_multi()
90
92
}
91
93
}
92
94
93
- struct thread_data {
94
- Semaphore *sem;
95
- uint32_t data;
96
- };
97
-
98
- void single_thread (struct thread_data *data)
95
+ void single_thread (struct test_data *data)
99
96
{
100
97
data->sem ->acquire ();
101
98
data->data ++;
@@ -115,7 +112,7 @@ void test_single_thread()
115
112
{
116
113
Thread t (osPriorityNormal, THREAD_STACK_SIZE);
117
114
Semaphore sem (0 );
118
- struct thread_data data;
115
+ struct test_data data;
119
116
osStatus res;
120
117
121
118
data.sem = &sem;
@@ -167,6 +164,97 @@ void test_timeout()
167
164
t.join ();
168
165
TEST_ASSERT_UINT32_WITHIN (5000 , 30000 , timer.read_us ());
169
166
}
167
+ #endif
168
+
169
+ void test_ticker_release (struct test_data *data)
170
+ {
171
+ osStatus res;
172
+ data->data ++;
173
+ res = data->sem ->release ();
174
+ TEST_ASSERT_EQUAL (osOK, res);
175
+ }
176
+
177
+ /* * Test semaphore acquire
178
+
179
+ Given a semaphore with no tokens available and ticker with the callback registered
180
+ when callbacks update the test data and release semaphore
181
+ which will unblock the main thread which is blocked on semaphore acquire.
182
+ */
183
+ void test_semaphore_acquire ()
184
+ {
185
+ Semaphore sem (0 );
186
+ struct test_data data;
187
+
188
+ data.sem = &sem;
189
+ data.data = 0 ;
190
+ Ticker t1;
191
+ t1.attach_us (callback (test_ticker_release, &data), 3000 );
192
+ sem.acquire ();
193
+ t1.detach ();
194
+
195
+ TEST_ASSERT_EQUAL (1 , data.data );
196
+ }
197
+
198
+ void test_ticker_try_acquire (Semaphore *sem)
199
+ {
200
+ osStatus res;
201
+ res = sem->try_acquire ();
202
+ TEST_ASSERT_FALSE (res);
203
+ }
204
+
205
+ /* * Test semaphore try acquire
206
+
207
+ Given a semaphore with no tokens available and ticker with the callback registered
208
+ when callbacks try to acquire the semaphore will fail.
209
+ */
210
+ void test_semaphore_try_acquire ()
211
+ {
212
+ Semaphore sem (0 );
213
+ Ticker t1;
214
+ t1.attach_us (callback (test_ticker_try_acquire, &sem), 3000 );
215
+ ThisThread::sleep_for (3 );
216
+ t1.detach ();
217
+ }
218
+
219
+
220
+ /* * Test semaphore try timeout
221
+
222
+ Given a semaphore with no tokens available
223
+ when the main thread calls @a wait on the semaphore with try timeout of 5ms
224
+ then the main thread waits for 5ms and timeouts after
225
+ */
226
+ void test_semaphore_try_timeout ()
227
+ {
228
+ Semaphore sem (0 );
229
+ bool res;
230
+ res = sem.try_acquire_for (5 );
231
+ TEST_ASSERT_FALSE (res);
232
+ }
233
+
234
+
235
+ void test_ticker_semaphore_release (struct Semaphore *sem)
236
+ {
237
+ osStatus res;
238
+ res = sem->release ();
239
+ TEST_ASSERT_EQUAL (osOK, res);
240
+ }
241
+
242
+ /* * Test semaphore try acquire timeout
243
+
244
+ Given a semaphore with no tokens available and ticker with the callback registered
245
+ when callbacks release the semaphore will unblock the main thread
246
+ which is waiting for semaphore with a timeout.
247
+ */
248
+ void test_semaphore_try_acquire_timeout ()
249
+ {
250
+ Semaphore sem (0 );
251
+ bool res;
252
+ Ticker t1;
253
+ t1.attach_us (callback (test_ticker_semaphore_release, &sem), 3000 );
254
+ res = sem.try_acquire_for (30 );
255
+ t1.detach ();
256
+ TEST_ASSERT_TRUE (res);
257
+ }
170
258
171
259
/* * Test no timeouts
172
260
@@ -235,13 +323,19 @@ utest::v1::status_t test_setup(const size_t number_of_cases)
235
323
}
236
324
237
325
Case cases[] = {
238
- Case (" Test single thread" , test_single_thread),
239
- Case (" Test timeout" , test_timeout),
240
326
Case (" Test 1 token no timeout" , test_no_timeout<1 >),
241
327
Case (" Test 0 tokens no timeout" , test_no_timeout<0 >),
242
328
Case (" Test multiple tokens wait" , test_multiple_tokens_wait),
243
329
Case (" Test multiple tokens release" , test_multiple_tokens_release),
330
+ Case (" Test semaphore acquire" , test_semaphore_acquire),
331
+ Case (" Test semaphore try acquire" , test_semaphore_try_acquire),
332
+ Case (" Test semaphore try timeout" , test_semaphore_try_timeout),
333
+ Case (" Test semaphore try acquire timeout" , test_semaphore_try_acquire_timeout),
334
+ #if defined(MBED_CONF_RTOS_PRESENT)
335
+ Case (" Test single thread" , test_single_thread),
336
+ Case (" Test timeout" , test_timeout),
244
337
Case (" Test multiple threads" , test_multi)
338
+ #endif
245
339
};
246
340
247
341
Specification specification (test_setup, cases);
@@ -252,4 +346,3 @@ int main()
252
346
}
253
347
254
348
#endif // !DEVICE_USTICKER
255
- #endif // defined(MBED_RTOS_SINGLE_THREAD) || !defined(MBED_CONF_RTOS_PRESENT)
0 commit comments