hip/hip.h

142 lines
3.2 KiB
C
Raw Normal View History

2025-06-10 22:27:12 +09:00
#ifndef _HIP_H_
#define _HIP_H_
#include <ucontext.h>
#define HIP_DEBUG
/* TODO: define these properly */
#define HIP_LIKELY(x) x
#define HIP_UNLIKELY(x) x
#define HIP_SIZEOF(x) sizeof(x)
2025-06-10 22:27:12 +09:00
typedef unsigned char hip_uint8_t;
typedef unsigned short hip_uint16_t;
typedef unsigned int hip_uint32_t;
typedef unsigned long long hip_uint64_t;
typedef signed char hip_int8_t;
typedef signed short hip_int16_t;
typedef signed int hip_int32_t;
typedef signed long long hip_int64_t;
typedef hip_uint64_t hip_nsecdur_t;
#if defined(__ILP32__) || defined(_WIN32)
# define MKCTX_NARGS 1
typedef hip_uint32_t hip_oow_t;
typedef hip_int32_t hip_ooi_t;
#else
# define MKCTX_NARGS 2
typedef hip_uint64_t hip_oow_t;
typedef hip_int64_t hip_ooi_t;
#endif
#define HIP_NULL ((void*)0)
#define HIP_INVALID_FD (-1)
typedef struct hip_t hip_t;
typedef struct hip_uctx_t hip_uctx_t;
typedef struct hip_uctx_link_t hip_uctx_link_t;
typedef void (*hip_ufun_t) (hip_uctx_t* uc, void* ctx);
typedef struct hip_chan_t hip_chan_t;
2025-06-10 22:27:12 +09:00
enum hip_io_flag_t
{
HIP_IO_READ = (1 << 0),
HIP_IO_WRITE = (1 << 1)
};
typedef enum hip_io_flag_t hip_io_flag_t;
struct hip_uctx_link_t
{
hip_uctx_link_t* next;
hip_uctx_link_t* prev;
};
#define HIP_OFFSETOF(type, field) ((hip_oow_t)&(((type*)0)->field))
#define HIP_CONTAINEROF(ptr, type, field) ((type*)((hip_uint8_t*)ptr - HIP_OFFSETOF(type, field)))
enum hip_flag_t
{
HIP_FLAG_LAZY = (1 << 0)
};
typedef enum hip_flag_t hip_flag_t;
enum hip_rtn_flag_t
{
HIP_RTN_FLAG_DUMMY = (1 << 0)
2025-06-10 22:27:12 +09:00
};
typedef enum hip_rtn_flag_t hip_rtn_flag_t;
#define HIP_LIST_INIT(_link) do { \
(_link)->prev = _link; \
(_link)->next = _link; \
} while(0)
#define HIP_LIST_CHAIN(_link, _prev, _next) do { \
hip_uctx_link_t* _p = _prev; \
hip_uctx_link_t* _n = _next; \
(_n)->prev = _link; \
(_link)->prev = _p; \
(_link)->next = _n; \
(_p)->next = _link; \
} while(0)
#if defined(HIP_DEBUG)
#define HIP_LIST_UNCHAIN(_link) do { \
(_link)->next->prev = (_link)->prev; \
(_link)->prev->next = (_link)->next; \
(_link)->next = HIP_NULL; \
(_link)->prev = HIP_NULL; \
} while(0)
#else
#define HIP_LIST_UNCHAIN(_link) do { \
(_link)->next->prev = (_link)->prev; \
(_link)->prev->next = (_link)->next; \
} while(0)
#endif
#define HIP_LIST_HEAD(ll) ((ll)->next)
#define HIP_LIST_TAIL(ll) ((ll)->prev)
#define HIP_LIST_ADD_FRONT(_link, ll) HIP_LIST_CHAIN(_link, (ll), HIP_LIST_HEAD(ll))
#define HIP_LIST_ADD_BACK(_link, ll) HIP_LIST_CHAIN(_link, HIP_LIST_TAIL(ll), (ll))
#define HIP_LIST_IS_EMPTY(ll) (HIP_LIST_HEAD(ll) == ll)
2025-06-10 22:27:12 +09:00
#if defined(__cplusplus)
extern "C" {
#endif
hip_t* hip_open(int flags);
void hip_close(hip_t* hip);
hip_uctx_t* hip_newrtn(hip_t* hip, int flags, hip_ufun_t uf, void* ctx);
hip_uctx_t* hip_uctx_open(hip_t* hip, hip_oow_t stack_size, int flags, hip_ufun_t uf, void* ctx);
void hip_uctx_close(hip_uctx_t* uctx);
void hip_yield(hip_t* hip);
void hip_sleep(hip_t* hip, hip_nsecdur_t nsecs);
void hip_switch(hip_t* hip, hip_uctx_link_t* list_to_deposit);
void hip_suspend(hip_t* hip);
hip_chan_t* hip_chan_open(hip_t* hip, hip_oow_t unit_size, hip_oow_t unit_capa);
void hip_chan_close(hip_chan_t* c);
int hip_chan_send(hip_chan_t* c, const void* ptr, hip_oow_t len);
int hip_chan_recv(hip_chan_t* c, void* ptr, hip_oow_t len);
2025-06-10 22:27:12 +09:00
#if defined(__cplusplus)
}
#endif
#endif