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

alloc: properly allocate memory handler per thread #2099

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bouk
Copy link

@bouk bouk commented Apr 10, 2020

Right now the memory handler is allocated once, for the first thread
that happens to call it. If pthread_getspecific is then called from
another thread, the memory handler is not allocated (because
pthread_once is only called for all threads, not per thread) and jq
aborts because it thinks memory allocation failed.

What we need to do instead is call pthread_key_create from the
initializer, and then allocate a memory handler for every thread when it
sees there's none yet.

I also removed the atexit stuff, and instead use the destructor
function you can pass into pthread_key_create.

I figured this out by looking at this example: https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahw/rzahwe21rx.htm and the pthread docs

Right now the memory handler is allocated once, for the first thread
that happens to call it. If pthread_getspecific is then called from
another thread, the memory handler is not allocated (because
pthread_once is only called for *all* threads, not per thread) and jq
aborts because it thinks memory allocation failed.

What we need to do instead is call pthread_key_create from the
initializer, and then allocate a memory handler for every thread when it
sees there's none yet.

I also removed the atexit stuff, and instead use the destructor
function you can pass into pthread_key_create.
@coveralls
Copy link

Coverage Status

Coverage increased (+0.4%) to 84.517% when pulling 0be689b on bouk:fix-threads into ccc79e5 on stedolan:master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants