|
|
|
|
@ -22,7 +22,8 @@ typedef struct {
|
|
|
|
|
static os_thread_local_attribute os_signal_handler signal_handler;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static void *os_thread_wrapper(void *arg)
|
|
|
|
|
static void *
|
|
|
|
|
os_thread_wrapper(void *arg)
|
|
|
|
|
{
|
|
|
|
|
thread_wrapper_arg *targ = arg;
|
|
|
|
|
thread_start_routine_t start_func = targ->start;
|
|
|
|
|
@ -44,8 +45,9 @@ static void *os_thread_wrapper(void *arg)
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
|
|
|
|
|
void *arg, unsigned int stack_size, int prio)
|
|
|
|
|
int
|
|
|
|
|
os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
|
|
|
|
|
void *arg, unsigned int stack_size, int prio)
|
|
|
|
|
{
|
|
|
|
|
pthread_attr_t tattr;
|
|
|
|
|
thread_wrapper_arg *targ;
|
|
|
|
|
@ -63,7 +65,7 @@ int os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
|
|
|
|
|
return BHT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
targ = (thread_wrapper_arg*) BH_MALLOC(sizeof(*targ));
|
|
|
|
|
targ = (thread_wrapper_arg *)BH_MALLOC(sizeof(*targ));
|
|
|
|
|
if (!targ) {
|
|
|
|
|
pthread_attr_destroy(&tattr);
|
|
|
|
|
return BHT_ERROR;
|
|
|
|
|
@ -85,24 +87,28 @@ int os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
|
|
|
|
|
return BHT_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
|
|
|
|
|
unsigned int stack_size)
|
|
|
|
|
int
|
|
|
|
|
os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
|
|
|
|
|
unsigned int stack_size)
|
|
|
|
|
{
|
|
|
|
|
return os_thread_create_with_prio(tid, start, arg, stack_size,
|
|
|
|
|
BH_THREAD_DEFAULT_PRIORITY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
korp_tid os_self_thread()
|
|
|
|
|
korp_tid
|
|
|
|
|
os_self_thread()
|
|
|
|
|
{
|
|
|
|
|
return (korp_tid) pthread_self();
|
|
|
|
|
return (korp_tid)pthread_self();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int os_mutex_init(korp_mutex *mutex)
|
|
|
|
|
int
|
|
|
|
|
os_mutex_init(korp_mutex *mutex)
|
|
|
|
|
{
|
|
|
|
|
return pthread_mutex_init(mutex, NULL) == 0 ? BHT_OK : BHT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int os_recursive_mutex_init(korp_mutex *mutex)
|
|
|
|
|
int
|
|
|
|
|
os_recursive_mutex_init(korp_mutex *mutex)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
@ -120,7 +126,8 @@ int os_recursive_mutex_init(korp_mutex *mutex)
|
|
|
|
|
return ret == 0 ? BHT_OK : BHT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int os_mutex_destroy(korp_mutex *mutex)
|
|
|
|
|
int
|
|
|
|
|
os_mutex_destroy(korp_mutex *mutex)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
@ -130,7 +137,8 @@ int os_mutex_destroy(korp_mutex *mutex)
|
|
|
|
|
return ret == 0 ? BHT_OK : BHT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int os_mutex_lock(korp_mutex *mutex)
|
|
|
|
|
int
|
|
|
|
|
os_mutex_lock(korp_mutex *mutex)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
@ -140,7 +148,8 @@ int os_mutex_lock(korp_mutex *mutex)
|
|
|
|
|
return ret == 0 ? BHT_OK : BHT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int os_mutex_unlock(korp_mutex *mutex)
|
|
|
|
|
int
|
|
|
|
|
os_mutex_unlock(korp_mutex *mutex)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
@ -150,7 +159,8 @@ int os_mutex_unlock(korp_mutex *mutex)
|
|
|
|
|
return ret == 0 ? BHT_OK : BHT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int os_cond_init(korp_cond *cond)
|
|
|
|
|
int
|
|
|
|
|
os_cond_init(korp_cond *cond)
|
|
|
|
|
{
|
|
|
|
|
assert(cond);
|
|
|
|
|
|
|
|
|
|
@ -160,7 +170,8 @@ int os_cond_init(korp_cond *cond)
|
|
|
|
|
return BHT_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int os_cond_destroy(korp_cond *cond)
|
|
|
|
|
int
|
|
|
|
|
os_cond_destroy(korp_cond *cond)
|
|
|
|
|
{
|
|
|
|
|
assert(cond);
|
|
|
|
|
|
|
|
|
|
@ -170,7 +181,8 @@ int os_cond_destroy(korp_cond *cond)
|
|
|
|
|
return BHT_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int os_cond_wait(korp_cond *cond, korp_mutex *mutex)
|
|
|
|
|
int
|
|
|
|
|
os_cond_wait(korp_cond *cond, korp_mutex *mutex)
|
|
|
|
|
{
|
|
|
|
|
assert(cond);
|
|
|
|
|
assert(mutex);
|
|
|
|
|
@ -181,7 +193,8 @@ int os_cond_wait(korp_cond *cond, korp_mutex *mutex)
|
|
|
|
|
return BHT_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void msec_nsec_to_abstime(struct timespec *ts, uint64 usec)
|
|
|
|
|
static void
|
|
|
|
|
msec_nsec_to_abstime(struct timespec *ts, uint64 usec)
|
|
|
|
|
{
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
time_t tv_sec_new;
|
|
|
|
|
@ -201,8 +214,7 @@ static void msec_nsec_to_abstime(struct timespec *ts, uint64 usec)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tv_nsec_new = (long int)(tv.tv_usec * 1000 + (usec % 1000000) * 1000);
|
|
|
|
|
if (tv.tv_usec * 1000 >= tv.tv_usec
|
|
|
|
|
&& tv_nsec_new >= tv.tv_usec * 1000) {
|
|
|
|
|
if (tv.tv_usec * 1000 >= tv.tv_usec && tv_nsec_new >= tv.tv_usec * 1000) {
|
|
|
|
|
ts->tv_nsec = tv_nsec_new;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
@ -218,7 +230,8 @@ static void msec_nsec_to_abstime(struct timespec *ts, uint64 usec)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
|
|
|
|
|
int
|
|
|
|
|
os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
struct timespec abstime;
|
|
|
|
|
@ -236,7 +249,8 @@ int os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int os_cond_signal(korp_cond *cond)
|
|
|
|
|
int
|
|
|
|
|
os_cond_signal(korp_cond *cond)
|
|
|
|
|
{
|
|
|
|
|
assert(cond);
|
|
|
|
|
|
|
|
|
|
@ -246,17 +260,20 @@ int os_cond_signal(korp_cond *cond)
|
|
|
|
|
return BHT_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int os_thread_join(korp_tid thread, void **value_ptr)
|
|
|
|
|
int
|
|
|
|
|
os_thread_join(korp_tid thread, void **value_ptr)
|
|
|
|
|
{
|
|
|
|
|
return pthread_join(thread, value_ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int os_thread_detach(korp_tid thread)
|
|
|
|
|
int
|
|
|
|
|
os_thread_detach(korp_tid thread)
|
|
|
|
|
{
|
|
|
|
|
return pthread_detach(thread);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void os_thread_exit(void *retval)
|
|
|
|
|
void
|
|
|
|
|
os_thread_exit(void *retval)
|
|
|
|
|
{
|
|
|
|
|
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
|
|
|
|
os_thread_signal_destroy();
|
|
|
|
|
@ -268,7 +285,8 @@ void os_thread_exit(void *retval)
|
|
|
|
|
static os_thread_local_attribute uint8 *thread_stack_boundary = NULL;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
uint8 *os_thread_get_stack_boundary()
|
|
|
|
|
uint8 *
|
|
|
|
|
os_thread_get_stack_boundary()
|
|
|
|
|
{
|
|
|
|
|
pthread_t self;
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
@ -286,15 +304,15 @@ uint8 *os_thread_get_stack_boundary()
|
|
|
|
|
|
|
|
|
|
page_size = getpagesize();
|
|
|
|
|
self = pthread_self();
|
|
|
|
|
max_stack_size = (size_t)(APP_THREAD_STACK_SIZE_MAX + page_size - 1)
|
|
|
|
|
& ~(page_size - 1);
|
|
|
|
|
max_stack_size =
|
|
|
|
|
(size_t)(APP_THREAD_STACK_SIZE_MAX + page_size - 1) & ~(page_size - 1);
|
|
|
|
|
|
|
|
|
|
if (max_stack_size < APP_THREAD_STACK_SIZE_DEFAULT)
|
|
|
|
|
max_stack_size = APP_THREAD_STACK_SIZE_DEFAULT;
|
|
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
if (pthread_getattr_np(self, &attr) == 0) {
|
|
|
|
|
pthread_attr_getstack(&attr, (void**)&addr, &stack_size);
|
|
|
|
|
pthread_attr_getstack(&attr, (void **)&addr, &stack_size);
|
|
|
|
|
pthread_attr_getguardsize(&attr, &guard_size);
|
|
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
|
if (stack_size > max_stack_size)
|
|
|
|
|
@ -306,7 +324,7 @@ uint8 *os_thread_get_stack_boundary()
|
|
|
|
|
}
|
|
|
|
|
(void)stack_size;
|
|
|
|
|
#elif defined(__APPLE__) || defined(__NuttX__)
|
|
|
|
|
if ((addr = (uint8*)pthread_get_stackaddr_np(self))) {
|
|
|
|
|
if ((addr = (uint8 *)pthread_get_stackaddr_np(self))) {
|
|
|
|
|
stack_size = pthread_get_stacksize_np(self);
|
|
|
|
|
if (stack_size > max_stack_size)
|
|
|
|
|
addr -= max_stack_size;
|
|
|
|
|
@ -349,8 +367,7 @@ touch_pages(uint8 *stack_min_addr, uint32 page_size)
|
|
|
|
|
{
|
|
|
|
|
uint8 sum = 0;
|
|
|
|
|
while (1) {
|
|
|
|
|
volatile uint8 *touch_addr =
|
|
|
|
|
(volatile uint8*)os_alloca(page_size / 2);
|
|
|
|
|
volatile uint8 *touch_addr = (volatile uint8 *)os_alloca(page_size / 2);
|
|
|
|
|
if (touch_addr < stack_min_addr + page_size) {
|
|
|
|
|
sum += *(stack_min_addr + page_size - 1);
|
|
|
|
|
break;
|
|
|
|
|
@ -378,7 +395,8 @@ init_stack_guard_pages()
|
|
|
|
|
(void)touch_pages(stack_min_addr, page_size);
|
|
|
|
|
/* First time to call aot function, protect guard pages */
|
|
|
|
|
if (os_mprotect(stack_min_addr, page_size * guard_page_count,
|
|
|
|
|
MMAP_PROT_NONE) != 0) {
|
|
|
|
|
MMAP_PROT_NONE)
|
|
|
|
|
!= 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
@ -413,8 +431,7 @@ signal_callback(int sig_num, siginfo_t *sig_info, void *sig_ucontext)
|
|
|
|
|
|
|
|
|
|
mask_signals(SIG_BLOCK);
|
|
|
|
|
|
|
|
|
|
if (signal_handler
|
|
|
|
|
&& (sig_num == SIGSEGV || sig_num == SIGBUS)) {
|
|
|
|
|
if (signal_handler && (sig_num == SIGSEGV || sig_num == SIGBUS)) {
|
|
|
|
|
signal_handler(sig_addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -427,8 +444,7 @@ signal_callback(int sig_num, siginfo_t *sig_info, void *sig_ucontext)
|
|
|
|
|
os_printf("unhandled SIGBUS, si_addr: %p\n", sig_addr);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
os_printf("unhandle signal %d, si_addr: %p\n",
|
|
|
|
|
sig_num, sig_addr);
|
|
|
|
|
os_printf("unhandle signal %d, si_addr: %p\n", sig_num, sig_addr);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -452,8 +468,7 @@ os_thread_signal_init(os_signal_handler handler)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Initialize memory for signal alternate stack of current thread */
|
|
|
|
|
if (!(map_addr = os_mmap(NULL, map_size,
|
|
|
|
|
MMAP_PROT_READ | MMAP_PROT_WRITE,
|
|
|
|
|
if (!(map_addr = os_mmap(NULL, map_size, MMAP_PROT_READ | MMAP_PROT_WRITE,
|
|
|
|
|
MMAP_MAP_NONE))) {
|
|
|
|
|
os_printf("Failed to mmap memory for alternate stack\n");
|
|
|
|
|
goto fail1;
|
|
|
|
|
@ -533,7 +548,7 @@ void
|
|
|
|
|
os_sigreturn()
|
|
|
|
|
{
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
#define UC_RESET_ALT_STACK 0x80000000
|
|
|
|
|
#define UC_RESET_ALT_STACK 0x80000000
|
|
|
|
|
extern int __sigreturn(void *, int);
|
|
|
|
|
|
|
|
|
|
/* It's necessary to call __sigreturn to restore the sigaltstack state
|
|
|
|
|
@ -542,4 +557,3 @@ os_sigreturn()
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */
|
|
|
|
|
|
|
|
|
|
|