wut/include/coreinit/semaphore.h

105 lines
2.1 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_semaphore Semaphore
* \ingroup coreinit
2016-01-07 20:24:54 +01:00
*
* Similar to Windows <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms685129(v=vs.85).aspx">Semaphore Objects</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
typedef struct OSSemaphore OSSemaphore;
#define OS_SEMAPHORE_TAG 0x73506852u
2016-01-07 13:07:13 +01:00
struct OSSemaphore
{
2016-01-07 20:24:54 +01:00
//! Should always be set to the value OS_SEMAPHORE_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
//! Current count of semaphore
2016-01-07 13:07:13 +01:00
int32_t count;
2016-01-07 20:24:54 +01:00
//! Queue of threads waiting on semaphore object with OSWaitSemaphore
2016-01-07 13:07:13 +01:00
OSThreadQueue queue;
};
CHECK_OFFSET(OSSemaphore, 0x00, tag);
CHECK_OFFSET(OSSemaphore, 0x04, name);
CHECK_OFFSET(OSSemaphore, 0x0C, count);
CHECK_OFFSET(OSSemaphore, 0x10, queue);
CHECK_SIZE(OSSemaphore, 0x20);
2016-01-07 20:24:54 +01:00
/**
* Initialise semaphore object with count.
*/
2016-01-07 13:07:13 +01:00
void
OSInitSemaphore(OSSemaphore *semaphore,
int32_t count);
2016-01-07 20:24:54 +01:00
/**
* Initialise semaphore object with count and name.
*/
2016-01-07 13:07:13 +01:00
void
OSInitSemaphoreEx(OSSemaphore *semaphore,
int32_t count,
const char *name);
2016-01-07 20:24:54 +01:00
/**
* Get the current semaphore count.
*/
2016-01-07 13:07:13 +01:00
int32_t
OSGetSemaphoreCount(OSSemaphore *semaphore);
2016-01-07 20:24:54 +01:00
/**
* Increase the semaphore value.
*
* If any threads are waiting for semaphore, they are woken.
*/
2016-01-07 13:07:13 +01:00
int32_t
OSSignalSemaphore(OSSemaphore *semaphore);
2016-01-07 20:24:54 +01:00
/**
* Decrease the semaphore value.
*
* If the value is less than or equal to zero the current thread will be put to
* sleep until the count is above zero and it can decrement it safely.
*/
2016-01-07 13:07:13 +01:00
int32_t
OSWaitSemaphore(OSSemaphore *semaphore);
2016-01-07 20:24:54 +01:00
/**
* Try to decrease the semaphore value.
*
* If the value is greater than zero then it will be decremented, else the function
* will return immediately with a value <= 0 indicating a failure.
*
* \return Returns previous semaphore count, before the decrement in this function.
* If the value is >0 then it means the call was succesful.
*/
2016-01-07 13:07:13 +01:00
int32_t
OSTryWaitSemaphore(OSSemaphore *semaphore);
2016-01-07 20:24:54 +01:00
2016-01-07 15:09:43 +01:00
#ifdef __cplusplus
}
#endif
2016-01-07 17:02:54 +01:00
/** @} */