Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

newlib threadsafe: Control allocation through thread flag #10

Open
wants to merge 5 commits into
base: pr/newlib_thread_safe
Choose a base branch
from

Conversation

jnohlgard
Copy link

Example proof of concept for introducing a thread flag for allocating a thread local reentrancy struct.

See tests/thread_msg for an example printout of thread local vs. global reent pointers.
Threads main and nr3 have thread local reents, threads nr1, nr2 use the global reent struct.

@jnohlgard
Copy link
Author

Example output, note that the stack usage on the idle thread has not increased because it uses the global reent struct (__global_impure_ptr). However, all threads which have per thread allocated reentrancy has increased their stack usage by 116 bytes.

2018-02-27 15:31:02,925 - INFO # main(): This is RIOT! (Version: 2018.04-devel-395-gd87ed7-cheesecake-spr/newlib-threadsafe)
2018-02-27 15:31:02,926 - INFO # PID    2 reent: 0x1fff0d40
2018-02-27 15:31:02,928 - INFO # 	pid | name                 | state    Q | pri | stack  ( used) | base addr  | current     
2018-02-27 15:31:02,941 - INFO # 	  - | isr_stack            | -        - |   - |    512 (  184) | 0x1fff0000 | 0x1fff01c8
2018-02-27 15:31:02,942 - INFO # 	  1 | idle                 | pending  Q |  15 |    384 (  124) | 0x1fff05d8 | 0x1fff06dc 
2018-02-27 15:31:02,957 - INFO # 	  2 | main                 | running  Q |   7 |   1664 (  572) | 0x1fff0758 | 0x1fff0cf4 
2018-02-27 15:31:02,958 - INFO # 	    | SUM                  |            |     |   2560 (  880)
2018-02-27 15:31:02,958 - INFO # THREADS CREATED
2018-02-27 15:31:02,958 - INFO # 
2018-02-27 15:31:02,973 - INFO # 	pid | name                 | state    Q | pri | stack  ( used) | base addr  | current     
2018-02-27 15:31:02,975 - INFO # 	  - | isr_stack            | -        - |   - |    512 (  184) | 0x1fff0000 | 0x1fff01c8
2018-02-27 15:31:02,989 - INFO # 	  1 | idle                 | pending  Q |  15 |    384 (  124) | 0x1fff05d8 | 0x1fff06dc 
2018-02-27 15:31:02,990 - INFO # 	  2 | main                 | running  Q |   7 |   1664 (  572) | 0x1fff0758 | 0x1fff0cf4 
2018-02-27 15:31:03,005 - INFO # 	  3 | nr1                  | pending  Q |   6 |   1664 (  128) | 0x1fff1074 | 0x1fff1674 
2018-02-27 15:31:03,007 - INFO # 	  4 | nr2                  | pending  Q |   6 |   1664 (  128) | 0x1fff16f4 | 0x1fff1cf4 
2018-02-27 15:31:03,021 - INFO # 	  5 | nr3                  | pending  Q |   6 |   1664 (  224) | 0x1fff1d74 | 0x1fff2314 
2018-02-27 15:31:03,021 - INFO # 	    | SUM                  |            |     |   7552 ( 1360)
2018-02-27 15:31:03,022 - INFO # THREAD 1 start
2018-02-27 15:31:03,022 - INFO # 
2018-02-27 15:31:03,022 - INFO # PID    3 reent: 0x1fff02a8
2018-02-27 15:31:03,023 - INFO # THREAD 2 start
2018-02-27 15:31:03,023 - INFO # 
2018-02-27 15:31:03,036 - INFO # PID    4 reent: 0x1fff02a8
2018-02-27 15:31:03,037 - INFO # THREAD 3 start
2018-02-27 15:31:03,037 - INFO # 
2018-02-27 15:31:03,037 - INFO # PID    5 reent: 0x1fff235c
2018-02-27 15:31:03,037 - INFO # T3 i=0
2018-02-27 15:31:03,037 - INFO # T3 i=1
2018-02-27 15:31:03,037 - INFO # T1 recv: 0(i=0)
2018-02-27 15:31:03,038 - INFO # T2 got 0 (i=0)
2018-02-27 15:31:03,038 - INFO # T1 got reply: 0 (i=0)
2018-02-27 15:31:03,038 - INFO # T1 recv: 1(i=1)
2018-02-27 15:31:03,038 - INFO # T3 i=2
2018-02-27 15:31:03,039 - INFO # T2 got 1 (i=1)
2018-02-27 15:31:03,039 - INFO # T1 got reply: 1 (i=1)
2018-02-27 15:31:03,052 - INFO # T1 recv: 2(i=2)
2018-02-27 15:31:03,053 - INFO # T3 i=3
2018-02-27 15:31:03,053 - INFO # T2 got 2 (i=2)
2018-02-27 15:31:03,053 - INFO # T1 got reply: 2 (i=2)
2018-02-27 15:31:03,053 - INFO # THREAD 1 end
2018-02-27 15:31:03,053 - INFO # 
2018-02-27 15:31:03,054 - INFO # SUCCESS

@vincent-d
Copy link
Member

Looks good.

I'm just wondering if __global_impure_ptr is meant to be used like this. But I can't find any useful documentation on this...

@jnohlgard
Copy link
Author

I'm not sure either. I am also not sure which reent will be used from interrupt context, I assume it will be whatever thread was active when the interrupt happened.

@jnohlgard
Copy link
Author

I have tested this very little, just wrote it to prove to myself that it would work like this. The documentation on the internals of newlib are pretty scarce.

@vincent-d vincent-d force-pushed the pr/newlib_thread_safe branch from d05c147 to 3d2318d Compare December 21, 2018 10:29
@vincent-d vincent-d force-pushed the pr/newlib_thread_safe branch from 2a31cbf to 8b43d5b Compare August 21, 2019 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants