Project Alice
Loading...
Searching...
No Matches
threading.c
Go to the documentation of this file.
1
18#include "threading.h"
19
20/* create fake symbol to avoid empty translation unit warning */
22
23#if defined(ZSTD_MULTITHREAD) && defined(_WIN32)
24
30/* === Dependencies === */
31#include <process.h>
32#include <errno.h>
33
34
35/* === Implementation === */
36
37typedef struct {
38 void* (*start_routine)(void*);
39 void* arg;
40 int initialized;
41 ZSTD_pthread_cond_t initialized_cond;
42 ZSTD_pthread_mutex_t initialized_mutex;
43} ZSTD_thread_params_t;
44
45static unsigned __stdcall worker(void *arg)
46{
47 void* (*start_routine)(void*);
48 void* thread_arg;
49
50 /* Initialized thread_arg and start_routine and signal main thread that we don't need it
51 * to wait any longer.
52 */
53 {
54 ZSTD_thread_params_t* thread_param = (ZSTD_thread_params_t*)arg;
55 thread_arg = thread_param->arg;
56 start_routine = thread_param->start_routine;
57
58 /* Signal main thread that we are running and do not depend on its memory anymore */
59 ZSTD_pthread_mutex_lock(&thread_param->initialized_mutex);
60 thread_param->initialized = 1;
61 ZSTD_pthread_cond_signal(&thread_param->initialized_cond);
62 ZSTD_pthread_mutex_unlock(&thread_param->initialized_mutex);
63 }
64
65 start_routine(thread_arg);
66
67 return 0;
68}
69
70int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,
71 void* (*start_routine) (void*), void* arg)
72{
73 ZSTD_thread_params_t thread_param;
74 (void)unused;
75
76 if (thread==NULL) return -1;
77 *thread = NULL;
78
79 thread_param.start_routine = start_routine;
80 thread_param.arg = arg;
81 thread_param.initialized = 0;
82
83 /* Setup thread initialization synchronization */
84 if(ZSTD_pthread_cond_init(&thread_param.initialized_cond, NULL)) {
85 /* Should never happen on Windows */
86 return -1;
87 }
88 if(ZSTD_pthread_mutex_init(&thread_param.initialized_mutex, NULL)) {
89 /* Should never happen on Windows */
90 ZSTD_pthread_cond_destroy(&thread_param.initialized_cond);
91 return -1;
92 }
93
94 /* Spawn thread */
95 *thread = (HANDLE)_beginthreadex(NULL, 0, worker, &thread_param, 0, NULL);
96 if (*thread==NULL) {
97 ZSTD_pthread_mutex_destroy(&thread_param.initialized_mutex);
98 ZSTD_pthread_cond_destroy(&thread_param.initialized_cond);
99 return errno;
100 }
101
102 /* Wait for thread to be initialized */
103 ZSTD_pthread_mutex_lock(&thread_param.initialized_mutex);
104 while(!thread_param.initialized) {
105 ZSTD_pthread_cond_wait(&thread_param.initialized_cond, &thread_param.initialized_mutex);
106 }
107 ZSTD_pthread_mutex_unlock(&thread_param.initialized_mutex);
108 ZSTD_pthread_mutex_destroy(&thread_param.initialized_mutex);
109 ZSTD_pthread_cond_destroy(&thread_param.initialized_cond);
110
111 return 0;
112}
113
114int ZSTD_pthread_join(ZSTD_pthread_t thread)
115{
116 DWORD result;
117
118 if (!thread) return 0;
119
120 result = WaitForSingleObject(thread, INFINITE);
121 CloseHandle(thread);
122
123 switch (result) {
124 case WAIT_OBJECT_0:
125 return 0;
126 case WAIT_ABANDONED:
127 return EINVAL;
128 default:
129 return GetLastError();
130 }
131}
132
133#endif /* ZSTD_MULTITHREAD */
134
135#if defined(ZSTD_MULTITHREAD) && DEBUGLEVEL >= 1 && !defined(_WIN32)
136
137#define ZSTD_DEPS_NEED_MALLOC
138#include "zstd_deps.h"
139
140int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr)
141{
142 assert(mutex != NULL);
143 *mutex = (pthread_mutex_t*)ZSTD_malloc(sizeof(pthread_mutex_t));
144 if (!*mutex)
145 return 1;
146 return pthread_mutex_init(*mutex, attr);
147}
148
150{
151 assert(mutex != NULL);
152 if (!*mutex)
153 return 0;
154 {
155 int const ret = pthread_mutex_destroy(*mutex);
156 ZSTD_free(*mutex);
157 return ret;
158 }
159}
160
161int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr)
162{
163 assert(cond != NULL);
164 *cond = (pthread_cond_t*)ZSTD_malloc(sizeof(pthread_cond_t));
165 if (!*cond)
166 return 1;
167 return pthread_cond_init(*cond, attr);
168}
169
171{
172 assert(cond != NULL);
173 if (!*cond)
174 return 0;
175 {
176 int const ret = pthread_cond_destroy(*cond);
177 ZSTD_free(*cond);
178 return ret;
179 }
180}
181
182#endif
#define assert(condition)
Definition: debug.h:74
int g_ZSTD_threading_useless_symbol
Definition: threading.c:21
#define ZSTD_pthread_mutex_init(a, b)
Definition: threading.h:130
int ZSTD_pthread_mutex_t
Definition: threading.h:129
int ZSTD_pthread_cond_t
Definition: threading.h:135
#define ZSTD_pthread_mutex_unlock(a)
Definition: threading.h:133
#define ZSTD_pthread_cond_signal(a)
Definition: threading.h:139
#define ZSTD_pthread_cond_wait(a, b)
Definition: threading.h:138
#define ZSTD_pthread_mutex_lock(a)
Definition: threading.h:132
#define ZSTD_pthread_mutex_destroy(a)
Definition: threading.h:131
#define ZSTD_pthread_cond_init(a, b)
Definition: threading.h:136
#define ZSTD_pthread_cond_destroy(a)
Definition: threading.h:137