-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Windows MT layer bug fixes #3364
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
500f02e
Fixes two bugs in the Windows thread / pthread translation layer
yoniko ec42c92
Fix race condition in the Windows thread / pthread translation layer
yoniko aaa38b2
meson: zstreamtests should now pass on Windows
yoniko 26f1bf7
CR fixes
yoniko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,39 +34,92 @@ int g_ZSTD_threading_useless_symbol; | |
|
|
||
| /* === Implementation === */ | ||
|
|
||
| typedef struct { | ||
| void* (*start_routine)(void*); | ||
| void* arg; | ||
| int initialized; | ||
| ZSTD_pthread_cond_t initialized_cond; | ||
| ZSTD_pthread_mutex_t initialized_mutex; | ||
| } ZSTD_thread_params_t; | ||
|
|
||
| static unsigned __stdcall worker(void *arg) | ||
| { | ||
| ZSTD_pthread_t* const thread = (ZSTD_pthread_t*) arg; | ||
| thread->arg = thread->start_routine(thread->arg); | ||
| void* (*start_routine)(void*); | ||
| void* thread_arg; | ||
|
|
||
| /* Inialized thread_arg and start_routine and signal main thread that we don't need it | ||
| * to wait any longer. | ||
| */ | ||
| { | ||
| ZSTD_thread_params_t* thread_param = (ZSTD_thread_params_t*)arg; | ||
| thread_arg = thread_param->arg; | ||
| start_routine = thread_param->start_routine; | ||
|
|
||
| /* Signal main thread that we are running and do not depend on its memory anymore */ | ||
| ZSTD_pthread_mutex_lock(&thread_param->initialized_mutex); | ||
| thread_param->initialized = 1; | ||
| ZSTD_pthread_cond_signal(&thread_param->initialized_cond); | ||
| ZSTD_pthread_mutex_unlock(&thread_param->initialized_mutex); | ||
| } | ||
|
|
||
| start_routine(thread_arg); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused, | ||
| void* (*start_routine) (void*), void* arg) | ||
| { | ||
| ZSTD_thread_params_t thread_param; | ||
| (void)unused; | ||
| thread->arg = arg; | ||
| thread->start_routine = start_routine; | ||
| thread->handle = (HANDLE) _beginthreadex(NULL, 0, worker, thread, 0, NULL); | ||
|
|
||
| if (!thread->handle) | ||
| thread_param.start_routine = start_routine; | ||
| thread_param.arg = arg; | ||
| thread_param.initialized = 0; | ||
| *thread = NULL; | ||
|
|
||
terrelln marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /* Setup thread initialization synchronization */ | ||
| if(ZSTD_pthread_cond_init(&thread_param.initialized_cond, NULL)) { | ||
| /* Should never happen on Windows */ | ||
| return -1; | ||
| } | ||
| if(ZSTD_pthread_mutex_init(&thread_param.initialized_mutex, NULL)) { | ||
| /* Should never happen on Windows */ | ||
| ZSTD_pthread_cond_destroy(&thread_param.initialized_cond); | ||
| return -1; | ||
| } | ||
|
|
||
| /* Spawn thread */ | ||
| *thread = (HANDLE)_beginthreadex(NULL, 0, worker, &thread_param, 0, NULL); | ||
| if (!thread) { | ||
| ZSTD_pthread_mutex_destroy(&thread_param.initialized_mutex); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also need to destroy the condition variable. |
||
| ZSTD_pthread_cond_destroy(&thread_param.initialized_cond); | ||
| return errno; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to unlock (until it is moved below the thread creation), and destroy the mutex/cond in this case. |
||
| else | ||
| return 0; | ||
| } | ||
|
|
||
| /* Wait for thread to be initialized */ | ||
| ZSTD_pthread_mutex_lock(&thread_param.initialized_mutex); | ||
| while(!thread_param.initialized) { | ||
| ZSTD_pthread_cond_wait(&thread_param.initialized_cond, &thread_param.initialized_mutex); | ||
| } | ||
| ZSTD_pthread_mutex_unlock(&thread_param.initialized_mutex); | ||
| ZSTD_pthread_mutex_destroy(&thread_param.initialized_mutex); | ||
| ZSTD_pthread_cond_destroy(&thread_param.initialized_cond); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| int ZSTD_pthread_join(ZSTD_pthread_t thread, void **value_ptr) | ||
| int ZSTD_pthread_join(ZSTD_pthread_t thread) | ||
| { | ||
| DWORD result; | ||
|
|
||
| if (!thread.handle) return 0; | ||
| if (!thread) return 0; | ||
|
|
||
| result = WaitForSingleObject(thread.handle, INFINITE); | ||
| CloseHandle(thread.handle); | ||
| result = WaitForSingleObject(thread, INFINITE); | ||
| CloseHandle(thread); | ||
|
|
||
| switch (result) { | ||
| case WAIT_OBJECT_0: | ||
| if (value_ptr) *value_ptr = thread.arg; | ||
| return 0; | ||
| case WAIT_ABANDONED: | ||
| return EINVAL; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.