xref: /llvm-project/libc/src/threads/thrd_create.cpp (revision 46944b0cbc9a9d8daad0182c40fcd3560bc9ca35)
12ce09e68SSiva Chandra Reddy //===-- Linux implementation of the thrd_create function ------------------===//
22ce09e68SSiva Chandra Reddy //
32ce09e68SSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42ce09e68SSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
52ce09e68SSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62ce09e68SSiva Chandra Reddy //
72ce09e68SSiva Chandra Reddy //===----------------------------------------------------------------------===//
82ce09e68SSiva Chandra Reddy 
92ce09e68SSiva Chandra Reddy #include "src/threads/thrd_create.h"
102ce09e68SSiva Chandra Reddy #include "src/__support/common.h"
115ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
122ce09e68SSiva Chandra Reddy #include "src/__support/threads/thread.h"
13*46944b0cSJob Henandez Lara #include "src/errno/libc_errno.h"
142ce09e68SSiva Chandra Reddy 
152ce09e68SSiva Chandra Reddy #include <threads.h> // For thrd_* type definitions.
162ce09e68SSiva Chandra Reddy 
175ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
182ce09e68SSiva Chandra Reddy 
19b6bc9d72SGuillaume Chatelet static_assert(sizeof(thrd_t) == sizeof(LIBC_NAMESPACE::Thread),
20f4580c6dSSiva Chandra Reddy               "Mismatch between thrd_t and internal Thread.");
212ce09e68SSiva Chandra Reddy 
222ce09e68SSiva Chandra Reddy LLVM_LIBC_FUNCTION(int, thrd_create,
232ce09e68SSiva Chandra Reddy                    (thrd_t * th, thrd_start_t func, void *arg)) {
24b6bc9d72SGuillaume Chatelet   auto *thread = reinterpret_cast<LIBC_NAMESPACE::Thread *>(th);
256a185718SNoah Goldstein   int result = thread->run(func, arg);
262ce09e68SSiva Chandra Reddy   if (result == 0)
272ce09e68SSiva Chandra Reddy     return thrd_success;
282ce09e68SSiva Chandra Reddy   else if (result == ENOMEM)
292ce09e68SSiva Chandra Reddy     return thrd_nomem;
302ce09e68SSiva Chandra Reddy   else
312ce09e68SSiva Chandra Reddy     return thrd_error;
322ce09e68SSiva Chandra Reddy }
332ce09e68SSiva Chandra Reddy 
345ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
35