Skip to content

Commit 16ebc6a

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 1c04514 commit 16ebc6a

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
@@ -38,7 +38,7 @@ int g_ZSTD_threading_useless_symbol;
3838
static unsigned __stdcall worker(void *arg)
3939
{
4040
ZSTD_pthread_t* const thread = (ZSTD_pthread_t*) arg;
41-
thread->arg = thread->start_routine(thread->arg);
41+
thread->start_routine(thread->arg);
4242
return 0;
4343
}
4444

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

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

@@ -67,7 +67,6 @@ int ZSTD_pthread_join(ZSTD_pthread_t thread, void **value_ptr)
6767

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

lib/common/threading.h

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

74-
int ZSTD_pthread_join(ZSTD_pthread_t thread, void** value_ptr);
74+
int ZSTD_pthread_join(ZSTD_pthread_t thread);
7575

7676
/**
7777
* add here more wrappers as required
@@ -99,7 +99,7 @@ int ZSTD_pthread_join(ZSTD_pthread_t thread, void** value_ptr);
9999

100100
#define ZSTD_pthread_t pthread_t
101101
#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
102-
#define ZSTD_pthread_join(a, b) pthread_join((a),(b))
102+
#define ZSTD_pthread_join(a) pthread_join((a),NULL)
103103

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

@@ -124,7 +124,7 @@ int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond);
124124

125125
#define ZSTD_pthread_t pthread_t
126126
#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
127-
#define ZSTD_pthread_join(a, b) pthread_join((a),(b))
127+
#define ZSTD_pthread_join(a) pthread_join((a),NULL)
128128

129129
#endif
130130

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)