wut/include/coreinit/alarm.h

231 lines
5.0 KiB
C
Raw Normal View History

2016-01-07 13:07:13 +01:00
#pragma once
#include <wut.h>
#include "thread.h"
#include "threadqueue.h"
#include "time.h"
2016-01-07 17:02:54 +01:00
/**
* \defgroup coreinit_alarms Alarms
* \ingroup coreinit
*
* The alarm family of functions are used for creating alarms which call
* a callback or wake up waiting threads after a period of time.
*
* Alarms can be one shot alarms which trigger once after a period of time,
* or periodic which trigger at regular intervals until they are cancelled.
*
* @{
*/
2016-01-07 15:09:43 +01:00
#ifdef __cplusplus
extern "C" {
#endif
2016-01-07 13:07:13 +01:00
typedef struct OSAlarm OSAlarm;
typedef struct OSAlarmLink OSAlarmLink;
typedef struct OSAlarmQueue OSAlarmQueue;
typedef void (*OSAlarmCallback)(OSAlarm *, OSContext *);
#define OS_ALARM_QUEUE_TAG 0x614C6D51u
2016-01-07 13:07:13 +01:00
struct OSAlarmQueue
{
// OS_ALARM_QUEUE_TAG
2016-01-07 13:07:13 +01:00
uint32_t tag;
// Name set by OSInitAlarmQueueEx
2016-01-07 13:07:13 +01:00
const char *name;
UNKNOWN(4);
2016-01-07 13:07:13 +01:00
OSThreadQueue threadQueue;
OSAlarm *head;
OSAlarm *tail;
};
CHECK_OFFSET(OSAlarmQueue, 0x00, tag);
CHECK_OFFSET(OSAlarmQueue, 0x04, name);
CHECK_OFFSET(OSAlarmQueue, 0x0c, threadQueue);
CHECK_OFFSET(OSAlarmQueue, 0x1c, head);
CHECK_OFFSET(OSAlarmQueue, 0x20, tail);
CHECK_SIZE(OSAlarmQueue, 0x24);
struct OSAlarmLink
{
OSAlarm *prev;
OSAlarm *next;
};
CHECK_OFFSET(OSAlarmLink, 0x00, prev);
CHECK_OFFSET(OSAlarmLink, 0x04, next);
CHECK_SIZE(OSAlarmLink, 0x08);
#define OS_ALARM_TAG 0x614C724Du
2016-01-07 13:07:13 +01:00
struct OSAlarm
{
2016-01-07 17:02:54 +01:00
//! Should always be set to the value OS_ALARM_TAG.
2016-01-07 13:07:13 +01:00
uint32_t tag;
2016-01-07 17:02:54 +01:00
//! Name set from OSCreateAlarmEx.
2016-01-07 13:07:13 +01:00
const char *name;
2016-01-07 17:02:54 +01:00
2016-01-07 13:07:13 +01:00
UNKNOWN(4);
2016-01-07 17:02:54 +01:00
//! The callback to execute once the alarm is triggered.
2016-01-07 13:07:13 +01:00
OSAlarmCallback callback;
2016-01-07 17:02:54 +01:00
//! Used with OSCancelAlarms for bulk cancellation of alarms.
2016-01-07 13:07:13 +01:00
uint32_t group;
2016-01-07 17:02:54 +01:00
2016-01-07 13:07:13 +01:00
UNKNOWN(4);
2016-01-07 17:02:54 +01:00
//! The time when the alarm will next be triggered.
2016-01-07 13:07:13 +01:00
OSTime nextFire;
2016-01-07 17:02:54 +01:00
//! Link used for when this OSAlarm object is inside an OSAlarmQueue
2016-01-07 13:07:13 +01:00
OSAlarmLink link;
2016-01-07 17:02:54 +01:00
//! The period between alarm triggers, this is only set for periodic alarms.
2016-01-07 13:07:13 +01:00
OSTime period;
2016-01-07 17:02:54 +01:00
//! The time the alarm was started.
OSTime start;
//! User data set with OSSetAlarmUserData and retrieved with OSGetAlarmUserData.
2016-01-07 13:07:13 +01:00
void *userData;
2016-01-07 17:02:54 +01:00
//! The current state of the alarm, internal values.
2016-01-07 13:07:13 +01:00
uint32_t state;
2016-01-07 17:02:54 +01:00
//! Queue of threads currently waiting for the alarm to trigger with OSWaitAlarm.
2016-01-07 13:07:13 +01:00
OSThreadQueue threadQueue;
2016-01-07 17:02:54 +01:00
//! The queue that this alarm is currently in.
2016-01-07 13:07:13 +01:00
OSAlarmQueue *alarmQueue;
2016-01-07 17:02:54 +01:00
//! The context the alarm was triggered on.
2016-01-07 13:07:13 +01:00
OSContext *context;
};
CHECK_OFFSET(OSAlarm, 0x00, tag);
CHECK_OFFSET(OSAlarm, 0x04, name);
CHECK_OFFSET(OSAlarm, 0x0c, callback);
CHECK_OFFSET(OSAlarm, 0x10, group);
CHECK_OFFSET(OSAlarm, 0x18, nextFire);
CHECK_OFFSET(OSAlarm, 0x20, link);
CHECK_OFFSET(OSAlarm, 0x28, period);
2016-01-08 17:52:12 +01:00
CHECK_OFFSET(OSAlarm, 0x30, start);
2016-01-07 13:07:13 +01:00
CHECK_OFFSET(OSAlarm, 0x38, userData);
CHECK_OFFSET(OSAlarm, 0x3c, state);
CHECK_OFFSET(OSAlarm, 0x40, threadQueue);
CHECK_OFFSET(OSAlarm, 0x50, alarmQueue);
CHECK_OFFSET(OSAlarm, 0x54, context);
CHECK_SIZE(OSAlarm, 0x58);
2016-01-07 17:02:54 +01:00
/**
* Cancel an alarm.
*/
2016-01-07 13:07:13 +01:00
BOOL
OSCancelAlarm(OSAlarm *alarm);
2016-01-07 17:02:54 +01:00
/**
* Cancel all alarms which have a matching tag set by OSSetAlarmTag.
*
* \param group The alarm tag to cancel.
*/
2016-01-07 13:07:13 +01:00
void
OSCancelAlarms(uint32_t group);
2016-01-07 17:02:54 +01:00
/**
* Initialise an alarm structure.
*/
2016-01-07 13:07:13 +01:00
void
OSCreateAlarm(OSAlarm *alarm);
2016-01-07 17:02:54 +01:00
/**
* Initialise an alarm structure with a name.
*/
2016-01-07 13:07:13 +01:00
void
OSCreateAlarmEx(OSAlarm *alarm,
const char *name);
2016-01-07 17:02:54 +01:00
/**
* Return user data set by OSSetAlarmUserData.
*/
2016-01-07 13:07:13 +01:00
void *
OSGetAlarmUserData(OSAlarm *alarm);
2016-01-07 17:02:54 +01:00
/**
* Initialise an alarm queue structure.
*/
2016-01-07 13:07:13 +01:00
void
OSInitAlarmQueue(OSAlarmQueue *queue);
2016-01-07 17:02:54 +01:00
/**
* Initialise an alarm queue structure with a name.
*/
void
OSInitAlarmQueueEx(OSAlarmQueue *queue,
const char *name);
2016-01-07 17:02:54 +01:00
/**
* Set a one shot alarm to perform a callback after a set amount of time.
*
* \param alarm The alarm to set.
* \param time The duration until the alarm should be triggered.
* \param callback The alarm callback to call when the alarm is triggered.
*/
2016-01-07 13:07:13 +01:00
BOOL
OSSetAlarm(OSAlarm *alarm,
OSTime time,
2016-01-07 17:02:54 +01:00
OSAlarmCallback callback);
2016-01-07 13:07:13 +01:00
2016-01-07 17:02:54 +01:00
/**
* Set a repeated alarm to execute a callback every interval from start.
*
* \param alarm The alarm to set.
* \param start The duration until the alarm should first be triggered.
* \param interval The interval between triggers after the first trigger.
* \param callback The alarm callback to call when the alarm is triggered.
*/
2016-01-07 13:07:13 +01:00
BOOL
OSSetPeriodicAlarm(OSAlarm *alarm,
OSTime start,
OSTime interval,
2016-01-07 17:02:54 +01:00
OSAlarmCallback callback);
2016-01-07 13:07:13 +01:00
2016-01-07 17:02:54 +01:00
/**
* Set an alarm tag which is used in OSCancelAlarms for bulk cancellation.
*/
2016-01-07 13:07:13 +01:00
void
OSSetAlarmTag(OSAlarm *alarm,
uint32_t group);
2016-01-07 17:02:54 +01:00
/**
* Set alarm user data which is returned by OSGetAlarmUserData.
*/
2016-01-07 13:07:13 +01:00
void
OSSetAlarmUserData(OSAlarm *alarm,
void *data);
2016-01-07 17:02:54 +01:00
/**
* Sleep the current thread until the alarm has been triggered or cancelled.
*/
2016-01-07 13:07:13 +01:00
BOOL
OSWaitAlarm(OSAlarm *alarm);
2016-01-07 15:09:43 +01:00
#ifdef __cplusplus
}
#endif
2016-01-07 17:02:54 +01:00
/** @} */