pthread.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <AK/Atomic.h>
  28. #include <AK/StdLibExtras.h>
  29. #include <Kernel/API/Syscall.h>
  30. #include <limits.h>
  31. #include <pthread.h>
  32. #include <serenity.h>
  33. #include <signal.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <sys/mman.h>
  37. #include <time.h>
  38. #include <unistd.h>
  39. //#define PTHREAD_DEBUG
  40. namespace {
  41. using PthreadAttrImpl = Syscall::SC_create_thread_params;
  42. } // end anonymous namespace
  43. constexpr size_t required_stack_alignment = 4 * MiB;
  44. constexpr size_t highest_reasonable_guard_size = 32 * PAGE_SIZE;
  45. constexpr size_t highest_reasonable_stack_size = 8 * MiB; // That's the default in Ubuntu?
  46. #define __RETURN_PTHREAD_ERROR(rc) \
  47. return ((rc) < 0 ? -(rc) : 0)
  48. extern "C" {
  49. static void* pthread_create_helper(void* (*routine)(void*), void* argument)
  50. {
  51. void* ret_val = routine(argument);
  52. pthread_exit(ret_val);
  53. return nullptr;
  54. }
  55. static int create_thread(pthread_t* thread, void* (*entry)(void*), void* argument, PthreadAttrImpl* thread_params)
  56. {
  57. void** stack = (void**)((uintptr_t)thread_params->m_stack_location + thread_params->m_stack_size);
  58. auto push_on_stack = [&](void* data) {
  59. stack--;
  60. *stack = data;
  61. thread_params->m_stack_size -= sizeof(void*);
  62. };
  63. // We set up the stack for pthread_create_helper.
  64. // Note that we need to align the stack to 16B, accounting for
  65. // the fact that we also push 8 bytes.
  66. while (((uintptr_t)stack - 8) % 16 != 0)
  67. push_on_stack(nullptr);
  68. push_on_stack(argument);
  69. push_on_stack((void*)entry);
  70. ASSERT((uintptr_t)stack % 16 == 0);
  71. // Push a fake return address
  72. push_on_stack(nullptr);
  73. int rc = syscall(SC_create_thread, pthread_create_helper, thread_params);
  74. if (rc >= 0)
  75. *thread = rc;
  76. __RETURN_PTHREAD_ERROR(rc);
  77. }
  78. [[noreturn]] static void exit_thread(void* code)
  79. {
  80. syscall(SC_exit_thread, code);
  81. ASSERT_NOT_REACHED();
  82. }
  83. int pthread_self()
  84. {
  85. return gettid();
  86. }
  87. int pthread_create(pthread_t* thread, pthread_attr_t* attributes, void* (*start_routine)(void*), void* argument_to_start_routine)
  88. {
  89. if (!thread)
  90. return -EINVAL;
  91. PthreadAttrImpl default_attributes {};
  92. PthreadAttrImpl** arg_attributes = reinterpret_cast<PthreadAttrImpl**>(attributes);
  93. PthreadAttrImpl* used_attributes = arg_attributes ? *arg_attributes : &default_attributes;
  94. if (!used_attributes->m_stack_location) {
  95. // adjust stack size, user might have called setstacksize, which has no restrictions on size/alignment
  96. if (0 != (used_attributes->m_stack_size % required_stack_alignment))
  97. used_attributes->m_stack_size += required_stack_alignment - (used_attributes->m_stack_size % required_stack_alignment);
  98. 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");
  99. if (!used_attributes->m_stack_location)
  100. return -1;
  101. }
  102. #ifdef PTHREAD_DEBUG
  103. dbgprintf("pthread_create: Creating thread with attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  104. used_attributes,
  105. (PTHREAD_CREATE_JOINABLE == used_attributes->m_detach_state) ? "joinable" : "detached",
  106. used_attributes->m_schedule_priority,
  107. used_attributes->m_guard_page_size,
  108. used_attributes->m_stack_size,
  109. used_attributes->m_stack_location);
  110. #endif
  111. return create_thread(thread, start_routine, argument_to_start_routine, used_attributes);
  112. }
  113. void pthread_exit(void* value_ptr)
  114. {
  115. exit_thread(value_ptr);
  116. }
  117. int pthread_join(pthread_t thread, void** exit_value_ptr)
  118. {
  119. int rc = syscall(SC_join_thread, thread, exit_value_ptr);
  120. __RETURN_PTHREAD_ERROR(rc);
  121. }
  122. int pthread_detach(pthread_t thread)
  123. {
  124. int rc = syscall(SC_detach_thread, thread);
  125. __RETURN_PTHREAD_ERROR(rc);
  126. }
  127. int pthread_sigmask(int how, const sigset_t* set, sigset_t* old_set)
  128. {
  129. if (sigprocmask(how, set, old_set))
  130. return errno;
  131. return 0;
  132. }
  133. int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attributes)
  134. {
  135. mutex->lock = 0;
  136. mutex->owner = 0;
  137. mutex->level = 0;
  138. mutex->type = attributes ? attributes->type : PTHREAD_MUTEX_NORMAL;
  139. return 0;
  140. }
  141. int pthread_mutex_destroy(pthread_mutex_t*)
  142. {
  143. return 0;
  144. }
  145. int pthread_mutex_lock(pthread_mutex_t* mutex)
  146. {
  147. auto& atomic = reinterpret_cast<Atomic<u32>&>(mutex->lock);
  148. pthread_t this_thread = pthread_self();
  149. for (;;) {
  150. u32 expected = false;
  151. if (!atomic.compare_exchange_strong(expected, true, AK::memory_order_acq_rel)) {
  152. if (mutex->type == PTHREAD_MUTEX_RECURSIVE && mutex->owner == this_thread) {
  153. mutex->level++;
  154. return 0;
  155. }
  156. sched_yield();
  157. continue;
  158. }
  159. mutex->owner = this_thread;
  160. mutex->level = 0;
  161. return 0;
  162. }
  163. }
  164. int pthread_mutex_trylock(pthread_mutex_t* mutex)
  165. {
  166. auto& atomic = reinterpret_cast<Atomic<u32>&>(mutex->lock);
  167. u32 expected = false;
  168. if (!atomic.compare_exchange_strong(expected, true, AK::memory_order_acq_rel)) {
  169. if (mutex->type == PTHREAD_MUTEX_RECURSIVE && mutex->owner == pthread_self()) {
  170. mutex->level++;
  171. return 0;
  172. }
  173. return EBUSY;
  174. }
  175. mutex->owner = pthread_self();
  176. mutex->level = 0;
  177. return 0;
  178. }
  179. int pthread_mutex_unlock(pthread_mutex_t* mutex)
  180. {
  181. if (mutex->type == PTHREAD_MUTEX_RECURSIVE && mutex->level > 0) {
  182. mutex->level--;
  183. return 0;
  184. }
  185. mutex->owner = 0;
  186. mutex->lock = 0;
  187. return 0;
  188. }
  189. int pthread_mutexattr_init(pthread_mutexattr_t* attr)
  190. {
  191. attr->type = PTHREAD_MUTEX_NORMAL;
  192. return 0;
  193. }
  194. int pthread_mutexattr_destroy(pthread_mutexattr_t*)
  195. {
  196. return 0;
  197. }
  198. int pthread_mutexattr_settype(pthread_mutexattr_t* attr, int type)
  199. {
  200. if (!attr)
  201. return EINVAL;
  202. if (type != PTHREAD_MUTEX_NORMAL && type != PTHREAD_MUTEX_RECURSIVE)
  203. return EINVAL;
  204. attr->type = type;
  205. return 0;
  206. }
  207. int pthread_attr_init(pthread_attr_t* attributes)
  208. {
  209. auto* impl = new PthreadAttrImpl {};
  210. *attributes = impl;
  211. #ifdef PTHREAD_DEBUG
  212. dbgprintf("pthread_attr_init: New thread attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  213. impl,
  214. (PTHREAD_CREATE_JOINABLE == impl->m_detach_state) ? "joinable" : "detached",
  215. impl->m_schedule_priority,
  216. impl->m_guard_page_size,
  217. impl->m_stack_size,
  218. impl->m_stack_location);
  219. #endif
  220. return 0;
  221. }
  222. int pthread_attr_destroy(pthread_attr_t* attributes)
  223. {
  224. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  225. delete attributes_impl;
  226. return 0;
  227. }
  228. int pthread_attr_getdetachstate(const pthread_attr_t* attributes, int* p_detach_state)
  229. {
  230. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  231. if (!attributes_impl || !p_detach_state)
  232. return EINVAL;
  233. *p_detach_state = attributes_impl->m_detach_state;
  234. return 0;
  235. }
  236. int pthread_attr_setdetachstate(pthread_attr_t* attributes, int detach_state)
  237. {
  238. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  239. if (!attributes_impl)
  240. return EINVAL;
  241. if (detach_state != PTHREAD_CREATE_JOINABLE && detach_state != PTHREAD_CREATE_DETACHED)
  242. return EINVAL;
  243. attributes_impl->m_detach_state = detach_state;
  244. #ifdef PTHREAD_DEBUG
  245. dbgprintf("pthread_attr_setdetachstate: Thread attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  246. attributes_impl,
  247. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  248. attributes_impl->m_schedule_priority,
  249. attributes_impl->m_guard_page_size,
  250. attributes_impl->m_stack_size,
  251. attributes_impl->m_stack_location);
  252. #endif
  253. return 0;
  254. }
  255. int pthread_attr_getguardsize(const pthread_attr_t* attributes, size_t* p_guard_size)
  256. {
  257. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  258. if (!attributes_impl || !p_guard_size)
  259. return EINVAL;
  260. *p_guard_size = attributes_impl->m_reported_guard_page_size;
  261. return 0;
  262. }
  263. int pthread_attr_setguardsize(pthread_attr_t* attributes, size_t guard_size)
  264. {
  265. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  266. if (!attributes_impl)
  267. return EINVAL;
  268. size_t actual_guard_size = guard_size;
  269. // round up
  270. if (0 != (guard_size % PAGE_SIZE))
  271. actual_guard_size += PAGE_SIZE - (guard_size % PAGE_SIZE);
  272. // what is the user even doing?
  273. if (actual_guard_size > highest_reasonable_guard_size) {
  274. return EINVAL;
  275. }
  276. attributes_impl->m_guard_page_size = actual_guard_size;
  277. attributes_impl->m_reported_guard_page_size = guard_size; // POSIX, why?
  278. #ifdef PTHREAD_DEBUG
  279. dbgprintf("pthread_attr_setguardsize: Thread attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  280. attributes_impl,
  281. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  282. attributes_impl->m_schedule_priority,
  283. attributes_impl->m_guard_page_size,
  284. attributes_impl->m_stack_size,
  285. attributes_impl->m_stack_location);
  286. #endif
  287. return 0;
  288. }
  289. int pthread_attr_getschedparam(const pthread_attr_t* attributes, struct sched_param* p_sched_param)
  290. {
  291. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  292. if (!attributes_impl || !p_sched_param)
  293. return EINVAL;
  294. p_sched_param->sched_priority = attributes_impl->m_schedule_priority;
  295. return 0;
  296. }
  297. int pthread_attr_setschedparam(pthread_attr_t* attributes, const struct sched_param* p_sched_param)
  298. {
  299. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  300. if (!attributes_impl || !p_sched_param)
  301. return EINVAL;
  302. if (p_sched_param->sched_priority < THREAD_PRIORITY_MIN || p_sched_param->sched_priority > THREAD_PRIORITY_MAX)
  303. return ENOTSUP;
  304. attributes_impl->m_schedule_priority = p_sched_param->sched_priority;
  305. #ifdef PTHREAD_DEBUG
  306. dbgprintf("pthread_attr_setschedparam: Thread attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  307. attributes_impl,
  308. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  309. attributes_impl->m_schedule_priority,
  310. attributes_impl->m_guard_page_size,
  311. attributes_impl->m_stack_size,
  312. attributes_impl->m_stack_location);
  313. #endif
  314. return 0;
  315. }
  316. int pthread_attr_getstack(const pthread_attr_t* attributes, void** p_stack_ptr, size_t* p_stack_size)
  317. {
  318. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  319. if (!attributes_impl || !p_stack_ptr || !p_stack_size)
  320. return EINVAL;
  321. *p_stack_ptr = attributes_impl->m_stack_location;
  322. *p_stack_size = attributes_impl->m_stack_size;
  323. return 0;
  324. }
  325. int pthread_attr_setstack(pthread_attr_t* attributes, void* p_stack, size_t stack_size)
  326. {
  327. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  328. if (!attributes_impl || !p_stack)
  329. return EINVAL;
  330. // Check for required alignment on size
  331. if (0 != (stack_size % required_stack_alignment))
  332. return EINVAL;
  333. // FIXME: Check for required alignment on pointer?
  334. // FIXME: "[EACCES] The stack page(s) described by stackaddr and stacksize are not both readable and writable by the thread."
  335. // Have to check that the whole range is mapped to this process/thread? Can we defer this to create_thread?
  336. attributes_impl->m_stack_size = stack_size;
  337. attributes_impl->m_stack_location = p_stack;
  338. #ifdef PTHREAD_DEBUG
  339. dbgprintf("pthread_attr_setstack: Thread attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  340. attributes_impl,
  341. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  342. attributes_impl->m_schedule_priority,
  343. attributes_impl->m_guard_page_size,
  344. attributes_impl->m_stack_size,
  345. attributes_impl->m_stack_location);
  346. #endif
  347. return 0;
  348. }
  349. int pthread_attr_getstacksize(const pthread_attr_t* attributes, size_t* p_stack_size)
  350. {
  351. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  352. if (!attributes_impl || !p_stack_size)
  353. return EINVAL;
  354. *p_stack_size = attributes_impl->m_stack_size;
  355. return 0;
  356. }
  357. int pthread_attr_setstacksize(pthread_attr_t* attributes, size_t stack_size)
  358. {
  359. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  360. if (!attributes_impl)
  361. return EINVAL;
  362. if ((stack_size < PTHREAD_STACK_MIN) || stack_size > highest_reasonable_stack_size)
  363. return EINVAL;
  364. attributes_impl->m_stack_size = stack_size;
  365. #ifdef PTHREAD_DEBUG
  366. dbgprintf("pthread_attr_setstacksize: Thread attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  367. attributes_impl,
  368. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  369. attributes_impl->m_schedule_priority,
  370. attributes_impl->m_guard_page_size,
  371. attributes_impl->m_stack_size,
  372. attributes_impl->m_stack_location);
  373. #endif
  374. return 0;
  375. }
  376. int pthread_getschedparam([[maybe_unused]] pthread_t thread, [[maybe_unused]] int* policy, [[maybe_unused]] struct sched_param* param)
  377. {
  378. return 0;
  379. }
  380. int pthread_setschedparam([[maybe_unused]] pthread_t thread, [[maybe_unused]] int policy, [[maybe_unused]] const struct sched_param* param)
  381. {
  382. return 0;
  383. }
  384. int pthread_cond_init(pthread_cond_t* cond, const pthread_condattr_t* attr)
  385. {
  386. cond->value = 0;
  387. cond->previous = 0;
  388. cond->clockid = attr ? attr->clockid : CLOCK_MONOTONIC;
  389. return 0;
  390. }
  391. int pthread_cond_destroy(pthread_cond_t*)
  392. {
  393. return 0;
  394. }
  395. static int cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* abstime)
  396. {
  397. i32 value = cond->value;
  398. cond->previous = value;
  399. pthread_mutex_unlock(mutex);
  400. int rc = futex(&cond->value, FUTEX_WAIT, value, abstime);
  401. pthread_mutex_lock(mutex);
  402. return rc;
  403. }
  404. int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex)
  405. {
  406. int rc = cond_wait(cond, mutex, nullptr);
  407. ASSERT(rc == 0);
  408. return 0;
  409. }
  410. int pthread_condattr_init(pthread_condattr_t* attr)
  411. {
  412. attr->clockid = CLOCK_MONOTONIC;
  413. return 0;
  414. }
  415. int pthread_condattr_destroy(pthread_condattr_t*)
  416. {
  417. return 0;
  418. }
  419. int pthread_condattr_setclock(pthread_condattr_t* attr, clockid_t clock)
  420. {
  421. attr->clockid = clock;
  422. return 0;
  423. }
  424. int pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* abstime)
  425. {
  426. return cond_wait(cond, mutex, abstime);
  427. }
  428. int pthread_cond_signal(pthread_cond_t* cond)
  429. {
  430. u32 value = cond->previous + 1;
  431. cond->value = value;
  432. int rc = futex(&cond->value, FUTEX_WAKE, 1, nullptr);
  433. ASSERT(rc == 0);
  434. return 0;
  435. }
  436. int pthread_cond_broadcast(pthread_cond_t* cond)
  437. {
  438. u32 value = cond->previous + 1;
  439. cond->value = value;
  440. int rc = futex(&cond->value, FUTEX_WAKE, INT32_MAX, nullptr);
  441. ASSERT(rc == 0);
  442. return 0;
  443. }
  444. static const int max_keys = 64;
  445. typedef void (*KeyDestructor)(void*);
  446. struct KeyTable {
  447. // FIXME: Invoke key destructors on thread exit!
  448. KeyDestructor destructors[64] { nullptr };
  449. int next { 0 };
  450. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  451. };
  452. struct SpecificTable {
  453. void* values[64] { nullptr };
  454. };
  455. static KeyTable s_keys;
  456. __thread SpecificTable t_specifics;
  457. int pthread_key_create(pthread_key_t* key, KeyDestructor destructor)
  458. {
  459. int ret = 0;
  460. pthread_mutex_lock(&s_keys.mutex);
  461. if (s_keys.next >= max_keys) {
  462. ret = ENOMEM;
  463. } else {
  464. *key = s_keys.next++;
  465. s_keys.destructors[*key] = destructor;
  466. ret = 0;
  467. }
  468. pthread_mutex_unlock(&s_keys.mutex);
  469. return ret;
  470. }
  471. void* pthread_getspecific(pthread_key_t key)
  472. {
  473. if (key < 0)
  474. return nullptr;
  475. if (key >= max_keys)
  476. return nullptr;
  477. return t_specifics.values[key];
  478. }
  479. int pthread_setspecific(pthread_key_t key, const void* value)
  480. {
  481. if (key < 0)
  482. return EINVAL;
  483. if (key >= max_keys)
  484. return EINVAL;
  485. t_specifics.values[key] = const_cast<void*>(value);
  486. return 0;
  487. }
  488. int pthread_setname_np(pthread_t thread, const char* name)
  489. {
  490. if (!name)
  491. return EFAULT;
  492. int rc = syscall(SC_set_thread_name, thread, name, strlen(name));
  493. __RETURN_PTHREAD_ERROR(rc);
  494. }
  495. int pthread_getname_np(pthread_t thread, char* buffer, size_t buffer_size)
  496. {
  497. int rc = syscall(SC_get_thread_name, thread, buffer, buffer_size);
  498. __RETURN_PTHREAD_ERROR(rc);
  499. }
  500. } // extern "C"