PThread.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #pragma once
  17. #include <folly/portability/Config.h>
  18. #if !defined(_WIN32)
  19. #include <pthread.h>
  20. #elif !FOLLY_HAVE_PTHREAD
  21. #include <cstdint>
  22. #include <memory>
  23. #include <folly/portability/Sched.h>
  24. #include <folly/portability/Time.h>
  25. #include <folly/portability/Windows.h>
  26. #define PTHREAD_CREATE_JOINABLE 0
  27. #define PTHREAD_CREATE_DETACHED 1
  28. #define PTHREAD_MUTEX_NORMAL 0
  29. #define PTHREAD_MUTEX_RECURSIVE 1
  30. #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
  31. #define _POSIX_TIMEOUTS 200112L
  32. namespace folly {
  33. namespace portability {
  34. namespace pthread {
  35. struct pthread_attr_t {
  36. size_t stackSize;
  37. bool detached;
  38. };
  39. int pthread_attr_init(pthread_attr_t* attr);
  40. int pthread_attr_setdetachstate(pthread_attr_t* attr, int state);
  41. int pthread_attr_setstacksize(pthread_attr_t* attr, size_t kb);
  42. namespace pthread_detail {
  43. struct pthread_t {
  44. HANDLE handle{INVALID_HANDLE_VALUE};
  45. DWORD threadID{0};
  46. bool detached{false};
  47. ~pthread_t() noexcept;
  48. };
  49. } // namespace pthread_detail
  50. using pthread_t = std::shared_ptr<pthread_detail::pthread_t>;
  51. int pthread_equal(pthread_t threadA, pthread_t threadB);
  52. int pthread_create(
  53. pthread_t* thread,
  54. const pthread_attr_t* attr,
  55. void* (*start_routine)(void*),
  56. void* arg);
  57. pthread_t pthread_self();
  58. int pthread_join(pthread_t thread, void** exitCode);
  59. HANDLE pthread_getw32threadhandle_np(pthread_t thread);
  60. DWORD pthread_getw32threadid_np(pthread_t thread);
  61. int pthread_setschedparam(
  62. pthread_t thread,
  63. int policy,
  64. const sched_param* param);
  65. struct pthread_mutexattr_t {
  66. int type;
  67. };
  68. int pthread_mutexattr_init(pthread_mutexattr_t* attr);
  69. int pthread_mutexattr_destroy(pthread_mutexattr_t* attr);
  70. int pthread_mutexattr_settype(pthread_mutexattr_t* attr, int type);
  71. using pthread_mutex_t = struct pthread_mutex_t_*;
  72. int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr);
  73. int pthread_mutex_destroy(pthread_mutex_t* mutex);
  74. int pthread_mutex_lock(pthread_mutex_t* mutex);
  75. int pthread_mutex_trylock(pthread_mutex_t* mutex);
  76. int pthread_mutex_unlock(pthread_mutex_t* mutex);
  77. int pthread_mutex_timedlock(
  78. pthread_mutex_t* mutex,
  79. const timespec* abs_timeout);
  80. using pthread_rwlock_t = struct pthread_rwlock_t_*;
  81. // Technically the second argument here is supposed to be a
  82. // const pthread_rwlockattr_t* but we don support it, so we
  83. // simply don't define pthread_rwlockattr_t at all to cause
  84. // a build-break if anyone tries to use it.
  85. int pthread_rwlock_init(pthread_rwlock_t* rwlock, const void* attr);
  86. int pthread_rwlock_destroy(pthread_rwlock_t* rwlock);
  87. int pthread_rwlock_rdlock(pthread_rwlock_t* rwlock);
  88. int pthread_rwlock_tryrdlock(pthread_rwlock_t* rwlock);
  89. int pthread_rwlock_timedrdlock(
  90. pthread_rwlock_t* rwlock,
  91. const timespec* abs_timeout);
  92. int pthread_rwlock_wrlock(pthread_rwlock_t* rwlock);
  93. int pthread_rwlock_trywrlock(pthread_rwlock_t* rwlock);
  94. int pthread_rwlock_timedwrlock(
  95. pthread_rwlock_t* rwlock,
  96. const timespec* abs_timeout);
  97. int pthread_rwlock_unlock(pthread_rwlock_t* rwlock);
  98. using pthread_cond_t = struct pthread_cond_t_*;
  99. // Once again, technically the second argument should be a
  100. // pthread_condattr_t, but we don't implement it, so void*
  101. // it is.
  102. int pthread_cond_init(pthread_cond_t* cond, const void* attr);
  103. int pthread_cond_destroy(pthread_cond_t* cond);
  104. int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex);
  105. int pthread_cond_timedwait(
  106. pthread_cond_t* cond,
  107. pthread_mutex_t* mutex,
  108. const timespec* abstime);
  109. int pthread_cond_signal(pthread_cond_t* cond);
  110. int pthread_cond_broadcast(pthread_cond_t* cond);
  111. // In reality, this is boost::thread_specific_ptr*, but we're attempting
  112. // to avoid introducing boost into a portability header.
  113. using pthread_key_t = void*;
  114. int pthread_key_create(pthread_key_t* key, void (*destructor)(void*));
  115. int pthread_key_delete(pthread_key_t key);
  116. void* pthread_getspecific(pthread_key_t key);
  117. int pthread_setspecific(pthread_key_t key, const void* value);
  118. } // namespace pthread
  119. } // namespace portability
  120. } // namespace folly
  121. /* using override */ using namespace folly::portability::pthread;
  122. #endif