1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

research st: update public and common.

This commit is contained in:
winlin 2014-11-05 17:22:36 +08:00
parent 315707d726
commit 856ba07bd3
3 changed files with 137 additions and 145 deletions

View file

@ -50,7 +50,7 @@
/* Enable assertions only if DEBUG is defined */ /* Enable assertions only if DEBUG is defined */
#ifndef DEBUG #ifndef DEBUG
#define NDEBUG #define NDEBUG
#endif #endif
#include <assert.h> #include <assert.h>
#define ST_ASSERT(expr) assert(expr) #define ST_ASSERT(expr) assert(expr)
@ -59,15 +59,14 @@
#define ST_END_MACRO } #define ST_END_MACRO }
#ifdef DEBUG #ifdef DEBUG
#define ST_HIDDEN /*nothing*/ #define ST_HIDDEN /*nothing*/
#else #else
#define ST_HIDDEN static #define ST_HIDDEN static
#endif #endif
#include "public.h" #include "public.h"
#include "md.h" #include "md.h"
/***************************************** /*****************************************
* Circular linked list definitions * Circular linked list definitions
*/ */
@ -137,7 +136,6 @@ typedef struct _st_clist {
typedef void (*_st_destructor_t)(void *); typedef void (*_st_destructor_t)(void *);
typedef struct _st_stack { typedef struct _st_stack {
_st_clist_t links; _st_clist_t links;
char *vaddr; /* Base of stack's allocated memory */ char *vaddr; /* Base of stack's allocated memory */
@ -146,9 +144,9 @@ typedef struct _st_stack {
char *stk_bottom; /* Lowest address of stack's usable portion */ char *stk_bottom; /* Lowest address of stack's usable portion */
char *stk_top; /* Highest address of stack's usable portion */ char *stk_top; /* Highest address of stack's usable portion */
void *sp; /* Stack pointer from C's point of view */ void *sp; /* Stack pointer from C's point of view */
#ifdef __ia64__ #ifdef __ia64__
void *bsp; /* Register stack backing store pointer */ void *bsp; /* Register stack backing store pointer */
#endif #endif
} _st_stack_t; } _st_stack_t;
@ -171,9 +169,9 @@ struct _st_thread {
_st_clist_t links; /* For putting on run/sleep/zombie queue */ _st_clist_t links; /* For putting on run/sleep/zombie queue */
_st_clist_t wait_links; /* For putting on mutex/condvar wait queue */ _st_clist_t wait_links; /* For putting on mutex/condvar wait queue */
#ifdef DEBUG #ifdef DEBUG
_st_clist_t tlink; /* For putting on thread queue */ _st_clist_t tlink; /* For putting on thread queue */
#endif #endif
st_utime_t due; /* Wakeup time when thread is sleeping */ st_utime_t due; /* Wakeup time when thread is sleeping */
_st_thread_t *left; /* For putting in timeout heap */ _st_thread_t *left; /* For putting in timeout heap */
@ -223,18 +221,18 @@ typedef struct _st_vp {
_st_clist_t run_q; /* run queue for this vp */ _st_clist_t run_q; /* run queue for this vp */
_st_clist_t io_q; /* io queue for this vp */ _st_clist_t io_q; /* io queue for this vp */
_st_clist_t zombie_q; /* zombie queue for this vp */ _st_clist_t zombie_q; /* zombie queue for this vp */
#ifdef DEBUG #ifdef DEBUG
_st_clist_t thread_q; /* all threads of this vp */ _st_clist_t thread_q; /* all threads of this vp */
#endif #endif
int pagesize; int pagesize;
_st_thread_t *sleep_q; /* sleep queue for this vp */ _st_thread_t *sleep_q; /* sleep queue for this vp */
int sleepq_size; /* number of threads on sleep queue */ int sleepq_size; /* number of threads on sleep queue */
#ifdef ST_SWITCH_CB #ifdef ST_SWITCH_CB
st_switch_cb_t switch_out_cb; /* called when a thread is switched out */ st_switch_cb_t switch_out_cb; /* called when a thread is switched out */
st_switch_cb_t switch_in_cb; /* called when a thread is switched in */ st_switch_cb_t switch_in_cb; /* called when a thread is switched in */
#endif #endif
} _st_vp_t; } _st_vp_t;
@ -265,7 +263,7 @@ extern _st_eventsys_t *_st_eventsys;
#define _ST_IOQ (_st_this_vp.io_q) #define _ST_IOQ (_st_this_vp.io_q)
#define _ST_ZOMBIEQ (_st_this_vp.zombie_q) #define _ST_ZOMBIEQ (_st_this_vp.zombie_q)
#ifdef DEBUG #ifdef DEBUG
#define _ST_THREADQ (_st_this_vp.thread_q) #define _ST_THREADQ (_st_this_vp.thread_q)
#endif #endif
#define _ST_PAGE_SIZE (_st_this_vp.pagesize) #define _ST_PAGE_SIZE (_st_this_vp.pagesize)
@ -293,8 +291,8 @@ extern _st_eventsys_t *_st_eventsys;
#define _ST_DEL_ZOMBIEQ(_thr) ST_REMOVE_LINK(&(_thr)->links) #define _ST_DEL_ZOMBIEQ(_thr) ST_REMOVE_LINK(&(_thr)->links)
#ifdef DEBUG #ifdef DEBUG
#define _ST_ADD_THREADQ(_thr) ST_APPEND_LINK(&(_thr)->tlink, &_ST_THREADQ) #define _ST_ADD_THREADQ(_thr) ST_APPEND_LINK(&(_thr)->tlink, &_ST_THREADQ)
#define _ST_DEL_THREADQ(_thr) ST_REMOVE_LINK(&(_thr)->tlink) #define _ST_DEL_THREADQ(_thr) ST_REMOVE_LINK(&(_thr)->tlink)
#endif #endif
@ -317,13 +315,12 @@ extern _st_eventsys_t *_st_eventsys;
#define _ST_FL_INTERRUPT 0x08 #define _ST_FL_INTERRUPT 0x08
#define _ST_FL_TIMEDOUT 0x10 #define _ST_FL_TIMEDOUT 0x10
/***************************************** /*****************************************
* Pointer conversion * Pointer conversion
*/ */
#ifndef offsetof #ifndef offsetof
#define offsetof(type, identifier) ((size_t)&(((type *)0)->identifier)) #define offsetof(type, identifier) ((size_t)&(((type *)0)->identifier))
#endif #endif
#define _ST_THREAD_PTR(_qp) \ #define _ST_THREAD_PTR(_qp) \
@ -339,7 +336,7 @@ extern _st_eventsys_t *_st_eventsys;
((_st_pollq_t *)((char *)(_qp) - offsetof(_st_pollq_t, links))) ((_st_pollq_t *)((char *)(_qp) - offsetof(_st_pollq_t, links)))
#ifdef DEBUG #ifdef DEBUG
#define _ST_THREAD_THREADQ_PTR(_qp) \ #define _ST_THREAD_THREADQ_PTR(_qp) \
((_st_thread_t *)((char *)(_qp) - offsetof(_st_thread_t, tlink))) ((_st_thread_t *)((char *)(_qp) - offsetof(_st_thread_t, tlink)))
#endif #endif
@ -349,21 +346,21 @@ extern _st_eventsys_t *_st_eventsys;
*/ */
#ifndef ST_UTIME_NO_TIMEOUT #ifndef ST_UTIME_NO_TIMEOUT
#define ST_UTIME_NO_TIMEOUT ((st_utime_t) -1LL) #define ST_UTIME_NO_TIMEOUT ((st_utime_t) -1LL)
#endif #endif
#ifndef __ia64__ #ifndef __ia64__
#define ST_DEFAULT_STACK_SIZE (64*1024) #define ST_DEFAULT_STACK_SIZE (64*1024)
#else #else
#define ST_DEFAULT_STACK_SIZE (128*1024) /* Includes register stack size */ #define ST_DEFAULT_STACK_SIZE (128*1024) /* Includes register stack size */
#endif #endif
#ifndef ST_KEYS_MAX #ifndef ST_KEYS_MAX
#define ST_KEYS_MAX 16 #define ST_KEYS_MAX 16
#endif #endif
#ifndef ST_MIN_POLLFDS_SIZE #ifndef ST_MIN_POLLFDS_SIZE
#define ST_MIN_POLLFDS_SIZE 64 #define ST_MIN_POLLFDS_SIZE 64
#endif #endif
@ -372,28 +369,28 @@ extern _st_eventsys_t *_st_eventsys;
*/ */
#ifdef DEBUG #ifdef DEBUG
void _st_iterate_threads(void); void _st_iterate_threads(void);
#define ST_DEBUG_ITERATE_THREADS() _st_iterate_threads() #define ST_DEBUG_ITERATE_THREADS() _st_iterate_threads()
#else #else
#define ST_DEBUG_ITERATE_THREADS() #define ST_DEBUG_ITERATE_THREADS()
#endif #endif
#ifdef ST_SWITCH_CB #ifdef ST_SWITCH_CB
#define ST_SWITCH_OUT_CB(_thread) \ #define ST_SWITCH_OUT_CB(_thread) \
if (_st_this_vp.switch_out_cb != NULL && \ if (_st_this_vp.switch_out_cb != NULL && \
_thread != _st_this_vp.idle_thread && \ _thread != _st_this_vp.idle_thread && \
_thread->state != _ST_ST_ZOMBIE) { \ _thread->state != _ST_ST_ZOMBIE) { \
_st_this_vp.switch_out_cb(); \ _st_this_vp.switch_out_cb(); \
} }
#define ST_SWITCH_IN_CB(_thread) \ #define ST_SWITCH_IN_CB(_thread) \
if (_st_this_vp.switch_in_cb != NULL && \ if (_st_this_vp.switch_in_cb != NULL && \
_thread != _st_this_vp.idle_thread && \ _thread != _st_this_vp.idle_thread && \
_thread->state != _ST_ST_ZOMBIE) { \ _thread->state != _ST_ST_ZOMBIE) { \
_st_this_vp.switch_in_cb(); \ _st_this_vp.switch_in_cb(); \
} }
#else #else
#define ST_SWITCH_OUT_CB(_thread) #define ST_SWITCH_OUT_CB(_thread)
#define ST_SWITCH_IN_CB(_thread) #define ST_SWITCH_IN_CB(_thread)
#endif #endif
/* /*
@ -424,9 +421,9 @@ void _st_iterate_threads(void);
* Initialize the thread context preparing it to execute _main * Initialize the thread context preparing it to execute _main
*/ */
#ifdef MD_INIT_CONTEXT #ifdef MD_INIT_CONTEXT
#define _ST_INIT_CONTEXT MD_INIT_CONTEXT #define _ST_INIT_CONTEXT MD_INIT_CONTEXT
#else #else
#error Unknown OS #error Unknown OS
#endif #endif
/* /*
@ -456,11 +453,9 @@ int st_cond_destroy(_st_cond_t *cvar);
int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout); int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout);
int st_cond_signal(_st_cond_t *cvar); int st_cond_signal(_st_cond_t *cvar);
ssize_t st_read(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout); ssize_t st_read(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout);
ssize_t st_write(_st_netfd_t *fd, const void *buf, size_t nbyte, ssize_t st_write(_st_netfd_t *fd, const void *buf, size_t nbyte, st_utime_t timeout);
st_utime_t timeout);
int st_poll(struct pollfd *pds, int npds, st_utime_t timeout); int st_poll(struct pollfd *pds, int npds, st_utime_t timeout);
_st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stk_size);
int joinable, int stk_size);
#endif /* !__ST_COMMON_H__ */ #endif /* !__ST_COMMON_H__ */

View file

@ -43,15 +43,15 @@
#define __ST_MD_H__ #define __ST_MD_H__
#if defined(ETIMEDOUT) && !defined(ETIME) #if defined(ETIMEDOUT) && !defined(ETIME)
#define ETIME ETIMEDOUT #define ETIME ETIMEDOUT
#endif #endif
#if defined(MAP_ANONYMOUS) && !defined(MAP_ANON) #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
#define MAP_ANON MAP_ANONYMOUS #define MAP_ANON MAP_ANONYMOUS
#endif #endif
#ifndef MAP_FAILED #ifndef MAP_FAILED
#define MAP_FAILED -1 #define MAP_FAILED -1
#endif #endif
/***************************************** /*****************************************
@ -59,35 +59,33 @@
*/ */
#if defined (AIX) #if defined (AIX)
#define MD_STACK_GROWS_DOWN
#define MD_USE_SYSV_ANON_MMAP
#define MD_ACCEPT_NB_INHERITED
#define MD_ALWAYS_UNSERIALIZED_ACCEPT
#define MD_STACK_GROWS_DOWN #ifndef MD_HAVE_SOCKLEN_T
#define MD_USE_SYSV_ANON_MMAP #define MD_HAVE_SOCKLEN_T
#define MD_ACCEPT_NB_INHERITED #define socklen_t unsigned long
#define MD_ALWAYS_UNSERIALIZED_ACCEPT #endif
#ifndef MD_HAVE_SOCKLEN_T #define MD_SETJMP(env) _setjmp(env)
#define MD_HAVE_SOCKLEN_T #define MD_LONGJMP(env, val) _longjmp(env, val)
#define socklen_t unsigned long
#endif
#define MD_SETJMP(env) _setjmp(env) #define MD_INIT_CONTEXT(_thread, _sp, _main) \
#define MD_LONGJMP(env, val) _longjmp(env, val)
#define MD_INIT_CONTEXT(_thread, _sp, _main) \
ST_BEGIN_MACRO \ ST_BEGIN_MACRO \
if (MD_SETJMP((_thread)->context)) \ if (MD_SETJMP((_thread)->context)) \
_main(); \ _main(); \
(_thread)->context[3] = (long) (_sp); \ (_thread)->context[3] = (long) (_sp); \
ST_END_MACRO ST_END_MACRO
#define MD_GET_UTIME() \ #define MD_GET_UTIME() \
timebasestruct_t rt; \ timebasestruct_t rt; \
(void) read_real_time(&rt, TIMEBASE_SZ); \ (void) read_real_time(&rt, TIMEBASE_SZ); \
(void) time_base_to_time(&rt, TIMEBASE_SZ); \ (void) time_base_to_time(&rt, TIMEBASE_SZ); \
return (rt.tb_high * 1000000LL + rt.tb_low / 1000) return (rt.tb_high * 1000000LL + rt.tb_low / 1000)
#elif defined (CYGWIN) #elif defined (CYGWIN)
#define MD_STACK_GROWS_DOWN #define MD_STACK_GROWS_DOWN
#define MD_USE_BSD_ANON_MMAP #define MD_USE_BSD_ANON_MMAP
#define MD_ACCEPT_NB_NOT_INHERITED #define MD_ACCEPT_NB_NOT_INHERITED

View file

@ -129,8 +129,7 @@ extern "C" {
extern void st_netfd_free(st_netfd_t fd); extern void st_netfd_free(st_netfd_t fd);
extern int st_netfd_close(st_netfd_t fd); extern int st_netfd_close(st_netfd_t fd);
extern int st_netfd_fileno(st_netfd_t fd); extern int st_netfd_fileno(st_netfd_t fd);
extern void st_netfd_setspecific(st_netfd_t fd, void *value, extern void st_netfd_setspecific(st_netfd_t fd, void *value, void (*destructor)(void *));
void (*destructor)(void *));
extern void *st_netfd_getspecific(st_netfd_t fd); extern void *st_netfd_getspecific(st_netfd_t fd);
extern int st_netfd_serialize_accept(st_netfd_t fd); extern int st_netfd_serialize_accept(st_netfd_t fd);
extern int st_netfd_poll(st_netfd_t fd, int how, st_utime_t timeout); extern int st_netfd_poll(st_netfd_t fd, int how, st_utime_t timeout);