fxlibc/include/threads.h

78 lines
1.7 KiB
C
Raw Normal View History

2021-05-09 23:00:11 +02:00
#ifndef __THREADS_H__
# define __THREADS_H__
2020-09-17 19:27:01 +02:00
#include <stddef.h>
#include <stdint.h>
//---
// Warnig, this part is experimental and reserved for Vhex
//---
2020-09-17 19:27:01 +02:00
// Define Mutex type
enum {
2020-09-17 19:27:01 +02:00
mtx_plain = 0,
mtx_recursive = 1,
mtx_timed = 2
};
// Define mutex structure
// @note: This is a custom implementation
2020-09-17 19:27:01 +02:00
#define MTX_WATERMARK (0xdeadbeef)
2021-05-09 16:35:40 +02:00
struct __mtx_s {
2020-09-17 19:27:01 +02:00
uint32_t __watermark;
uint16_t lock;
uint8_t type;
2021-05-09 16:35:40 +02:00
};
2020-09-17 19:27:01 +02:00
typedef struct __mtx_s mtx_t;
//---
2020-09-17 19:27:01 +02:00
// Mutex functions
//---
2020-09-17 19:27:01 +02:00
/*
** Creates a new mutex object with type __TYPE.
** @note: If successful the new object is pointed by __MUTEX.
*/
2020-09-17 19:27:01 +02:00
extern int mtx_init(mtx_t *__mutex, int __type);
/*
** Block the current thread until the mutex pointed to by __MUTEX is unlocked.
** In that case current thread will not be blocked.
*/
2020-09-17 19:27:01 +02:00
extern int mtx_lock(mtx_t *__mutex);
/*
** Try to lock the mutex pointed by __MUTEX without blocking.
** @note: If the mutex is free the current threads takes control of it,
** otherwise it returns immediately.
*/
2020-09-17 19:27:01 +02:00
extern int mtx_trylock(mtx_t *__mutex);
/*
** Unlock the mutex pointed by __MUTEX.
** @note: It may potentially awake other threads waiting on this mutex.
*/
2020-09-17 19:27:01 +02:00
extern int mtx_unlock (mtx_t *__mutex);
/* Destroy the mutex object pointed by __MUTEX. */
2020-09-17 19:27:01 +02:00
extern void mtx_destroy(mtx_t *__mutex);
//---
2020-09-17 19:27:01 +02:00
// Atomic operations
//---
/*
** Save the current SR register and set the SR.BIT bit up (start atomic operations)
** @note: return the saved SR register (if has been saved), 0xffffffff otherwise.
*/
2020-09-17 19:27:01 +02:00
extern uint32_t __thread_atomic_start(void);
/*
** Restore the saved SR register
** @note: return the restored SR register or -1 otherwise.
*/
2020-09-17 19:27:01 +02:00
extern uint32_t __thread_atomic_stop(void);
2021-05-09 23:00:11 +02:00
#endif /*__THREADS_H__*/