s32 LWP_JoinThread(lwp_t thethread,void **value_ptr) { u32 level; void *return_ptr; lwp_cntrl *exec,*lwp_thread; lwp_thread = __lwp_cntrl_open(thethread); if(!lwp_thread) return 0; if(__lwp_thread_isexec(lwp_thread)) { __lwp_thread_dispatchenable(); return EDEADLK; //EDEADLK } exec = _thr_executing; _CPU_ISR_Disable(level); __lwp_threadqueue_csenter(&lwp_thread->join_list); exec->wait.ret_code = 0; exec->wait.ret_arg_1 = NULL; exec->wait.ret_arg = (void*)&return_ptr; exec->wait.queue = &lwp_thread->join_list; exec->wait.id = thethread; _CPU_ISR_Restore(level); __lwp_threadqueue_enqueue(&lwp_thread->join_list,LWP_WD_NOTIMEOUT); __lwp_thread_dispatchenable(); if(value_ptr) *value_ptr = return_ptr; return 0; }
u32 __lwp_sema_seize(lwp_sema *sema,u32 id,u32 wait,u64 timeout) { u32 level; lwp_cntrl *exec; exec = _thr_executing; exec->wait.ret_code = LWP_SEMA_SUCCESSFUL; _CPU_ISR_Disable(level); if(sema->count!=0) { --sema->count; _CPU_ISR_Restore(level); return LWP_SEMA_SUCCESSFUL; } if(!wait) { _CPU_ISR_Restore(level); exec->wait.ret_code = LWP_SEMA_UNSATISFIED_NOWAIT; return LWP_SEMA_UNSATISFIED_NOWAIT; } __lwp_threadqueue_csenter(&sema->wait_queue); exec->wait.queue = &sema->wait_queue; exec->wait.id = id; _CPU_ISR_Restore(level); __lwp_threadqueue_enqueue(&sema->wait_queue,timeout); return LWP_SEMA_SUCCESSFUL; }
s32 LWP_ThreadSleep(lwpq_t thequeue) { u32 level; tqueue_st *tq; lwp_cntrl *exec = NULL; tq = __lwp_tqueue_open(thequeue); if(!tq) return -1; exec = _thr_executing; _CPU_ISR_Disable(level); __lwp_threadqueue_csenter(&tq->tqueue); exec->wait.ret_code = 0; exec->wait.ret_arg = NULL; exec->wait.ret_arg_1 = NULL; exec->wait.queue = &tq->tqueue; exec->wait.id = thequeue; _CPU_ISR_Restore(level); __lwp_threadqueue_enqueue(&tq->tqueue,LWP_THREADQ_NOTIMEOUT); __lwp_thread_dispatchenable(); return 0; }
static s32 __lwp_cond_waitsupp(cond_t cond,mutex_t mutex,u64 timeout,u8 timedout) { u32 status,mstatus,level; cond_st *thecond = __lwp_cond_open(cond); if(!thecond) return -1; if(thecond->lock!=LWP_MUTEX_NULL && thecond->lock!=mutex) { __lwp_thread_dispatchenable(); return EINVAL; } LWP_MutexUnlock(mutex); if(!timedout) { thecond->lock = mutex; _CPU_ISR_Disable(level); __lwp_threadqueue_csenter(&thecond->wait_queue); _thr_executing->wait.ret_code = 0; _thr_executing->wait.queue = &thecond->wait_queue; _thr_executing->wait.id = cond; _CPU_ISR_Restore(level); __lwp_threadqueue_enqueue(&thecond->wait_queue,timeout); __lwp_thread_dispatchenable(); status = _thr_executing->wait.ret_code; if(status && status!=ETIMEDOUT) return status; } else { __lwp_thread_dispatchenable(); status = ETIMEDOUT; } mstatus = LWP_MutexLock(mutex); if(mstatus) return EINVAL; return status; }