pthread.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 * MB;
  44. constexpr size_t highest_reasonable_guard_size = 32 * PAGE_SIZE;
  45. constexpr size_t highest_reasonable_stack_size = 8 * MB; // That's the default in Ubuntu?
  46. extern "C" {
  47. static void* pthread_create_helper(void* (*routine)(void*), void* argument)
  48. {
  49. void* ret_val = routine(argument);
  50. pthread_exit(ret_val);
  51. return nullptr;
  52. }
  53. static int create_thread(void* (*entry)(void*), void* argument, PthreadAttrImpl* thread_params)
  54. {
  55. void** stack = (void**)((uintptr_t)thread_params->m_stack_location + thread_params->m_stack_size);
  56. auto push_on_stack = [&](void* data) {
  57. stack--;
  58. *stack = data;
  59. thread_params->m_stack_size -= sizeof(void*);
  60. };
  61. // We set up the stack for pthread_create_helper.
  62. // Note that we need to align the stack to 16B, accounting for
  63. // the fact that we also push 8 bytes.
  64. while (((uintptr_t)stack - 8) % 16 != 0)
  65. push_on_stack(nullptr);
  66. push_on_stack(argument);
  67. push_on_stack((void*)entry);
  68. ASSERT((uintptr_t)stack % 16 == 0);
  69. // Push a fake return address
  70. push_on_stack(nullptr);
  71. return syscall(SC_create_thread, pthread_create_helper, thread_params);
  72. }
  73. [[noreturn]]
  74. static void exit_thread(void* code)
  75. {
  76. syscall(SC_exit_thread, code);
  77. ASSERT_NOT_REACHED();
  78. }
  79. int pthread_self()
  80. {
  81. return gettid();
  82. }
  83. int pthread_create(pthread_t* thread, pthread_attr_t* attributes, void* (*start_routine)(void*), void* argument_to_start_routine)
  84. {
  85. if (!thread)
  86. return -EINVAL;
  87. PthreadAttrImpl default_attributes {};
  88. PthreadAttrImpl** arg_attributes = reinterpret_cast<PthreadAttrImpl**>(attributes);
  89. PthreadAttrImpl* used_attributes = arg_attributes ? *arg_attributes : &default_attributes;
  90. if (!used_attributes->m_stack_location) {
  91. // adjust stack size, user might have called setstacksize, which has no restrictions on size/alignment
  92. if (0 != (used_attributes->m_stack_size % required_stack_alignment))
  93. used_attributes->m_stack_size += required_stack_alignment - (used_attributes->m_stack_size % required_stack_alignment);
  94. 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");
  95. if (!used_attributes->m_stack_location)
  96. return -1;
  97. }
  98. #ifdef PTHREAD_DEBUG
  99. 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",
  100. used_attributes,
  101. (PTHREAD_CREATE_JOINABLE == used_attributes->m_detach_state) ? "joinable" : "detached",
  102. used_attributes->m_schedule_priority,
  103. used_attributes->m_guard_page_size,
  104. used_attributes->m_stack_size,
  105. used_attributes->m_stack_location);
  106. #endif
  107. int rc = create_thread(start_routine, argument_to_start_routine, used_attributes);
  108. if (rc < 0)
  109. return rc;
  110. *thread = rc;
  111. return 0;
  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. return syscall(SC_join_thread, thread, exit_value_ptr);
  120. }
  121. int pthread_detach(pthread_t thread)
  122. {
  123. return syscall(SC_detach_thread, thread);
  124. }
  125. int pthread_sigmask(int how, const sigset_t* set, sigset_t* old_set)
  126. {
  127. if (sigprocmask(how, set, old_set))
  128. return errno;
  129. return 0;
  130. }
  131. int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attributes)
  132. {
  133. mutex->lock = 0;
  134. mutex->owner = 0;
  135. mutex->level = 0;
  136. mutex->type = attributes ? attributes->type : PTHREAD_MUTEX_NORMAL;
  137. return 0;
  138. }
  139. int pthread_mutex_destroy(pthread_mutex_t*)
  140. {
  141. return 0;
  142. }
  143. int pthread_mutex_lock(pthread_mutex_t* mutex)
  144. {
  145. auto& atomic = reinterpret_cast<Atomic<u32>&>(mutex->lock);
  146. pthread_t this_thread = pthread_self();
  147. for (;;) {
  148. u32 expected = false;
  149. if (!atomic.compare_exchange_strong(expected, true, AK::memory_order_acq_rel)) {
  150. if (mutex->type == PTHREAD_MUTEX_RECURSIVE && mutex->owner == this_thread) {
  151. mutex->level++;
  152. return 0;
  153. }
  154. sched_yield();
  155. continue;
  156. }
  157. mutex->owner = this_thread;
  158. mutex->level = 0;
  159. return 0;
  160. }
  161. }
  162. int pthread_mutex_trylock(pthread_mutex_t* mutex)
  163. {
  164. auto& atomic = reinterpret_cast<Atomic<u32>&>(mutex->lock);
  165. u32 expected = false;
  166. if (!atomic.compare_exchange_strong(expected, true, AK::memory_order_acq_rel)) {
  167. if (mutex->type == PTHREAD_MUTEX_RECURSIVE && mutex->owner == pthread_self()) {
  168. mutex->level++;
  169. return 0;
  170. }
  171. return EBUSY;
  172. }
  173. mutex->owner = pthread_self();
  174. mutex->level = 0;
  175. return 0;
  176. }
  177. int pthread_mutex_unlock(pthread_mutex_t* mutex)
  178. {
  179. if (mutex->type == PTHREAD_MUTEX_RECURSIVE && mutex->level > 0) {
  180. mutex->level--;
  181. return 0;
  182. }
  183. mutex->owner = 0;
  184. mutex->lock = 0;
  185. return 0;
  186. }
  187. int pthread_mutexattr_init(pthread_mutexattr_t* attr)
  188. {
  189. attr->type = PTHREAD_MUTEX_NORMAL;
  190. return 0;
  191. }
  192. int pthread_mutexattr_destroy(pthread_mutexattr_t*)
  193. {
  194. return 0;
  195. }
  196. int pthread_mutexattr_settype(pthread_mutexattr_t* attr, int type)
  197. {
  198. if (!attr)
  199. return EINVAL;
  200. if (type != PTHREAD_MUTEX_NORMAL && type != PTHREAD_MUTEX_RECURSIVE)
  201. return EINVAL;
  202. attr->type = type;
  203. return 0;
  204. }
  205. int pthread_attr_init(pthread_attr_t* attributes)
  206. {
  207. auto* impl = new PthreadAttrImpl {};
  208. *attributes = impl;
  209. #ifdef PTHREAD_DEBUG
  210. 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",
  211. impl,
  212. (PTHREAD_CREATE_JOINABLE == impl->m_detach_state) ? "joinable" : "detached",
  213. impl->m_schedule_priority,
  214. impl->m_guard_page_size,
  215. impl->m_stack_size,
  216. impl->m_stack_location);
  217. #endif
  218. return 0;
  219. }
  220. int pthread_attr_destroy(pthread_attr_t* attributes)
  221. {
  222. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  223. delete attributes_impl;
  224. return 0;
  225. }
  226. int pthread_attr_getdetachstate(const pthread_attr_t* attributes, int* p_detach_state)
  227. {
  228. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  229. if (!attributes_impl || !p_detach_state)
  230. return EINVAL;
  231. *p_detach_state = attributes_impl->m_detach_state;
  232. return 0;
  233. }
  234. int pthread_attr_setdetachstate(pthread_attr_t* attributes, int detach_state)
  235. {
  236. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  237. if (!attributes_impl)
  238. return EINVAL;
  239. if ((PTHREAD_CREATE_JOINABLE != detach_state) || PTHREAD_CREATE_DETACHED != detach_state)
  240. return EINVAL;
  241. attributes_impl->m_detach_state = detach_state;
  242. #ifdef PTHREAD_DEBUG
  243. dbgprintf("pthread_attr_setdetachstate: Thread attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  244. attributes_impl,
  245. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  246. attributes_impl->m_schedule_priority,
  247. attributes_impl->m_guard_page_size,
  248. attributes_impl->m_stack_size,
  249. attributes_impl->m_stack_location);
  250. #endif
  251. return 0;
  252. }
  253. int pthread_attr_getguardsize(const pthread_attr_t* attributes, size_t* p_guard_size)
  254. {
  255. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  256. if (!attributes_impl || !p_guard_size)
  257. return EINVAL;
  258. *p_guard_size = attributes_impl->m_reported_guard_page_size;
  259. return 0;
  260. }
  261. int pthread_attr_setguardsize(pthread_attr_t* attributes, size_t guard_size)
  262. {
  263. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  264. if (!attributes_impl)
  265. return EINVAL;
  266. size_t actual_guard_size = guard_size;
  267. // round up
  268. if (0 != (guard_size % PAGE_SIZE))
  269. actual_guard_size += PAGE_SIZE - (guard_size % PAGE_SIZE);
  270. // what is the user even doing?
  271. if (actual_guard_size > highest_reasonable_guard_size) {
  272. return EINVAL;
  273. }
  274. attributes_impl->m_guard_page_size = actual_guard_size;
  275. attributes_impl->m_reported_guard_page_size = guard_size; // POSIX, why?
  276. #ifdef PTHREAD_DEBUG
  277. dbgprintf("pthread_attr_setguardsize: Thread attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  278. attributes_impl,
  279. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  280. attributes_impl->m_schedule_priority,
  281. attributes_impl->m_guard_page_size,
  282. attributes_impl->m_stack_size,
  283. attributes_impl->m_stack_location);
  284. #endif
  285. return 0;
  286. }
  287. int pthread_attr_getschedparam(const pthread_attr_t* attributes, struct sched_param* p_sched_param)
  288. {
  289. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  290. if (!attributes_impl || !p_sched_param)
  291. return EINVAL;
  292. p_sched_param->sched_priority = attributes_impl->m_schedule_priority;
  293. return 0;
  294. }
  295. int pthread_attr_setschedparam(pthread_attr_t* attributes, const struct sched_param* p_sched_param)
  296. {
  297. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  298. if (!attributes_impl || !p_sched_param)
  299. return EINVAL;
  300. if (p_sched_param->sched_priority < THREAD_PRIORITY_MIN || p_sched_param->sched_priority > THREAD_PRIORITY_MAX)
  301. return ENOTSUP;
  302. attributes_impl->m_schedule_priority = p_sched_param->sched_priority;
  303. #ifdef PTHREAD_DEBUG
  304. dbgprintf("pthread_attr_setschedparam: Thread attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  305. attributes_impl,
  306. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  307. attributes_impl->m_schedule_priority,
  308. attributes_impl->m_guard_page_size,
  309. attributes_impl->m_stack_size,
  310. attributes_impl->m_stack_location);
  311. #endif
  312. return 0;
  313. }
  314. int pthread_attr_getstack(const pthread_attr_t* attributes, void** p_stack_ptr, size_t* p_stack_size)
  315. {
  316. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  317. if (!attributes_impl || !p_stack_ptr || !p_stack_size)
  318. return EINVAL;
  319. *p_stack_ptr = attributes_impl->m_stack_location.ptr();
  320. *p_stack_size = attributes_impl->m_stack_size;
  321. return 0;
  322. }
  323. int pthread_attr_setstack(pthread_attr_t* attributes, void* p_stack, size_t stack_size)
  324. {
  325. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  326. if (!attributes_impl || !p_stack)
  327. return EINVAL;
  328. // Check for required alignment on size
  329. if (0 != (stack_size % required_stack_alignment))
  330. return EINVAL;
  331. // FIXME: Check for required alignment on pointer?
  332. // FIXME: "[EACCES] The stack page(s) described by stackaddr and stacksize are not both readable and writable by the thread."
  333. // Have to check that the whole range is mapped to this process/thread? Can we defer this to create_thread?
  334. attributes_impl->m_stack_size = stack_size;
  335. attributes_impl->m_stack_location = p_stack;
  336. #ifdef PTHREAD_DEBUG
  337. dbgprintf("pthread_attr_setstack: Thread attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  338. attributes_impl,
  339. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  340. attributes_impl->m_schedule_priority,
  341. attributes_impl->m_guard_page_size,
  342. attributes_impl->m_stack_size,
  343. attributes_impl->m_stack_location);
  344. #endif
  345. return 0;
  346. }
  347. int pthread_attr_getstacksize(const pthread_attr_t* attributes, size_t* p_stack_size)
  348. {
  349. auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
  350. if (!attributes_impl || !p_stack_size)
  351. return EINVAL;
  352. *p_stack_size = attributes_impl->m_stack_size;
  353. return 0;
  354. }
  355. int pthread_attr_setstacksize(pthread_attr_t* attributes, size_t stack_size)
  356. {
  357. auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
  358. if (!attributes_impl)
  359. return EINVAL;
  360. if ((stack_size < PTHREAD_STACK_MIN) || stack_size > highest_reasonable_stack_size)
  361. return EINVAL;
  362. attributes_impl->m_stack_size = stack_size;
  363. #ifdef PTHREAD_DEBUG
  364. dbgprintf("pthread_attr_setstacksize: Thread attributes at %p, detach state %s, priority %d, guard page size %d, stack size %d, stack location %p\n",
  365. attributes_impl,
  366. (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
  367. attributes_impl->m_schedule_priority,
  368. attributes_impl->m_guard_page_size,
  369. attributes_impl->m_stack_size,
  370. attributes_impl->m_stack_location);
  371. #endif
  372. return 0;
  373. }
  374. int pthread_getschedparam(pthread_t thread, int* policy, struct sched_param* param)
  375. {
  376. (void)thread;
  377. (void)policy;
  378. (void)param;
  379. return 0;
  380. }
  381. int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param* param)
  382. {
  383. (void)thread;
  384. (void)policy;
  385. (void)param;
  386. return 0;
  387. }
  388. int pthread_cond_init(pthread_cond_t* cond, const pthread_condattr_t* attr)
  389. {
  390. cond->value = 0;
  391. cond->previous = 0;
  392. cond->clockid = attr ? attr->clockid : CLOCK_MONOTONIC;
  393. return 0;
  394. }
  395. int pthread_cond_destroy(pthread_cond_t*)
  396. {
  397. return 0;
  398. }
  399. static int cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* abstime)
  400. {
  401. i32 value = cond->value;
  402. cond->previous = value;
  403. pthread_mutex_unlock(mutex);
  404. int rc = futex(&cond->value, FUTEX_WAIT, value, abstime);
  405. pthread_mutex_lock(mutex);
  406. return rc;
  407. }
  408. int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex)
  409. {
  410. int rc = cond_wait(cond, mutex, nullptr);
  411. ASSERT(rc == 0);
  412. return 0;
  413. }
  414. int pthread_condattr_init(pthread_condattr_t* attr)
  415. {
  416. attr->clockid = CLOCK_MONOTONIC;
  417. return 0;
  418. }
  419. int pthread_condattr_destroy(pthread_condattr_t*)
  420. {
  421. return 0;
  422. }
  423. int pthread_condattr_setclock(pthread_condattr_t* attr, clockid_t clock)
  424. {
  425. attr->clockid = clock;
  426. return 0;
  427. }
  428. int pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* abstime)
  429. {
  430. return cond_wait(cond, mutex, abstime);
  431. }
  432. int pthread_cond_signal(pthread_cond_t* cond)
  433. {
  434. u32 value = cond->previous + 1;
  435. cond->value = value;
  436. int rc = futex(&cond->value, FUTEX_WAKE, 1, nullptr);
  437. ASSERT(rc == 0);
  438. return 0;
  439. }
  440. int pthread_cond_broadcast(pthread_cond_t* cond)
  441. {
  442. u32 value = cond->previous + 1;
  443. cond->value = value;
  444. int rc = futex(&cond->value, FUTEX_WAKE, INT32_MAX, nullptr);
  445. ASSERT(rc == 0);
  446. return 0;
  447. }
  448. static const int max_keys = 64;
  449. typedef void (*KeyDestructor)(void*);
  450. struct KeyTable {
  451. // FIXME: Invoke key destructors on thread exit!
  452. KeyDestructor destructors[64] { nullptr };
  453. int next { 0 };
  454. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  455. };
  456. struct SpecificTable {
  457. void* values[64] { nullptr };
  458. };
  459. static KeyTable s_keys;
  460. __thread SpecificTable t_specifics;
  461. int pthread_key_create(pthread_key_t* key, KeyDestructor destructor)
  462. {
  463. int ret = 0;
  464. pthread_mutex_lock(&s_keys.mutex);
  465. if (s_keys.next >= max_keys) {
  466. ret = ENOMEM;
  467. } else {
  468. *key = s_keys.next++;
  469. s_keys.destructors[*key] = destructor;
  470. ret = 0;
  471. }
  472. pthread_mutex_unlock(&s_keys.mutex);
  473. return ret;
  474. }
  475. void* pthread_getspecific(pthread_key_t key)
  476. {
  477. if (key < 0)
  478. return nullptr;
  479. if (key >= max_keys)
  480. return nullptr;
  481. return t_specifics.values[key];
  482. }
  483. int pthread_setspecific(pthread_key_t key, const void* value)
  484. {
  485. if (key < 0)
  486. return EINVAL;
  487. if (key >= max_keys)
  488. return EINVAL;
  489. t_specifics.values[key] = const_cast<void*>(value);
  490. return 0;
  491. }
  492. int pthread_setname_np(pthread_t thread, const char* name)
  493. {
  494. if (!name)
  495. return EFAULT;
  496. return syscall(SC_set_thread_name, thread, name, strlen(name));
  497. }
  498. int pthread_getname_np(pthread_t thread, char* buffer, size_t buffer_size)
  499. {
  500. return syscall(SC_get_thread_name, thread, buffer, buffer_size);
  501. }
  502. } // extern "C"