2 * Copyright (c) 2016 Tino Reichardt
5 * You can contact the author at:
6 * - zstdmt source repository: https://github.com/mcmilk/zstdmt
8 * This source code is licensed under both the BSD-style license (found in the
9 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
10 * in the COPYING file in the root directory of this source tree).
11 * You may select, at your option, one of the above-listed licenses.
15 * This file will hold wrapper for systems, which do not support pthreads
18 #include "threading.h"
20 /* create fake symbol to avoid empty translation unit warning */
21 int g_ZSTD_threading_useless_symbol;
23 #if defined(ZSTD_MULTITHREAD) && defined(_WIN32)
26 * Windows minimalist Pthread Wrapper
30 /* === Dependencies === */
35 /* === Implementation === */
38 void* (*start_routine)(void*);
41 ZSTD_pthread_cond_t initialized_cond;
42 ZSTD_pthread_mutex_t initialized_mutex;
43 } ZSTD_thread_params_t;
45 static unsigned __stdcall worker(void *arg)
47 void* (*start_routine)(void*);
50 /* Initialized thread_arg and start_routine and signal main thread that we don't need it
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;
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);
65 start_routine(thread_arg);
70 int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,
71 void* (*start_routine) (void*), void* arg)
73 ZSTD_thread_params_t thread_param;
76 if (thread==NULL) return -1;
79 thread_param.start_routine = start_routine;
80 thread_param.arg = arg;
81 thread_param.initialized = 0;
83 /* Setup thread initialization synchronization */
84 if(ZSTD_pthread_cond_init(&thread_param.initialized_cond, NULL)) {
85 /* Should never happen on Windows */
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);
95 *thread = (HANDLE)_beginthreadex(NULL, 0, worker, &thread_param, 0, NULL);
97 ZSTD_pthread_mutex_destroy(&thread_param.initialized_mutex);
98 ZSTD_pthread_cond_destroy(&thread_param.initialized_cond);
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);
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);
114 int ZSTD_pthread_join(ZSTD_pthread_t thread)
118 if (!thread) return 0;
120 result = WaitForSingleObject(thread, INFINITE);
129 return GetLastError();
133 #endif /* ZSTD_MULTITHREAD */
135 #if defined(ZSTD_MULTITHREAD) && DEBUGLEVEL >= 1 && !defined(_WIN32)
137 #define ZSTD_DEPS_NEED_MALLOC
138 #include "zstd_deps.h"
140 int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr)
142 assert(mutex != NULL);
143 *mutex = (pthread_mutex_t*)ZSTD_malloc(sizeof(pthread_mutex_t));
146 return pthread_mutex_init(*mutex, attr);
149 int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex)
151 assert(mutex != NULL);
155 int const ret = pthread_mutex_destroy(*mutex);
161 int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr)
163 assert(cond != NULL);
164 *cond = (pthread_cond_t*)ZSTD_malloc(sizeof(pthread_cond_t));
167 return pthread_cond_init(*cond, attr);
170 int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond)
172 assert(cond != NULL);
176 int const ret = pthread_cond_destroy(*cond);