acl/lib_fiber/c.old/include/fiber/lib_fiber.h

577 lines
19 KiB
C
Raw Normal View History

#ifndef LIB_ACL_FIBER_INCLUDE_H
2018-11-30 14:38:22 +08:00
#define LIB_ACL_FIBER_INCLUDE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <sys/types.h>
#include <sys/socket.h>
//typedef struct ACL_VSTREAM ACL_VSTREAM;
/**
*
2018-11-30 14:38:22 +08:00
*/
typedef struct ACL_FIBER ACL_FIBER;
/**
* hook IO API 1
* @param onoff {int} hook
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_hook_api(int onoff);
/**
*
* @param fn {void (*)(ACL_FIBER*, void*)}
* @param arg {void*} fn
* @param size {size_t}
2018-11-30 14:38:22 +08:00
* @return {ACL_FIBER*}
*/
ACL_FIBER* acl_fiber_create(void (*fn)(ACL_FIBER*, void*),
void* arg, size_t size);
/**
* 线
2018-11-30 14:38:22 +08:00
* @retur {int}
*/
int acl_fiber_ndead(void);
void acl_fiber_check_timer(size_t max);
/**
*
* @retur {ACL_FIBER*} NULL
2018-11-30 14:38:22 +08:00
*/
ACL_FIBER* acl_fiber_running(void);
/**
* ID
* @param fiber {const ACL_FIBER*} acl_fiber_create
* @return {unsigned int} ID
2018-11-30 14:38:22 +08:00
*/
unsigned int acl_fiber_id(const ACL_FIBER* fiber);
/**
* ID
* @return {unsigned int} ID
2018-11-30 14:38:22 +08:00
*/
unsigned int acl_fiber_self(void);
/**
*
* @param fiber {ACL_FIBER*} NULL 使
* @param errnum {int}
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_set_errno(ACL_FIBER* fiber, int errnum);
/**
*
* @param fiber {ACL_FIBER*} NULL 使
* @return {int}
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_errno(ACL_FIBER* fiber);
/**
* errno
2018-11-30 14:38:22 +08:00
* @return {int}
*/
int acl_fiber_sys_errno(void);
/**
* errno
2018-11-30 14:38:22 +08:00
* @param errnum {int}
*/
void acl_fiber_sys_errno_set(int errnum);
/**
*
* errno
* @param fiber {ACL_FIBER*} NULL 使
* @param yesno {int}
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_keep_errno(ACL_FIBER* fiber, int yesno);
/**
*
* @param fiber {const ACL_FIBER*} NULL 使
* @return {int}
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_status(const ACL_FIBER* fiber);
/**
* 退
* @param fiber {const ACL_FIBER*} NULL
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_kill(ACL_FIBER* fiber);
/**
* 退
* @param fiber {const ACL_FIBER*} NULL 使
*
* @return {int} 0 退 0 退
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_killed(ACL_FIBER* fiber);
/**
* IO
* @param fiber {const ACL_FIBER*} NULL
* @param signum {int} SIGINT, SIGKILL, SIGTERM ... bits/signum.h
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_signal(ACL_FIBER* fiber, int signum);
/**
*
* @param fiber {const ACL_FIBER*} NULL 使
* @retur {int}
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_signum(ACL_FIBER* fiber);
/**
*
2018-11-30 14:38:22 +08:00
* @return {int}
*/
int acl_fiber_yield(void);
/**
*
* @param fiber {ACL_FIBER*} NULL
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_ready(ACL_FIBER* fiber);
/**
*
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_switch(void);
/**
*
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_schedule(void);
/**
* 线
* @return {int} 0 0
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_scheduled(void);
/**
*
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_schedule_stop(void);
/**
* 使
* @param milliseconds {unsigned int}
* @return {unsigned int}
2018-11-30 14:38:22 +08:00
*/
unsigned int acl_fiber_delay(unsigned int milliseconds);
/**
* 使
* @param seconds {unsigned int}
* @return {unsigned int}
2018-11-30 14:38:22 +08:00
*/
unsigned int acl_fiber_sleep(unsigned int seconds);
/**
*
* @param milliseconds {unsigned int}
* @param size {size_t}
* @param fn {void (*)(ACL_FIBER*, void*)}
* @param ctx {void*} fn
* @return {ACL_FIBER*}
2018-11-30 14:38:22 +08:00
*/
ACL_FIBER* acl_fiber_create_timer(unsigned int milliseconds, size_t size,
void (*fn)(ACL_FIBER*, void*), void* ctx);
/**
*
* @param timer {ACL_FIBER*} acl_fiber_create_timer
* @param milliseconds {unsigned int}
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_reset_timer(ACL_FIBER* timer, unsigned int milliseconds);
/**
* DNS
* @param ip {const char*} DNS IP
* @param port {int} DNS
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_set_dns(const char* ip, int port);
/* for fiber specific */
/**
*
* @param key {int*} <= 0
* > 0
* NULL
* @param ctx {void *}
* @param free_fn {void (*)(void*)} 退
* @return {int} -1
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_set_specific(int* key, void* ctx, void (*free_fn)(void*));
/**
*
* @param key {int} acl_fiber_set_specific
* @retur {void*} NULL
2018-11-30 14:38:22 +08:00
*/
void* acl_fiber_get_specific(int key);
/* fiber locking */
/**
* 线线
2018-11-30 14:38:22 +08:00
*/
typedef struct ACL_FIBER_MUTEX ACL_FIBER_MUTEX;
/**
* 线线
2018-11-30 14:38:22 +08:00
*/
typedef struct ACL_FIBER_RWLOCK ACL_FIBER_RWLOCK;
/**
* 线线
2018-11-30 14:38:22 +08:00
* @return {ACL_FIBER_MUTEX*}
*/
ACL_FIBER_MUTEX* acl_fiber_mutex_create(void);
/**
*
* @param l {ACL_FIBER_MUTEX*} acl_fiber_mutex_create
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_mutex_free(ACL_FIBER_MUTEX* l);
/**
*
* @param l {ACL_FIBER_MUTEX*} acl_fiber_mutex_create
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_mutex_lock(ACL_FIBER_MUTEX* l);
/**
*
* @param l {ACL_FIBER_MUTEX*} acl_fiber_mutex_create
* @return {int} 0 -1
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_mutex_trylock(ACL_FIBER_MUTEX* l);
/**
*
*
* @param l {ACL_FIBER_MUTEX*} acl_fiber_mutex_create
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_mutex_unlock(ACL_FIBER_MUTEX* l);
/**
* 线线
2018-11-30 14:38:22 +08:00
* @return {ACL_FIBER_RWLOCK*}
*/
ACL_FIBER_RWLOCK* acl_fiber_rwlock_create(void);
/**
*
* @param l {ACL_FIBER_RWLOCK*} acl_fiber_rwlock_create
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_rwlock_free(ACL_FIBER_RWLOCK* l);
/**
*
*
*
* @param l {ACL_FIBER_RWLOCK*} acl_fiber_rwlock_create
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_rwlock_rlock(ACL_FIBER_RWLOCK* l);
/**
*
* @param l {ACL_FIBER_RWLOCK*} acl_fiber_rwlock_create
* @retur {int} 1 0
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_rwlock_tryrlock(ACL_FIBER_RWLOCK* l);
/**
* /
*
* @param l {ACL_FIBER_RWLOCK*} acl_fiber_rwlock_create
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_rwlock_wlock(ACL_FIBER_RWLOCK* l);
/**
*
* @param l {ACL_FIBER_RWLOCK*} acl_fiber_rwlock_create
* @return {int} 1 0
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_rwlock_trywlock(ACL_FIBER_RWLOCK* l);
/**
*
*
* @param l {ACL_FIBER_RWLOCK*} acl_fiber_rwlock_create
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_rwlock_runlock(ACL_FIBER_RWLOCK* l);
/**
*
*
* @param l {ACL_FIBER_RWLOCK*} acl_fiber_rwlock_create
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_rwlock_wunlock(ACL_FIBER_RWLOCK* l);
/* fiber_event.c */
/* 线程安全的协程锁,可以用在不同线程的协程之间及不同线程之间的互斥 */
2018-11-30 14:38:22 +08:00
typedef struct ACL_FIBER_EVENT ACL_FIBER_EVENT;
/**
* /线
2018-11-30 14:38:22 +08:00
* @return {ACL_FIBER_EVENT *}
*/
ACL_FIBER_EVENT *acl_fiber_event_create(void);
/**
*
2018-11-30 14:38:22 +08:00
* @param {ACL_FIBER_EVENT *}
*/
void acl_fiber_event_free(ACL_FIBER_EVENT *event);
/**
*
2018-11-30 14:38:22 +08:00
* @param {ACL_FIBER_EVENT *}
* @return {int} 0 -1
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_event_wait(ACL_FIBER_EVENT *event);
/**
*
2018-11-30 14:38:22 +08:00
* @param {ACL_FIBER_EVENT *}
* @return {int} 0 -1
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_event_trywait(ACL_FIBER_EVENT *event);
/**
*
2018-11-30 14:38:22 +08:00
* @param {ACL_FIBER_EVENT *}
* @return {int} 0 -1
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_event_notify(ACL_FIBER_EVENT *event);
/* fiber semaphore */
typedef struct ACL_FIBER_SEM ACL_FIBER_SEM;
/**
* 线
* @param num {int} >= 0
2018-11-30 14:38:22 +08:00
* @return {ACL_FIBER_SEM *}
*/
ACL_FIBER_SEM* acl_fiber_sem_create(int num);
/**
*
2018-11-30 14:38:22 +08:00
* @param {ACL_FIBER_SEM *}
*/
void acl_fiber_sem_free(ACL_FIBER_SEM* sem);
/**
* 线 ID
* @param sem {ACL_FIBER_SEM*}
2018-11-30 14:38:22 +08:00
* @return {acl_pthread_t}
*/
acl_pthread_t acl_fiber_sem_get_tid(ACL_FIBER_SEM* sem);
/**
* 线 ID线
* 0 fatal
* @param sem {ACL_FIBER_SEM*}
* @param {acl_pthread_t} 线 ID
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_sem_set_tid(ACL_FIBER_SEM* sem, acl_pthread_t tid);
/**
* > 0 使 1 > 0
2018-11-30 14:38:22 +08:00
* @param sem {ACL_FIBER_SEM *}
* @retur {int} -1 线线
* 线
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_sem_wait(ACL_FIBER_SEM* sem);
/**
* 使 1
2018-11-30 14:38:22 +08:00
* @param sem {ACL_FIBER_SEM *}
* @retur {int} 1 >= 0 -1
* 线线线
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_sem_trywait(ACL_FIBER_SEM* sem);
/**
* 使 1
2018-11-30 14:38:22 +08:00
* @param sem {ACL_FIBER_SEM *}
* @retur {int} -1 线
* 线线
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_sem_post(ACL_FIBER_SEM* sem);
/**
*
2018-11-30 14:38:22 +08:00
* @param sem {ACL_FIBER_SEM*}
* @retur {int}
*/
int acl_fiber_sem_num(ACL_FIBER_SEM* sem);
/* channel communication */
/**
*
2018-11-30 14:38:22 +08:00
*/
typedef struct ACL_CHANNEL ACL_CHANNEL;
/**
*
* @param elemsize {int} ACL_CHANNEL
* @param bufsize {int} ACL_CHANNEL elemsize
*
2018-11-30 14:38:22 +08:00
* @return {CHANNNEL*}
*/
ACL_CHANNEL* acl_channel_create(int elemsize, int bufsize);
/**
* acl_channel_create
* @param c {ACL_CHANNEL*} acl_channel_create
2018-11-30 14:38:22 +08:00
*/
void acl_channel_free(ACL_CHANNEL* c);
/**
* ACL_CHANNEL
* @param c {ACL_CHANNEL*} acl_channel_create
* @param v {void*}
* @return {int} >= 0
2018-11-30 14:38:22 +08:00
*/
int acl_channel_send(ACL_CHANNEL* c, void* v);
/**
* ACL_CHANNEL acl_channel_create
* elemsize
* @param c {ACL_CHANNEL*} acl_channel_create
* @param v {void*}
2018-11-30 14:38:22 +08:00
*/
int acl_channel_send_nb(ACL_CHANNEL* c, void* v);
/**
* ACL_CHANNEL
* @param c {ACL_CHANNEL*} acl_channel_create
* @param v {void*}
* @return {int} >= 0
2018-11-30 14:38:22 +08:00
*/
int acl_channel_recv(ACL_CHANNEL* c, void* v);
/**
* ACL_CHANNEL
* @param c {ACL_CHANNEL*} acl_channel_create
* @param v {void*}
* @return {int} >= 0
2018-11-30 14:38:22 +08:00
*/
int acl_channel_recv_nb(ACL_CHANNEL* c, void* v);
/**
* ACL_CHANNEL
* @param c {ACL_CHANNEL*} acl_channel_create
* @param v {void*}
* @return {int} >= 0
2018-11-30 14:38:22 +08:00
*/
int acl_channel_sendp(ACL_CHANNEL* c, void* v);
/**
* CHANNLE acl_channel_sendp
* @param c {ACL_CHANNEL*} acl_channel_create
* @return {void*} NULL
2018-11-30 14:38:22 +08:00
*/
void* acl_channel_recvp(ACL_CHANNEL* c);
/**
* ACL_CHANNEL
* @param c {ACL_CHANNEL*} acl_channel_create
* @param v {void*}
* @return {int} >= 0
2018-11-30 14:38:22 +08:00
*/
int acl_channel_sendp_nb(ACL_CHANNEL* c, void* v);
/**
* CHANNLE acl_channel_sendp
* @param c {ACL_CHANNEL*} acl_channel_create
* @return {void*} NULL NULL
*
2018-11-30 14:38:22 +08:00
*/
void* acl_channel_recvp_nb(ACL_CHANNEL* c);
/**
* ACL_CHANNEL
* @param c {ACL_CHANNEL*} acl_channel_create
* @param val {unsigned long}
* @return {int} >= 0
2018-11-30 14:38:22 +08:00
*/
int acl_channel_sendul(ACL_CHANNEL* c, unsigned long val);
/**
* ACL_CHANNEL
* @param c {ACL_CHANNEL*} acl_channel_create
2018-11-30 14:38:22 +08:00
* @return {unsigned long}
*/
unsigned long acl_channel_recvul(ACL_CHANNEL* c);
/**
* ACL_CHANNEL
* @param c {ACL_CHANNEL*} acl_channel_create
* @param val {unsigned long}
* @return {int} >= 0
2018-11-30 14:38:22 +08:00
*/
int acl_channel_sendul_nb(ACL_CHANNEL* c, unsigned long val);
/**
* ACL_CHANNEL
* @param c {ACL_CHANNEL*} acl_channel_create
2018-11-30 14:38:22 +08:00
* @return {unsigned long}
*/
unsigned long acl_channel_recvul_nb(ACL_CHANNEL* c);
/* master fibers server */
/**
* acl_master
* @param argc {int} 使 argv
* @param argv {char*[]}
* @param service {void (*)(ACL_VSTREAM*, void*)}
*
* @param ctx {void*} service
* @param name {int}
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_server_main(int argc, char* argv[],
void (*service)(void*, ACL_VSTREAM*), void* ctx, int name, ...);
const char* acl_fiber_server_conf(void);
void acl_fiber_chat_main(int argc, char* argv[],
int (*service)(ACL_VSTREAM*, void*), void* ctx, int name, ...);
/**************************** fiber iostuff *********************************/
ssize_t fiber_read(int fd, void* buf, size_t count);
ssize_t fiber_readv(int fd, const struct iovec* iov, int iovcnt);
ssize_t fiber_recv(int sockfd, void* buf, size_t len, int flags);
ssize_t fiber_recvfrom(int sockfd, void* buf, size_t len, int flags,
struct sockaddr* src_addr, socklen_t* addrlen);
ssize_t fiber_recvmsg(int sockfd, struct msghdr* msg, int flags);
ssize_t fiber_write(int fd, const void* buf, size_t count);
ssize_t fiber_writev(int fd, const struct iovec* iov, int iovcnt);
ssize_t fiber_send(int sockfd, const void* buf, size_t len, int flags);
ssize_t fiber_sendto(int sockfd, const void* buf, size_t len, int flags,
const struct sockaddr* dest_addr, socklen_t addrlen);
ssize_t fiber_sendmsg(int sockfd, const struct msghdr* msg, int flags);
/****************************************************************************/
#ifdef __cplusplus
}
#endif
#endif