1 /*-------------------------------------------------------------
3 mutex.c -- Thread subsystem III
6 Michael Wiedenbauer (shagkur)
7 Dave Murphy (WinterMute)
9 This software is provided 'as-is', without any express or implied
10 warranty. In no event will the authors be held liable for any
11 damages arising from the use of this software.
13 Permission is granted to anyone to use this software for any
14 purpose, including commercial applications, and to alter it and
15 redistribute it freely, subject to the following restrictions:
17 1. The origin of this software must not be misrepresented; you
18 must not claim that you wrote the original software. If you use
19 this software in a product, an acknowledgment in the product
20 documentation would be appreciated but is not required.
22 2. Altered source versions must be plainly marked as such, and
23 must not be misrepresented as being the original software.
25 3. This notice may not be removed or altered from any source
28 -------------------------------------------------------------*/
34 #include "lwp_mutex.h"
35 #include "lwp_objmgr.h"
36 #include "lwp_config.h"
39 #define LWP_OBJTYPE_MUTEX 3
41 #define LWP_CHECK_MUTEX(hndl) \
43 if(((hndl)==LWP_MUTEX_NULL) || (LWP_OBJTYPE(hndl)!=LWP_OBJTYPE_MUTEX)) \
47 typedef struct _mutex_st
53 lwp_objinfo _lwp_mutex_objects
;
55 static s32
__lwp_mutex_locksupp(mutex_t lock
,u32 timeout
,u8 block
)
60 if(lock
==LWP_MUTEX_NULL
|| LWP_OBJTYPE(lock
)!=LWP_OBJTYPE_MUTEX
) return -1;
62 p
= (mutex_st
*)__lwp_objmgr_getisrdisable(&_lwp_mutex_objects
,LWP_OBJMASKID(lock
),&level
);
65 __lwp_mutex_seize(&p
->mutex
,p
->object
.id
,block
,timeout
,level
);
66 return _thr_executing
->wait
.ret_code
;
69 void __lwp_mutex_init()
71 __lwp_objmgr_initinfo(&_lwp_mutex_objects
,LWP_MAX_MUTEXES
,sizeof(mutex_st
));
75 static __inline__ mutex_st
* __lwp_mutex_open(mutex_t lock
)
77 LWP_CHECK_MUTEX(lock
);
78 return (mutex_st
*)__lwp_objmgr_get(&_lwp_mutex_objects
,LWP_OBJMASKID(lock
));
81 static __inline__
void __lwp_mutex_free(mutex_st
*lock
)
83 __lwp_objmgr_close(&_lwp_mutex_objects
,&lock
->object
);
84 __lwp_objmgr_free(&_lwp_mutex_objects
,&lock
->object
);
87 static mutex_st
* __lwp_mutex_allocate()
91 __lwp_thread_dispatchdisable();
92 lock
= (mutex_st
*)__lwp_objmgr_allocate(&_lwp_mutex_objects
);
94 __lwp_objmgr_open(&_lwp_mutex_objects
,&lock
->object
);
97 __lwp_thread_dispatchunnest();
101 s32
LWP_MutexInit(mutex_t
*mutex
,bool use_recursive
)
106 if(!mutex
) return -1;
108 ret
= __lwp_mutex_allocate();
111 attr
.mode
= LWP_MUTEX_FIFO
;
112 attr
.nest_behavior
= use_recursive
?LWP_MUTEX_NEST_ACQUIRE
:LWP_MUTEX_NEST_ERROR
;
113 attr
.onlyownerrelease
= TRUE
;
114 attr
.prioceil
= 1; //__lwp_priotocore(LWP_PRIO_MAX-1);
115 __lwp_mutex_initialize(&ret
->mutex
,&attr
,LWP_MUTEX_UNLOCKED
);
117 *mutex
= (mutex_t
)(LWP_OBJMASKTYPE(LWP_OBJTYPE_MUTEX
)|LWP_OBJMASKID(ret
->object
.id
));
118 __lwp_thread_dispatchunnest();
122 s32
LWP_MutexDestroy(mutex_t mutex
)
126 p
= __lwp_mutex_open(mutex
);
129 if(__lwp_mutex_locked(&p
->mutex
)) {
130 __lwp_thread_dispatchenable();
133 __lwp_mutex_flush(&p
->mutex
,EINVAL
);
134 __lwp_thread_dispatchenable();
140 s32
LWP_MutexLock(mutex_t mutex
)
142 return __lwp_mutex_locksupp(mutex
,LWP_THREADQ_NOTIMEOUT
,TRUE
);
145 s32
LWP_MutexTryLock(mutex_t mutex
)
147 return __lwp_mutex_locksupp(mutex
,LWP_THREADQ_NOTIMEOUT
,FALSE
);
150 s32
LWP_MutexUnlock(mutex_t mutex
)
155 lock
= __lwp_mutex_open(mutex
);
158 ret
= __lwp_mutex_surrender(&lock
->mutex
);
159 __lwp_thread_dispatchenable();