4 void __lwp_mutex_initialize(lwp_mutex
*mutex
,lwp_mutex_attr
*attrs
,u32 init_lock
)
7 mutex
->lock
= init_lock
;
8 mutex
->blocked_cnt
= 0;
10 if(init_lock
==LWP_MUTEX_LOCKED
) {
12 mutex
->holder
= _thr_executing
;
13 if(__lwp_mutex_isinheritprio(attrs
) || __lwp_mutex_isprioceiling(attrs
))
14 _thr_executing
->res_cnt
++;
20 __lwp_threadqueue_init(&mutex
->wait_queue
,__lwp_mutex_isfifo(attrs
)?LWP_THREADQ_MODEFIFO
:LWP_THREADQ_MODEPRIORITY
,LWP_STATES_WAITING_FOR_MUTEX
,LWP_MUTEX_TIMEOUT
);
23 u32
__lwp_mutex_surrender(lwp_mutex
*mutex
)
28 holder
= mutex
->holder
;
30 if(mutex
->atrrs
.onlyownerrelease
) {
31 if(!__lwp_thread_isexec(holder
))
32 return LWP_MUTEX_NOTOWNER
;
36 return LWP_MUTEX_SUCCESSFUL
;
39 if(mutex
->nest_cnt
!=0) {
40 switch(mutex
->atrrs
.nest_behavior
) {
41 case LWP_MUTEX_NEST_ACQUIRE
:
42 return LWP_MUTEX_SUCCESSFUL
;
43 case LWP_MUTEX_NEST_ERROR
:
44 return LWP_MUTEX_NEST_NOTALLOWED
;
45 case LWP_MUTEX_NEST_BLOCK
:
50 if(__lwp_mutex_isinheritprio(&mutex
->atrrs
) || __lwp_mutex_isprioceiling(&mutex
->atrrs
))
54 if(__lwp_mutex_isinheritprio(&mutex
->atrrs
) || __lwp_mutex_isprioceiling(&mutex
->atrrs
)) {
55 if(holder
->res_cnt
==0 && holder
->real_prio
!=holder
->cur_prio
)
56 __lwp_thread_changepriority(holder
,holder
->real_prio
,TRUE
);
59 if((thethread
=__lwp_threadqueue_dequeue(&mutex
->wait_queue
))) {
61 mutex
->holder
= thethread
;
62 if(__lwp_mutex_isinheritprio(&mutex
->atrrs
) || __lwp_mutex_isprioceiling(&mutex
->atrrs
))
65 mutex
->lock
= LWP_MUTEX_UNLOCKED
;
67 return LWP_MUTEX_SUCCESSFUL
;
70 void __lwp_mutex_seize_irq_blocking(lwp_mutex
*mutex
,u64 timeout
)
74 exec
= _thr_executing
;
75 if(__lwp_mutex_isinheritprio(&mutex
->atrrs
)){
76 if(mutex
->holder
->cur_prio
>exec
->cur_prio
)
77 __lwp_thread_changepriority(mutex
->holder
,exec
->cur_prio
,FALSE
);
81 __lwp_threadqueue_enqueue(&mutex
->wait_queue
,timeout
);
83 if(_thr_executing
->wait
.ret_code
==LWP_MUTEX_SUCCESSFUL
) {
84 if(__lwp_mutex_isprioceiling(&mutex
->atrrs
)) {
85 if(mutex
->atrrs
.prioceil
<exec
->cur_prio
)
86 __lwp_thread_changepriority(exec
,mutex
->atrrs
.prioceil
,FALSE
);
89 __lwp_thread_dispatchenable();
92 void __lwp_mutex_flush(lwp_mutex
*mutex
,u32 status
)
94 __lwp_threadqueue_flush(&mutex
->wait_queue
,status
);