This source file includes following definitions.
- ThreadMain
- IrtThreadCreate
- IrtThreadExit
- IrtThreadNice
- IrtTlsInit
- IrtTlsGet
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "components/nacl/loader/nonsfi/irt_interfaces.h"
namespace nacl {
namespace nonsfi {
namespace {
const int kStackSize = 1024 * 1024;
class ScopedPthreadAttrPtr {
public:
ScopedPthreadAttrPtr(pthread_attr_t* attr) : attr_(attr) {
}
~ScopedPthreadAttrPtr() {
pthread_attr_destroy(attr_);
}
private:
pthread_attr_t* attr_;
};
struct ThreadContext {
void (*start_func)();
void* thread_ptr;
};
__thread void* g_thread_ptr;
void* ThreadMain(void *arg) {
::scoped_ptr<ThreadContext> context(static_cast<ThreadContext*>(arg));
g_thread_ptr = context->thread_ptr;
void (*start_func)() = context->start_func;
context.reset();
start_func();
abort();
}
int IrtThreadCreate(void (*start_func)(), void* stack, void* thread_ptr) {
pthread_attr_t attr;
int error = pthread_attr_init(&attr);
if (error != 0)
return error;
ScopedPthreadAttrPtr scoped_attr_ptr(&attr);
error = pthread_attr_setstacksize(&attr, kStackSize);
if (error != 0)
return error;
error = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (error != 0)
return error;
::scoped_ptr<ThreadContext> context(new ThreadContext);
context->start_func = start_func;
context->thread_ptr = thread_ptr;
pthread_t tid;
error = pthread_create(&tid, &attr, ThreadMain, context.get());
if (error != 0)
return error;
ignore_result(context.release());
return 0;
}
void IrtThreadExit(int32_t* stack_flag) {
if (stack_flag)
*stack_flag = 0;
pthread_exit(NULL);
}
int IrtThreadNice(const int nice) {
return 0;
}
int IrtTlsInit(void* thread_ptr) {
g_thread_ptr = thread_ptr;
return 0;
}
void* IrtTlsGet() {
return g_thread_ptr;
}
}
const nacl_irt_thread kIrtThread = {
IrtThreadCreate,
IrtThreadExit,
IrtThreadNice,
};
const nacl_irt_tls kIrtTls = {
IrtTlsInit,
IrtTlsGet,
};
}
}