1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - OS - include
3   File:     mutex.h
4 
5   Copyright 2003-2008 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-11-11#$
14   $Rev: 9287 $
15   $Author: yada $
16 
17  *---------------------------------------------------------------------------*/
18 
19 #ifndef NITRO_OS_MUTEX_H_
20 #define NITRO_OS_MUTEX_H_
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #include <nitro/misc.h>
27 #include <nitro/types.h>
28 #include <nitro/os/common/thread.h>
29 
30 //----------------------------------------------------------------
31 //---- mutex type
32 #define OSi_MUTEX_TYPE_SHIFT  24
33 #define OSi_MUTEX_TYPE_MASK   (0xff << OSi_MUTEX_TYPE_SHIFT)
34 #define OS_MUTEX_TYPE_NONE    (0x00 << OSi_MUTEX_TYPE_SHIFT)
35 #define OS_MUTEX_TYPE_STD     (0x10 << OSi_MUTEX_TYPE_SHIFT)
36 #define OS_MUTEX_TYPE_R       (0x20 << OSi_MUTEX_TYPE_SHIFT)
37 #define OS_MUTEX_TYPE_W       (0x30 << OSi_MUTEX_TYPE_SHIFT)
38 
39 #define OSi_MUTEX_COUNT_MASK  0xffffff
40 
41 #ifndef SDK_THREAD_INFINITY
42 typedef struct OSMutex OSMutex;
43 #endif
44 
45 #pragma  warn_padding off
46 struct OSMutex
47 {
48     OSThreadQueue queue;
49     OSThread *thread;                  // current owner
50     s32     count;                     // lock count (notice: use upper 1byte as mutex type)
51 
52 #ifndef SDK_THREAD_INFINITY
53     OSMutex *prev;                     // link for OSThread.queueMutex
54     OSMutex *next;                     // link for OSThread.queueMutex
55 #else
56     OSMutexLink link;
57 #endif
58 };
59 #pragma  warn_padding reset
60 
61 
OS_SetMutexCount(OSMutex * mutex,s32 count)62 static inline  void OS_SetMutexCount( OSMutex* mutex, s32 count )
63 {
64     mutex->count = (s32)( (mutex->count & OSi_MUTEX_TYPE_MASK) | (count & OSi_MUTEX_COUNT_MASK) );
65 }
OS_GetMutexCount(OSMutex * mutex)66 static inline  s32 OS_GetMutexCount( OSMutex* mutex )
67 {
68     return (s32)( mutex->count & OSi_MUTEX_COUNT_MASK);
69 }
OS_IncreaseMutexCount(OSMutex * mutex)70 static inline  void OS_IncreaseMutexCount( OSMutex* mutex )
71 {
72     u32 type = (u32)(mutex->count & OSi_MUTEX_TYPE_MASK);
73     mutex->count ++;
74     mutex->count = (s32)( type | (mutex->count & OSi_MUTEX_COUNT_MASK) );
75 }
OS_DecreaseMutexCount(OSMutex * mutex)76 static inline  void OS_DecreaseMutexCount( OSMutex* mutex )
77 {
78     u32 type = (u32)(mutex->count & OSi_MUTEX_TYPE_MASK);
79     mutex->count --;
80     mutex->count = (s32)( type | (mutex->count & OSi_MUTEX_COUNT_MASK) );
81 }
OS_SetMutexType(OSMutex * mutex,u32 type)82 static inline  void OS_SetMutexType( OSMutex* mutex, u32 type )
83 {
84     mutex->count = (s32)( type | (mutex->count & OSi_MUTEX_COUNT_MASK) );
85 }
OS_GetMutexType(OSMutex * mutex)86 static inline  u32 OS_GetMutexType( OSMutex* mutex )
87 {
88     return (u32)( mutex->count & OSi_MUTEX_TYPE_MASK );
89 }
90 
91 
92 /*---------------------------------------------------------------------------*
93   Name:         OS_InitMutex
94 
95   Description:  initialize mutex
96 
97   Arguments:    mutex       pointer to mutex structure
98                             to be initialized
99 
100   Returns:      None
101  *---------------------------------------------------------------------------*/
102 void    OS_InitMutex(OSMutex *mutex);
103 
104 /*---------------------------------------------------------------------------*
105   Name:         OS_LockMutex
106 
107   Description:  lock mutex
108 
109   Arguments:    mutex       pointer to mutex structure
110 
111   Returns:      None
112  *---------------------------------------------------------------------------*/
113 void    OS_LockMutex(OSMutex *mutex);
114 
115 /*---------------------------------------------------------------------------*
116   Name:         OS_UnlockMutex
117 
118   Description:  unlock mutex
119 
120   Arguments:    mutex       pointer to mutex structure
121 
122   Returns:      None
123  *---------------------------------------------------------------------------*/
124 void    OS_UnlockMutex(OSMutex *mutex);
125 
126 /*---------------------------------------------------------------------------*
127   Name:         OS_TryLockMutex
128 
129   Description:  try to lock mutex
130 
131   Arguments:    mutex       pointer to mutex structure
132 
133   Returns:      True if lock
134  *---------------------------------------------------------------------------*/
135 BOOL    OS_TryLockMutex(OSMutex *mutex);
136 
137 /*---------------------------------------------------------------------------*
138   Name:         OSi_UnlockAllMutex
139 
140   Description:  unlocks all the mutexes locked by the thread
141 
142   Arguments:    mutex       pointer to mutex structure
143 
144   Returns:      None.
145  *---------------------------------------------------------------------------*/
146 void    OSi_UnlockAllMutex(OSThread *thread);
147 
148 
149 /*---------------------------------------------------------------------------*
150   Name:         OS_LockMutexR
151 
152   Description:  lock RW mutex as READ access
153 
154   Arguments:    mutex       pointer to RW mutex structure
155 
156   Returns:      None
157  *---------------------------------------------------------------------------*/
158 void OS_LockMutexR(OSMutex *mutex);
159 
160 /*---------------------------------------------------------------------------*
161   Name:         OS_LockMutexW
162 
163   Description:  lock RW mutex as WRITE access
164 
165   Arguments:    mutex       pointer to RW mutex structure
166 
167   Returns:      None
168  *---------------------------------------------------------------------------*/
169 void OS_LockMutexW(OSMutex *mutex);
170 
171 /*---------------------------------------------------------------------------*
172   Name:         OS_TryLockMutexR
173 
174   Description:  try to lock RW mutex as READ access
175 
176   Arguments:    mutex       pointer to RW mutex structure
177 
178   Returns:      TRUE if locked
179  *---------------------------------------------------------------------------*/
180 BOOL OS_TryLockMutexR(OSMutex *mutex);
181 
182 /*---------------------------------------------------------------------------*
183   Name:         OS_TryLockMutexW
184 
185   Description:  try to lock RW mutex as WRITE access
186 
187   Arguments:    mutex       pointer to RW mutex structure
188 
189   Returns:      TRUE if locked
190  *---------------------------------------------------------------------------*/
191 BOOL OS_TryLockMutexW(OSMutex *mutex);
192 
193 /*---------------------------------------------------------------------------*
194   Name:         OS_UnlockMutexR
195 
196   Description:  unlock mutex locked as READ access
197 
198   Arguments:    mutex       pointer to mutex structure
199 
200   Returns:      None
201  *---------------------------------------------------------------------------*/
202 void OS_UnlockMutexR(OSMutex *mutex);
203 
204 /*---------------------------------------------------------------------------*
205   Name:         OS_UnlockMutexW
206 
207   Description:  unlock mutex locked as WRITE access
208 
209   Arguments:    mutex       pointer to mutex structure
210 
211   Returns:      None
212  *---------------------------------------------------------------------------*/
213 void OS_UnlockMutexW(OSMutex *mutex);
214 
215 /*---------------------------------------------------------------------------*
216   Name:         OS_UnlockMutexRW
217 
218   Description:  unlock mutex locked as READ/WRITE access
219 
220   Arguments:    mutex       pointer to mutex structure
221 
222   Returns:      None
223  *---------------------------------------------------------------------------*/
224 void OS_UnlockMutexRW(OSMutex *mutex);
225 
226 /*---------------------------------------------------------------------------*
227   Name:         OS_LockMutexFromRToW
228 
229   Description:  Promote mutexR lock to mutexW lock without unlock.
230                 Wait till success.
231 
232   Arguments:    mutex       pointer to mutex structure
233 
234   Returns:      None
235  *---------------------------------------------------------------------------*/
236 void OS_LockMutexFromRToW(OSMutex *mutex);
237 
238 /*---------------------------------------------------------------------------*
239   Name:         OS_TryLockMutexFromRToW
240 
241   Description:  Try to promote mutexR lock to mutexW lock without unlock.
242 
243   Arguments:    mutex       pointer to mutex structure
244 
245   Returns:      TRUE if success
246  *---------------------------------------------------------------------------*/
247 BOOL OS_TryLockMutexFromRToW(OSMutex *mutex);
248 
249 /*---------------------------------------------------------------------------*
250   Name:         OS_LockMutexFromWToR
251 
252   Description:  Demote mutexW lock to mutexR lock without unlock.
253                 Wait till success.
254 
255   Arguments:    mutex       pointer to mutex structure
256 
257   Returns:      None
258  *---------------------------------------------------------------------------*/
259 void OS_LockMutexFromWToR(OSMutex *mutex);
260 
261 /*---------------------------------------------------------------------------*
262   Name:         OS_TryLockMutexFromWToR
263 
264   Description:  Try to demote mutexW lock to mutexR lock without unlock.
265 
266   Arguments:    mutex       pointer to mutex structure
267 
268   Returns:      TRUE if success
269  *---------------------------------------------------------------------------*/
270 BOOL OS_TryLockMutexFromWToR(OSMutex *mutex);
271 
272 #ifdef __cplusplus
273 } /* extern "C" */
274 #endif
275 
276 /* NITRO_OS_MUTEX_H_ */
277 #endif
278