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

sys:posix:pthread added dynamic pthread thread local storage #1222

Merged
merged 1 commit into from
Mar 3, 2015

Conversation

BytesGalore
Copy link
Member

added a dynamic tls implementation to pthread.c

This is an alternative approach to #827

The impact is additional 4 bytes per pthread_thread_t when not used.
If used, 12 bytes for each key and additional 12 bytes for each tls is consumed.

@BytesGalore BytesGalore changed the title sy:posix:pthread added dynamic pthread thread local storage sys:posix:pthread added dynamic pthread thread local storage May 21, 2014
@Kijewski
Copy link
Contributor

Please note that the overhead of malloc must not be neglected.
The code needs a lot of formatting.
The destructor is not called on the thread's death.

Compare #827, which implements the feature differently.

@BytesGalore
Copy link
Member Author

Please note that the overhead of malloc must not be neglected.

absolutely. I know, this implementation is a bit naive.
I thought about to ease this problematic by pre-allocate a specific amount of memory, and reuse once allocated key storage (say for 3 keys per pthread).
But trying to predict a specific size is more or less just random and would only fit in some cases.

@OlegHahm OlegHahm added this to the POSIX compliance milestone May 22, 2014
@BytesGalore BytesGalore removed the WIP label May 23, 2014
@BytesGalore
Copy link
Member Author

ok, added destructor and cleanup calls, cleaned up formatting and rebased on current master

return -1;
}

sched_switch(sched_active_thread->priority, PRIORITY_MAIN);
sched_switch (sched_active_thread->priority, PRIORITY_MAIN);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

astyle

@Kijewski
Copy link
Contributor

Granted, your implementation looks easier on the eyes than mine. I'll give it a proper review.



/** @} */
#endif /* __SYS__POSIX__PTHREAD_TLS__H */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swap these last two lines.

@BytesGalore
Copy link
Member Author

reverted astyle code styling to apply it when finished

@Kijewski
Copy link
Contributor

Kijewski commented Jun 3, 2014

"break out iteration since unique key has been found already"

pthread_key_create() deletes the key, not only the value in the caller. For all pthreads the tls_data_t has to be free'd.

@BytesGalore
Copy link
Member Author

This is one point where I'm undecided.
The opengroup states to pthread_key_delete():
...It is the responsibility of the application to free any application storage or perform any cleanup actions for data structures related to the deleted key...

and further (only destructor function related but fitting):
Thus, they cannot be used to free thread-specific data in other threads at key deletion time

@Kijewski
Copy link
Contributor

Kijewski commented Jun 3, 2014

The key becomes invalid, non-existent. To single thread the pointer that it stored in there is lost, essentially because an invalid key cannot be used to look up the value again.

@BytesGalore
Copy link
Member Author

ok, but for pthread_key_create() it is said that:
Although the same key value may be used by different threads, the values bound to the key by pthread_setspecific() are maintained on a per-thread basis and persist for the life of the calling thread.

That's the reason why I made BytesGalore@e33c649 (tracking number of pthreads using a specific key)

@Kijewski
Copy link
Contributor

Kijewski commented Jun 3, 2014

E.g. Dietlibc interprets it like I do: https://github.com/ensc/dietlibc/blob/master/libpthread/pthread_key.c. pthread_key_delete() sets __thread_keys[key].used = 0, and pthread_setspecific() tests if __thread_keys[key].used != 0.

@BytesGalore
Copy link
Member Author

true, tracking and keeping the keys was not the expected behaviour.
But still, I have a bad feeling to free the tls_data_t of foreign pthreads.
It's never a good idea to interfere memory management of "others"

I propose to delete the key and only this thread's data, and let the __pthread_keys_exit() function free the data of all pthreads sharing the deleted key

@BytesGalore BytesGalore force-pushed the add_pthread_simple_tls branch from 3a7e57d to 148c72b Compare November 12, 2014 08:21
@BytesGalore BytesGalore force-pushed the add_pthread_simple_tls branch 4 times, most recently from 64d53ae to 7e462ff Compare November 25, 2014 09:12
@BytesGalore BytesGalore force-pushed the add_pthread_simple_tls branch from 7e462ff to e7a6f21 Compare December 1, 2014 07:48
@BytesGalore BytesGalore force-pushed the add_pthread_simple_tls branch 2 times, most recently from d274ffd to bfcc273 Compare December 12, 2014 08:19
@BytesGalore BytesGalore force-pushed the add_pthread_simple_tls branch from bfcc273 to 3d50162 Compare December 17, 2014 14:53
@LudwigKnuepfer
Copy link
Member

@OlegHahm @Kijewski was there any outcome? Apparently this is a wanted feature and in good shape.

@OlegHahm
Copy link
Member

We didn't look at it IIRC. I have nob objections against merging.

@OlegHahm
Copy link
Member

Maybe @josephnoir wants to review/ack/comment?

@BytesGalore BytesGalore force-pushed the add_pthread_simple_tls branch from 3d50162 to ae75123 Compare January 5, 2015 07:50
@OlegHahm OlegHahm added the Community: Hack'n'ACK candidate This PR is a candidate for review and discussion during one of RIOT's monthly Hack'n'ACK parties label Jan 6, 2015
@BytesGalore BytesGalore force-pushed the add_pthread_simple_tls branch from ae75123 to 855f1b2 Compare January 27, 2015 16:52
@josephnoir
Copy link
Contributor

Looked at the code, looks good and the test compiles and runs. However, I believe the test name is outdated?

@BytesGalore
Copy link
Member Author

@josephnoir like naming the test without test prefix right?

@BytesGalore BytesGalore added the CI: needs squashing Commits in this PR need to be squashed; If set, CI systems will mark this PR as unmergable label Jan 27, 2015
@BytesGalore
Copy link
Member Author

@josephnoir renamed the test, will squash when travis succeeds

@BytesGalore BytesGalore force-pushed the add_pthread_simple_tls branch from 5379e1f to 6e90ad6 Compare January 27, 2015 22:08
@BytesGalore
Copy link
Member Author

squashed

@BytesGalore BytesGalore removed the CI: needs squashing Commits in this PR need to be squashed; If set, CI systems will mark this PR as unmergable label Jan 27, 2015
@Kijewski Kijewski removed their assignment Jan 28, 2015
@OlegHahm
Copy link
Member

OlegHahm commented Feb 6, 2015

@ALL: please do not remove your assignment without reassigning - if you don't know (or don't care) who to assign, just pick me (or whoever comes first).

josephnoir added a commit that referenced this pull request Mar 3, 2015
Everything seems to work, so here we go: sys:posix:pthread added dynamic pthread thread local storage
@josephnoir josephnoir merged commit f614d4b into RIOT-OS:master Mar 3, 2015
@BytesGalore BytesGalore deleted the add_pthread_simple_tls branch April 27, 2015 06:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: POSIX Area: POSIX API wrapper Community: Hack'n'ACK candidate This PR is a candidate for review and discussion during one of RIOT's monthly Hack'n'ACK parties Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants