/*---------------------------------------------------------------------------* Project: CtrBrom - OS - include File: mutex.h Copyright 2008 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Date:: $ $Rev$ $Author$ *---------------------------------------------------------------------------*/ #ifndef BROM_OS_MUTEX_H_ #define BROM_OS_MUTEX_H_ #include #include #include #ifdef __cplusplus extern "C" { #endif //---------------------------------------------------------------- //---- mutex type #define OSi_MUTEX_TYPE_SHIFT 24 #define OSi_MUTEX_TYPE_MASK (0xff << OSi_MUTEX_TYPE_SHIFT) #define OS_MUTEX_TYPE_NONE (0x00 << OSi_MUTEX_TYPE_SHIFT) #define OS_MUTEX_TYPE_STD (0x10 << OSi_MUTEX_TYPE_SHIFT) #define OS_MUTEX_TYPE_R (0x20 << OSi_MUTEX_TYPE_SHIFT) #define OS_MUTEX_TYPE_W (0x30 << OSi_MUTEX_TYPE_SHIFT) #define OSi_MUTEX_COUNT_MASK 0xffffff #ifndef SDK_THREAD_INFINITY typedef struct OSMutex OSMutex; #endif //#pragma warn_padding off struct OSMutex { OSThreadQueue queue; OSThread *thread; // current owner s32 count; // lock count (notice: use upper 1byte as mutex type) #ifndef SDK_THREAD_INFINITY OSMutex *prev; // link for OSThread.queueMutex OSMutex *next; // link for OSThread.queueMutex #else OSMutexLink link; #endif }; //#pragma warn_padding reset static inline void osSetMutexCount( OSMutex* mutex, s32 count ) { mutex->count = (s32)( (mutex->count & OSi_MUTEX_TYPE_MASK) | (count & OSi_MUTEX_COUNT_MASK) ); } static inline s32 osGetMutexCount( OSMutex* mutex ) { return (s32)( mutex->count & OSi_MUTEX_COUNT_MASK); } static inline void osIncreaseMutexCount( OSMutex* mutex ) { u32 type = (u32)(mutex->count & OSi_MUTEX_TYPE_MASK); mutex->count ++; mutex->count = (s32)( type | (mutex->count & OSi_MUTEX_COUNT_MASK) ); } static inline void osDecreaseMutexCount( OSMutex* mutex ) { u32 type = (u32)(mutex->count & OSi_MUTEX_TYPE_MASK); mutex->count --; mutex->count = (s32)( type | (mutex->count & OSi_MUTEX_COUNT_MASK) ); } static inline void osSetMutexType( OSMutex* mutex, u32 type ) { mutex->count = (s32)( type | (mutex->count & OSi_MUTEX_COUNT_MASK) ); } static inline u32 osGetMutexType( OSMutex* mutex ) { return (u32)( mutex->count & OSi_MUTEX_TYPE_MASK ); } /*---------------------------------------------------------------------------* Name: osInitMutex Description: initialize mutex Arguments: mutex pointer to mutex structure to be initialized Returns: None *---------------------------------------------------------------------------*/ void osInitMutex(OSMutex *mutex); /*---------------------------------------------------------------------------* Name: osLockMutex Description: lock mutex Arguments: mutex pointer to mutex structure Returns: None *---------------------------------------------------------------------------*/ void osLockMutex(OSMutex *mutex); /*---------------------------------------------------------------------------* Name: osUnlockMutex Description: unlock mutex Arguments: mutex pointer to mutex structure Returns: None *---------------------------------------------------------------------------*/ void osUnlockMutex(OSMutex *mutex); /*---------------------------------------------------------------------------* Name: osTryLockMutex Description: try to lock mutex Arguments: mutex pointer to mutex structure Returns: True if lock *---------------------------------------------------------------------------*/ BOOL osTryLockMutex(OSMutex *mutex); /*---------------------------------------------------------------------------* Name: i_osUnlockAllMutex Description: unlocks all the mutexes locked by the thread Arguments: mutex pointer to mutex structure Returns: None. *---------------------------------------------------------------------------*/ void i_osUnlockAllMutex(OSThread *thread); /*---------------------------------------------------------------------------* Name: osLockMutexR Description: lock RW mutex as READ access Arguments: mutex pointer to RW mutex structure Returns: None *---------------------------------------------------------------------------*/ void osLockMutexR(OSMutex *mutex); /*---------------------------------------------------------------------------* Name: osLockMutexW Description: lock RW mutex as WRITE access Arguments: mutex pointer to RW mutex structure Returns: None *---------------------------------------------------------------------------*/ void osLockMutexW(OSMutex *mutex); /*---------------------------------------------------------------------------* Name: osTryLockMutexR Description: try to lock RW mutex as READ access Arguments: mutex pointer to RW mutex structure Returns: TRUE if locked *---------------------------------------------------------------------------*/ BOOL osTryLockMutexR(OSMutex *mutex); /*---------------------------------------------------------------------------* Name: osTryLockMutexW Description: try to lock RW mutex as WRITE access Arguments: mutex pointer to RW mutex structure Returns: TRUE if locked *---------------------------------------------------------------------------*/ BOOL osTryLockMutexW(OSMutex *mutex); /*---------------------------------------------------------------------------* Name: osUnlockMutexR Description: unlock mutex locked as READ access Arguments: mutex pointer to mutex structure Returns: None *---------------------------------------------------------------------------*/ void osUnlockMutexR(OSMutex *mutex); /*---------------------------------------------------------------------------* Name: osUnlockMutexW Description: unlock mutex locked as WRITE access Arguments: mutex pointer to mutex structure Returns: None *---------------------------------------------------------------------------*/ void osUnlockMutexW(OSMutex *mutex); /*---------------------------------------------------------------------------* Name: osUnlockMutexRW Description: unlock mutex locked as READ/WRITE access Arguments: mutex pointer to mutex structure Returns: None *---------------------------------------------------------------------------*/ void osUnlockMutexRW(OSMutex *mutex); /*---------------------------------------------------------------------------* Name: osLockMutexFromRToW Description: Promote mutexR lock to mutexW lock without unlock. Wait till success. Arguments: mutex pointer to mutex structure Returns: None *---------------------------------------------------------------------------*/ void osLockMutexFromRToW(OSMutex *mutex); /*---------------------------------------------------------------------------* Name: osTryLockMutexFromRToW Description: Try to promote mutexR lock to mutexW lock without unlock. Arguments: mutex pointer to mutex structure Returns: TRUE if success *---------------------------------------------------------------------------*/ BOOL osTryLockMutexFromRToW(OSMutex *mutex); /*---------------------------------------------------------------------------* Name: osLockMutexFromWToR Description: Demote mutexW lock to mutexR lock without unlock. Wait till success. Arguments: mutex pointer to mutex structure Returns: None *---------------------------------------------------------------------------*/ void osLockMutexFromWToR(OSMutex *mutex); /*---------------------------------------------------------------------------* Name: osTryLockMutexFromWToR Description: Try to demote mutexW lock to mutexR lock without unlock. Arguments: mutex pointer to mutex structure Returns: TRUE if success *---------------------------------------------------------------------------*/ BOOL osTryLockMutexFromWToR(OSMutex *mutex); #ifdef __cplusplus } /* extern "C" */ #endif /* BROM_OS_MUTEX_H_ */ #endif