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
43namespace folly {
44namespace portability {
45namespace pthread {
46struct pthread_attr_t {
47 size_t stackSize;
48 bool detached;
49};
50
51int pthread_attr_init(pthread_attr_t* attr);
52int pthread_attr_setdetachstate(pthread_attr_t* attr, int state);
53int pthread_attr_setstacksize(pthread_attr_t* attr, size_t kb);
54
55namespace pthread_detail {
56struct 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
64using pthread_t = std::shared_ptr<pthread_detail::pthread_t>;
65
66int pthread_equal(pthread_t threadA, pthread_t threadB);
67int pthread_create(
68 pthread_t* thread,
69 const pthread_attr_t* attr,
70 void* (*start_routine)(void*),
71 void* arg);
72pthread_t pthread_self();
73int pthread_join(pthread_t thread, void** exitCode);
74
75HANDLE pthread_getw32threadhandle_np(pthread_t thread);
76DWORD pthread_getw32threadid_np(pthread_t thread);
77
78int pthread_setschedparam(
79 pthread_t thread,
80 int policy,
81 const sched_param* param);
82
83struct pthread_mutexattr_t {
84 int type;
85};
86int pthread_mutexattr_init(pthread_mutexattr_t* attr);
87int pthread_mutexattr_destroy(pthread_mutexattr_t* attr);
88int pthread_mutexattr_settype(pthread_mutexattr_t* attr, int type);
89
90using pthread_mutex_t = struct pthread_mutex_t_*;
91int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr);
92int pthread_mutex_destroy(pthread_mutex_t* mutex);
93int pthread_mutex_lock(pthread_mutex_t* mutex);
94int pthread_mutex_trylock(pthread_mutex_t* mutex);
95int pthread_mutex_unlock(pthread_mutex_t* mutex);
96int pthread_mutex_timedlock(
97 pthread_mutex_t* mutex,
98 const timespec* abs_timeout);
99
100using 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.
105int pthread_rwlock_init(pthread_rwlock_t* rwlock, const void* attr);
106int pthread_rwlock_destroy(pthread_rwlock_t* rwlock);
107int pthread_rwlock_rdlock(pthread_rwlock_t* rwlock);
108int pthread_rwlock_tryrdlock(pthread_rwlock_t* rwlock);
109int pthread_rwlock_timedrdlock(
110 pthread_rwlock_t* rwlock,
111 const timespec* abs_timeout);
112int pthread_rwlock_wrlock(pthread_rwlock_t* rwlock);
113int pthread_rwlock_trywrlock(pthread_rwlock_t* rwlock);
114int pthread_rwlock_timedwrlock(
115 pthread_rwlock_t* rwlock,
116 const timespec* abs_timeout);
117int pthread_rwlock_unlock(pthread_rwlock_t* rwlock);
118
119using 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.
123int pthread_cond_init(pthread_cond_t* cond, const void* attr);
124int pthread_cond_destroy(pthread_cond_t* cond);
125int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex);
126int pthread_cond_timedwait(
127 pthread_cond_t* cond,
128 pthread_mutex_t* mutex,
129 const timespec* abstime);
130int pthread_cond_signal(pthread_cond_t* cond);
131int 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.
135using pthread_key_t = void*;
136
137int pthread_key_create(pthread_key_t* key, void (*destructor)(void*));
138int pthread_key_delete(pthread_key_t key);
139void* pthread_getspecific(pthread_key_t key);
140int 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