libnx/nx/include/switch/kernel/mutex.h
2018-12-29 22:49:34 +01:00

77 lines
1.7 KiB
C

/**
* @file mutex.h
* @brief Mutex synchronization primitive.
* @author plutoo
* @copyright libnx Authors
*/
#pragma once
#include <sys/lock.h>
#include "../types.h"
/// Mutex datatype, defined in newlib.
typedef _LOCK_T Mutex;
/// Recursive mutex datatype, defined in newlib.
typedef _LOCK_RECURSIVE_T RMutex;
/**
* @brief Initializes a mutex.
* @param m Mutex object.
* @note A mutex can also be statically initialized by assigning 0 to it.
*/
static inline void mutexInit(Mutex* m)
{
*m = 0;
}
/**
* @brief Locks a mutex.
* @param m Mutex object.
* @return 0 if the mutex is already owned by this thread, and 1 if it was sucessfully acquired.
*/
bool mutexLock(Mutex* m);
/**
* @brief Attempts to lock a mutex without waiting.
* @param m Mutex object.
* @return 1 if the mutex has been acquired successfully, and 0 on contention.
*/
bool mutexTryLock(Mutex* m);
/**
* @brief Unlocks a mutex.
* @param m Mutex object.
* @return 1 if the mutex was released, and 0 if the mutex does not belong to this thread.
*/
bool mutexUnlock(Mutex* m);
/**
* @brief Initializes a recursive mutex.
* @param m Recursive mutex object.
* @note A recursive mutex can also be statically initialized by assigning {0,0,0} to it.
*/
static inline void rmutexInit(RMutex* m)
{
m->lock = 0;
m->thread_tag = 0;
m->counter = 0;
}
/**
* @brief Locks a recursive mutex.
* @param m Recursive mutex object.
*/
void rmutexLock(RMutex* m);
/**
* @brief Attempts to lock a recursive mutex without waiting.
* @param m Recursive mutex object.
* @return 1 if the mutex has been acquired successfully, and 0 on contention.
*/
bool rmutexTryLock(RMutex* m);
/**
* @brief Unlocks a recursive mutex.
* @param m Recursive mutex object.
*/
void rmutexUnlock(RMutex* m);