1 //===-- Linux implementation of the pthread_create function ---------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "pthread_create.h"
11 #include "pthread_attr_destroy.h"
12 #include "pthread_attr_init.h"
14 #include "pthread_attr_getdetachstate.h"
15 #include "pthread_attr_getguardsize.h"
16 #include "pthread_attr_getstack.h"
18 #include "src/__support/common.h"
19 #include "src/__support/macros/optimization.h"
20 #include "src/__support/threads/thread.h"
23 #include <pthread.h> // For pthread_* type definitions.
25 namespace LIBC_NAMESPACE
{
27 static_assert(sizeof(pthread_t
) == sizeof(LIBC_NAMESPACE::Thread
),
28 "Mismatch between pthread_t and internal Thread.");
30 LLVM_LIBC_FUNCTION(int, pthread_create
,
31 (pthread_t
*__restrict th
,
32 const pthread_attr_t
*__restrict attr
,
33 __pthread_start_t func
, void *arg
)) {
34 pthread_attr_t default_attr
;
35 if (attr
== nullptr) {
36 // We failed to initialize attributes (should be impossible)
37 if (LIBC_UNLIKELY(LIBC_NAMESPACE::pthread_attr_init(&default_attr
) != 0))
44 size_t stacksize
, guardsize
;
47 // As of writing this all the `pthread_attr_get*` functions always succeed.
49 LIBC_NAMESPACE::pthread_attr_getstack(attr
, &stack
, &stacksize
) != 0))
53 LIBC_NAMESPACE::pthread_attr_getguardsize(attr
, &guardsize
) != 0))
57 LIBC_NAMESPACE::pthread_attr_getdetachstate(attr
, &detachstate
) != 0))
60 if (attr
== &default_attr
)
61 // Should we fail here? Its non-issue as the moment as pthread_attr_destroy
63 if (LIBC_UNLIKELY(LIBC_NAMESPACE::pthread_attr_destroy(&default_attr
) != 0))
66 if (stacksize
&& stacksize
< PTHREAD_STACK_MIN
)
69 if (guardsize
% EXEC_PAGESIZE
!= 0)
72 if (detachstate
!= PTHREAD_CREATE_DETACHED
&&
73 detachstate
!= PTHREAD_CREATE_JOINABLE
)
76 // Thread::run will check validity of the `stack` argument (stack alignment is
77 // universal, not sure a pthread requirement).
79 auto *thread
= reinterpret_cast<LIBC_NAMESPACE::Thread
*>(th
);
80 int result
= thread
->run(func
, arg
, stack
, stacksize
, guardsize
,
81 detachstate
== PTHREAD_CREATE_DETACHED
);
82 if (result
!= 0 && result
!= EPERM
&& result
!= EINVAL
)
87 } // namespace LIBC_NAMESPACE