From cfa97e9af7571d45ac49ff2e9d0e3344a84474f3 Mon Sep 17 00:00:00 2001 From: winlin Date: Tue, 27 Jul 2021 08:36:32 +0800 Subject: [PATCH] ST: Fix build issues for Cygwin64 (#20) --- .gitignore | 1 + .run/cygwin64-debug.run.xml | 8 + .run/darwin-debug.run.xml | 8 + Makefile | 23 ++- event.c | 324 +++++++++++++++++++++++++++++++++++- md.h | 38 ++++- md_cygwin64.S | 76 +++++++++ public.h | 1 + 8 files changed, 474 insertions(+), 5 deletions(-) create mode 100644 .run/cygwin64-debug.run.xml create mode 100644 .run/darwin-debug.run.xml create mode 100644 md_cygwin64.S diff --git a/.gitignore b/.gitignore index e62491c..e9b5ce8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ DARWIN_*_DBG LINUX_*_DBG +CYGWIN64_*_DBG obj st.pc .idea diff --git a/.run/cygwin64-debug.run.xml b/.run/cygwin64-debug.run.xml new file mode 100644 index 0000000..4f18ea9 --- /dev/null +++ b/.run/cygwin64-debug.run.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.run/darwin-debug.run.xml b/.run/darwin-debug.run.xml new file mode 100644 index 0000000..dec88ae --- /dev/null +++ b/.run/darwin-debug.run.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Makefile b/Makefile index 1c95225..7c1bbc9 100644 --- a/Makefile +++ b/Makefile @@ -58,6 +58,12 @@ ECHO = /bin/echo BUILD = DBG TARGETDIR = $(OS)_$(shell uname -r)_$(BUILD) +# For cygwin/windows, the 'uname -r' generate path with parentheses, +# which cause the make fails, so we use 'uname -s' instead. +ifeq ($(OS), CYGWIN64) +TARGETDIR = $(OS)_$(shell uname -s)_$(BUILD) +endif + DEFINES = -D$(OS) CFLAGS = SFLAGS = @@ -73,7 +79,8 @@ DESC = st.pc # Possible targets: TARGETS = darwin-debug darwin-optimized \ - linux-debug linux-optimized + linux-debug linux-optimized \ + cygwin64-debug UTEST_TARGETS = darwin-debug-utest linux-debug-utest \ darwin-debug-gcov linux-debug-gcov @@ -102,6 +109,14 @@ OTHER_FLAGS = -Wall DEFINES += -DMD_HAVE_EPOLL endif +ifeq ($(OS), CYGWIN64) +EXTRA_OBJS = $(TARGETDIR)/md_cygwin64.o +SFLAGS = -fPIC +LDFLAGS = -shared -soname=$(SONAME) -lc +OTHER_FLAGS = -Wall +DEFINES += -DMD_HAVE_SELECT +endif + # # End of platform section. ########################## @@ -249,6 +264,9 @@ $(TARGETDIR)/md_linux.o: md_linux.S $(TARGETDIR)/md_darwin.o: md_darwin.S $(CC) $(CFLAGS) -c $< -o $@ +$(TARGETDIR)/md_cygwin64.o: md_cygwin64.S + $(CC) $(CFLAGS) -c $< -o $@ + $(TARGETDIR)/%.o: %.c common.h md.h $(CC) $(CFLAGS) -c $< -o $@ @@ -281,6 +299,9 @@ linux-debug: linux-optimized: $(MAKE) OS="LINUX" BUILD="OPT" +cygwin64-debug: + $(MAKE) OS="CYGWIN64" BUILD="DBG" + darwin-debug-utest: @echo "Build utest for state-threads" $(MAKE) OS="DARWIN" BUILD="DBG" diff --git a/event.c b/event.c index d7e770d..02a1695 100644 --- a/event.c +++ b/event.c @@ -55,8 +55,25 @@ unsigned long long _st_stat_epoll_shake = 0; unsigned long long _st_stat_epoll_spin = 0; #endif -#if !defined(MD_HAVE_KQUEUE) && !defined(MD_HAVE_EPOLL) - #error Only support epoll(for Linux) or kqueue(for Darwin) +#if !defined(MD_HAVE_KQUEUE) && !defined(MD_HAVE_EPOLL) && !defined(MD_HAVE_SELECT) + #error Only support epoll(for Linux), kqueue(for Darwin) or select(for Cygwin) +#endif + + +#ifdef MD_HAVE_SELECT +static struct _st_seldata { + fd_set fd_read_set, fd_write_set, fd_exception_set; + int fd_ref_cnts[FD_SETSIZE][3]; + int maxfd; +} *_st_select_data; + +#define _ST_SELECT_MAX_OSFD (_st_select_data->maxfd) +#define _ST_SELECT_READ_SET (_st_select_data->fd_read_set) +#define _ST_SELECT_WRITE_SET (_st_select_data->fd_write_set) +#define _ST_SELECT_EXCEP_SET (_st_select_data->fd_exception_set) +#define _ST_SELECT_READ_CNT(fd) (_st_select_data->fd_ref_cnts[fd][0]) +#define _ST_SELECT_WRITE_CNT(fd) (_st_select_data->fd_ref_cnts[fd][1]) +#define _ST_SELECT_EXCEP_CNT(fd) (_st_select_data->fd_ref_cnts[fd][2]) #endif @@ -130,6 +147,300 @@ static struct _st_epolldata { _st_eventsys_t *_st_eventsys = NULL; +#ifdef MD_HAVE_SELECT +/***************************************** + * select event system + */ + +ST_HIDDEN int _st_select_init(void) +{ + _st_select_data = (struct _st_seldata *) malloc(sizeof(*_st_select_data)); + if (!_st_select_data) + return -1; + + memset(_st_select_data, 0, sizeof(*_st_select_data)); + _st_select_data->maxfd = -1; + + return 0; +} + +ST_HIDDEN int _st_select_pollset_add(struct pollfd *pds, int npds) +{ + struct pollfd *pd; + struct pollfd *epd = pds + npds; + + /* Do checks up front */ + for (pd = pds; pd < epd; pd++) { + if (pd->fd < 0 || pd->fd >= FD_SETSIZE || !pd->events || + (pd->events & ~(POLLIN | POLLOUT | POLLPRI))) { + errno = EINVAL; + return -1; + } + } + + for (pd = pds; pd < epd; pd++) { + if (pd->events & POLLIN) { + FD_SET(pd->fd, &_ST_SELECT_READ_SET); + _ST_SELECT_READ_CNT(pd->fd)++; + } + if (pd->events & POLLOUT) { + FD_SET(pd->fd, &_ST_SELECT_WRITE_SET); + _ST_SELECT_WRITE_CNT(pd->fd)++; + } + if (pd->events & POLLPRI) { + FD_SET(pd->fd, &_ST_SELECT_EXCEP_SET); + _ST_SELECT_EXCEP_CNT(pd->fd)++; + } + if (_ST_SELECT_MAX_OSFD < pd->fd) + _ST_SELECT_MAX_OSFD = pd->fd; + } + + return 0; +} + +ST_HIDDEN void _st_select_pollset_del(struct pollfd *pds, int npds) +{ + struct pollfd *pd; + struct pollfd *epd = pds + npds; + + for (pd = pds; pd < epd; pd++) { + if (pd->events & POLLIN) { + if (--_ST_SELECT_READ_CNT(pd->fd) == 0) + FD_CLR(pd->fd, &_ST_SELECT_READ_SET); + } + if (pd->events & POLLOUT) { + if (--_ST_SELECT_WRITE_CNT(pd->fd) == 0) + FD_CLR(pd->fd, &_ST_SELECT_WRITE_SET); + } + if (pd->events & POLLPRI) { + if (--_ST_SELECT_EXCEP_CNT(pd->fd) == 0) + FD_CLR(pd->fd, &_ST_SELECT_EXCEP_SET); + } + } +} + +ST_HIDDEN void _st_select_find_bad_fd(void) +{ + _st_clist_t *q; + _st_pollq_t *pq; + int notify; + struct pollfd *pds, *epds; + int pq_max_osfd, osfd; + short events; + + _ST_SELECT_MAX_OSFD = -1; + + for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) { + pq = _ST_POLLQUEUE_PTR(q); + notify = 0; + epds = pq->pds + pq->npds; + pq_max_osfd = -1; + + for (pds = pq->pds; pds < epds; pds++) { + osfd = pds->fd; + pds->revents = 0; + if (pds->events == 0) + continue; + if (fcntl(osfd, F_GETFL, 0) < 0) { + pds->revents = POLLNVAL; + notify = 1; + } + if (osfd > pq_max_osfd) { + pq_max_osfd = osfd; + } + } + + if (notify) { + ST_REMOVE_LINK(&pq->links); + pq->on_ioq = 0; + /* + * Decrement the count of descriptors for each descriptor/event + * because this I/O request is being removed from the ioq + */ + for (pds = pq->pds; pds < epds; pds++) { + osfd = pds->fd; + events = pds->events; + if (events & POLLIN) { + if (--_ST_SELECT_READ_CNT(osfd) == 0) { + FD_CLR(osfd, &_ST_SELECT_READ_SET); + } + } + if (events & POLLOUT) { + if (--_ST_SELECT_WRITE_CNT(osfd) == 0) { + FD_CLR(osfd, &_ST_SELECT_WRITE_SET); + } + } + if (events & POLLPRI) { + if (--_ST_SELECT_EXCEP_CNT(osfd) == 0) { + FD_CLR(osfd, &_ST_SELECT_EXCEP_SET); + } + } + } + + if (pq->thread->flags & _ST_FL_ON_SLEEPQ) + _ST_DEL_SLEEPQ(pq->thread); + pq->thread->state = _ST_ST_RUNNABLE; + _ST_ADD_RUNQ(pq->thread); + } else { + if (_ST_SELECT_MAX_OSFD < pq_max_osfd) + _ST_SELECT_MAX_OSFD = pq_max_osfd; + } + } +} + +ST_HIDDEN void _st_select_dispatch(void) +{ + struct timeval timeout, *tvp; + fd_set r, w, e; + fd_set *rp, *wp, *ep; + int nfd, pq_max_osfd, osfd; + _st_clist_t *q; + st_utime_t min_timeout; + _st_pollq_t *pq; + int notify; + struct pollfd *pds, *epds; + short events, revents; + + /* + * Assignment of fd_sets + */ + r = _ST_SELECT_READ_SET; + w = _ST_SELECT_WRITE_SET; + e = _ST_SELECT_EXCEP_SET; + + rp = &r; + wp = &w; + ep = &e; + + if (_ST_SLEEPQ == NULL) { + tvp = NULL; + } else { + min_timeout = (_ST_SLEEPQ->due <= _ST_LAST_CLOCK) ? 0 : + (_ST_SLEEPQ->due - _ST_LAST_CLOCK); + timeout.tv_sec = (int) (min_timeout / 1000000); + timeout.tv_usec = (int) (min_timeout % 1000000); + tvp = &timeout; + } + + /* Check for I/O operations */ + nfd = select(_ST_SELECT_MAX_OSFD + 1, rp, wp, ep, tvp); + + /* Notify threads that are associated with the selected descriptors */ + if (nfd > 0) { + _ST_SELECT_MAX_OSFD = -1; + for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) { + pq = _ST_POLLQUEUE_PTR(q); + notify = 0; + epds = pq->pds + pq->npds; + pq_max_osfd = -1; + + for (pds = pq->pds; pds < epds; pds++) { + osfd = pds->fd; + events = pds->events; + revents = 0; + if ((events & POLLIN) && FD_ISSET(osfd, rp)) { + revents |= POLLIN; + } + if ((events & POLLOUT) && FD_ISSET(osfd, wp)) { + revents |= POLLOUT; + } + if ((events & POLLPRI) && FD_ISSET(osfd, ep)) { + revents |= POLLPRI; + } + pds->revents = revents; + if (revents) { + notify = 1; + } + if (osfd > pq_max_osfd) { + pq_max_osfd = osfd; + } + } + if (notify) { + ST_REMOVE_LINK(&pq->links); + pq->on_ioq = 0; + /* + * Decrement the count of descriptors for each descriptor/event + * because this I/O request is being removed from the ioq + */ + for (pds = pq->pds; pds < epds; pds++) { + osfd = pds->fd; + events = pds->events; + if (events & POLLIN) { + if (--_ST_SELECT_READ_CNT(osfd) == 0) { + FD_CLR(osfd, &_ST_SELECT_READ_SET); + } + } + if (events & POLLOUT) { + if (--_ST_SELECT_WRITE_CNT(osfd) == 0) { + FD_CLR(osfd, &_ST_SELECT_WRITE_SET); + } + } + if (events & POLLPRI) { + if (--_ST_SELECT_EXCEP_CNT(osfd) == 0) { + FD_CLR(osfd, &_ST_SELECT_EXCEP_SET); + } + } + } + + if (pq->thread->flags & _ST_FL_ON_SLEEPQ) + _ST_DEL_SLEEPQ(pq->thread); + pq->thread->state = _ST_ST_RUNNABLE; + _ST_ADD_RUNQ(pq->thread); + } else { + if (_ST_SELECT_MAX_OSFD < pq_max_osfd) + _ST_SELECT_MAX_OSFD = pq_max_osfd; + } + } + } else if (nfd < 0) { + /* + * It can happen when a thread closes file descriptor + * that is being used by some other thread -- BAD! + */ + if (errno == EBADF) + _st_select_find_bad_fd(); + } +} + +ST_HIDDEN int _st_select_fd_new(int osfd) +{ + if (osfd >= FD_SETSIZE) { + errno = EMFILE; + return -1; + } + + return 0; +} + +ST_HIDDEN int _st_select_fd_close(int osfd) +{ + if (_ST_SELECT_READ_CNT(osfd) || _ST_SELECT_WRITE_CNT(osfd) || + _ST_SELECT_EXCEP_CNT(osfd)) { + errno = EBUSY; + return -1; + } + + return 0; +} + +ST_HIDDEN int _st_select_fd_getlimit(void) +{ + return FD_SETSIZE; +} + +static _st_eventsys_t _st_select_eventsys = { + "select", + ST_EVENTSYS_SELECT, + _st_select_init, + _st_select_dispatch, + _st_select_pollset_add, + _st_select_pollset_del, + _st_select_fd_new, + _st_select_fd_close, + _st_select_fd_getlimit +}; +#endif + + #ifdef MD_HAVE_KQUEUE /***************************************** * kqueue event system @@ -885,6 +1196,13 @@ int st_set_eventsys(int eventsys) return -1; } +#if defined (MD_HAVE_SELECT) + if (eventsys == ST_EVENTSYS_SELECT) { + _st_eventsys = &_st_select_eventsys; + return 0; + } +#endif + #if defined (MD_HAVE_KQUEUE) _st_eventsys = &_st_kq_eventsys; return 0; @@ -897,7 +1215,7 @@ int st_set_eventsys(int eventsys) errno = EINVAL; return -1; #else - #error Only support epoll(for Linux) or kqueue(for Darwin) + #error Only support epoll(for Linux), kqueue(for Darwin) or select(for Cygwin) #endif } diff --git a/md.h b/md.h index a7e77ea..1bebb64 100644 --- a/md.h +++ b/md.h @@ -67,7 +67,7 @@ #define MD_USE_BUILTIN_SETJMP #if defined(__amd64__) || defined(__x86_64__) - #define JB_SP 12 + #define JB_SP 12 /* The jmpbuf is int(4B) array, while MD_GET_SP covert to long(8B) pointer, so the JB_SP should be 12 which is 6*sizeof(long)/sizeof(int) */ #define MD_GET_SP(_t) *((long *)&((_t)->context[JB_SP])) #else #error Unknown CPU architecture @@ -212,6 +212,42 @@ #define MD_LONGJMP(env, val) longjmp(env, val) #endif +#elif defined (CYGWIN64) + + // For CYGWIN64, build SRS on Windows. + #define MD_USE_BSD_ANON_MMAP + #define MD_ACCEPT_NB_INHERITED + #define MD_HAVE_SOCKLEN_T + + #define MD_USE_BUILTIN_SETJMP + + #if defined(__amd64__) || defined(__x86_64__) + #define JB_SP 12 + #define MD_GET_SP(_t) *((long *)&((_t)->context[JB_SP])) + #else + #error Unknown CPU architecture + #endif + + #define MD_INIT_CONTEXT(_thread, _sp, _main) \ + ST_BEGIN_MACRO \ + if (MD_SETJMP((_thread)->context)) \ + _main(); \ + MD_GET_SP(_thread) = (long) (_sp); \ + ST_END_MACRO + + #if defined(MD_USE_BUILTIN_SETJMP) + #define MD_SETJMP(env) _st_md_cxt_save(env) + #define MD_LONGJMP(env, val) _st_md_cxt_restore(env, val) + + extern int _st_md_cxt_save(jmp_buf env); + extern void _st_md_cxt_restore(jmp_buf env, int val); + #endif + + #define MD_GET_UTIME() \ + struct timeval tv; \ + (void) gettimeofday(&tv, NULL); \ + return (tv.tv_sec * 1000000LL + tv.tv_usec) + #else #error Unknown OS #endif /* OS */ diff --git a/md_cygwin64.S b/md_cygwin64.S new file mode 100644 index 0000000..c8ba3e6 --- /dev/null +++ b/md_cygwin64.S @@ -0,0 +1,76 @@ + +/* If user disable the ASM, such as avoiding bugs in ASM, donot compile it. */ +#if !defined(MD_ST_NO_ASM) + +#if defined(__amd64__) || defined(__x86_64__) + + /****************************************************************/ + + /* + * Internal __jmp_buf layout + */ + #define JB_RBX 0 + #define JB_RBP 1 + #define JB_R12 2 /* Backup IP, https://www.cnblogs.com/Five100Miles/p/8458561.html */ + #define JB_R13 3 /* Backup SP, https://www.cnblogs.com/Five100Miles/p/8458561.html */ + #define JB_R14 4 /* Backup LR, https://www.cnblogs.com/Five100Miles/p/8458561.html */ + #define JB_R15 5 /* Backup PC, https://www.cnblogs.com/Five100Miles/p/8458561.html */ + #define JB_RSP 6 + #define JB_PC 7 + + .file "md_cygwin64.S" + .text + + /* _st_md_cxt_save(__jmp_buf env) */ /* The env is rdi, http://blog.chinaunix.net/uid-20157960-id-1974354.html */ + .globl __st_md_cxt_save + .align 16 + __st_md_cxt_save: + /* + * Save registers. + */ + movq %rbx, (JB_RBX*8)(%rdi) /* Save rbx to env[0], *(int64_t*)(rdi+0)=rbx */ + movq %rbp, (JB_RBP*8)(%rdi) /* Save rbp to env[1], *(int64_t*)(rdi+1)=rbp */ + movq %r12, (JB_R12*8)(%rdi) /* Save r12 to env[2], *(int64_t*)(rdi+2)=r12 */ + movq %r13, (JB_R13*8)(%rdi) /* Save r13 to env[3], *(int64_t*)(rdi+3)=r13 */ + movq %r14, (JB_R14*8)(%rdi) /* Save r14 to env[4], *(int64_t*)(rdi+4)=r14 */ + movq %r15, (JB_R15*8)(%rdi) /* Save r15 to env[5], *(int64_t*)(rdi+5)=r15 */ + /* Save SP */ + leaq 8(%rsp), %rdx /* Save *(int64_t*)(rsp+8) to rdx, https://my.oschina.net/guonaihong/blog/508907 */ + movq %rdx, (JB_RSP*8)(%rdi) /* Save rdx(rsp) to env[6], *(int64_t*)(rdi+6)=rdx */ + /* Save PC we are returning to */ + movq (%rsp), %rax /* Save PC(parent function address) %(rsp) to rax */ + movq %rax, (JB_PC*8)(%rdi) /* Save rax(PC) to env[7], *(int64_t*)(rdi+7)=rax */ + xorq %rax, %rax /* Reset rax to 0 */ + ret + + + /****************************************************************/ + + /* _st_md_cxt_restore(__jmp_buf env, int val) */ /* The env is rdi, val is esi/rsi, http://blog.chinaunix.net/uid-20157960-id-1974354.html */ + .globl __st_md_cxt_restore + .align 16 + __st_md_cxt_restore: + /* + * Restore registers. + */ + movq (JB_RBX*8)(%rdi), %rbx /* Load rbx from env[0] */ + movq (JB_RBP*8)(%rdi), %rbp /* Load rbp from env[1] */ + movq (JB_R12*8)(%rdi), %r12 /* Load r12 from env[2] */ + movq (JB_R13*8)(%rdi), %r13 /* Load r13 from env[3] */ + movq (JB_R14*8)(%rdi), %r14 /* Load r14 from env[4] */ + movq (JB_R15*8)(%rdi), %r15 /* Load r15 from env[5] */ + /* Set return value */ /* The esi is param1 val, the eax is return value */ + test %esi, %esi /* if (!val) { */ + mov $01, %eax /* val=1; */ + cmove %eax, %esi /* } */ + mov %esi, %eax /* return val; */ + movq (JB_PC*8)(%rdi), %rdx /* Load rdx(PC) from env[7] */ + movq (JB_RSP*8)(%rdi), %rsp /* Load rsp from env[6] */ + /* Jump to saved PC */ + jmpq *%rdx /* Jump to rdx(PC) */ + + /****************************************************************/ + +#endif + +#endif diff --git a/public.h b/public.h index b7217ca..04b658b 100644 --- a/public.h +++ b/public.h @@ -65,6 +65,7 @@ #endif #define ST_EVENTSYS_DEFAULT 0 +#define ST_EVENTSYS_SELECT 1 #define ST_EVENTSYS_ALT 3 #ifdef __cplusplus