]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/zstd/lib/common/threading.c
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304460, and update
[FreeBSD/FreeBSD.git] / contrib / zstd / lib / common / threading.c
1
2 /**
3  * Copyright (c) 2016 Tino Reichardt
4  * All rights reserved.
5  *
6  * This source code is licensed under the BSD-style license found in the
7  * LICENSE file in the root directory of this source tree. An additional grant
8  * of patent rights can be found in the PATENTS file in the same directory.
9  *
10  * You can contact the author at:
11  * - zstdmt source repository: https://github.com/mcmilk/zstdmt
12  */
13
14 /**
15  * This file will hold wrapper for systems, which do not support pthreads
16  */
17
18 /* When ZSTD_MULTITHREAD is not defined, this file would become an empty translation unit.
19 * Include some ISO C header code to prevent this and portably avoid related warnings.
20 * (Visual C++: C4206 / GCC: -Wpedantic / Clang: -Wempty-translation-unit)
21 */
22 #include <stddef.h>
23
24
25 #if defined(ZSTD_MULTITHREAD) && defined(_WIN32)
26
27 /**
28  * Windows minimalist Pthread Wrapper, based on :
29  * http://www.cse.wustl.edu/~schmidt/win32-cv-1.html
30  */
31
32
33 /* ===  Dependencies  === */
34 #include <process.h>
35 #include <errno.h>
36 #include "threading.h"
37
38
39 /* ===  Implementation  === */
40
41 static unsigned __stdcall worker(void *arg)
42 {
43     pthread_t* const thread = (pthread_t*) arg;
44     thread->arg = thread->start_routine(thread->arg);
45     return 0;
46 }
47
48 int pthread_create(pthread_t* thread, const void* unused,
49             void* (*start_routine) (void*), void* arg)
50 {
51     (void)unused;
52     thread->arg = arg;
53     thread->start_routine = start_routine;
54     thread->handle = (HANDLE) _beginthreadex(NULL, 0, worker, thread, 0, NULL);
55
56     if (!thread->handle)
57         return errno;
58     else
59         return 0;
60 }
61
62 int _pthread_join(pthread_t * thread, void **value_ptr)
63 {
64     DWORD result;
65
66     if (!thread->handle) return 0;
67
68     result = WaitForSingleObject(thread->handle, INFINITE);
69     switch (result) {
70     case WAIT_OBJECT_0:
71         if (value_ptr) *value_ptr = thread->arg;
72         return 0;
73     case WAIT_ABANDONED:
74         return EINVAL;
75     default:
76         return GetLastError();
77     }
78 }
79
80 #endif   /* ZSTD_MULTITHREAD */