1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
20017: tests/core/thread_cooperation: clean up test r=maribu a=maribu

### Contribution description

- add `static` qualifiers where sensible
- drop `volatile` qualifier that was incorrectly used
- add a comment on use of shared memory for inter-thread communication

### Testing procedure

<details><summary><code>make BOARD=nrf52840dk -j -C tests/core/thread_cooperation flash test</code></summary>

```
make: Entering directory '/home/maribu/Repos/software/RIOT/master/tests/core/thread_cooperation'
Building application "tests_thread_cooperation" for "nrf52840dk" with MCU "nrf52".

"make" -C /home/maribu/Repos/software/RIOT/master/pkg/cmsis/ 
"make" -C /home/maribu/Repos/software/RIOT/master/boards/common/init
"make" -C /home/maribu/Repos/software/RIOT/master/boards/nrf52840dk
"make" -C /home/maribu/Repos/software/RIOT/master/core
"make" -C /home/maribu/Repos/software/RIOT/master/core/lib
"make" -C /home/maribu/Repos/software/RIOT/master/cpu/nrf52
"make" -C /home/maribu/Repos/software/RIOT/master/drivers
"make" -C /home/maribu/Repos/software/RIOT/master/sys
"make" -C /home/maribu/Repos/software/RIOT/master/boards/common/nrf52xxxdk
"make" -C /home/maribu/Repos/software/RIOT/master/drivers/periph_common
"make" -C /home/maribu/Repos/software/RIOT/master/cpu/cortexm_common
"make" -C /home/maribu/Repos/software/RIOT/master/cpu/nrf52/periph
"make" -C /home/maribu/Repos/software/RIOT/master/cpu/nrf52/vectors
"make" -C /home/maribu/Repos/software/RIOT/master/cpu/nrf5x_common
"make" -C /home/maribu/Repos/software/RIOT/master/cpu/cortexm_common/periph
"make" -C /home/maribu/Repos/software/RIOT/master/sys/auto_init
"make" -C /home/maribu/Repos/software/RIOT/master/sys/div
"make" -C /home/maribu/Repos/software/RIOT/master/cpu/nrf5x_common/periph
"make" -C /home/maribu/Repos/software/RIOT/master/sys/isrpipe
"make" -C /home/maribu/Repos/software/RIOT/master/sys/libc
"make" -C /home/maribu/Repos/software/RIOT/master/sys/malloc_thread_safe
"make" -C /home/maribu/Repos/software/RIOT/master/sys/newlib_syscalls_default
"make" -C /home/maribu/Repos/software/RIOT/master/sys/preprocessor
"make" -C /home/maribu/Repos/software/RIOT/master/sys/stdio_uart
"make" -C /home/maribu/Repos/software/RIOT/master/sys/test_utils/interactive_sync
"make" -C /home/maribu/Repos/software/RIOT/master/sys/test_utils/print_stack_usage
"make" -C /home/maribu/Repos/software/RIOT/master/sys/tsrb
   text	  data	   bss	   dec	   hex	filename
  10844	   124	 15004	 25972	  6574	/home/maribu/Repos/software/RIOT/master/tests/core/thread_cooperation/bin/nrf52840dk/tests_thread_cooperation.elf
/home/maribu/Repos/software/RIOT/master/dist/tools/openocd/openocd.sh flash /home/maribu/Repos/software/RIOT/master/tests/core/thread_cooperation/bin/nrf52840dk/tests_thread_cooperation.elf
### Flashing Target ###
Open On-Chip Debugger 0.12.0+dev-snapshot (2023-06-12-09:31)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
DEPRECATED! use 'adapter serial' not 'jlink serial'
swd
Info : J-Link OB-SAM3U128-V2-NordicSemi compiled Jan 20 2022 16:43:31
Info : Hardware version: 1.00
Info : VTarget = 3.300 V
Info : clock speed 1000 kHz
Info : SWD DPIDR 0x2ba01477
Info : [nrf52.cpu] Cortex-M4 r0p1 processor detected
Info : [nrf52.cpu] target has 6 breakpoints, 4 watchpoints
Info : starting gdb server for nrf52.cpu on 0
Info : Listening on port 33571 for gdb connections
    TargetName         Type       Endian TapName            State       
--  ------------------ ---------- ------ ------------------ ------------
 0* nrf52.cpu          cortex_m   little nrf52.cpu          unknown
[nrf52.cpu] halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000838 msp: 0x20000200
Info : nRF52840-xxAA(build code: D0) 1024kB Flash, 256kB RAM
Warn : Adding extra erase range, 0x00002ad8 .. 0x00002fff
auto erase enabled
wrote 10968 bytes from file /home/maribu/Repos/software/RIOT/master/tests/core/thread_cooperation/bin/nrf52840dk/tests_thread_cooperation.elf in 0.710183s (15.082 KiB/s)
verified 10968 bytes in 0.107174s (99.940 KiB/s)
shutdown command invoked
Done flashing
r
/home/maribu/Repos/software/RIOT/master/dist/tools/pyterm/pyterm -p "/dev/ttyACM1" -b "115200" --no-reconnect --noprefix --no-repeat-command-on-empty-line 
Connect to serial port /dev/ttyACM1
Welcome to pyterm!
Type '/exit' to exit.
READY
s
START
main(): This is RIOT! (Version: 2023.10-devel-433-g647a1)
[START] compute 12! (factorial).
MAIN: create thread, arg: 1
MAIN: msg to T-2
T-02: alive
T-02: got arg 1
MAIN: create thread, arg: 2
MAIN: msg to T-3
T-03: alive
T-03: got arg 2
MAIN: create thread, arg: 3
MAIN: msg to T-4
T-04: alive
T-04: got arg 3
MAIN: create thread, arg: 4
MAIN: msg to T-5
T-05: alive
T-05: got arg 4
MAIN: create thread, arg: 5
MAIN: msg to T-6
T-06: alive
T-06: got arg 5
MAIN: create thread, arg: 6
MAIN: msg to T-7
T-07: alive
T-07: got arg 6
MAIN: create thread, arg: 7
MAIN: msg to T-8
T-08: alive
T-08: got arg 7
MAIN: create thread, arg: 8
MAIN: msg to T-9
T-09: alive
T-09: got arg 8
MAIN: create thread, arg: 9
MAIN: msg to T-10
T-10: alive
T-10: got arg 9
MAIN: create thread, arg: 10
MAIN: msg to T-11
T-11: alive
T-11: got arg 10
MAIN: create thread, arg: 11
MAIN: msg to T-12
T-12: alive
T-12: got arg 11
MAIN: create thread, arg: 12
MAIN: msg to T-13
T-13: alive
T-13: got arg 12
{ "threads": [{ "name": "thread", "stack_size": 1024, "stack_used": 388 }]}
MAIN: reply from T-2
{ "threads": [{ "name": "thread", "stack_size": 1024, "stack_used": 388 }]}
MAIN: reply from T-3
{ "threads": [{ "name": "thread", "stack_size": 1024, "stack_used": 388 }]}
MAIN: reply from T-4
{ "threads": [{ "name": "thread", "stack_size": 1024, "stack_used": 388 }]}
MAIN: reply from T-5
{ "threads": [{ "name": "thread", "stack_size": 1024, "stack_used": 388 }]}
MAIN: reply from T-6
{ "threads": [{ "name": "thread", "stack_size": 1024, "stack_used": 388 }]}
MAIN: reply from T-7
{ "threads": [{ "name": "thread", "stack_size": 1024, "stack_used": 388 }]}
MAIN: reply from T-8
{ "threads": [{ "name": "thread", "stack_size": 1024, "stack_used": 388 }]}
MAIN: reply from T-9
{ "threads": [{ "name": "thread", "stack_size": 1024, "stack_used": 388 }]}
MAIN: reply from T-10
{ "threads": [{ "name": "thread", "stack_size": 1024, "stack_used": 388 }]}
MAIN: reply from T-11
{ "threads": [{ "name": "thread", "stack_size": 1024, "stack_used": 388 }]}
MAIN: reply from T-12
{ "threads": [{ "name": "thread", "stack_size": 1024, "stack_used": 388 }]}
MAIN: reply from T-13
MAIN: 12! = 479001600
[SUCCESS]
```

</details>

### Issues/PRs references

None


Co-authored-by: Marian Buschsieweke <marian.buschsieweke@posteo.net>
This commit is contained in:
bors[bot] 2023-10-26 19:04:38 +00:00 committed by GitHub
commit 7da2817d4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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]");