wut/include/coreinit/mutex.h

133 lines
2.8 KiB
C
Raw Normal View History

2016-01-07 13:07:13 +01:00
#pragma once
#include <wut.h>
#include "threadqueue.h"
2016-01-07 17:02:54 +01:00
/**
* \defgroup coreinit_mutex Mutex
* \ingroup coreinit
2016-01-07 20:24:54 +01:00
*
* Standard mutex object, supports recursive locking.
*
* Similar to <a href="http://en.cppreference.com/w/cpp/thread/recursive_mutex">std::recursive_mutex</a>.
2016-01-07 17:02:54 +01:00
* @{
*/
2016-01-07 15:09:43 +01:00
#ifdef __cplusplus
extern "C" {
#endif
2016-01-07 13:07:13 +01:00
2016-01-08 17:52:12 +01:00
typedef struct OSThread OSThread;
2016-01-07 13:07:13 +01:00
typedef struct OSMutex OSMutex;
typedef struct OSMutexLink OSMutexLink;
struct OSMutexLink
{
OSMutex *next;
OSMutex *prev;
};
CHECK_OFFSET(OSMutexLink, 0x00, next);
CHECK_OFFSET(OSMutexLink, 0x04, prev);
CHECK_SIZE(OSMutexLink, 0x8);
#define OS_MUTEX_TAG 0x6D557458u
2016-01-07 13:07:13 +01:00
struct OSMutex
{
2016-01-07 20:24:54 +01:00
//! Should always be set to the value OS_MUTEX_TAG.
2016-01-07 13:07:13 +01:00
uint32_t tag;
2016-01-07 20:24:54 +01:00
//! Name set by OSInitMutexEx.
2016-01-07 13:07:13 +01:00
const char *name;
2016-01-07 20:24:54 +01:00
2016-01-07 13:07:13 +01:00
UNKNOWN(4);
2016-01-07 20:24:54 +01:00
//! Queue of threads waiting for this mutex to unlock.
2016-01-07 13:07:13 +01:00
OSThreadQueue queue;
2016-01-07 20:24:54 +01:00
//! Current owner of mutex.
2016-01-07 13:07:13 +01:00
OSThread *owner;
2016-01-07 20:24:54 +01:00
//! Current recursion lock count of mutex.
2016-01-07 13:07:13 +01:00
int32_t count;
2016-01-07 20:24:54 +01:00
//! Link used inside OSThread's mutex queue.
2016-01-07 13:07:13 +01:00
OSMutexLink link;
};
CHECK_OFFSET(OSMutex, 0x00, tag);
CHECK_OFFSET(OSMutex, 0x04, name);
CHECK_OFFSET(OSMutex, 0x0c, queue);
CHECK_OFFSET(OSMutex, 0x1c, owner);
CHECK_OFFSET(OSMutex, 0x20, count);
CHECK_OFFSET(OSMutex, 0x24, link);
CHECK_SIZE(OSMutex, 0x2c);
2016-01-07 20:24:54 +01:00
/**
* Initialise a mutex structure.
*/
2016-01-07 13:07:13 +01:00
void
OSInitMutex(OSMutex *mutex);
2016-01-07 20:24:54 +01:00
/**
* Initialise a mutex structure with a name.
*/
2016-01-07 13:07:13 +01:00
void
OSInitMutexEx(OSMutex *mutex,
const char *name);
2016-01-07 20:24:54 +01:00
/**
* Lock the mutex.
*
* If no one owns the mutex, set current thread as owner.
*
* If the lock is owned by the current thread, increase the recursion count.
*
* If the lock is owned by another thread, the current thread will sleep until
* the owner has unlocked this mutex.
*
* Similar to <a href="http://en.cppreference.com/w/cpp/thread/recursive_mutex/lock">std::recursive_mutex::lock</a>.
*/
2016-01-07 13:07:13 +01:00
void
OSLockMutex(OSMutex *mutex);
2016-01-07 20:24:54 +01:00
/**
* Try to lock a mutex.
*
* If no one owns the mutex, set current thread as owner.
*
* If the lock is owned by the current thread, increase the recursion count.
*
* If the lock is owned by another thread, do not block, return FALSE.
*
* \return TRUE if the mutex is locked, FALSE if the mutex is owned by another thread.
*
* Similar to <a href="http://en.cppreference.com/w/cpp/thread/recursive_mutex/try_lock">std::recursive_mutex::try_lock</a>.
*/
2016-01-07 13:07:13 +01:00
BOOL
OSTryLockMutex(OSMutex *mutex);
2016-01-07 20:24:54 +01:00
/**
* Unlocks the mutex.
*
* Will decrease the recursion count, will only unlock the mutex when the
* recursion count reaches 0.
*
* If any other threads are waiting to lock the mutex they will be woken.
*
* Similar to <a href="http://en.cppreference.com/w/cpp/thread/recursive_mutex/unlock">std::recursive_mutex::unlock</a>.
*/
void
OSUnlockMutex(OSMutex *mutex);
2016-01-07 15:09:43 +01:00
#ifdef __cplusplus
}
#endif
2016-01-07 17:02:54 +01:00
/** @} */