pthread.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #include <AK/Assertions.h>
  2. #include <AK/Atomic.h>
  3. #include <AK/InlineLinkedList.h>
  4. #include <AK/StdLibExtras.h>
  5. #include <Kernel/Syscall.h>
  6. #include <limits.h>
  7. #include <pthread.h>
  8. #include <stdio.h>
  9. #include <sys/mman.h>
  10. #include <time.h>
  11. #include <unistd.h>
  12. #define PTHREAD_DEBUG
  13. namespace {
  14. using PthreadAttrImpl = Syscall::SC_create_thread_params;
  15. } // end anonymous namespace
  16. constexpr size_t required_stack_alignment = 4 * MB;
  17. constexpr size_t highest_reasonable_guard_size = 32 * PAGE_SIZE;
  18. constexpr size_t highest_reasonable_stack_size = 8 * MB; // That's the default in Ubuntu?
  19. extern "C" {
  20. static int create_thread(void* (*entry)(void*), void* argument, void* stack)
  21. {
  22. int rc = syscall(SC_create_thread, entry, argument, stack);
  23. __RETURN_WITH_ERRNO(rc, rc, -1);
  24. }
  25. static void exit_thread(void* code)
  26. {
  27. syscall(SC_exit_thread, code);
  28. ASSERT_NOT_REACHED();
  29. }
  30. int pthread_create(pthread_t* thread, pthread_attr_t* attributes, void* (*start_routine)(void*), void* argument_to_start_routine)
  31. {
  32. if (!thread)
  33. return -EINVAL;
  34. PthreadAttrImpl default_attributes {};
  35. PthreadAttrImpl** arg_attributes = reinterpret_cast<PthreadAttrImpl**>(attributes);
  36. PthreadAttrImpl* used_attributes = arg_attributes ? *arg_attributes : &default_attributes;
  37. if (!used_attributes->m_stack_location) {
  38. // adjust stack size, user might have called setstacksize, which has no restrictions on size/alignment
  39. if (0 != (used_attributes->m_stack_size % required_stack_alignment))
  40. used_attributes->m_stack_size += required_stack_alignment - (used_attributes->m_stack_size % required_stack_alignment);
  41. 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");
  42. if (!used_attributes->m_stack_location)
  43. return -1;
  44. }
  45. #ifdef PTHREAD_DEBUG
  46. printf("pthread_create: Creating thread with attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  47. used_attributes,
  48. (PTHREAD_CREATE_JOINABLE == used_attributes->m_detach_state) ? "joinable" : "detached",
  49. used_attributes->m_schedule_priority,
  50. used_attributes->m_guard_page_size,
  51. used_attributes->m_stack_size,
  52. used_attributes->m_stack_location);
  53. #endif
  54. int rc = create_thread(start_routine, argument_to_start_routine, used_attributes);
  55. if (rc < 0)
  56. return rc;
  57. *thread = rc;
  58. return 0;
  59. }
  60. void pthread_exit(void* value_ptr)
  61. {
  62. exit_thread(value_ptr);
  63. }
  64. int pthread_join(pthread_t thread, void** exit_value_ptr)
  65. {
  66. int rc = syscall(SC_join_thread, thread, exit_value_ptr);
  67. __RETURN_WITH_ERRNO(rc, rc, -1);
  68. }
  69. int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attributes)
  70. {
  71. // FIXME: Implement mutex attributes
  72. UNUSED_PARAM(attributes);
  73. *mutex = 0;
  74. return 0;
  75. }
  76. int pthread_mutex_lock(pthread_mutex_t* mutex)
  77. {
  78. auto* atomic = reinterpret_cast<Atomic<u32>*>(mutex);
  79. for (;;) {
  80. u32 expected = false;
  81. if (atomic->compare_exchange_strong(expected, true, AK::memory_order_acq_rel))
  82. return 0;
  83. sched_yield();
  84. }
  85. }
  86. int pthread_mutex_unlock(pthread_mutex_t* mutex)
  87. {
  88. auto* atomic = reinterpret_cast<Atomic<u32>*>(mutex);
  89. atomic->store(false, AK::memory_order_release);
  90. return 0;
  91. }
  92. int pthread_attr_init(pthread_attr_t* attributes)
  93. {
  94. auto* impl = new PthreadAttrImpl {};
  95. *attributes = impl;
  96. #ifdef PTHREAD_DEBUG
  97. printf("pthread_attr_init: New thread attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  98. impl,
  99. (PTHREAD_CREATE_JOINABLE == impl->m_detach_state) ? "joinable" : "detached",
  100. impl->m_schedule_priority,
  101. impl->m_guard_page_size,
  102. impl->m_stack_size,
  103. impl->m_stack_location);
  104. #endif
  105. return 0;
  106. }
  107. int pthread_attr_destroy(pthread_attr_t* attributes)
  108. {
  109. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  110. delete attributes_impl;
  111. return 0;
  112. }
  113. int pthread_attr_getdetachstate(const pthread_attr_t* attributes, int* p_detach_state)
  114. {
  115. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  116. if (!attributes_impl || !p_detach_state)
  117. return EINVAL;
  118. *p_detach_state = attributes_impl->m_detach_state;
  119. return 0;
  120. }
  121. int pthread_attr_setdetachstate(pthread_attr_t* attributes, int detach_state)
  122. {
  123. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  124. if (!attributes_impl)
  125. return EINVAL;
  126. if ((PTHREAD_CREATE_JOINABLE != detach_state) || PTHREAD_CREATE_DETACHED != detach_state)
  127. return EINVAL;
  128. attributes_impl->m_detach_state = detach_state;
  129. #ifdef PTHREAD_DEBUG
  130. printf("pthread_attr_setdetachstate: Thread attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  131. attributes_impl,
  132. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  133. attributes_impl->m_schedule_priority,
  134. attributes_impl->m_guard_page_size,
  135. attributes_impl->m_stack_size,
  136. attributes_impl->m_stack_location);
  137. #endif
  138. return 0;
  139. }
  140. int pthread_attr_getguardsize(const pthread_attr_t* attributes, size_t* p_guard_size)
  141. {
  142. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  143. if (!attributes_impl || !p_guard_size)
  144. return EINVAL;
  145. *p_guard_size = attributes_impl->m_reported_guard_page_size;
  146. return 0;
  147. }
  148. int pthread_attr_setguardsize(pthread_attr_t* attributes, size_t guard_size)
  149. {
  150. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  151. if (!attributes_impl)
  152. return EINVAL;
  153. size_t actual_guard_size = guard_size;
  154. // round up
  155. if (0 != (guard_size % PAGE_SIZE))
  156. actual_guard_size += PAGE_SIZE - (guard_size % PAGE_SIZE);
  157. // what is the user even doing?
  158. if (actual_guard_size > highest_reasonable_guard_size) {
  159. return EINVAL;
  160. }
  161. attributes_impl->m_guard_page_size = actual_guard_size;
  162. attributes_impl->m_reported_guard_page_size = guard_size; // POSIX, why?
  163. #ifdef PTHREAD_DEBUG
  164. printf("pthread_attr_setguardsize: Thread attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  165. attributes_impl,
  166. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  167. attributes_impl->m_schedule_priority,
  168. attributes_impl->m_guard_page_size,
  169. attributes_impl->m_stack_size,
  170. attributes_impl->m_stack_location);
  171. #endif
  172. return 0;
  173. }
  174. int pthread_attr_getschedparam(const pthread_attr_t* attributes, struct sched_param* p_sched_param)
  175. {
  176. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  177. if (!attributes_impl || !p_sched_param)
  178. return EINVAL;
  179. p_sched_param->sched_priority = attributes_impl->m_schedule_priority;
  180. return 0;
  181. }
  182. int pthread_attr_setschedparam(pthread_attr_t* attributes, const struct sched_param* p_sched_param)
  183. {
  184. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  185. if (!attributes_impl || !p_sched_param)
  186. return EINVAL;
  187. // NOTE: This must track sched_get_priority_[min,max] and ThreadPriority enum in Thread.h
  188. if (p_sched_param->sched_priority < 0 || p_sched_param->sched_priority > 3)
  189. return ENOTSUP;
  190. attributes_impl->m_schedule_priority = p_sched_param->sched_priority;
  191. #ifdef PTHREAD_DEBUG
  192. printf("pthread_attr_setschedparam: Thread attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  193. attributes_impl,
  194. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  195. attributes_impl->m_schedule_priority,
  196. attributes_impl->m_guard_page_size,
  197. attributes_impl->m_stack_size,
  198. attributes_impl->m_stack_location);
  199. #endif
  200. return 0;
  201. }
  202. int pthread_attr_getstack(const pthread_attr_t* attributes, void** p_stack_ptr, size_t* p_stack_size)
  203. {
  204. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  205. if (!attributes_impl || !p_stack_ptr || !p_stack_size)
  206. return EINVAL;
  207. *p_stack_ptr = attributes_impl->m_stack_location;
  208. *p_stack_size = attributes_impl->m_stack_size;
  209. return 0;
  210. }
  211. int pthread_attr_setstack(pthread_attr_t* attributes, void* p_stack, size_t stack_size)
  212. {
  213. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  214. if (!attributes_impl || !p_stack)
  215. return EINVAL;
  216. // Check for required alignment on size
  217. if (0 != (stack_size % required_stack_alignment))
  218. return EINVAL;
  219. // FIXME: Check for required alignment on pointer?
  220. // FIXME: "[EACCES] The stack page(s) described by stackaddr and stacksize are not both readable and writable by the thread."
  221. // Have to check that the whole range is mapped to this process/thread? Can we defer this to create_thread?
  222. attributes_impl->m_stack_size = stack_size;
  223. attributes_impl->m_stack_location = p_stack;
  224. #ifdef PTHREAD_DEBUG
  225. printf("pthread_attr_setstack: Thread attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  226. attributes_impl,
  227. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  228. attributes_impl->m_schedule_priority,
  229. attributes_impl->m_guard_page_size,
  230. attributes_impl->m_stack_size,
  231. attributes_impl->m_stack_location);
  232. #endif
  233. return 0;
  234. }
  235. int pthread_attr_getstacksize(const pthread_attr_t* attributes, size_t* p_stack_size)
  236. {
  237. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  238. if (!attributes_impl || !p_stack_size)
  239. return EINVAL;
  240. *p_stack_size = attributes_impl->m_stack_size;
  241. return 0;
  242. }
  243. int pthread_attr_setstacksize(pthread_attr_t* attributes, size_t stack_size)
  244. {
  245. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  246. if (!attributes_impl)
  247. return EINVAL;
  248. if ((stack_size < PTHREAD_STACK_MIN) || stack_size > highest_reasonable_stack_size)
  249. return EINVAL;
  250. attributes_impl->m_stack_size = stack_size;
  251. #ifdef PTHREAD_DEBUG
  252. printf("pthread_attr_setstacksize: Thread attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  253. attributes_impl,
  254. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  255. attributes_impl->m_schedule_priority,
  256. attributes_impl->m_guard_page_size,
  257. attributes_impl->m_stack_size,
  258. attributes_impl->m_stack_location);
  259. #endif
  260. return 0;
  261. }
  262. struct WaitNode : public InlineLinkedListNode<WaitNode> {
  263. bool waiting { true };
  264. WaitNode* m_next { nullptr };
  265. WaitNode* m_prev { nullptr };
  266. };
  267. struct ConditionVariable {
  268. InlineLinkedList<WaitNode> waiters;
  269. clockid_t clock;
  270. };
  271. int pthread_cond_init(pthread_cond_t* cond, const pthread_condattr_t* attr)
  272. {
  273. auto& condvar = *new ConditionVariable;
  274. cond->storage = &condvar;
  275. if (attr)
  276. condvar.clock = attr->clockid;
  277. return 0;
  278. }
  279. int pthread_cond_destroy(pthread_cond_t* cond)
  280. {
  281. delete static_cast<ConditionVariable*>(cond->storage);
  282. return 0;
  283. }
  284. int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex)
  285. {
  286. WaitNode node;
  287. auto& condvar = *(ConditionVariable*)cond->storage;
  288. condvar.waiters.append(&node);
  289. while (node.waiting) {
  290. pthread_mutex_unlock(mutex);
  291. sched_yield();
  292. pthread_mutex_lock(mutex);
  293. }
  294. return 0;
  295. }
  296. int pthread_condattr_init(pthread_condattr_t* attr)
  297. {
  298. attr->clockid = CLOCK_MONOTONIC;
  299. return 0;
  300. }
  301. int pthread_condattr_destroy(pthread_condattr_t*)
  302. {
  303. return 0;
  304. }
  305. int pthread_condattr_setclock(pthread_condattr_t* attr, clockid_t clock)
  306. {
  307. attr->clockid = clock;
  308. return 0;
  309. }
  310. int pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* abstime)
  311. {
  312. WaitNode node;
  313. auto& condvar = *(ConditionVariable*)cond->storage;
  314. condvar.waiters.append(&node);
  315. while (node.waiting) {
  316. struct timespec now;
  317. if (clock_gettime(condvar.clock, &now) < 0)
  318. return -1;
  319. if ((abstime->tv_sec < now.tv_sec) || (abstime->tv_sec == now.tv_sec && abstime->tv_nsec <= now.tv_nsec)) {
  320. errno = ETIMEDOUT;
  321. return -1;
  322. }
  323. pthread_mutex_unlock(mutex);
  324. sched_yield();
  325. pthread_mutex_lock(mutex);
  326. }
  327. return 0;
  328. }
  329. int pthread_cond_signal(pthread_cond_t* cond)
  330. {
  331. auto& condvar = *(ConditionVariable*)cond->storage;
  332. if (condvar.waiters.is_empty())
  333. return 0;
  334. auto* node = condvar.waiters.remove_head();
  335. node->waiting = false;
  336. return 0;
  337. }
  338. int pthread_cond_broadcast(pthread_cond_t* cond)
  339. {
  340. auto& condvar = *(ConditionVariable*)cond->storage;
  341. while (!condvar.waiters.is_empty()) {
  342. auto* node = condvar.waiters.remove_head();
  343. node->waiting = false;
  344. }
  345. return 0;
  346. }
  347. }