1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "components/nacl/loader/nonsfi/irt_interfaces.h"
17 // We heuristically chose 1M for the stack size per thread.
18 const int kStackSize
= 1024 * 1024;
20 // For RAII of pthread_attr_t.
21 class ScopedPthreadAttrPtr
{
23 ScopedPthreadAttrPtr(pthread_attr_t
* attr
) : attr_(attr
) {
25 ~ScopedPthreadAttrPtr() {
26 pthread_attr_destroy(attr_
);
30 pthread_attr_t
* attr_
;
33 struct ThreadContext
{
38 // A thread local pointer to support nacl_irt_tls.
39 // This should be initialized at the beginning of ThreadMain, which is a thin
40 // wrapper of a user function called on a newly created thread, and may be
41 // reset via IrtTlsInit(). The pointer can be obtained via IrtTlsGet().
42 __thread
void* g_thread_ptr
;
44 void* ThreadMain(void *arg
) {
45 ::scoped_ptr
<ThreadContext
> context(static_cast<ThreadContext
*>(arg
));
46 g_thread_ptr
= context
->thread_ptr
;
48 // Release the memory of context before running start_func.
49 void (*start_func
)() = context
->start_func
;
56 int IrtThreadCreate(void (*start_func
)(), void* stack
, void* thread_ptr
) {
58 int error
= pthread_attr_init(&attr
);
61 ScopedPthreadAttrPtr
scoped_attr_ptr(&attr
);
63 // Note: Currently we ignore the argument stack.
64 error
= pthread_attr_setstacksize(&attr
, kStackSize
);
68 error
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
72 ::scoped_ptr
<ThreadContext
> context(new ThreadContext
);
73 context
->start_func
= start_func
;
74 context
->thread_ptr
= thread_ptr
;
77 error
= pthread_create(&tid
, &attr
, ThreadMain
, context
.get());
81 // The ownership of the context is taken by the created thread. So, here we
82 // just manually release it.
83 ignore_result(context
.release());
87 void IrtThreadExit(int32_t* stack_flag
) {
88 // As we actually don't use stack given to thread_create, it means that the
89 // memory can be released whenever.
95 int IrtThreadNice(const int nice
) {
96 // TODO(https://code.google.com/p/nativeclient/issues/detail?id=3734):
97 // Implement this method.
98 // Note that this is just a hint, so here we just return success without
103 int IrtTlsInit(void* thread_ptr
) {
104 g_thread_ptr
= thread_ptr
;
114 const nacl_irt_thread kIrtThread
= {
120 const nacl_irt_tls kIrtTls
= {
125 } // namespace nonsfi