xref: /llvm-project/libc/src/pthread/pthread_create.cpp (revision f4580c6d5a44379f3b1be033f39ec7af78dbbbfa)
1 //===-- Linux implementation of the pthread_create function ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "pthread_create.h"
10 
11 #include "src/__support/common.h"
12 #include "src/__support/threads/thread.h"
13 
14 #include <errno.h>
15 #include <pthread.h> // For pthread_* type definitions.
16 
17 namespace __llvm_libc {
18 
19 static_assert(sizeof(pthread_t) == sizeof(__llvm_libc::Thread),
20               "Mismatch between pthread_t and internal Thread.");
21 
22 LLVM_LIBC_FUNCTION(int, pthread_create,
23                    (pthread_t *__restrict th,
24                     const pthread_attr_t *__restrict attr,
25                     __pthread_start_t func, void *arg)) {
26   auto *thread = reinterpret_cast<__llvm_libc::Thread *>(th);
27   int result = thread->run(func, arg, nullptr, 0);
28   if (result != 0 && result != EPERM)
29     return EAGAIN;
30   return result;
31 }
32 
33 } // namespace __llvm_libc
34