2016-02-25 00:14:51 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup tests
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief simple thread flags test application
|
|
|
|
*
|
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2017-11-04 16:51:03 +01:00
|
|
|
#include <stdlib.h>
|
2016-02-25 00:14:51 +01:00
|
|
|
#include "thread.h"
|
2017-09-01 23:54:36 +02:00
|
|
|
#include "xtimer.h"
|
2016-02-25 00:14:51 +01:00
|
|
|
|
|
|
|
static char stack[THREAD_STACKSIZE_MAIN];
|
|
|
|
|
2017-02-21 10:37:47 +01:00
|
|
|
volatile unsigned done;
|
|
|
|
|
2017-12-19 11:04:18 +01:00
|
|
|
#define TIMEOUT (100UL * US_PER_MS)
|
|
|
|
#define THRESHOLD (500U)
|
2017-11-04 16:51:03 +01:00
|
|
|
|
2016-02-25 00:14:51 +01:00
|
|
|
static void *_thread(void *arg)
|
|
|
|
{
|
|
|
|
(void) arg;
|
|
|
|
|
|
|
|
thread_flags_t flags;
|
|
|
|
|
2017-02-01 14:43:43 +01:00
|
|
|
puts("thread(): waiting for 0x1...");
|
2016-02-25 00:14:51 +01:00
|
|
|
flags = thread_flags_wait_any(0x1);
|
|
|
|
printf("thread(): received flags: 0x%04x\n", (unsigned)flags & 0xFFFF);
|
|
|
|
|
2017-02-01 14:43:43 +01:00
|
|
|
puts("thread(): waiting for 0x1 || 0x64...");
|
2016-02-25 00:14:51 +01:00
|
|
|
flags = thread_flags_wait_any(0x1 | 0x64);
|
|
|
|
printf("thread(): received flags: 0x%04x\n", (unsigned)flags & 0xFFFF);
|
|
|
|
|
2017-02-01 14:43:43 +01:00
|
|
|
puts("thread(): waiting for 0x2 && 0x4...");
|
2016-02-25 00:14:51 +01:00
|
|
|
flags = thread_flags_wait_all(0x2 | 0x4);
|
|
|
|
printf("thread(): received flags: 0x%04x\n", (unsigned)flags & 0xFFFF);
|
|
|
|
|
2017-02-01 14:43:43 +01:00
|
|
|
puts("thread(): waiting for any flag, one by one");
|
2016-03-30 14:11:56 +02:00
|
|
|
flags = thread_flags_wait_one(0xFFFF);
|
|
|
|
printf("thread(): received flags: 0x%04x\n", (unsigned)flags & 0xFFFF);
|
|
|
|
|
2017-02-01 14:43:43 +01:00
|
|
|
puts("thread(): waiting for any flag, one by one");
|
2016-03-30 14:11:56 +02:00
|
|
|
flags = thread_flags_wait_one(0xFFFF);
|
|
|
|
printf("thread(): received flags: 0x%04x\n", (unsigned)flags & 0xFFFF);
|
|
|
|
|
2017-02-21 10:37:47 +01:00
|
|
|
done = 1;
|
|
|
|
|
2016-02-25 00:14:51 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _set(thread_t *thread, thread_flags_t flags)
|
|
|
|
{
|
|
|
|
printf("main(): setting flag 0x%04x\n", (unsigned)flags & 0xFFFF);
|
|
|
|
thread_flags_set(thread, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2017-11-04 16:51:03 +01:00
|
|
|
puts("START");
|
2016-02-25 00:14:51 +01:00
|
|
|
|
|
|
|
kernel_pid_t pid = thread_create(stack,
|
|
|
|
sizeof(stack),
|
|
|
|
THREAD_PRIORITY_MAIN - 1,
|
|
|
|
THREAD_CREATE_STACKTEST,
|
|
|
|
_thread,
|
|
|
|
NULL,
|
|
|
|
"second_thread");
|
|
|
|
|
|
|
|
thread_t *thread = (thread_t*) thread_get(pid);
|
|
|
|
_set(thread, 0x1);
|
|
|
|
_set(thread, 0x64);
|
|
|
|
_set(thread, 0x1);
|
2016-03-30 14:11:56 +02:00
|
|
|
_set(thread, 0x8);
|
2016-02-25 00:14:51 +01:00
|
|
|
_set(thread, 0x2);
|
|
|
|
_set(thread, 0x4);
|
|
|
|
|
2017-02-21 10:37:47 +01:00
|
|
|
while(!done) {};
|
|
|
|
|
2017-09-01 23:54:36 +02:00
|
|
|
puts("main: setting 100ms timeout...");
|
|
|
|
xtimer_t t;
|
|
|
|
uint32_t before = xtimer_now_usec();
|
2017-11-04 16:51:03 +01:00
|
|
|
xtimer_set_timeout_flag(&t, TIMEOUT);
|
2017-09-01 23:54:36 +02:00
|
|
|
thread_flags_wait_any(THREAD_FLAG_TIMEOUT);
|
|
|
|
uint32_t diff = xtimer_now_usec() - before;
|
2018-04-27 20:29:09 +02:00
|
|
|
printf("main: timeout triggered. time passed: %"PRIu32"us\n", diff);
|
2017-09-01 23:54:36 +02:00
|
|
|
|
2020-02-13 15:37:30 +01:00
|
|
|
puts("main: setting 100ms timeout (using uint64)...");
|
|
|
|
uint64_t timeout64 = TIMEOUT;
|
|
|
|
before = xtimer_now_usec();
|
|
|
|
xtimer_set_timeout_flag64(&t, timeout64);
|
|
|
|
thread_flags_wait_any(THREAD_FLAG_TIMEOUT);
|
|
|
|
diff = xtimer_now_usec() - before;
|
|
|
|
printf("main: timeout triggered. time passed: %"PRIu32"us\n", diff);
|
|
|
|
|
2017-12-19 11:04:18 +01:00
|
|
|
if (diff < (TIMEOUT + THRESHOLD)) {
|
2017-11-04 16:51:03 +01:00
|
|
|
puts("SUCCESS");
|
2017-12-19 11:04:18 +01:00
|
|
|
return 0;
|
2017-11-04 16:51:03 +01:00
|
|
|
}
|
2017-12-19 11:04:18 +01:00
|
|
|
puts("FAILURE");
|
2017-02-21 10:37:47 +01:00
|
|
|
|
2017-12-19 11:04:18 +01:00
|
|
|
return 1;
|
2016-02-25 00:14:51 +01:00
|
|
|
}
|