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

577 lines
23 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;
/**
* <EFBFBD> hook IO 稿<EFBFBD> API锛屽唴閮ㄧ己鐪佸 1
* @param onoff {int} <EFBFBD> hook
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_hook_api(int onoff);
/**
* <EFBFBD>
* @param fn {void (*)(ACL_FIBER*, void*)}
* @param arg {void*} fn <EFBFBD>
* @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);
/**
* <EFBFBD>
2018-11-30 14:38:22 +08:00
* @retur {int}
*/
int acl_fiber_ndead(void);
void acl_fiber_check_timer(size_t max);
/**
* <EFBFBD>
* @retur {ACL_FIBER*} NULL <EFBFBD>
2018-11-30 14:38:22 +08:00
*/
ACL_FIBER* acl_fiber_running(void);
/**
* ID <EFBFBD>
* @param fiber {const ACL_FIBER*} acl_fiber_create
* @return {unsigned int} ID <EFBFBD>
2018-11-30 14:38:22 +08:00
*/
unsigned int acl_fiber_id(const ACL_FIBER* fiber);
/**
* ID <EFBFBD>
* @return {unsigned int} <EFBFBD> ID <EFBFBD>
2018-11-30 14:38:22 +08:00
*/
unsigned int acl_fiber_self(void);
/**
* <EFBFBD>
* @param fiber {ACL_FIBER*} <EFBFBD> NULL
* @param errnum {int} <EFBFBD>
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_set_errno(ACL_FIBER* fiber, int errnum);
/**
* 彿
* @param fiber {ACL_FIBER*} NULL <EFBFBD>
* @return {int} 彿
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_errno(ACL_FIBER* fiber);
/**
* х errno <EFBFBD>
2018-11-30 14:38:22 +08:00
* @return {int}
*/
int acl_fiber_sys_errno(void);
/**
* <EFBFBD> errno <EFBFBD>
2018-11-30 14:38:22 +08:00
* @param errnum {int}
*/
void acl_fiber_sys_errno_set(int errnum);
/**
* 彿<EFBFBD>
* <EFBFBD> errno <EFBFBD>
* @param fiber {ACL_FIBER*} <EFBFBD> NULL
* @param yesno {int}
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_keep_errno(ACL_FIBER* fiber, int yesno);
/**
* <EFBFBD>
* @param fiber {const ACL_FIBER*} <EFBFBD> NULL <EFBFBD>
* @return {int} <EFBFBD>
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_status(const ACL_FIBER* fiber);
/**
* <EFBFBD>
* @param fiber {const ACL_FIBER*} <EFBFBD> NULL
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_kill(ACL_FIBER* fiber);
/**
* <EFBFBD>
* @param fiber {const ACL_FIBER*} NULL <EFBFBD>
* <EFBFBD>
* @return {int} 0 <EFBFBD> 0 <EFBFBD>
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_killed(ACL_FIBER* fiber);
/**
* <EFBFBD> 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);
/**
* <EFBFBD>
* @param fiber {const ACL_FIBER*} <EFBFBD> NULL
* @retur {int} <EFBFBD>
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);
/**
* <EFBFBD>
* @param fiber {ACL_FIBER*} NULL
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_ready(ACL_FIBER* fiber);
/**
* <EFBFBD>
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);
/**
* <EFBFBD>
* @return {int} 0 <EFBFBD> 0 <EFBFBD>
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);
/**
* <EFBFBD>
* @param milliseconds {unsigned int} <EFBFBD>
* @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} <EFBFBD>
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 <EFBFBD>
* @return {ACL_FIBER*} <EFBFBD>
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*} <EFBFBD> acl_fiber_create_timer
* @param milliseconds {unsigned int} <EFBFBD>
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_reset_timer(ACL_FIBER* timer, unsigned int milliseconds);
/**
* <EFBFBD> DNS
* @param ip {const char*} DNS <EFBFBD> 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 */
/**
* <EFBFBD>
* @param key {int*} <= 0
* <EFBFBD> > 0
* NULL
* @param ctx {void *} <EFBFBD>
* @param free_fn {void (*)(void*)} <EFBFBD>
* @return {int} -1 <EFBFBD>
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_set_specific(int* key, void* ctx, void (*free_fn)(void*));
/**
* <EFBFBD>
* @param key {int} <EFBFBD> acl_fiber_set_specific <EFBFBD>
* @retur {void*} NULL <EFBFBD>
2018-11-30 14:38:22 +08:00
*/
void* acl_fiber_get_specific(int key);
/* fiber locking */
/**
* <EFBFBD>
2018-11-30 14:38:22 +08:00
*/
typedef struct ACL_FIBER_MUTEX ACL_FIBER_MUTEX;
/**
* <EFBFBD>
2018-11-30 14:38:22 +08:00
*/
typedef struct ACL_FIBER_RWLOCK ACL_FIBER_RWLOCK;
/**
* <EFBFBD>
2018-11-30 14:38:22 +08:00
* @return {ACL_FIBER_MUTEX*}
*/
ACL_FIBER_MUTEX* acl_fiber_mutex_create(void);
/**
* <EFBFBD>
* @param l {ACL_FIBER_MUTEX*} <EFBFBD> acl_fiber_mutex_create
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_mutex_free(ACL_FIBER_MUTEX* l);
/**
* <EFBFBD>
* @param l {ACL_FIBER_MUTEX*} <EFBFBD> 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*} <EFBFBD> acl_fiber_mutex_create
* @return {int} <EFBFBD> 0 -1
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_mutex_trylock(ACL_FIBER_MUTEX* l);
/**
*
*
* @param l {ACL_FIBER_MUTEX*} <EFBFBD> acl_fiber_mutex_create
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_mutex_unlock(ACL_FIBER_MUTEX* l);
/**
* <EFBFBD>
2018-11-30 14:38:22 +08:00
* @return {ACL_FIBER_RWLOCK*}
*/
ACL_FIBER_RWLOCK* acl_fiber_rwlock_create(void);
/**
* <EFBFBD>
* @param l {ACL_FIBER_RWLOCK*} <EFBFBD> acl_fiber_rwlock_create
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_rwlock_free(ACL_FIBER_RWLOCK* l);
/**
* <EFBFBD>
*
*
* @param l {ACL_FIBER_RWLOCK*} <EFBFBD> acl_fiber_rwlock_create
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_rwlock_rlock(ACL_FIBER_RWLOCK* l);
/**
* у<EFBFBD>
* @param l {ACL_FIBER_RWLOCK*} <EFBFBD> acl_fiber_rwlock_create
* @retur {int} 1 <EFBFBD> 0
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_rwlock_tryrlock(ACL_FIBER_RWLOCK* l);
/**
* <EFBFBD>/<EFBFBD>
*
* @param l {ACL_FIBER_RWLOCK*} <EFBFBD> acl_fiber_rwlock_create
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_rwlock_wlock(ACL_FIBER_RWLOCK* l);
/**
* у<EFBFBD>
* @param l {ACL_FIBER_RWLOCK*} <EFBFBD> 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);
/**
* В<EFBFBD>
*
* @param l {ACL_FIBER_RWLOCK*} <EFBFBD> acl_fiber_rwlock_create
2018-11-30 14:38:22 +08:00
*/
void acl_fiber_rwlock_runlock(ACL_FIBER_RWLOCK* l);
/**
* В<EFBFBD>
*
* @param l {ACL_FIBER_RWLOCK*} <EFBFBD> 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;
/**
* <EFBFBD>/<EFBFBD>
2018-11-30 14:38:22 +08:00
* @return {ACL_FIBER_EVENT *}
*/
ACL_FIBER_EVENT *acl_fiber_event_create(void);
/**
* <EFBFBD>
2018-11-30 14:38:22 +08:00
* @param {ACL_FIBER_EVENT *}
*/
void acl_fiber_event_free(ACL_FIBER_EVENT *event);
/**
* <EFBFBD>
2018-11-30 14:38:22 +08:00
* @param {ACL_FIBER_EVENT *}
* @return {int} 0 <EFBFBD>-1
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_event_wait(ACL_FIBER_EVENT *event);
/**
* <EFBFBD>
2018-11-30 14:38:22 +08:00
* @param {ACL_FIBER_EVENT *}
* @return {int} 0 <EFBFBD>-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 <EFBFBD>-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;
/**
* 彿彿<EFBFBD>
* @param num {int} 彿 >= 0<EFBFBD>
2018-11-30 14:38:22 +08:00
* @return {ACL_FIBER_SEM *}
*/
ACL_FIBER_SEM* acl_fiber_sem_create(int num);
/**
* 彿<EFBFBD>
2018-11-30 14:38:22 +08:00
* @param {ACL_FIBER_SEM *}
*/
void acl_fiber_sem_free(ACL_FIBER_SEM* sem);
/**
* 彿<EFBFBD> ID
* @param sem {ACL_FIBER_SEM*} 彿<EFBFBD>
2018-11-30 14:38:22 +08:00
* @return {acl_pthread_t}
*/
acl_pthread_t acl_fiber_sem_get_tid(ACL_FIBER_SEM* sem);
/**
* 彿<EFBFBD> ID锛屽綋鏀瑰彉鏈
* <EFBFBD> 0 <EFBFBD> fatal锛屽嵆褰撳崗绋嬩俊鍙烽噺涓婄瓑寰呭崗绋嬮潪绌烘椂绂佹<EFBFBD>
* @param sem {ACL_FIBER_SEM*} 彿<EFBFBD>
* @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 <EFBFBD>
* <EFBFBD>
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_sem_wait(ACL_FIBER_SEM* sem);
/**
* <EFBFBD> 1
2018-11-30 14:38:22 +08:00
* @param sem {ACL_FIBER_SEM *}
* @retur {int} <EFBFBD> 1 <EFBFBD> >= 0<EFBFBD> -1 彿
* 彿
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_sem_trywait(ACL_FIBER_SEM* sem);
/**
* <EFBFBD> 1
2018-11-30 14:38:22 +08:00
* @param sem {ACL_FIBER_SEM *}
* @retur {int} 彿 -1 彿<EFBFBD>
*
2018-11-30 14:38:22 +08:00
*/
int acl_fiber_sem_post(ACL_FIBER_SEM* sem);
/**
* 彿<EFBFBD>
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 */
/**
* <EFBFBD>
2018-11-30 14:38:22 +08:00
*/
typedef struct ACL_CHANNEL ACL_CHANNEL;
/**
*
* @param elemsize {int} <EFBFBD> ACL_CHANNEL у
* @param bufsize {int} ACL_CHANNEL <EFBFBD> elemsize у
* <EFBFBD>
2018-11-30 14:38:22 +08:00
* @return {CHANNNEL*}
*/
ACL_CHANNEL* acl_channel_create(int elemsize, int bufsize);
/**
* <EFBFBD> acl_channel_create
* @param c {ACL_CHANNEL*} <EFBFBD> acl_channel_create <EFBFBD>
2018-11-30 14:38:22 +08:00
*/
void acl_channel_free(ACL_CHANNEL* c);
/**
* ACL_CHANNEL
* @param c {ACL_CHANNEL*} <EFBFBD> acl_channel_create <EFBFBD>
* @param v {void*}
* @return {int} <EFBFBD> >= 0
2018-11-30 14:38:22 +08:00
*/
int acl_channel_send(ACL_CHANNEL* c, void* v);
/**
* <EFBFBD> ACL_CHANNEL acl_channel_create <EFBFBD>
* <EFBFBD> elemsize у
* @param c {ACL_CHANNEL*} <EFBFBD> acl_channel_create <EFBFBD>
* @param v {void*}
2018-11-30 14:38:22 +08:00
*/
int acl_channel_send_nb(ACL_CHANNEL* c, void* v);
/**
* ACL_CHANNEL <EFBFBD>
* @param c {ACL_CHANNEL*} <EFBFBD> acl_channel_create <EFBFBD>
* @param v {void*}
* @return {int} <EFBFBD> >= 0
2018-11-30 14:38:22 +08:00
*/
int acl_channel_recv(ACL_CHANNEL* c, void* v);
/**
* ACL_CHANNEL
* @param c {ACL_CHANNEL*} <EFBFBD> acl_channel_create <EFBFBD>
* @param v {void*}
* @return {int} <EFBFBD> >= 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*} <EFBFBD> acl_channel_create <EFBFBD>
* @param v {void*}
* @return {int} <EFBFBD> >= 0
2018-11-30 14:38:22 +08:00
*/
int acl_channel_sendp(ACL_CHANNEL* c, void* v);
/**
* CHANNLE <EFBFBD> acl_channel_sendp
* @param c {ACL_CHANNEL*} <EFBFBD> acl_channel_create <EFBFBD>
* @return {void*} <EFBFBD> NULL锛屾寚瀹氭帴鏀跺埌鐨勫
2018-11-30 14:38:22 +08:00
*/
void* acl_channel_recvp(ACL_CHANNEL* c);
/**
* ACL_CHANNEL
* @param c {ACL_CHANNEL*} <EFBFBD> acl_channel_create <EFBFBD>
* @param v {void*}
* @return {int} <EFBFBD> >= 0
2018-11-30 14:38:22 +08:00
*/
int acl_channel_sendp_nb(ACL_CHANNEL* c, void* v);
/**
* CHANNLE <EFBFBD> acl_channel_sendp
* @param c {ACL_CHANNEL*} <EFBFBD> acl_channel_create <EFBFBD>
* @return {void*} <EFBFBD> NULL锛屾寚瀹氭帴鏀跺埌鐨勫<EFBFBD> NULL
*
2018-11-30 14:38:22 +08:00
*/
void* acl_channel_recvp_nb(ACL_CHANNEL* c);
/**
* ACL_CHANNEL 彿<EFBFBD>
* @param c {ACL_CHANNEL*} <EFBFBD> acl_channel_create <EFBFBD>
* @param val {unsigned long} <EFBFBD>
* @return {int} <EFBFBD> >= 0
2018-11-30 14:38:22 +08:00
*/
int acl_channel_sendul(ACL_CHANNEL* c, unsigned long val);
/**
* ACL_CHANNEL 彿<EFBFBD>
* @param c {ACL_CHANNEL*} <EFBFBD> acl_channel_create <EFBFBD>
2018-11-30 14:38:22 +08:00
* @return {unsigned long}
*/
unsigned long acl_channel_recvul(ACL_CHANNEL* c);
/**
* ACL_CHANNEL 彿<EFBFBD>
* @param c {ACL_CHANNEL*} <EFBFBD> acl_channel_create <EFBFBD>
* @param val {unsigned long} <EFBFBD>
* @return {int} <EFBFBD> >= 0
2018-11-30 14:38:22 +08:00
*/
int acl_channel_sendul_nb(ACL_CHANNEL* c, unsigned long val);
/**
* ACL_CHANNEL 彿<EFBFBD>
* @param c {ACL_CHANNEL*} <EFBFBD> acl_channel_create <EFBFBD>
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 <EFBFBD>
* @param argv {char*[]} у
* @param service {void (*)(ACL_VSTREAM*, void*)} <EFBFBD>
*
* @param ctx {void*} service
* @param name {int} у<EFBFBD>
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