async net init (dhewg)
[libogc.git] / libogc / mutex.c
blob7477e3c9b6735ea193863732990a3a2ae572ef92
1 /*-------------------------------------------------------------
3 mutex.c -- Thread subsystem III
5 Copyright (C) 2004
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
26 distribution.
28 -------------------------------------------------------------*/
31 #include <stdlib.h>
32 #include <errno.h>
33 #include "asm.h"
34 #include "lwp_mutex.h"
35 #include "lwp_objmgr.h"
36 #include "lwp_config.h"
37 #include "mutex.h"
39 #define LWP_OBJTYPE_MUTEX 3
41 #define LWP_CHECK_MUTEX(hndl) \
42 { \
43 if(((hndl)==LWP_MUTEX_NULL) || (LWP_OBJTYPE(hndl)!=LWP_OBJTYPE_MUTEX)) \
44 return NULL; \
47 typedef struct _mutex_st
49 lwp_obj object;
50 lwp_mutex mutex;
51 } mutex_st;
53 lwp_objinfo _lwp_mutex_objects;
55 static s32 __lwp_mutex_locksupp(mutex_t lock,u32 timeout,u8 block)
57 u32 level;
58 mutex_st *p;
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);
63 if(!p) return -1;
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()
89 mutex_st *lock;
91 __lwp_thread_dispatchdisable();
92 lock = (mutex_st*)__lwp_objmgr_allocate(&_lwp_mutex_objects);
93 if(lock) {
94 __lwp_objmgr_open(&_lwp_mutex_objects,&lock->object);
95 return lock;
97 __lwp_thread_dispatchunnest();
98 return NULL;
101 s32 LWP_MutexInit(mutex_t *mutex,bool use_recursive)
103 lwp_mutex_attr attr;
104 mutex_st *ret;
106 if(!mutex) return -1;
108 ret = __lwp_mutex_allocate();
109 if(!ret) return -1;
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();
119 return 0;
122 s32 LWP_MutexDestroy(mutex_t mutex)
124 mutex_st *p;
126 p = __lwp_mutex_open(mutex);
127 if(!p) return 0;
129 if(__lwp_mutex_locked(&p->mutex)) {
130 __lwp_thread_dispatchenable();
131 return EBUSY;
133 __lwp_mutex_flush(&p->mutex,EINVAL);
134 __lwp_thread_dispatchenable();
136 __lwp_mutex_free(p);
137 return 0;
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)
152 u32 ret;
153 mutex_st *lock;
155 lock = __lwp_mutex_open(mutex);
156 if(!lock) return -1;
158 ret = __lwp_mutex_surrender(&lock->mutex);
159 __lwp_thread_dispatchenable();
161 return ret;