1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

tests/core/thread_cooperation: clean up test

- add `static` qualifiers where sensible
- drop `volatile` qualifier that was incorrectly used
- add a comment on use of shared memory for inter-thread communication
This commit is contained in:
Marian Buschsieweke 2023-10-26 10:34:01 +02:00
parent 647a13c392
commit ba44bb93d7
No known key found for this signature in database
GPG Key ID: CB8E3238CE715A94

View File

@ -29,13 +29,13 @@
#define PROBLEM 12
#endif
mutex_t mtx = MUTEX_INIT;
volatile uint32_t storage = 1;
char stacks[PROBLEM][THREAD_STACKSIZE_DEFAULT];
static mutex_t mtx = MUTEX_INIT;
static uint32_t storage = 1;
static char stacks[PROBLEM][THREAD_STACKSIZE_DEFAULT];
void *run(void *arg)
static void *run(void *arg)
{
(void) arg;
(void)arg;
msg_t m, final;
kernel_pid_t me = thread_getpid();
@ -45,6 +45,12 @@ void *run(void *arg)
msg_receive(&m);
printf("T-%02d: got arg %" PRIu32 "\n", me, m.content.value);
/* Accessing shared variable `storage` requires a critical section to avoid
* data races. The mutex provides this and `mutex_lock()`/`mutex_unlock()`
* are an implicit memory barrier that will ensure that `storage` is read
* indeed from memory and the new value is written back to memory within
* the critical section. The use of `volatile` is, hence, not needed here
* (and in fact incorrect). */
mutex_lock(&mtx);
storage *= m.content.value;
mutex_unlock(&mtx);
@ -71,9 +77,9 @@ int main(void)
factorial *= arg;
printf("MAIN: create thread, arg: %d\n", arg);
ths = thread_create(stacks[i], sizeof(stacks[i]),
THREAD_PRIORITY_MAIN - 1,
THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST,
run, NULL, "thread");
THREAD_PRIORITY_MAIN - 1,
THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST,
run, NULL, "thread");
if (ths < 0) {
puts("[ERROR]");