pthread.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/Atomic.h>
  8. #include <AK/Debug.h>
  9. #include <AK/Format.h>
  10. #include <AK/StdLibExtras.h>
  11. #include <Kernel/API/Syscall.h>
  12. #include <LibSystem/syscall.h>
  13. #include <bits/pthread_integration.h>
  14. #include <limits.h>
  15. #include <pthread.h>
  16. #include <serenity.h>
  17. #include <signal.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <sys/mman.h>
  21. #include <syscall.h>
  22. #include <time.h>
  23. #include <unistd.h>
  24. namespace {
  25. using PthreadAttrImpl = Syscall::SC_create_thread_params;
  26. } // end anonymous namespace
  27. constexpr size_t required_stack_alignment = 4 * MiB;
  28. constexpr size_t highest_reasonable_guard_size = 32 * PAGE_SIZE;
  29. constexpr size_t highest_reasonable_stack_size = 8 * MiB; // That's the default in Ubuntu?
  30. #define __RETURN_PTHREAD_ERROR(rc) \
  31. return ((rc) < 0 ? -(rc) : 0)
  32. extern "C" {
  33. static void* pthread_create_helper(void* (*routine)(void*), void* argument)
  34. {
  35. void* ret_val = routine(argument);
  36. pthread_exit(ret_val);
  37. return nullptr;
  38. }
  39. static int create_thread(pthread_t* thread, void* (*entry)(void*), void* argument, PthreadAttrImpl* thread_params)
  40. {
  41. void** stack = (void**)((uintptr_t)thread_params->m_stack_location + thread_params->m_stack_size);
  42. auto push_on_stack = [&](void* data) {
  43. stack--;
  44. *stack = data;
  45. thread_params->m_stack_size -= sizeof(void*);
  46. };
  47. // We set up the stack for pthread_create_helper.
  48. // Note that we need to align the stack to 16B, accounting for
  49. // the fact that we also push 8 bytes.
  50. while (((uintptr_t)stack - 8) % 16 != 0)
  51. push_on_stack(nullptr);
  52. push_on_stack(argument);
  53. push_on_stack((void*)entry);
  54. VERIFY((uintptr_t)stack % 16 == 0);
  55. // Push a fake return address
  56. push_on_stack(nullptr);
  57. int rc = syscall(SC_create_thread, pthread_create_helper, thread_params);
  58. if (rc >= 0)
  59. *thread = rc;
  60. __RETURN_PTHREAD_ERROR(rc);
  61. }
  62. [[noreturn]] static void exit_thread(void* code)
  63. {
  64. __pthread_key_destroy_for_current_thread();
  65. syscall(SC_exit_thread, code);
  66. VERIFY_NOT_REACHED();
  67. }
  68. int pthread_self()
  69. {
  70. return __pthread_self();
  71. }
  72. int pthread_create(pthread_t* thread, pthread_attr_t* attributes, void* (*start_routine)(void*), void* argument_to_start_routine)
  73. {
  74. if (!thread)
  75. return -EINVAL;
  76. PthreadAttrImpl default_attributes {};
  77. PthreadAttrImpl** arg_attributes = reinterpret_cast<PthreadAttrImpl**>(attributes);
  78. PthreadAttrImpl* used_attributes = arg_attributes ? *arg_attributes : &default_attributes;
  79. if (!used_attributes->m_stack_location) {
  80. // adjust stack size, user might have called setstacksize, which has no restrictions on size/alignment
  81. if (0 != (used_attributes->m_stack_size % required_stack_alignment))
  82. used_attributes->m_stack_size += required_stack_alignment - (used_attributes->m_stack_size % required_stack_alignment);
  83. used_attributes->m_stack_location = mmap_with_name(nullptr, used_attributes->m_stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, 0, 0, "Thread stack");
  84. if (!used_attributes->m_stack_location)
  85. return -1;
  86. }
  87. dbgln_if(PTHREAD_DEBUG, "pthread_create: Creating thread with attributes at {}, detach state {}, priority {}, guard page size {}, stack size {}, stack location {}",
  88. used_attributes,
  89. (PTHREAD_CREATE_JOINABLE == used_attributes->m_detach_state) ? "joinable" : "detached",
  90. used_attributes->m_schedule_priority,
  91. used_attributes->m_guard_page_size,
  92. used_attributes->m_stack_size,
  93. used_attributes->m_stack_location);
  94. return create_thread(thread, start_routine, argument_to_start_routine, used_attributes);
  95. }
  96. void pthread_exit(void* value_ptr)
  97. {
  98. exit_thread(value_ptr);
  99. }
  100. void pthread_cleanup_push([[maybe_unused]] void (*routine)(void*), [[maybe_unused]] void* arg)
  101. {
  102. TODO();
  103. }
  104. void pthread_cleanup_pop([[maybe_unused]] int execute)
  105. {
  106. TODO();
  107. }
  108. int pthread_join(pthread_t thread, void** exit_value_ptr)
  109. {
  110. int rc = syscall(SC_join_thread, thread, exit_value_ptr);
  111. __RETURN_PTHREAD_ERROR(rc);
  112. }
  113. int pthread_detach(pthread_t thread)
  114. {
  115. int rc = syscall(SC_detach_thread, thread);
  116. __RETURN_PTHREAD_ERROR(rc);
  117. }
  118. int pthread_sigmask(int how, const sigset_t* set, sigset_t* old_set)
  119. {
  120. if (sigprocmask(how, set, old_set))
  121. return errno;
  122. return 0;
  123. }
  124. int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attributes)
  125. {
  126. return __pthread_mutex_init(mutex, attributes);
  127. }
  128. int pthread_mutex_destroy(pthread_mutex_t*)
  129. {
  130. return 0;
  131. }
  132. int pthread_mutex_lock(pthread_mutex_t* mutex)
  133. {
  134. return __pthread_mutex_lock(mutex);
  135. }
  136. int pthread_mutex_trylock(pthread_mutex_t* mutex)
  137. {
  138. return __pthread_mutex_trylock(mutex);
  139. }
  140. int pthread_mutex_unlock(pthread_mutex_t* mutex)
  141. {
  142. return __pthread_mutex_unlock(mutex);
  143. }
  144. int pthread_mutexattr_init(pthread_mutexattr_t* attr)
  145. {
  146. attr->type = PTHREAD_MUTEX_NORMAL;
  147. return 0;
  148. }
  149. int pthread_mutexattr_destroy(pthread_mutexattr_t*)
  150. {
  151. return 0;
  152. }
  153. int pthread_mutexattr_settype(pthread_mutexattr_t* attr, int type)
  154. {
  155. if (!attr)
  156. return EINVAL;
  157. if (type != PTHREAD_MUTEX_NORMAL && type != PTHREAD_MUTEX_RECURSIVE)
  158. return EINVAL;
  159. attr->type = type;
  160. return 0;
  161. }
  162. int pthread_attr_init(pthread_attr_t* attributes)
  163. {
  164. auto* impl = new PthreadAttrImpl {};
  165. *attributes = impl;
  166. dbgln_if(PTHREAD_DEBUG, "pthread_attr_init: New thread attributes at {}, detach state {}, priority {}, guard page size {}, stack size {}, stack location {}",
  167. impl,
  168. (PTHREAD_CREATE_JOINABLE == impl->m_detach_state) ? "joinable" : "detached",
  169. impl->m_schedule_priority,
  170. impl->m_guard_page_size,
  171. impl->m_stack_size,
  172. impl->m_stack_location);
  173. return 0;
  174. }
  175. int pthread_attr_destroy(pthread_attr_t* attributes)
  176. {
  177. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  178. delete attributes_impl;
  179. return 0;
  180. }
  181. int pthread_attr_getdetachstate(const pthread_attr_t* attributes, int* p_detach_state)
  182. {
  183. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  184. if (!attributes_impl || !p_detach_state)
  185. return EINVAL;
  186. *p_detach_state = attributes_impl->m_detach_state;
  187. return 0;
  188. }
  189. int pthread_attr_setdetachstate(pthread_attr_t* attributes, int detach_state)
  190. {
  191. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  192. if (!attributes_impl)
  193. return EINVAL;
  194. if (detach_state != PTHREAD_CREATE_JOINABLE && detach_state != PTHREAD_CREATE_DETACHED)
  195. return EINVAL;
  196. attributes_impl->m_detach_state = detach_state;
  197. dbgln_if(PTHREAD_DEBUG, "pthread_attr_setdetachstate: Thread attributes at {}, detach state {}, priority {}, guard page size {}, stack size {}, stack location {}",
  198. attributes_impl,
  199. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  200. attributes_impl->m_schedule_priority,
  201. attributes_impl->m_guard_page_size,
  202. attributes_impl->m_stack_size,
  203. attributes_impl->m_stack_location);
  204. return 0;
  205. }
  206. int pthread_attr_getguardsize(const pthread_attr_t* attributes, size_t* p_guard_size)
  207. {
  208. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  209. if (!attributes_impl || !p_guard_size)
  210. return EINVAL;
  211. *p_guard_size = attributes_impl->m_reported_guard_page_size;
  212. return 0;
  213. }
  214. int pthread_attr_setguardsize(pthread_attr_t* attributes, size_t guard_size)
  215. {
  216. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  217. if (!attributes_impl)
  218. return EINVAL;
  219. size_t actual_guard_size = guard_size;
  220. // round up
  221. if (0 != (guard_size % PAGE_SIZE))
  222. actual_guard_size += PAGE_SIZE - (guard_size % PAGE_SIZE);
  223. // what is the user even doing?
  224. if (actual_guard_size > highest_reasonable_guard_size) {
  225. return EINVAL;
  226. }
  227. attributes_impl->m_guard_page_size = actual_guard_size;
  228. attributes_impl->m_reported_guard_page_size = guard_size; // POSIX, why?
  229. dbgln_if(PTHREAD_DEBUG, "pthread_attr_setguardsize: Thread attributes at {}, detach state {}, priority {}, guard page size {}, stack size {}, stack location {}",
  230. attributes_impl,
  231. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  232. attributes_impl->m_schedule_priority,
  233. attributes_impl->m_guard_page_size,
  234. attributes_impl->m_stack_size,
  235. attributes_impl->m_stack_location);
  236. return 0;
  237. }
  238. int pthread_attr_getschedparam(const pthread_attr_t* attributes, struct sched_param* p_sched_param)
  239. {
  240. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  241. if (!attributes_impl || !p_sched_param)
  242. return EINVAL;
  243. p_sched_param->sched_priority = attributes_impl->m_schedule_priority;
  244. return 0;
  245. }
  246. int pthread_attr_setschedparam(pthread_attr_t* attributes, const struct sched_param* p_sched_param)
  247. {
  248. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  249. if (!attributes_impl || !p_sched_param)
  250. return EINVAL;
  251. if (p_sched_param->sched_priority < THREAD_PRIORITY_MIN || p_sched_param->sched_priority > THREAD_PRIORITY_MAX)
  252. return ENOTSUP;
  253. attributes_impl->m_schedule_priority = p_sched_param->sched_priority;
  254. dbgln_if(PTHREAD_DEBUG, "pthread_attr_setschedparam: Thread attributes at {}, detach state {}, priority {}, guard page size {}, stack size {}, stack location {}",
  255. attributes_impl,
  256. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  257. attributes_impl->m_schedule_priority,
  258. attributes_impl->m_guard_page_size,
  259. attributes_impl->m_stack_size,
  260. attributes_impl->m_stack_location);
  261. return 0;
  262. }
  263. int pthread_attr_getstack(const pthread_attr_t* attributes, void** p_stack_ptr, size_t* p_stack_size)
  264. {
  265. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  266. if (!attributes_impl || !p_stack_ptr || !p_stack_size)
  267. return EINVAL;
  268. *p_stack_ptr = attributes_impl->m_stack_location;
  269. *p_stack_size = attributes_impl->m_stack_size;
  270. return 0;
  271. }
  272. int pthread_attr_setstack(pthread_attr_t* attributes, void* p_stack, size_t stack_size)
  273. {
  274. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  275. if (!attributes_impl || !p_stack)
  276. return EINVAL;
  277. // Check for required alignment on size
  278. if (0 != (stack_size % required_stack_alignment))
  279. return EINVAL;
  280. // FIXME: Check for required alignment on pointer?
  281. // FIXME: "[EACCES] The stack page(s) described by stackaddr and stacksize are not both readable and writable by the thread."
  282. // Have to check that the whole range is mapped to this process/thread? Can we defer this to create_thread?
  283. attributes_impl->m_stack_size = stack_size;
  284. attributes_impl->m_stack_location = p_stack;
  285. dbgln_if(PTHREAD_DEBUG, "pthread_attr_setstack: Thread attributes at {}, detach state {}, priority {}, guard page size {}, stack size {}, stack location {}",
  286. attributes_impl,
  287. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  288. attributes_impl->m_schedule_priority,
  289. attributes_impl->m_guard_page_size,
  290. attributes_impl->m_stack_size,
  291. attributes_impl->m_stack_location);
  292. return 0;
  293. }
  294. int pthread_attr_getstacksize(const pthread_attr_t* attributes, size_t* p_stack_size)
  295. {
  296. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  297. if (!attributes_impl || !p_stack_size)
  298. return EINVAL;
  299. *p_stack_size = attributes_impl->m_stack_size;
  300. return 0;
  301. }
  302. int pthread_attr_setstacksize(pthread_attr_t* attributes, size_t stack_size)
  303. {
  304. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  305. if (!attributes_impl)
  306. return EINVAL;
  307. if ((stack_size < PTHREAD_STACK_MIN) || stack_size > highest_reasonable_stack_size)
  308. return EINVAL;
  309. attributes_impl->m_stack_size = stack_size;
  310. dbgln_if(PTHREAD_DEBUG, "pthread_attr_setstacksize: Thread attributes at {}, detach state {}, priority {}, guard page size {}, stack size {}, stack location {}",
  311. attributes_impl,
  312. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  313. attributes_impl->m_schedule_priority,
  314. attributes_impl->m_guard_page_size,
  315. attributes_impl->m_stack_size,
  316. attributes_impl->m_stack_location);
  317. return 0;
  318. }
  319. int pthread_attr_getscope([[maybe_unused]] const pthread_attr_t* attributes, [[maybe_unused]] int* contention_scope)
  320. {
  321. return 0;
  322. }
  323. int pthread_attr_setscope([[maybe_unused]] pthread_attr_t* attributes, [[maybe_unused]] int contention_scope)
  324. {
  325. return 0;
  326. }
  327. int pthread_getschedparam([[maybe_unused]] pthread_t thread, [[maybe_unused]] int* policy, [[maybe_unused]] struct sched_param* param)
  328. {
  329. return 0;
  330. }
  331. int pthread_setschedparam([[maybe_unused]] pthread_t thread, [[maybe_unused]] int policy, [[maybe_unused]] const struct sched_param* param)
  332. {
  333. return 0;
  334. }
  335. int pthread_cond_init(pthread_cond_t* cond, const pthread_condattr_t* attr)
  336. {
  337. cond->value = 0;
  338. cond->previous = 0;
  339. cond->clockid = attr ? attr->clockid : CLOCK_MONOTONIC_COARSE;
  340. return 0;
  341. }
  342. int pthread_cond_destroy(pthread_cond_t*)
  343. {
  344. return 0;
  345. }
  346. static int futex_wait(uint32_t& futex_addr, uint32_t value, const struct timespec* abstime)
  347. {
  348. int saved_errno = errno;
  349. // NOTE: FUTEX_WAIT takes a relative timeout, so use FUTEX_WAIT_BITSET instead!
  350. int rc = futex(&futex_addr, FUTEX_WAIT_BITSET, value, abstime, nullptr, FUTEX_BITSET_MATCH_ANY);
  351. if (rc < 0 && errno == EAGAIN) {
  352. // If we didn't wait, that's not an error
  353. errno = saved_errno;
  354. rc = 0;
  355. }
  356. return rc;
  357. }
  358. static int cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* abstime)
  359. {
  360. u32 value = cond->value;
  361. cond->previous = value;
  362. pthread_mutex_unlock(mutex);
  363. int rc = futex_wait(cond->value, value, abstime);
  364. pthread_mutex_lock(mutex);
  365. return rc;
  366. }
  367. int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex)
  368. {
  369. int rc = cond_wait(cond, mutex, nullptr);
  370. VERIFY(rc == 0);
  371. return 0;
  372. }
  373. int pthread_condattr_init(pthread_condattr_t* attr)
  374. {
  375. attr->clockid = CLOCK_MONOTONIC_COARSE;
  376. return 0;
  377. }
  378. int pthread_condattr_destroy(pthread_condattr_t*)
  379. {
  380. return 0;
  381. }
  382. int pthread_condattr_setclock(pthread_condattr_t* attr, clockid_t clock)
  383. {
  384. attr->clockid = clock;
  385. return 0;
  386. }
  387. int pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* abstime)
  388. {
  389. return cond_wait(cond, mutex, abstime);
  390. }
  391. int pthread_cond_signal(pthread_cond_t* cond)
  392. {
  393. u32 value = cond->previous + 1;
  394. cond->value = value;
  395. int rc = futex(&cond->value, FUTEX_WAKE, 1, nullptr, nullptr, 0);
  396. VERIFY(rc >= 0);
  397. return 0;
  398. }
  399. int pthread_cond_broadcast(pthread_cond_t* cond)
  400. {
  401. u32 value = cond->previous + 1;
  402. cond->value = value;
  403. int rc = futex(&cond->value, FUTEX_WAKE, INT32_MAX, nullptr, nullptr, 0);
  404. VERIFY(rc >= 0);
  405. return 0;
  406. }
  407. // libgcc expects this function to exist in libpthread, even
  408. // if it is not implemented.
  409. int pthread_cancel(pthread_t)
  410. {
  411. TODO();
  412. }
  413. int pthread_key_create(pthread_key_t* key, KeyDestructor destructor)
  414. {
  415. return __pthread_key_create(key, destructor);
  416. }
  417. int pthread_key_delete(pthread_key_t key)
  418. {
  419. return __pthread_key_delete(key);
  420. }
  421. void* pthread_getspecific(pthread_key_t key)
  422. {
  423. return __pthread_getspecific(key);
  424. }
  425. int pthread_setspecific(pthread_key_t key, const void* value)
  426. {
  427. return __pthread_setspecific(key, value);
  428. }
  429. int pthread_setname_np(pthread_t thread, const char* name)
  430. {
  431. if (!name)
  432. return EFAULT;
  433. int rc = syscall(SC_set_thread_name, thread, name, strlen(name));
  434. __RETURN_PTHREAD_ERROR(rc);
  435. }
  436. int pthread_getname_np(pthread_t thread, char* buffer, size_t buffer_size)
  437. {
  438. int rc = syscall(SC_get_thread_name, thread, buffer, buffer_size);
  439. __RETURN_PTHREAD_ERROR(rc);
  440. }
  441. int pthread_setcancelstate([[maybe_unused]] int state, [[maybe_unused]] int* oldstate)
  442. {
  443. TODO();
  444. }
  445. int pthread_setcanceltype([[maybe_unused]] int type, [[maybe_unused]] int* oldtype)
  446. {
  447. TODO();
  448. }
  449. int pthread_spin_destroy([[maybe_unused]] pthread_spinlock_t* lock)
  450. {
  451. TODO();
  452. }
  453. int pthread_spin_init([[maybe_unused]] pthread_spinlock_t* lock, [[maybe_unused]] int shared)
  454. {
  455. TODO();
  456. }
  457. int pthread_spin_lock([[maybe_unused]] pthread_spinlock_t* lock)
  458. {
  459. TODO();
  460. }
  461. int pthread_spin_trylock([[maybe_unused]] pthread_spinlock_t* lock)
  462. {
  463. TODO();
  464. }
  465. int pthread_spin_unlock([[maybe_unused]] pthread_spinlock_t* lock)
  466. {
  467. TODO();
  468. }
  469. int pthread_equal(pthread_t t1, pthread_t t2)
  470. {
  471. return t1 == t2;
  472. }
  473. // FIXME: Use the fancy futex mechanism above to write an rw lock.
  474. // For the time being, let's just use a less-than-good lock to get things working.
  475. int pthread_rwlock_destroy(pthread_rwlock_t* rl)
  476. {
  477. if (!rl)
  478. return 0;
  479. return 0;
  480. }
  481. // In a very non-straightforward way, this value is composed of two 32-bit integers
  482. // the top 32 bits are reserved for the ID of write-locking thread (if any)
  483. // and the bottom 32 bits are:
  484. // top 2 bits (30,31): reader wake mask, writer wake mask
  485. // middle 16 bits: information
  486. // bit 16: someone is waiting to write
  487. // bit 17: locked for write
  488. // bottom 16 bits (0..15): reader count
  489. constexpr static u32 reader_wake_mask = 1 << 30;
  490. constexpr static u32 writer_wake_mask = 1 << 31;
  491. constexpr static u32 writer_locked_mask = 1 << 17;
  492. constexpr static u32 writer_intent_mask = 1 << 16;
  493. int pthread_rwlock_init(pthread_rwlock_t* __restrict lockp, const pthread_rwlockattr_t* __restrict attr)
  494. {
  495. // Just ignore the attributes. use defaults for now.
  496. (void)attr;
  497. // No readers, no writer, not locked at all.
  498. *lockp = 0;
  499. return 0;
  500. }
  501. // Note that this function does not care about the top 32 bits at all.
  502. static int rwlock_rdlock_maybe_timed(u32* lockp, const struct timespec* timeout = nullptr, bool only_once = false, int value_if_timeout = -1, int value_if_okay = -2)
  503. {
  504. auto current = AK::atomic_load(lockp);
  505. for (; !only_once;) {
  506. // First, see if this is locked for writing
  507. // if it's not, try to add to the counter.
  508. // If someone is waiting to write, and there is one or no other readers, let them have the lock.
  509. if (!(current & writer_locked_mask)) {
  510. auto count = (u16)current;
  511. if (!(current & writer_intent_mask) || count > 1) {
  512. ++count;
  513. auto desired = (current << 16) | count;
  514. auto did_exchange = AK::atomic_compare_exchange_strong(lockp, current, desired, AK::MemoryOrder::memory_order_acquire);
  515. if (!did_exchange)
  516. continue; // tough luck, try again.
  517. return value_if_okay;
  518. }
  519. }
  520. // If no one else is waiting for the read wake bit, set it.
  521. if (!(current & reader_wake_mask)) {
  522. auto desired = current | reader_wake_mask;
  523. auto did_exchange = AK::atomic_compare_exchange_strong(lockp, current, desired, AK::MemoryOrder::memory_order_acquire);
  524. if (!did_exchange)
  525. continue; // Something interesting happened!
  526. current = desired;
  527. }
  528. // Seems like someone is writing (or is interested in writing and we let them have the lock)
  529. // wait until they're done.
  530. auto rc = futex(lockp, FUTEX_WAIT_BITSET, current, timeout, nullptr, reader_wake_mask);
  531. if (rc < 0 && errno == ETIMEDOUT && timeout) {
  532. return value_if_timeout;
  533. }
  534. if (rc < 0 && errno != EAGAIN) {
  535. // Something broke. let's just bail out.
  536. return errno;
  537. }
  538. errno = 0;
  539. // Reload the 'current' value
  540. current = AK::atomic_load(lockp);
  541. }
  542. return value_if_timeout;
  543. }
  544. static int rwlock_wrlock_maybe_timed(pthread_rwlock_t* lockval_p, const struct timespec* timeout = nullptr, bool only_once = false, int value_if_timeout = -1, int value_if_okay = -2)
  545. {
  546. u32* lockp = reinterpret_cast<u32*>(lockval_p);
  547. auto current = AK::atomic_load(lockp);
  548. for (; !only_once;) {
  549. // First, see if this is locked for writing, and if there are any readers.
  550. // if not, lock it.
  551. // If someone is waiting to write, let them have the lock.
  552. if (!(current & writer_locked_mask) && ((u16)current) == 0) {
  553. if (!(current & writer_intent_mask)) {
  554. auto desired = current | writer_locked_mask | writer_intent_mask;
  555. auto did_exchange = AK::atomic_compare_exchange_strong(lockp, current, desired, AK::MemoryOrder::memory_order_acquire);
  556. if (!did_exchange)
  557. continue;
  558. // Now that we've locked the value, it's safe to set our thread ID.
  559. AK::atomic_store(reinterpret_cast<i32*>(lockval_p) + 1, pthread_self());
  560. return value_if_okay;
  561. }
  562. }
  563. // That didn't work, if no one else is waiting for the write bit, set it.
  564. if (!(current & writer_wake_mask)) {
  565. auto desired = current | writer_wake_mask | writer_intent_mask;
  566. auto did_exchange = AK::atomic_compare_exchange_strong(lockp, current, desired, AK::MemoryOrder::memory_order_acquire);
  567. if (!did_exchange)
  568. continue; // Something interesting happened!
  569. current = desired;
  570. }
  571. // Seems like someone is writing (or is interested in writing and we let them have the lock)
  572. // wait until they're done.
  573. auto rc = futex(lockp, FUTEX_WAIT_BITSET, current, timeout, nullptr, writer_wake_mask);
  574. if (rc < 0 && errno == ETIMEDOUT && timeout) {
  575. return value_if_timeout;
  576. }
  577. if (rc < 0 && errno != EAGAIN) {
  578. // Something broke. let's just bail out.
  579. return errno;
  580. }
  581. errno = 0;
  582. // Reload the 'current' value
  583. current = AK::atomic_load(lockp);
  584. }
  585. return value_if_timeout;
  586. }
  587. int pthread_rwlock_rdlock(pthread_rwlock_t* lockp)
  588. {
  589. if (!lockp)
  590. return EINVAL;
  591. return rwlock_rdlock_maybe_timed(reinterpret_cast<u32*>(lockp), nullptr, false, 0, 0);
  592. }
  593. int pthread_rwlock_timedrdlock(pthread_rwlock_t* __restrict lockp, const struct timespec* __restrict timespec)
  594. {
  595. if (!lockp)
  596. return EINVAL;
  597. auto rc = rwlock_rdlock_maybe_timed(reinterpret_cast<u32*>(lockp), timespec);
  598. if (rc == -1) // "ok"
  599. return 0;
  600. if (rc == -2) // "timed out"
  601. return 1;
  602. return rc;
  603. }
  604. int pthread_rwlock_timedwrlock(pthread_rwlock_t* __restrict lockp, const struct timespec* __restrict timespec)
  605. {
  606. if (!lockp)
  607. return EINVAL;
  608. auto rc = rwlock_wrlock_maybe_timed(lockp, timespec);
  609. if (rc == -1) // "ok"
  610. return 0;
  611. if (rc == -2) // "timed out"
  612. return 1;
  613. return rc;
  614. }
  615. int pthread_rwlock_tryrdlock(pthread_rwlock_t* lockp)
  616. {
  617. if (!lockp)
  618. return EINVAL;
  619. return rwlock_rdlock_maybe_timed(reinterpret_cast<u32*>(lockp), nullptr, true, EBUSY, 0);
  620. }
  621. int pthread_rwlock_trywrlock(pthread_rwlock_t* lockp)
  622. {
  623. if (!lockp)
  624. return EINVAL;
  625. return rwlock_wrlock_maybe_timed(lockp, nullptr, true, EBUSY, 0);
  626. }
  627. int pthread_rwlock_unlock(pthread_rwlock_t* lockval_p)
  628. {
  629. if (!lockval_p)
  630. return EINVAL;
  631. // This is a weird API, we don't really know whether we're unlocking write or read...
  632. auto lockp = reinterpret_cast<u32*>(lockval_p);
  633. auto current = AK::atomic_load(lockp, AK::MemoryOrder::memory_order_relaxed);
  634. if (current & writer_locked_mask) {
  635. // If this lock is locked for writing, its owner better be us!
  636. auto owner_id = AK::atomic_load(reinterpret_cast<i32*>(lockval_p) + 1);
  637. auto my_id = pthread_self();
  638. if (owner_id != my_id)
  639. return EINVAL; // you don't own this lock, silly.
  640. // Now just unlock it.
  641. auto desired = current & ~(writer_locked_mask | writer_intent_mask);
  642. AK::atomic_store(lockp, desired, AK::MemoryOrder::memory_order_release);
  643. // Then wake both readers and writers, if any.
  644. auto rc = futex(lockp, FUTEX_WAKE_BITSET, current, nullptr, nullptr, (current & writer_wake_mask) | reader_wake_mask);
  645. if (rc < 0)
  646. return errno;
  647. return 0;
  648. }
  649. for (;;) {
  650. auto count = (u16)current;
  651. if (!count) {
  652. // Are you crazy? this isn't even locked!
  653. return EINVAL;
  654. }
  655. --count;
  656. auto desired = (current << 16) | count;
  657. auto did_exchange = AK::atomic_compare_exchange_strong(lockp, current, desired, AK::MemoryOrder::memory_order_release);
  658. if (!did_exchange)
  659. continue; // tough luck, try again.
  660. }
  661. // Finally, unlocked at last!
  662. return 0;
  663. }
  664. int pthread_rwlock_wrlock(pthread_rwlock_t* lockp)
  665. {
  666. if (!lockp)
  667. return EINVAL;
  668. return rwlock_wrlock_maybe_timed(lockp, nullptr, false, 0, 0);
  669. }
  670. int pthread_rwlockattr_destroy(pthread_rwlockattr_t*)
  671. {
  672. return 0;
  673. }
  674. int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t* __restrict, int* __restrict)
  675. {
  676. VERIFY_NOT_REACHED();
  677. }
  678. int pthread_rwlockattr_init(pthread_rwlockattr_t*)
  679. {
  680. VERIFY_NOT_REACHED();
  681. }
  682. int pthread_rwlockattr_setpshared(pthread_rwlockattr_t*, int)
  683. {
  684. VERIFY_NOT_REACHED();
  685. }
  686. int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
  687. {
  688. if (prepare)
  689. __pthread_fork_atfork_register_prepare(prepare);
  690. if (parent)
  691. __pthread_fork_atfork_register_parent(parent);
  692. if (child)
  693. __pthread_fork_atfork_register_child(child);
  694. return 0;
  695. }
  696. } // extern "C"