2020-09-17 19:27:01 +02:00
|
|
|
#ifndef __LIB_PTHREAD_H__
|
|
|
|
# define __LIB_PTHREAD_H__
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2020-10-14 11:45:08 +02:00
|
|
|
//---
|
|
|
|
// Warnig, this part is experimental and reserved for Vhex
|
|
|
|
//---
|
|
|
|
|
2020-09-17 19:27:01 +02:00
|
|
|
// Define Mutex type
|
2020-10-14 11:45:08 +02:00
|
|
|
enum {
|
2020-09-17 19:27:01 +02:00
|
|
|
mtx_plain = 0,
|
|
|
|
mtx_recursive = 1,
|
|
|
|
mtx_timed = 2
|
|
|
|
};
|
|
|
|
|
|
|
|
// Define mutex structure
|
2020-10-14 11:45:08 +02:00
|
|
|
// @note: This is a custom implementation
|
2020-09-17 19:27:01 +02:00
|
|
|
#define MTX_WATERMARK (0xdeadbeef)
|
|
|
|
struct __mtx_s
|
|
|
|
{
|
|
|
|
uint32_t __watermark;
|
|
|
|
uint16_t lock;
|
|
|
|
uint8_t type;
|
|
|
|
} __attribute__((packed, aligned(4)));
|
|
|
|
typedef struct __mtx_s mtx_t;
|
|
|
|
|
|
|
|
|
2020-10-14 11:45:08 +02:00
|
|
|
//---
|
2020-09-17 19:27:01 +02:00
|
|
|
// Mutex functions
|
2020-10-14 11:45:08 +02:00
|
|
|
//---
|
2020-09-17 19:27:01 +02:00
|
|
|
|
2020-10-14 11:45:08 +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);
|
|
|
|
|
2020-10-14 11:45:08 +02:00
|
|
|
/*
|
|
|
|
** 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);
|
|
|
|
|
2020-10-14 11:45:08 +02:00
|
|
|
/*
|
|
|
|
** 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);
|
|
|
|
|
2020-10-14 11:45:08 +02:00
|
|
|
/*
|
|
|
|
** 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);
|
|
|
|
|
2020-10-14 11:45:08 +02:00
|
|
|
/* Destroy the mutex object pointed by __MUTEX. */
|
2020-09-17 19:27:01 +02:00
|
|
|
extern void mtx_destroy(mtx_t *__mutex);
|
|
|
|
|
|
|
|
|
2020-10-14 11:45:08 +02:00
|
|
|
//---
|
2020-09-17 19:27:01 +02:00
|
|
|
// Atomic operations
|
2020-10-14 11:45:08 +02:00
|
|
|
//---
|
|
|
|
/*
|
|
|
|
** 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);
|
|
|
|
|
2020-10-14 11:45:08 +02:00
|
|
|
/*
|
|
|
|
** 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);
|
2020-10-14 11:45:08 +02:00
|
|
|
|
2020-09-17 19:27:01 +02:00
|
|
|
#endif /*__LIB_PTHREAD_H__*/
|