1 | /* |
2 | * Copyright 2016-present Facebook, Inc. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | #pragma once |
18 | |
19 | #include <folly/portability/Config.h> |
20 | |
21 | #if !defined(_WIN32) |
22 | |
23 | #include <pthread.h> |
24 | |
25 | #elif !FOLLY_HAVE_PTHREAD |
26 | |
27 | #include <cstdint> |
28 | #include <memory> |
29 | |
30 | #include <folly/portability/Sched.h> |
31 | #include <folly/portability/Time.h> |
32 | #include <folly/portability/Windows.h> |
33 | |
34 | #define PTHREAD_CREATE_JOINABLE 0 |
35 | #define PTHREAD_CREATE_DETACHED 1 |
36 | |
37 | #define PTHREAD_MUTEX_NORMAL 0 |
38 | #define PTHREAD_MUTEX_RECURSIVE 1 |
39 | #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL |
40 | |
41 | #define _POSIX_TIMEOUTS 200112L |
42 | |
43 | namespace folly { |
44 | namespace portability { |
45 | namespace pthread { |
46 | struct pthread_attr_t { |
47 | size_t stackSize; |
48 | bool detached; |
49 | }; |
50 | |
51 | int pthread_attr_init(pthread_attr_t* attr); |
52 | int pthread_attr_setdetachstate(pthread_attr_t* attr, int state); |
53 | int pthread_attr_setstacksize(pthread_attr_t* attr, size_t kb); |
54 | |
55 | namespace pthread_detail { |
56 | struct pthread_t { |
57 | HANDLE handle{INVALID_HANDLE_VALUE}; |
58 | DWORD threadID{0}; |
59 | bool detached{false}; |
60 | |
61 | ~pthread_t() noexcept; |
62 | }; |
63 | } // namespace pthread_detail |
64 | using pthread_t = std::shared_ptr<pthread_detail::pthread_t>; |
65 | |
66 | int pthread_equal(pthread_t threadA, pthread_t threadB); |
67 | int pthread_create( |
68 | pthread_t* thread, |
69 | const pthread_attr_t* attr, |
70 | void* (*start_routine)(void*), |
71 | void* arg); |
72 | pthread_t pthread_self(); |
73 | int pthread_join(pthread_t thread, void** exitCode); |
74 | |
75 | HANDLE pthread_getw32threadhandle_np(pthread_t thread); |
76 | DWORD pthread_getw32threadid_np(pthread_t thread); |
77 | |
78 | int pthread_setschedparam( |
79 | pthread_t thread, |
80 | int policy, |
81 | const sched_param* param); |
82 | |
83 | struct pthread_mutexattr_t { |
84 | int type; |
85 | }; |
86 | int pthread_mutexattr_init(pthread_mutexattr_t* attr); |
87 | int pthread_mutexattr_destroy(pthread_mutexattr_t* attr); |
88 | int pthread_mutexattr_settype(pthread_mutexattr_t* attr, int type); |
89 | |
90 | using pthread_mutex_t = struct pthread_mutex_t_*; |
91 | int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr); |
92 | int pthread_mutex_destroy(pthread_mutex_t* mutex); |
93 | int pthread_mutex_lock(pthread_mutex_t* mutex); |
94 | int pthread_mutex_trylock(pthread_mutex_t* mutex); |
95 | int pthread_mutex_unlock(pthread_mutex_t* mutex); |
96 | int pthread_mutex_timedlock( |
97 | pthread_mutex_t* mutex, |
98 | const timespec* abs_timeout); |
99 | |
100 | using pthread_rwlock_t = struct pthread_rwlock_t_*; |
101 | // Technically the second argument here is supposed to be a |
102 | // const pthread_rwlockattr_t* but we don support it, so we |
103 | // simply don't define pthread_rwlockattr_t at all to cause |
104 | // a build-break if anyone tries to use it. |
105 | int pthread_rwlock_init(pthread_rwlock_t* rwlock, const void* attr); |
106 | int pthread_rwlock_destroy(pthread_rwlock_t* rwlock); |
107 | int pthread_rwlock_rdlock(pthread_rwlock_t* rwlock); |
108 | int pthread_rwlock_tryrdlock(pthread_rwlock_t* rwlock); |
109 | int pthread_rwlock_timedrdlock( |
110 | pthread_rwlock_t* rwlock, |
111 | const timespec* abs_timeout); |
112 | int pthread_rwlock_wrlock(pthread_rwlock_t* rwlock); |
113 | int pthread_rwlock_trywrlock(pthread_rwlock_t* rwlock); |
114 | int pthread_rwlock_timedwrlock( |
115 | pthread_rwlock_t* rwlock, |
116 | const timespec* abs_timeout); |
117 | int pthread_rwlock_unlock(pthread_rwlock_t* rwlock); |
118 | |
119 | using pthread_cond_t = struct pthread_cond_t_*; |
120 | // Once again, technically the second argument should be a |
121 | // pthread_condattr_t, but we don't implement it, so void* |
122 | // it is. |
123 | int pthread_cond_init(pthread_cond_t* cond, const void* attr); |
124 | int pthread_cond_destroy(pthread_cond_t* cond); |
125 | int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex); |
126 | int pthread_cond_timedwait( |
127 | pthread_cond_t* cond, |
128 | pthread_mutex_t* mutex, |
129 | const timespec* abstime); |
130 | int pthread_cond_signal(pthread_cond_t* cond); |
131 | int pthread_cond_broadcast(pthread_cond_t* cond); |
132 | |
133 | // In reality, this is boost::thread_specific_ptr*, but we're attempting |
134 | // to avoid introducing boost into a portability header. |
135 | using pthread_key_t = void*; |
136 | |
137 | int pthread_key_create(pthread_key_t* key, void (*destructor)(void*)); |
138 | int pthread_key_delete(pthread_key_t key); |
139 | void* pthread_getspecific(pthread_key_t key); |
140 | int pthread_setspecific(pthread_key_t key, const void* value); |
141 | } // namespace pthread |
142 | } // namespace portability |
143 | } // namespace folly |
144 | |
145 | /* using override */ using namespace folly::portability::pthread; |
146 | #endif |
147 | |