Skip to content

Commit 870e63c

Browse files
committed
Fixes two bugs in the Windows thread / pthread translation layer
1. If threads are resized the threads' `ZSTD_pthread_t` might move while the worker still holds a pointer into it (see more details in facebook#3120). 2. The join operation was waiting for a thread and then return its `thread.arg` as a return value, but since the `ZSTD_pthread_t thread` was passed by value it would have a stale `arg` that wouldn't match the thread's actual return value. This fix changes the `ZSTD_pthread_join` API and removes support for returning a value. This means that we are diverging from the `pthread_join` API and this is no longer just an alias. In the future, if needed, we could return a Windows thread's return value using `GetExitCodeThread`, but as this path wouldn't be excised in any case, it's preferable to not add it right now.
1 parent 887d06a commit 870e63c

File tree

4 files changed

+8
-9
lines changed

4 files changed

+8
-9
lines changed

lib/common/pool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ static void POOL_join(POOL_ctx* ctx) {
173173
/* Join all of the threads */
174174
{ size_t i;
175175
for (i = 0; i < ctx->threadCapacity; ++i) {
176-
ZSTD_pthread_join(ctx->threads[i], NULL); /* note : could fail */
176+
ZSTD_pthread_join(ctx->threads[i]); /* note : could fail */
177177
} }
178178
}
179179

lib/common/threading.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ int g_ZSTD_threading_useless_symbol;
3737
static unsigned __stdcall worker(void *arg)
3838
{
3939
ZSTD_pthread_t* const thread = (ZSTD_pthread_t*) arg;
40-
thread->arg = thread->start_routine(thread->arg);
40+
thread->start_routine(thread->arg);
4141
return 0;
4242
}
4343

@@ -55,7 +55,7 @@ int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,
5555
return 0;
5656
}
5757

58-
int ZSTD_pthread_join(ZSTD_pthread_t thread, void **value_ptr)
58+
int ZSTD_pthread_join(ZSTD_pthread_t thread)
5959
{
6060
DWORD result;
6161

@@ -66,7 +66,6 @@ int ZSTD_pthread_join(ZSTD_pthread_t thread, void **value_ptr)
6666

6767
switch (result) {
6868
case WAIT_OBJECT_0:
69-
if (value_ptr) *value_ptr = thread.arg;
7069
return 0;
7170
case WAIT_ABANDONED:
7271
return EINVAL;

lib/common/threading.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ typedef struct {
7070
int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,
7171
void* (*start_routine) (void*), void* arg);
7272

73-
int ZSTD_pthread_join(ZSTD_pthread_t thread, void** value_ptr);
73+
int ZSTD_pthread_join(ZSTD_pthread_t thread);
7474

7575
/**
7676
* add here more wrappers as required
@@ -98,7 +98,7 @@ int ZSTD_pthread_join(ZSTD_pthread_t thread, void** value_ptr);
9898

9999
#define ZSTD_pthread_t pthread_t
100100
#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
101-
#define ZSTD_pthread_join(a, b) pthread_join((a),(b))
101+
#define ZSTD_pthread_join(a) pthread_join((a),NULL)
102102

103103
#else /* DEBUGLEVEL >= 1 */
104104

@@ -123,7 +123,7 @@ int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond);
123123

124124
#define ZSTD_pthread_t pthread_t
125125
#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
126-
#define ZSTD_pthread_join(a, b) pthread_join((a),(b))
126+
#define ZSTD_pthread_join(a) pthread_join((a),NULL)
127127

128128
#endif
129129

tests/fuzzer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ static int threadPoolTests(void) {
429429

430430
ZSTD_pthread_create(&t1, NULL, threadPoolTests_compressionJob, &p1);
431431
ZSTD_pthread_create(&t2, NULL, threadPoolTests_compressionJob, &p2);
432-
ZSTD_pthread_join(t1, NULL);
433-
ZSTD_pthread_join(t2, NULL);
432+
ZSTD_pthread_join(t1);
433+
ZSTD_pthread_join(t2);
434434

435435
assert(!memcmp(decodedBuffer, decodedBuffer2, CNBuffSize));
436436
free(decodedBuffer2);

0 commit comments

Comments
 (0)