From a649f55195c37c45c7f66a4d42a3268b7603f48b Mon Sep 17 00:00:00 2001 From: freemine Date: Fri, 15 Jan 2021 22:34:48 +0800 Subject: [PATCH 01/17] eok --- src/CMakeLists.txt | 2 +- src/client/CMakeLists.txt | 4 +- src/client/src/tscSQLParser.c | 2 + src/client/src/tscUtil.c | 4 + src/kit/shell/src/shellDarwin.c | 2 + src/os/inc/eok.h | 74 ++++ src/os/inc/osDarwin.h | 11 +- src/os/src/darwin/darwinEnv.c | 3 + src/os/src/darwin/eok.c | 580 ++++++++++++++++++++++++++++++ src/os/src/detail/osSocket.c | 3 +- src/plugins/http/src/httpServer.c | 4 + src/rpc/src/rpcTcp.c | 22 ++ src/sync/src/syncTcp.c | 14 + src/tsdb/inc/tsdbMain.h | 6 +- src/tsdb/src/tsdbCommit.c | 4 + src/tsdb/src/tsdbMain.c | 16 + src/tsdb/src/tsdbMemTable.c | 11 +- src/util/src/tsocket.c | 4 +- tests/examples/c/CMakeLists.txt | 5 + 19 files changed, 763 insertions(+), 8 deletions(-) create mode 100644 src/os/inc/eok.h create mode 100644 src/os/src/darwin/eok.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 931a0a132e..f7304ae72f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -19,5 +19,5 @@ ADD_SUBDIRECTORY(wal) ADD_SUBDIRECTORY(cq) ADD_SUBDIRECTORY(dnode) #ADD_SUBDIRECTORY(connector/odbc) -ADD_SUBDIRECTORY(connector/jdbc) +#ADD_SUBDIRECTORY(connector/jdbc) diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index daf7c5e534..3fd29b474e 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -49,12 +49,12 @@ ELSEIF (TD_DARWIN) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/jni/linux) ADD_LIBRARY(taos_static STATIC ${SRC}) - TARGET_LINK_LIBRARIES(taos_static trpc tutil pthread m) + TARGET_LINK_LIBRARIES(taos_static query trpc tutil pthread m) SET_TARGET_PROPERTIES(taos_static PROPERTIES OUTPUT_NAME "taos_static") # generate dynamic library (*.dylib) ADD_LIBRARY(taos SHARED ${SRC}) - TARGET_LINK_LIBRARIES(taos trpc tutil pthread m) + TARGET_LINK_LIBRARIES(taos query trpc tutil pthread m) SET_TARGET_PROPERTIES(taos PROPERTIES CLEAN_DIRECT_OUTPUT 1) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index a08482f570..4a24f2bfdb 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -13,10 +13,12 @@ * along with this program. If not, see . */ +#ifndef __APPLE__ #define _BSD_SOURCE #define _XOPEN_SOURCE 500 #define _DEFAULT_SOURCE #define _GNU_SOURCE +#endif // __APPLE__ #include "os.h" #include "ttype.h" diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index b44ebb3c98..d6151c1a88 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -2486,7 +2486,11 @@ bool tscSetSqlOwner(SSqlObj* pSql) { SSqlRes* pRes = &pSql->res; // set the sql object owner +#ifdef __APPLE__ + pthread_t threadId = (pthread_t)taosGetSelfPthreadId(); +#else uint64_t threadId = taosGetSelfPthreadId(); +#endif if (atomic_val_compare_exchange_64(&pSql->owner, 0, threadId) != 0) { pRes->code = TSDB_CODE_QRY_IN_EXEC; return false; diff --git a/src/kit/shell/src/shellDarwin.c b/src/kit/shell/src/shellDarwin.c index ddf7b21bef..d6aed4401c 100644 --- a/src/kit/shell/src/shellDarwin.c +++ b/src/kit/shell/src/shellDarwin.c @@ -21,6 +21,8 @@ #include "shellCommand.h" #include "tkey.h" +#include "tscLog.h" + #define OPT_ABORT 1 /* �Cabort */ int indicator = 1; diff --git a/src/os/inc/eok.h b/src/os/inc/eok.h new file mode 100644 index 0000000000..8892e50c35 --- /dev/null +++ b/src/os/inc/eok.h @@ -0,0 +1,74 @@ +#ifndef _eok_h_fd274616_996c_400e_9023_ae70be881fa3_ +#define _eok_h_fd274616_996c_400e_9023_ae70be881fa3_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +enum EPOLL_EVENTS + { + EPOLLIN = 0x001, +#define EPOLLIN EPOLLIN + EPOLLPRI = 0x002, +#define EPOLLPRI EPOLLPRI + EPOLLOUT = 0x004, +#define EPOLLOUT EPOLLOUT + EPOLLRDNORM = 0x040, +#define EPOLLRDNORM EPOLLRDNORM + EPOLLRDBAND = 0x080, +#define EPOLLRDBAND EPOLLRDBAND + EPOLLWRNORM = 0x100, +#define EPOLLWRNORM EPOLLWRNORM + EPOLLWRBAND = 0x200, +#define EPOLLWRBAND EPOLLWRBAND + EPOLLMSG = 0x400, +#define EPOLLMSG EPOLLMSG + EPOLLERR = 0x008, +#define EPOLLERR EPOLLERR + EPOLLHUP = 0x010, +#define EPOLLHUP EPOLLHUP + EPOLLRDHUP = 0x2000, +#define EPOLLRDHUP EPOLLRDHUP + EPOLLEXCLUSIVE = 1u << 28, +#define EPOLLEXCLUSIVE EPOLLEXCLUSIVE + EPOLLWAKEUP = 1u << 29, +#define EPOLLWAKEUP EPOLLWAKEUP + EPOLLONESHOT = 1u << 30, +#define EPOLLONESHOT EPOLLONESHOT + EPOLLET = 1u << 31 +#define EPOLLET EPOLLET + }; + +/* Valid opcodes ( "op" parameter ) to issue to epoll_ctl(). */ +#define EPOLL_CTL_ADD 1 /* Add a file descriptor to the interface. */ +#define EPOLL_CTL_DEL 2 /* Remove a file descriptor from the interface. */ +#define EPOLL_CTL_MOD 3 /* Change file descriptor epoll_event structure. */ + + +typedef union epoll_data +{ + void *ptr; + int fd; + uint32_t u32; + uint64_t u64; +} epoll_data_t; + +struct epoll_event +{ + uint32_t events; /* Epoll events */ + epoll_data_t data; /* User data variable */ +}; + +int epoll_create(int size); +int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); +int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout); +int epoll_close(int epfd); + +#ifdef __cplusplus +} +#endif + +#endif // _eok_h_fd274616_996c_400e_9023_ae70be881fa3_ + diff --git a/src/os/inc/osDarwin.h b/src/os/inc/osDarwin.h index 1461ec6d3b..52bb661f89 100644 --- a/src/os/inc/osDarwin.h +++ b/src/os/inc/osDarwin.h @@ -91,7 +91,7 @@ extern "C" { typedef int(*__compar_fn_t)(const void *, const void *); // for send function in tsocket.c -#define MSG_NOSIGNAL 0 +// #define MSG_NOSIGNAL 0 #define SO_NO_CHECK 0x1234 #define SOL_TCP 0x1234 #define TCP_KEEPIDLE 0x1234 @@ -100,6 +100,15 @@ typedef int(*__compar_fn_t)(const void *, const void *); #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE #endif +int64_t tsosStr2int64(char *str); + +#include "eok.h" + + + + + + #ifdef __cplusplus } #endif diff --git a/src/os/src/darwin/darwinEnv.c b/src/os/src/darwin/darwinEnv.c index 6adebabec0..28388f24d2 100644 --- a/src/os/src/darwin/darwinEnv.c +++ b/src/os/src/darwin/darwinEnv.c @@ -17,6 +17,8 @@ #include "os.h" #include "tglobal.h" +#include + void osInit() { if (configDir[0] == 0) { strcpy(configDir, "~/TDengine/cfg"); @@ -30,3 +32,4 @@ void osInit() { strcpy(tsScriptDir, "~/TDengine/cfg"); strcpy(tsOsName, "Darwin"); } + diff --git a/src/os/src/darwin/eok.c b/src/os/src/darwin/eok.c new file mode 100644 index 0000000000..ea67223c25 --- /dev/null +++ b/src/os/src/darwin/eok.c @@ -0,0 +1,580 @@ +#include "eok.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define D(fmt, ...) fprintf(stderr, "%s[%d]%s(): " fmt "\n", basename(__FILE__), __LINE__, __func__, ##__VA_ARGS__) +#define A(statement, fmt, ...) do { \ + if (statement) break; \ + fprintf(stderr, "%s[%d]%s(): assert [%s] failed: %d[%s]: " fmt "\n", \ + basename(__FILE__), __LINE__, __func__, \ + #statement, errno, strerror(errno), \ + ##__VA_ARGS__); \ + abort(); \ +} while (0) + +#define E(fmt, ...) do { \ + fprintf(stderr, "%s[%d]%s(): %d[%s]: " fmt "\n", \ + basename(__FILE__), __LINE__, __func__, \ + errno, strerror(errno), \ + ##__VA_ARGS__); \ +} while (0) + +static int eok_dummy = 0; + +typedef struct ep_over_kq_s ep_over_kq_t; +typedef struct eok_event_s eok_event_t; + +struct ep_over_kq_s { + int kq; + int idx; + ep_over_kq_t *next; + + int sv[2]; // 0 for read, 1 for write + + eok_event_t *evs_head; + eok_event_t *evs_tail; + eok_event_t *evs_free; + + struct kevent64_s *kchanges; + int nchanges; + int ichanges; + + struct kevent64_s *kevslist; + int nevslist; + + pthread_mutex_t lock; + + volatile unsigned int lock_valid:1; + volatile unsigned int waiting:1; + volatile unsigned int changed:1; + volatile unsigned int wakenup:1; + volatile unsigned int stopping:1; +}; + +struct eok_event_s { + int fd; + struct epoll_event epev; + volatile unsigned int changed; // 0:registered;1:add;2:mod;3:del + + eok_event_t *next; + eok_event_t *prev; +}; + +typedef struct eoks_s eoks_t; +struct eoks_s { + pthread_mutex_t lock; + ep_over_kq_t *eoks; + int neoks; + ep_over_kq_t *eoks_free; +}; +static eoks_t eoks = { + .lock = PTHREAD_MUTEX_INITIALIZER, + .eoks = NULL, + .neoks = 0, + .eoks_free = NULL, +}; + +static ep_over_kq_t* eoks_alloc(void); +static void eoks_free(ep_over_kq_t *eok); +static ep_over_kq_t* eoks_find(int epfd); + +static eok_event_t* eok_find_ev(ep_over_kq_t *eok, int fd); +static eok_event_t* eok_calloc_ev(ep_over_kq_t *eok); +static void eok_free_ev(ep_over_kq_t *eok, eok_event_t *ev); +static void eok_wakeup(ep_over_kq_t *eok); + +static int eok_chgs_refresh(ep_over_kq_t *eok, eok_event_t *oev, eok_event_t *ev, struct kevent64_s *krev, struct kevent64_s *kwev); + +static struct kevent64_s* eok_alloc_eventslist(ep_over_kq_t *eok, int maxevents); + +int epoll_create(int size) { + (void)size; + int e = 0; + ep_over_kq_t *eok = eoks_alloc(); + if (!eok) { + errno = ENOMEM; + return -1; + } + + A(eok->kq==-1, "internal logic error"); + A(eok->lock_valid, "internal logic error"); + A(eok->idx>=0 && eok->idxnext==NULL, "internal logic error"); + A(eok->sv[0]==-1, "internal logic error"); + A(eok->sv[1]==-1, "internal logic error"); + + eok->kq = kqueue(); + if (eok->kq==-1) { + e = errno; + eoks_free(eok); + errno = e; + return -1; + } + + if (socketpair(AF_LOCAL, SOCK_STREAM, 0, eok->sv)) { + e = errno; + eoks_free(eok); + errno = e; + return -1; + } + + struct epoll_event ev = {0}; + ev.events = EPOLLIN; + ev.data.ptr = &eok_dummy; + if (epoll_ctl(eok->idx, EPOLL_CTL_ADD, eok->sv[0], &ev)) { + A(0, "internal logic error"); + epoll_close(eok->idx); + return -1; + } + + return eok->idx; +} + +int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) { + int e = 0; + if (epfd<0 || epfd>=eoks.neoks) { + errno = EBADF; + return -1; + } + if (fd==-1) { + errno = EBADF; + return -1; + } + if (event && !(event->events & (EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP | EPOLLOUT))) { + e = ENOTSUP; + return -1; + } + + ep_over_kq_t *eok = eoks_find(epfd); + if (!eok) { + errno = EBADF; + return -1; + } + + A(0==pthread_mutex_lock(&eok->lock), ""); + do { + eok_event_t* oev = eok_find_ev(eok, fd); + if (op==EPOLL_CTL_ADD && oev) { + e = EEXIST; + break; + } + if (op!=EPOLL_CTL_ADD && !oev) { + e = ENOENT; + break; + } + + struct kevent64_s krev = {0}; + struct kevent64_s kwev = {0}; + krev.ident = -1; + kwev.ident = -1; + uint16_t flags = 0; + eok_event_t ev = {0}; + ev.fd = fd; + if (event) ev.epev = *event; + struct epoll_event *pev = event; + switch (op) { + case EPOLL_CTL_ADD: { + if (!event) { + e = EINVAL; + break; + } + flags = EV_ADD; + ev.changed = 1; + } break; + case EPOLL_CTL_MOD: { + if (!event) { + e = EINVAL; + break; + } + flags = EV_ADD; + ev.changed = 2; + } break; + case EPOLL_CTL_DEL: { + // event is ignored + pev = &oev->epev; + flags = EV_DELETE; + ev.changed = 3; + } break; + default: { + e = ENOTSUP; + } break; + } + + if (e) break; + + if (pev->events & (EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP)) { + flags |= EV_EOF; + EV_SET64(&krev, ev.fd, EVFILT_READ, flags, 0, 0, -1, 0, 0); + D("...."); + } + if (pev->events & EPOLLOUT) { + EV_SET64(&kwev, ev.fd, EVFILT_WRITE, flags, 0, 0, -1, 0, 0); + D("...."); + } + + if (eok_chgs_refresh(eok, oev, &ev, &krev, &kwev)) { + e = errno; + A(e, "internal logic error"); + break; + } + eok->changed = 1; + eok_wakeup(eok); + } while (0); + A(0==pthread_mutex_unlock(&eok->lock), ""); + + if (e) { + errno = e; + return -1; + } + + return 0; +} + +static struct timespec do_timespec_diff(struct timespec *from, struct timespec *to); + +int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) { + int e = 0; + if (epfd<0 || epfd>=eoks.neoks) { + errno = EBADF; + return -1; + } + if (!events) { + errno = EINVAL; + return -1; + } + if (maxevents<=0) { + errno = EINVAL; + return -1; + } + + int r = 0; + + ep_over_kq_t *eok = eoks_find(epfd); + if (!eok) { + errno = EBADF; + return -1; + } + + struct timespec abstime = {0}; + A(TIME_UTC==timespec_get(&abstime, TIME_UTC), "internal logic error"); + + if (timeout!=-1) { + if (timeout<0) timeout = 0; + int64_t t = abstime.tv_nsec + timeout * 1000000; + abstime.tv_sec += t / 1000000000; + abstime.tv_nsec %= 1000000000; + } + + int cnts = 0; + A(0==pthread_mutex_lock(&eok->lock), ""); + do { + cnts = 0; + A(eok->waiting==0, "internal logic error"); + struct kevent64_s *eventslist = eok_alloc_eventslist(eok, maxevents); + if (!eventslist) { + e = ENOMEM; + break; + } + memset(eventslist, 0, maxevents * sizeof(*eventslist)); + + struct timespec now = {0}; + A(TIME_UTC==timespec_get(&now, TIME_UTC), "internal logic error"); + struct timespec to = do_timespec_diff(&now, &abstime); + struct timespec *pto = NULL; + if (timeout!=-1) { + pto = &to; + } + + eok->changed = 0; + eok->wakenup = 0; + eok->waiting = 1; + + struct kevent64_s *kchanges = eok->kchanges; + int nchanges = eok->nchanges; + int ichanges = eok->ichanges; + eok->kchanges = NULL; + eok->nchanges = 0; + eok->ichanges = 0; + + A(0==pthread_mutex_unlock(&eok->lock), ""); + r = kevent64(eok->kq, kchanges, ichanges, eventslist, maxevents, 0, pto); + e = errno; + A(0==pthread_mutex_lock(&eok->lock), ""); + + eok->waiting = 0; + if (kchanges) { + free(kchanges); + kchanges = NULL; + nchanges = 0; + ichanges = 0; + } + + eok->waiting = 0; + if (r<0) break; + if (r==0) { + A(timeout!=-1, "internal logic error"); + } + for (int i=0; iudata && eok->evs_head && eok->evs_tail, "internal logic error"); + eok_event_t *ev = (eok_event_t*)kev->udata; + A(kev->ident == ev->fd, "internal logic error"); + D("..."); + switch (kev->filter) { + case EVFILT_READ: { + A((ev->epev.events & EPOLLIN), "internal logic errro"); + if (ev->epev.data.ptr==&eok_dummy) { + char c = '\0'; + A(1==recv(kev->ident, &c, 1, 0), "internal logic error"); + A(0==memcmp(&c, "1", 1), "internal logic error"); + D("..............."); + } else { + if (ev->changed==3) { + D("already requested to delete"); + // EV_DELETE? + continue; + } + struct epoll_event pev = {0}; + pev.data.ptr = ev->epev.data.ptr; + pev.events = EPOLLIN; + if (kev->flags & EV_EOF) { + pev.events |= (EPOLLHUP | EPOLLERR | EPOLLRDHUP); + } + pev.events &= ev->epev.events; + events[cnts++] = pev; + } + } break; + case EVFILT_WRITE: { + A(0, "not implemented yet"); + } break; + default: { + A(0, "internal logic error"); + } break; + } + } + } while (cnts==0); + A(0==pthread_mutex_unlock(&eok->lock), ""); + + if (e) { + errno = e; + return -1; + } + + return cnts; +} + +static struct timespec do_timespec_diff(struct timespec *from, struct timespec *to) { + struct timespec delta; + delta.tv_sec = to->tv_sec - from->tv_sec; + delta.tv_nsec = to->tv_nsec - from->tv_nsec; + while (delta.tv_nsec<0) { + delta.tv_sec -= 1; + delta.tv_nsec += 1000000000; + } + return delta; +} + +int epoll_close(int epfd) { + if (epfd<0 || epfd>=eoks.neoks) { + errno = EBADF; + return -1; + } + ep_over_kq_t *eok = eoks_find(epfd); + if (!eok) { + errno = EBADF; + return -1; + } + + A(0==pthread_mutex_lock(&eok->lock), ""); + do { + A(eok->stopping==0, "internal logic error"); + eok->stopping = 1; + A(eok->waiting, "internal logic error"); + + if (eok->kq!=-1) { + close(eok->kq); + eok->kq = -1; + } + } while (0); + A(0==pthread_mutex_unlock(&eok->lock), ""); + eoks_free(eok); + + return 0; +} + +static struct kevent64_s* eok_alloc_eventslist(ep_over_kq_t *eok, int maxevents) { + if (maxevents<=eok->nevslist) return eok->kevslist; + struct kevent64_s *p = (struct kevent64_s*)realloc(eok->kevslist, sizeof(*p)*maxevents); + if (!p) return NULL; + eok->kevslist = p; + eok->nevslist = maxevents; + return p; +} + +static eok_event_t* eok_find_ev(ep_over_kq_t *eok, int fd) { + eok_event_t *p = eok->evs_head; + while (p) { + if (p->fd == fd) return p; + p = p->next; + } + errno = ENOENT; + return NULL; +} + +static eok_event_t* eok_calloc_ev(ep_over_kq_t *eok) { + eok_event_t *p = NULL; + if (eok->evs_free) { + p = eok->evs_free; + eok->evs_free = p->next; + p->next = NULL; + } else { + p = (eok_event_t*)calloc(1, sizeof(*p)); + if (!p) return NULL; + } + // dirty link + p->prev = eok->evs_tail; + if (eok->evs_tail) eok->evs_tail->next = p; + else eok->evs_head = p; + eok->evs_tail = p; + + return p; +} + +static void eok_free_ev(ep_over_kq_t *eok, eok_event_t *ev) { + if (ev->prev) ev->prev->next = ev->next; + else eok->evs_head = ev->next; + ev->prev = NULL; + if (ev->next) ev->next->prev = ev->prev; + else eok->evs_tail = ev->prev; + ev->next = eok->evs_free; + eok->evs_free = ev->next; +} + +static void eok_wakeup(ep_over_kq_t *eok) { + if (!eok->waiting) return; + if (eok->wakenup) return; + eok->wakenup = 1; + send(eok->sv[1], "1", 1, 0); +} + +static int eok_chgs_refresh(ep_over_kq_t *eok, eok_event_t *oev, eok_event_t *ev, struct kevent64_s *krev, struct kevent64_s *kwev) { + if (!oev) oev = eok_calloc_ev(eok); + if (!oev) return -1; + int n = 0; + if (krev->ident==ev->fd) ++n; + if (kwev->ident==ev->fd) ++n; + A(n, "internal logic error"); + if (eok->ichanges+n>eok->nchanges) { + struct kevent64_s *p = (struct kevent64_s*)realloc(eok->kchanges, sizeof(*p) * (eok->nchanges + 10)); + if (!p) { + if (ev->changed==1) { + eok_free_ev(eok, oev); + } + errno = ENOMEM; + return -1; + } + eok->kchanges = p; + eok->nchanges += 10; + } + + oev->fd = ev->fd; + oev->epev = ev->epev; + oev->changed = ev->changed; + + if (krev->ident==ev->fd) { + krev->udata = (uint64_t)oev; + eok->kchanges[eok->ichanges++] = *krev; + } + if (kwev->ident==ev->fd) { + kwev->udata = (uint64_t)oev; + eok->kchanges[eok->ichanges++] = *kwev; + } + return 0; +} + +static ep_over_kq_t* eoks_alloc(void) { + ep_over_kq_t *eok = NULL; + + A(0==pthread_mutex_lock(&eoks.lock), ""); + do { + if (eoks.eoks_free) { + eok = eoks.eoks_free; + eoks.eoks_free = eok->next; + eok->next = NULL; + break; + } + ep_over_kq_t *p = (ep_over_kq_t*)realloc(eoks.eoks, sizeof(*p) * (eoks.neoks+1)); + if (!p) break; + eoks.eoks = p; + eok = eoks.eoks + eoks.neoks; + memset(eok, 0, sizeof(*eok)); + eok->idx = eoks.neoks; + eok->kq = -1; + eok->sv[0] = -1; + eok->sv[1] = -1; + eoks.neoks += 1; + } while (0); + A(0==pthread_mutex_unlock(&eoks.lock), ""); + + if (!eok) return NULL; + if (eok->lock_valid) { + return eok; + } + + if (0==pthread_mutex_init(&eok->lock, NULL)) { + eok->lock_valid = 1; + return eok; + } + + eoks_free(eok); + return NULL; +} + +static void eoks_free(ep_over_kq_t *eok) { + A(0==pthread_mutex_lock(&eoks.lock), ""); + do { + A(eok->next==NULL, "internal logic error"); + A(eok->evs_head==NULL, "internal logic error"); + A(eok->waiting==0, "internal logic error"); + if (eok->sv[0]!=-1) { + close(eok->sv[0]); + eok->sv[0] = -1; + } + if (eok->sv[1]!=-1) { + close(eok->sv[1]); + eok->sv[1] = -1; + } + if (eok->kq!=-1) { + close(eok->kq); + eok->kq = -1; + } + eok->next = eoks.eoks_free; + eoks.eoks_free = eok; + } while (0); + A(0==pthread_mutex_unlock(&eoks.lock), ""); +} + +static ep_over_kq_t* eoks_find(int epfd) { + ep_over_kq_t *eok = NULL; + A(0==pthread_mutex_lock(&eoks.lock), ""); + do { + if (epfd<0 || epfd>=eoks.neoks) { + break; + } + A(eoks.eoks, "internal logic error"); + eok = eoks.eoks + epfd; + A(eok->next==NULL, "internal logic error"); + A(eok->lock_valid, "internal logic error"); + } while (0); + A(0==pthread_mutex_unlock(&eoks.lock), ""); + return eok; +} + diff --git a/src/os/src/detail/osSocket.c b/src/os/src/detail/osSocket.c index 729471247f..c6cbbe6b83 100644 --- a/src/os/src/detail/osSocket.c +++ b/src/os/src/detail/osSocket.c @@ -58,6 +58,7 @@ void taosBlockSIGPIPE() { #ifndef TAOS_OS_FUNC_SOCKET_SETSOCKETOPT int32_t taosSetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t optlen) { + fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); return setsockopt(socketfd, level, optname, optval, (socklen_t)optlen); } @@ -73,4 +74,4 @@ const char *taosInetNtoa(struct in_addr ipInt) { return inet_ntoa(ipInt); } -#endif \ No newline at end of file +#endif diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index 4896d50c6c..1cc73aef06 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -52,7 +52,11 @@ static void httpStopThread(HttpThread* pThread) { close(fd); } +#ifdef __APPLE__ + epoll_close(pThread->pollFd); +#else close(pThread->pollFd); +#endif pthread_mutex_destroy(&(pThread->threadMutex)); } diff --git a/src/rpc/src/rpcTcp.c b/src/rpc/src/rpcTcp.c index 178b96c423..ff2a5882f8 100644 --- a/src/rpc/src/rpcTcp.c +++ b/src/rpc/src/rpcTcp.c @@ -133,6 +133,7 @@ void *taosInitTcpServer(uint32_t ip, uint16_t port, char *label, int numOfThread } pThreadObj->pollFd = (int64_t)epoll_create(10); // size does not matter + fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); if (pThreadObj->pollFd < 0) { tError("%s failed to create TCP epoll", label); code = -1; @@ -293,6 +294,7 @@ void *taosInitTcpClient(uint32_t ip, uint16_t port, char *label, int num, void * } pThreadObj->pollFd = (SOCKET)epoll_create(10); // size does not matter + fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); if (pThreadObj->pollFd < 0) { tError("%s failed to create TCP client epoll", label); free(pThreadObj); @@ -307,7 +309,11 @@ void *taosInitTcpClient(uint32_t ip, uint16_t port, char *label, int num, void * int code = pthread_create(&(pThreadObj->thread), &thattr, taosProcessTcpData, (void *)(pThreadObj)); pthread_attr_destroy(&thattr); if (code != 0) { +#ifdef __APPLE__ + epoll_close(pThreadObj->pollFd); +#else taosCloseSocket(pThreadObj->pollFd); +#endif free(pThreadObj); terrno = TAOS_SYSTEM_ERROR(errno); tError("%s failed to create TCP read data thread(%s)", label, strerror(errno)); @@ -337,6 +343,10 @@ void taosCleanUpTcpClient(void *chandle) { void *taosOpenTcpClientConnection(void *shandle, void *thandle, uint32_t ip, uint16_t port) { SThreadObj * pThreadObj = shandle; + fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); + fprintf(stderr, "pThreadObj->ip:%d\n", pThreadObj->ip); + fprintf(stderr, "PF_INET/AF_INET:%d/%d\n", PF_INET, AF_INET); + fprintf(stderr, "ip/port:%x/%d\n", ip, port); SOCKET fd = taosOpenTcpClientSocket(ip, port, pThreadObj->ip); if (fd < 0) return NULL; @@ -348,6 +358,7 @@ void *taosOpenTcpClientConnection(void *shandle, void *thandle, uint32_t ip, uin localPort = (uint16_t)ntohs(sin.sin_port); } + fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); SFdObj *pFdObj = taosMallocFdObj(pThreadObj, fd); if (pFdObj) { @@ -358,6 +369,7 @@ void *taosOpenTcpClientConnection(void *shandle, void *thandle, uint32_t ip, uin pThreadObj->label, thandle, ip, port, localPort, pFdObj, pThreadObj->numOfFds); } else { tError("%s failed to malloc client FdObj(%s)", pThreadObj->label, strerror(errno)); + fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); taosCloseSocket(fd); } @@ -480,27 +492,32 @@ static void *taosProcessTcpData(void *param) { if (fdNum < 0) continue; for (int i = 0; i < fdNum; ++i) { + fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); pFdObj = events[i].data.ptr; if (events[i].events & EPOLLERR) { tDebug("%s %p FD:%p epoll errors", pThreadObj->label, pFdObj->thandle, pFdObj); + fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); taosReportBrokenLink(pFdObj); continue; } if (events[i].events & EPOLLRDHUP) { tDebug("%s %p FD:%p RD hang up", pThreadObj->label, pFdObj->thandle, pFdObj); + fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); taosReportBrokenLink(pFdObj); continue; } if (events[i].events & EPOLLHUP) { tDebug("%s %p FD:%p hang up", pThreadObj->label, pFdObj->thandle, pFdObj); + fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); taosReportBrokenLink(pFdObj); continue; } if (taosReadTcpData(pFdObj, &recvInfo) < 0) { + fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); shutdown(pFdObj->fd, SHUT_WR); continue; } @@ -512,7 +529,11 @@ static void *taosProcessTcpData(void *param) { if (pThreadObj->stop) break; } +#ifdef __APPLE__ + if (pThreadObj->pollFd >=0) epoll_close(pThreadObj->pollFd); +#else if (pThreadObj->pollFd >=0) taosCloseSocket(pThreadObj->pollFd); +#endif while (pThreadObj->pHead) { SFdObj *pFdObj = pThreadObj->pHead; @@ -542,6 +563,7 @@ static SFdObj *taosMallocFdObj(SThreadObj *pThreadObj, SOCKET fd) { event.events = EPOLLIN | EPOLLRDHUP; event.data.ptr = pFdObj; + fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); if (epoll_ctl(pThreadObj->pollFd, EPOLL_CTL_ADD, fd, &event) < 0) { tfree(pFdObj); terrno = TAOS_SYSTEM_ERROR(errno); diff --git a/src/sync/src/syncTcp.c b/src/sync/src/syncTcp.c index 4744666737..be85853819 100644 --- a/src/sync/src/syncTcp.c +++ b/src/sync/src/syncTcp.c @@ -136,12 +136,15 @@ void *syncAllocateTcpConn(void *param, int64_t rid, int32_t connFd) { event.events = EPOLLIN | EPOLLRDHUP; event.data.ptr = pConn; + fprintf(stderr, ">>>>>>>>>>>>>>>>>\n"); if (epoll_ctl(pThread->pollFd, EPOLL_CTL_ADD, connFd, &event) < 0) { + fprintf(stderr, "<<<<<<<<<<<<<<<<<\n"); sError("failed to add fd:%d since %s", connFd, strerror(errno)); terrno = TAOS_SYSTEM_ERROR(errno); tfree(pConn); pConn = NULL; } else { + fprintf(stderr, "<<<<<<<<<<<<<<<<<\n"); pThread->numOfFds++; sDebug("%p fd:%d is added to epoll thread, num:%d", pThread, connFd, pThread->numOfFds); } @@ -167,7 +170,9 @@ static void taosProcessBrokenLink(SConnObj *pConn) { (*pInfo->processBrokenLink)(pConn->handleId); pThread->numOfFds--; + fprintf(stderr, "<<<<<<<<<<<<<<<<<\n"); epoll_ctl(pThread->pollFd, EPOLL_CTL_DEL, pConn->fd, NULL); + fprintf(stderr, "<<<<<<<<<<<<<<<<<\n"); sDebug("%p fd:%d is removed from epoll thread, num:%d", pThread, pConn->fd, pThread->numOfFds); taosClose(pConn->fd); tfree(pConn); @@ -233,7 +238,11 @@ static void *syncProcessTcpData(void *param) { sDebug("%p TCP epoll thread exits", pThread); +#ifdef __APPLE__ + epoll_close(pThread->pollFd); +#else close(pThread->pollFd); +#endif tfree(pThread); tfree(buffer); return NULL; @@ -278,6 +287,7 @@ static SThreadObj *syncGetTcpThread(SPoolObj *pPool) { pThread->pPool = pPool; pThread->pollFd = epoll_create(10); // size does not matter + fprintf(stderr, "...............\n"); if (pThread->pollFd < 0) { tfree(pThread); return NULL; @@ -290,7 +300,11 @@ static SThreadObj *syncGetTcpThread(SPoolObj *pPool) { pthread_attr_destroy(&thattr); if (ret != 0) { +#ifdef __APPLE__ + epoll_close(pThread->pollFd); +#else close(pThread->pollFd); +#endif tfree(pThread); return NULL; } diff --git a/src/tsdb/inc/tsdbMain.h b/src/tsdb/inc/tsdbMain.h index 5067974903..984839162e 100644 --- a/src/tsdb/inc/tsdbMain.h +++ b/src/tsdb/inc/tsdbMain.h @@ -233,7 +233,11 @@ typedef struct { SMemTable* mem; SMemTable* imem; STsdbFileH* tsdbFileH; +#ifdef __APPLE__ + sem_t *readyToCommit; +#else sem_t readyToCommit; +#endif pthread_mutex_t mutex; bool repoLocked; int32_t code; // Commit code @@ -616,4 +620,4 @@ int tsdbScheduleCommit(STsdbRepo *pRepo); } #endif -#endif \ No newline at end of file +#endif diff --git a/src/tsdb/src/tsdbCommit.c b/src/tsdb/src/tsdbCommit.c index 696270d670..9551d1b3a5 100644 --- a/src/tsdb/src/tsdbCommit.c +++ b/src/tsdb/src/tsdbCommit.c @@ -166,7 +166,11 @@ static void tsdbEndCommit(STsdbRepo *pRepo, int eno) { pRepo->imem = NULL; tsdbUnlockRepo(pRepo); tsdbUnRefMemTable(pRepo, pIMem); +#ifdef __APPLE__ + sem_post(pRepo->readyToCommit); +#else sem_post(&(pRepo->readyToCommit)); +#endif } static int tsdbHasDataToCommit(SCommitIter *iters, int nIters, TSKEY minKey, TSKEY maxKey) { diff --git a/src/tsdb/src/tsdbMain.c b/src/tsdb/src/tsdbMain.c index b34b2fa9e6..602aea70e3 100644 --- a/src/tsdb/src/tsdbMain.c +++ b/src/tsdb/src/tsdbMain.c @@ -146,7 +146,11 @@ int tsdbCloseRepo(TSDB_REPO_T *repo, int toCommit) { if (toCommit) { tsdbAsyncCommit(pRepo); +#ifdef __APPLE__ + sem_wait(pRepo->readyToCommit); +#else sem_wait(&(pRepo->readyToCommit)); +#endif terrno = pRepo->code; } tsdbUnRefMemTable(pRepo, pRepo->mem); @@ -643,11 +647,19 @@ static STsdbRepo *tsdbNewRepo(char *rootDir, STsdbAppH *pAppH, STsdbCfg *pCfg) { goto _err; } +#ifdef __APPLE__ + pRepo->readyToCommit = sem_open(NULL, O_CREAT, 0644, 1); + if (!pRepo->readyToCommit) { + terrno = TAOS_SYSTEM_ERROR(code); + goto _err; + } +#else code = sem_init(&(pRepo->readyToCommit), 0, 1); if (code != 0) { terrno = TAOS_SYSTEM_ERROR(code); goto _err; } +#endif pRepo->repoLocked = false; @@ -693,7 +705,11 @@ static void tsdbFreeRepo(STsdbRepo *pRepo) { // tsdbFreeMemTable(pRepo->mem); // tsdbFreeMemTable(pRepo->imem); tfree(pRepo->rootDir); +#ifdef __APPLE__ + sem_close(pRepo->readyToCommit); +#else sem_destroy(&(pRepo->readyToCommit)); +#endif pthread_mutex_destroy(&pRepo->mutex); free(pRepo); } diff --git a/src/tsdb/src/tsdbMemTable.c b/src/tsdb/src/tsdbMemTable.c index 07f001f68a..202405df00 100644 --- a/src/tsdb/src/tsdbMemTable.c +++ b/src/tsdb/src/tsdbMemTable.c @@ -207,7 +207,11 @@ void *tsdbAllocBytes(STsdbRepo *pRepo, int bytes) { int tsdbAsyncCommit(STsdbRepo *pRepo) { if (pRepo->mem == NULL) return 0; +#ifdef __APPLE__ + sem_wait(pRepo->readyToCommit); +#else sem_wait(&(pRepo->readyToCommit)); +#endif ASSERT(pRepo->imem == NULL); @@ -229,8 +233,13 @@ int tsdbSyncCommit(TSDB_REPO_T *repo) { STsdbRepo *pRepo = (STsdbRepo *)repo; tsdbAsyncCommit(pRepo); +#ifdef __APPLE__ + sem_wait(pRepo->readyToCommit); + sem_post(pRepo->readyToCommit); +#else sem_wait(&(pRepo->readyToCommit)); sem_post(&(pRepo->readyToCommit)); +#endif if (pRepo->code != TSDB_CODE_SUCCESS) { terrno = pRepo->code; @@ -927,4 +936,4 @@ static int tsdbUpdateTableLatestInfo(STsdbRepo *pRepo, STable *pTable, SDataRow } return 0; -} \ No newline at end of file +} diff --git a/src/util/src/tsocket.c b/src/util/src/tsocket.c index 1a5c3bd64d..6b15d3677c 100644 --- a/src/util/src/tsocket.c +++ b/src/util/src/tsocket.c @@ -35,7 +35,7 @@ int32_t taosGetFqdn(char *fqdn) { hints.ai_flags = AI_CANONNAME; int32_t ret = getaddrinfo(hostname, NULL, &hints, &result); if (!result) { - uError("failed to get fqdn, code:%d, reason:%s", ret, gai_strerror(ret)); + uError("failed to get fqdn for hostname <%s>, code:%d, reason:%s", hostname, ret, gai_strerror(ret)); return -1; } @@ -341,6 +341,7 @@ int32_t taosKeepTcpAlive(SOCKET sockFd) { return -1; } +#ifndef __APPLE__ int32_t probes = 3; if (taosSetSockOpt(sockFd, SOL_TCP, TCP_KEEPCNT, (void *)&probes, sizeof(probes)) < 0) { uError("fd:%d setsockopt SO_KEEPCNT failed: %d (%s)", sockFd, errno, strerror(errno)); @@ -361,6 +362,7 @@ int32_t taosKeepTcpAlive(SOCKET sockFd) { taosCloseSocket(sockFd); return -1; } +#endif int32_t nodelay = 1; if (taosSetSockOpt(sockFd, IPPROTO_TCP, TCP_NODELAY, (void *)&nodelay, sizeof(nodelay)) < 0) { diff --git a/tests/examples/c/CMakeLists.txt b/tests/examples/c/CMakeLists.txt index 59bcb6eaff..4689ed8868 100644 --- a/tests/examples/c/CMakeLists.txt +++ b/tests/examples/c/CMakeLists.txt @@ -6,3 +6,8 @@ IF (TD_LINUX) ADD_EXECUTABLE(demo demo.c) TARGET_LINK_LIBRARIES(demo taos_static trpc tutil pthread ) ENDIF () +IF (TD_DARWIN) + INCLUDE_DIRECTORIES(. ${TD_COMMUNITY_DIR}/src/inc ${TD_COMMUNITY_DIR}/src/client/inc ${TD_COMMUNITY_DIR}/inc) + ADD_EXECUTABLE(epoll epoll.c) + TARGET_LINK_LIBRARIES(epoll taos_static trpc tutil pthread ) +ENDIF () From c46f6ac022309951f2af325018dacf04d5ccb064 Mon Sep 17 00:00:00 2001 From: freemine Date: Fri, 15 Jan 2021 22:39:56 +0800 Subject: [PATCH 02/17] add epoll.c tests --- tests/examples/c/epoll.c | 183 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 tests/examples/c/epoll.c diff --git a/tests/examples/c/epoll.c b/tests/examples/c/epoll.c new file mode 100644 index 0000000000..e0d63bae32 --- /dev/null +++ b/tests/examples/c/epoll.c @@ -0,0 +1,183 @@ +#ifdef __APPLE__ +#include "eok.h" +#else +#include +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define D(fmt, ...) fprintf(stderr, "%s[%d]%s(): " fmt "\n", basename(__FILE__), __LINE__, __func__, ##__VA_ARGS__) +#define A(statement, fmt, ...) do { \ + if (statement) break; \ + fprintf(stderr, "%s[%d]%s(): assert [%s] failed: %d[%s]: " fmt "\n", \ + basename(__FILE__), __LINE__, __func__, \ + #statement, errno, strerror(errno), \ + ##__VA_ARGS__); \ + abort(); \ +} while (0) + +#define E(fmt, ...) do { \ + fprintf(stderr, "%s[%d]%s(): %d[%s]: " fmt "\n", \ + basename(__FILE__), __LINE__, __func__, \ + errno, strerror(errno), \ + ##__VA_ARGS__); \ +} while (0) + +typedef struct ep_s ep_t; +struct ep_s { + int ep; + + pthread_mutex_t lock; + int sv[2]; // 0 for read, 1 for write; + pthread_t thread; + + volatile unsigned int stopping:1; + volatile unsigned int waiting:1; + volatile unsigned int wakenup:1; +}; + +static int ep_dummy = 0; + +static ep_t* ep_create(void); +static void ep_destroy(ep_t *ep); +static void* routine(void* arg); +static int open_connect(unsigned short port); + +int main(int argc, char *argv[]) { + ep_t* ep = ep_create(); + A(ep, "failed"); + int skt = open_connect(6789); + if (skt!=-1) { + struct epoll_event ev = {0}; + ev.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP; + ev.data.ptr = &skt; + A(0==epoll_ctl(ep->ep, EPOLL_CTL_ADD, skt, &ev), ""); + } + getchar(); + ep_destroy(ep); + D(""); + return 0; +} + +ep_t* ep_create(void) { + ep_t *ep = (ep_t*)calloc(1, sizeof(*ep)); + A(ep, "out of memory"); + A(-1!=(ep->ep = epoll_create(1)), ""); + ep->sv[0] = -1; + ep->sv[1] = -1; + A(0==socketpair(AF_LOCAL, SOCK_STREAM, 0, ep->sv), ""); + A(0==pthread_mutex_init(&ep->lock, NULL), ""); + A(0==pthread_mutex_lock(&ep->lock), ""); + struct epoll_event ev = {0}; + ev.events = EPOLLIN; + ev.data.ptr = &ep_dummy; + A(0==epoll_ctl(ep->ep, EPOLL_CTL_ADD, ep->sv[0], &ev), ""); + A(0==pthread_create(&ep->thread, NULL, routine, ep), ""); + A(0==pthread_mutex_unlock(&ep->lock), ""); + return ep; +} + +static void ep_destroy(ep_t *ep) { + A(ep, "invalid argument"); + ep->stopping = 1; + A(1==send(ep->sv[1], "1", 1, 0), ""); + A(0==pthread_join(ep->thread, NULL), ""); + A(0==pthread_mutex_destroy(&ep->lock), ""); + A(0==close(ep->sv[0]), ""); + A(0==close(ep->sv[1]), ""); + A(0==close(ep->ep), ""); + free(ep); +} + +static void* routine(void* arg) { + A(arg, "invalid argument"); + ep_t *ep = (ep_t*)arg; + + while (!ep->stopping) { + struct epoll_event evs[10] = {0}; + + A(0==pthread_mutex_lock(&ep->lock), ""); + A(ep->waiting==0, "internal logic error"); + ep->waiting = 1; + A(0==pthread_mutex_unlock(&ep->lock), ""); + + int r = epoll_wait(ep->ep, evs, sizeof(evs)/sizeof(evs[0]), -1); + A(r>0, "indefinite epoll_wait shall not timeout"); + + A(0==pthread_mutex_lock(&ep->lock), ""); + A(ep->waiting==1, "internal logic error"); + ep->waiting = 0; + A(0==pthread_mutex_unlock(&ep->lock), ""); + + for (int i=0; idata.ptr == &ep_dummy) { + char c = '\0'; + A(1==recv(ep->sv[0], &c, 1, 0), "internal logic error"); + A(0==pthread_mutex_lock(&ep->lock), ""); + ep->wakenup = 0; + A(0==pthread_mutex_unlock(&ep->lock), ""); + D("........"); + continue; + } + A(ev->data.ptr, "internal logic error"); + int skt = *(int*)ev->data.ptr; + if (ev->events & EPOLLIN) { + char buf[4]; + int n = recv(skt, buf, sizeof(buf)-1, 0); + A(n>=0 && nevents, buf); + } + if (ev->events & EPOLLRDHUP) { + A(0==epoll_ctl(ep->ep, EPOLL_CTL_DEL, skt, NULL), ""); + } + continue; + } + } + return NULL; +} + +static int open_connect(unsigned short port) { + int r = 0; + int skt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (skt==-1) { + E("socket() failed"); + return -1; + } + do { + struct sockaddr_in si = {0}; + si.sin_family = AF_INET; + si.sin_addr.s_addr = inet_addr("127.0.0.1"); + si.sin_port = htons(port); + r = connect(skt, (struct sockaddr*)&si, sizeof(si)); + if (r) { + E("connect(%u) failed", port); + break; + } + memset(&si, 0, sizeof(si)); + socklen_t len = sizeof(si); + r = getsockname(skt, (struct sockaddr *)&si, &len); + if (r) { + E("getsockname() failed"); + } + A(len==sizeof(si), "internal logic error"); + D("connected: %d", ntohs(si.sin_port)); + return skt; + } while (0); + close(skt); + return -1; +} + From 499644d5a7afc6c12d72d682b31915c9fc284b07 Mon Sep 17 00:00:00 2001 From: freemine Date: Fri, 15 Jan 2021 23:11:24 +0800 Subject: [PATCH 03/17] epoll.c test in CMakeLists.txt --- tests/examples/c/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/examples/c/CMakeLists.txt b/tests/examples/c/CMakeLists.txt index 4689ed8868..7cacefda57 100644 --- a/tests/examples/c/CMakeLists.txt +++ b/tests/examples/c/CMakeLists.txt @@ -5,6 +5,8 @@ IF (TD_LINUX) AUX_SOURCE_DIRECTORY(. SRC) ADD_EXECUTABLE(demo demo.c) TARGET_LINK_LIBRARIES(demo taos_static trpc tutil pthread ) + ADD_EXECUTABLE(epoll epoll.c) + TARGET_LINK_LIBRARIES(epoll taos_static trpc tutil pthread ) ENDIF () IF (TD_DARWIN) INCLUDE_DIRECTORIES(. ${TD_COMMUNITY_DIR}/src/inc ${TD_COMMUNITY_DIR}/src/client/inc ${TD_COMMUNITY_DIR}/inc) From 1e7037e8e17210a0e64bf9f130e11b4734e2675f Mon Sep 17 00:00:00 2001 From: freemine Date: Sat, 16 Jan 2021 13:11:33 +0800 Subject: [PATCH 04/17] epoll and kqueue --- src/os/src/darwin/eok.c | 199 +++++++++++++++++++++++++++++++++++++-- tests/examples/c/epoll.c | 155 ++++++++++++++++++++++++++---- 2 files changed, 329 insertions(+), 25 deletions(-) diff --git a/src/os/src/darwin/eok.c b/src/os/src/darwin/eok.c index ea67223c25..f83846b734 100644 --- a/src/os/src/darwin/eok.c +++ b/src/os/src/darwin/eok.c @@ -39,6 +39,7 @@ struct ep_over_kq_s { int sv[2]; // 0 for read, 1 for write + int evs_count; eok_event_t *evs_head; eok_event_t *evs_tail; eok_event_t *evs_free; @@ -82,6 +83,139 @@ static eoks_t eoks = { .eoks_free = NULL, }; +static const char* op_str(int op) { + switch (op) { + case EPOLL_CTL_ADD: return "EPOLL_CTL_ADD"; + case EPOLL_CTL_MOD: return "EPOLL_CTL_MOD"; + case EPOLL_CTL_DEL: return "EPOLL_CTL_DEL"; + default: return "UNKNOWN"; + } +} + +static __thread char buf_slots[10][1024] = {0}; +static __thread int buf_slots_linelen = sizeof(buf_slots[0])/sizeof(buf_slots[0][0]); +static __thread int buf_slots_count = sizeof(buf_slots)/(sizeof(buf_slots[0])/sizeof(buf_slots[0][0])); +static const char* events_str(uint32_t events, int slots) { + A(slots>=0 && slots0 && (events & (ev))==(ev)) { \ + n = snprintf(p, len, "%s%s", p!=buf ? "|" : "", #ev); \ + p += n; \ + len -= n; \ + } + CHK_EV(EPOLLIN); + CHK_EV(EPOLLPRI); + CHK_EV(EPOLLOUT); + CHK_EV(EPOLLRDNORM); + CHK_EV(EPOLLRDBAND); + CHK_EV(EPOLLWRNORM); + CHK_EV(EPOLLWRBAND); + CHK_EV(EPOLLMSG); + CHK_EV(EPOLLERR); + CHK_EV(EPOLLHUP); + CHK_EV(EPOLLRDHUP); + CHK_EV(EPOLLEXCLUSIVE); + CHK_EV(EPOLLWAKEUP); + CHK_EV(EPOLLONESHOT); + CHK_EV(EPOLLET); +#undef CHK_EV + return buf; +} + +static const char* kev_flags_str(uint16_t flags, int slots) { + A(slots>=0 && slots0 && (flags & (ev))==(ev)) { \ + n = snprintf(p, len, "%s%s", p!=buf ? "|" : "", #ev); \ + p += n; \ + len -= n; \ + } + CHK_EV(EV_ADD); + CHK_EV(EV_DELETE); + CHK_EV(EV_ENABLE); + CHK_EV(EV_DISABLE); + CHK_EV(EV_ONESHOT); + CHK_EV(EV_CLEAR); + CHK_EV(EV_RECEIPT); + CHK_EV(EV_DISPATCH); + CHK_EV(EV_UDATA_SPECIFIC); + CHK_EV(EV_DISPATCH2); + CHK_EV(EV_VANISHED); + CHK_EV(EV_SYSFLAGS); + CHK_EV(EV_FLAG0); + CHK_EV(EV_FLAG1); + CHK_EV(EV_EOF); + CHK_EV(EV_ERROR); +#undef CHK_EV + return buf; +} + static ep_over_kq_t* eoks_alloc(void); static void eoks_free(ep_over_kq_t *eok); static ep_over_kq_t* eoks_find(int epfd); @@ -135,10 +269,15 @@ int epoll_create(int size) { return -1; } + D("epoll_create epfd:[%d]", eok->idx); return eok->idx; } int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) { + D("epoll_ctling epfd:[%d], op:[%s], fd:[%d], events:[%04x:%s]", + epfd, op_str(op), fd, + event ? event->events : 0, + event ? events_str(event->events, 0) : "NULL"); int e = 0; if (epfd<0 || epfd>=eoks.neoks) { errno = EBADF; @@ -198,6 +337,7 @@ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) { ev.changed = 2; } break; case EPOLL_CTL_DEL: { + D("epoll_ctl adding..."); // event is ignored pev = &oev->epev; flags = EV_DELETE; @@ -213,11 +353,9 @@ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) { if (pev->events & (EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP)) { flags |= EV_EOF; EV_SET64(&krev, ev.fd, EVFILT_READ, flags, 0, 0, -1, 0, 0); - D("...."); } if (pev->events & EPOLLOUT) { EV_SET64(&kwev, ev.fd, EVFILT_WRITE, flags, 0, 0, -1, 0, 0); - D("...."); } if (eok_chgs_refresh(eok, oev, &ev, &krev, &kwev)) { @@ -244,14 +382,17 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) int e = 0; if (epfd<0 || epfd>=eoks.neoks) { errno = EBADF; + E("epoll_waiting epfd:[%d], maxevents:[%d], timeout:[%d] failed", epfd, maxevents, timeout); return -1; } if (!events) { errno = EINVAL; + E("epoll_waiting epfd:[%d], maxevents:[%d], timeout:[%d] failed", epfd, maxevents, timeout); return -1; } if (maxevents<=0) { errno = EINVAL; + E("epoll_waiting epfd:[%d], maxevents:[%d], timeout:[%d] failed", epfd, maxevents, timeout); return -1; } @@ -260,6 +401,7 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) ep_over_kq_t *eok = eoks_find(epfd); if (!eok) { errno = EBADF; + E("epoll_waiting epfd:[%d], maxevents:[%d], timeout:[%d] failed", epfd, maxevents, timeout); return -1; } @@ -281,6 +423,7 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) struct kevent64_s *eventslist = eok_alloc_eventslist(eok, maxevents); if (!eventslist) { e = ENOMEM; + E("epoll_waiting epfd:[%d], maxevents:[%d], timeout:[%d] failed", epfd, maxevents, timeout); break; } memset(eventslist, 0, maxevents * sizeof(*eventslist)); @@ -305,8 +448,15 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) eok->ichanges = 0; A(0==pthread_mutex_unlock(&eok->lock), ""); + if (ichanges>0) { + D("kevent64 changing [%d] changes and waiting...", ichanges); + } + errno = 0; r = kevent64(eok->kq, kchanges, ichanges, eventslist, maxevents, 0, pto); e = errno; + if (e) { + E("kevent64 waiting done, with r[%d]", r); + } A(0==pthread_mutex_lock(&eok->lock), ""); eok->waiting = 0; @@ -318,7 +468,6 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) } eok->waiting = 0; - if (r<0) break; if (r==0) { A(timeout!=-1, "internal logic error"); } @@ -327,7 +476,10 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) A(kev->udata && eok->evs_head && eok->evs_tail, "internal logic error"); eok_event_t *ev = (eok_event_t*)kev->udata; A(kev->ident == ev->fd, "internal logic error"); - D("..."); + if (kev->flags & EV_ERROR) { + D("error when processing change list for fd[%d], error[%s], kev_flags:[%04x:%s]", + ev->fd, strerror(kev->data), kev->flags, kev_flags_str(kev->flags, 0)); + } switch (kev->filter) { case EVFILT_READ: { A((ev->epev.events & EPOLLIN), "internal logic errro"); @@ -335,10 +487,10 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) char c = '\0'; A(1==recv(kev->ident, &c, 1, 0), "internal logic error"); A(0==memcmp(&c, "1", 1), "internal logic error"); - D("..............."); + D("wokenup"); } else { if (ev->changed==3) { - D("already requested to delete"); + D("already requested to delete for fd[%d]", ev->fd); // EV_DELETE? continue; } @@ -347,8 +499,13 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) pev.events = EPOLLIN; if (kev->flags & EV_EOF) { pev.events |= (EPOLLHUP | EPOLLERR | EPOLLRDHUP); + D(".........."); } - pev.events &= ev->epev.events; + pev.events = pev.events & ev->epev.events; + D("events found for fd[%d]: [%04x:%s], which was registered: [%04x:%s], kev_flags: [%04x:%s]", + ev->fd, pev.events, events_str(pev.events, 0), + ev->epev.events, events_str(ev->epev.events, 1), + kev->flags, kev_flags_str(kev->flags, 2)); events[cnts++] = pev; } } break; @@ -360,11 +517,26 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) } break; } } + if (r>=0) { + eok_event_t *p = eok->evs_head; + while (p) { + eok_event_t *next = p->next; + if (p->changed==3) { + D("removing registered event for fd[%d]: [%04x:%s]", p->fd, p->epev.events, events_str(p->epev.events, 0)); + eok_free_ev(eok, p); + } + p = next; + } + } } while (cnts==0); + if (cnts>0) { + D("kevent64 waiting done with [%d] events", cnts); + } A(0==pthread_mutex_unlock(&eok->lock), ""); if (e) { errno = e; + E("epoll_wait failed"); return -1; } @@ -383,6 +555,7 @@ static struct timespec do_timespec_diff(struct timespec *from, struct timespec * } int epoll_close(int epfd) { + D("epoll_closing epfd: [%d]", epfd); if (epfd<0 || epfd>=eoks.neoks) { errno = EBADF; return -1; @@ -445,6 +618,8 @@ static eok_event_t* eok_calloc_ev(ep_over_kq_t *eok) { else eok->evs_head = p; eok->evs_tail = p; + eok->evs_count += 1; + return p; } @@ -456,6 +631,8 @@ static void eok_free_ev(ep_over_kq_t *eok, eok_event_t *ev) { else eok->evs_tail = ev->prev; ev->next = eok->evs_free; eok->evs_free = ev->next; + + eok->evs_count -= 1; } static void eok_wakeup(ep_over_kq_t *eok) { @@ -486,17 +663,23 @@ static int eok_chgs_refresh(ep_over_kq_t *eok, eok_event_t *oev, eok_event_t *ev } oev->fd = ev->fd; - oev->epev = ev->epev; + if (ev->changed!=3) { + oev->epev = ev->epev; + } oev->changed = ev->changed; + n = 0; if (krev->ident==ev->fd) { krev->udata = (uint64_t)oev; eok->kchanges[eok->ichanges++] = *krev; + ++n; } if (kwev->ident==ev->fd) { kwev->udata = (uint64_t)oev; eok->kchanges[eok->ichanges++] = *kwev; + ++n; } + D("add changes[%d] for fd[%d], and changes/registers [%d/%d]", n, ev->fd, eok->ichanges, eok->evs_count); return 0; } diff --git a/tests/examples/c/epoll.c b/tests/examples/c/epoll.c index e0d63bae32..6047c9e4ea 100644 --- a/tests/examples/c/epoll.c +++ b/tests/examples/c/epoll.c @@ -54,18 +54,74 @@ static ep_t* ep_create(void); static void ep_destroy(ep_t *ep); static void* routine(void* arg); static int open_connect(unsigned short port); +static int open_listen(unsigned short port); + +typedef struct client_s client_t; +struct client_s { + int skt; + void (*on_event)(ep_t *ep, struct epoll_event *events, client_t *client); + volatile unsigned int state; // 1: listenning; 2: connected +}; + +static void echo_event(ep_t *ep, struct epoll_event *ev, client_t *client); int main(int argc, char *argv[]) { ep_t* ep = ep_create(); A(ep, "failed"); int skt = open_connect(6789); if (skt!=-1) { - struct epoll_event ev = {0}; - ev.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP; - ev.data.ptr = &skt; - A(0==epoll_ctl(ep->ep, EPOLL_CTL_ADD, skt, &ev), ""); + client_t *client = (client_t*)calloc(1, sizeof(*client)); + if (client) { + client->skt = skt; + client->on_event = echo_event; + client->state = 2; + struct epoll_event ev = {0}; + ev.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP; + ev.data.ptr = client; + A(0==epoll_ctl(ep->ep, EPOLL_CTL_ADD, skt, &ev), ""); + } + } + skt = open_listen(0); + if (skt!=-1) { + client_t *client = (client_t*)calloc(1, sizeof(*client)); + if (client) { + client->skt = skt; + client->on_event = echo_event; + client->state = 1; + struct epoll_event ev = {0}; + ev.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP; + ev.data.ptr = client; + A(0==epoll_ctl(ep->ep, EPOLL_CTL_ADD, skt, &ev), ""); + } + } + // char c = '\0'; + // while ((c=getchar())!=EOF) { + // switch (c) { + // case 'q': break; + // default: continue; + // } + // } + // getchar(); + char *line = NULL; + size_t linecap = 0; + ssize_t linelen; + while ((linelen = getline(&line, &linecap, stdin)) > 0) { + line[strlen(line)-1] = '\0'; + if (0==strcmp(line, "exit")) break; + if (0==strcmp(line, "quit")) break; + if (line==strstr(line, "close")) { + int fd = 0; + sscanf(line, "close %d", &fd); + if (fd<=2) { + fprintf(stderr, "fd [%d] invalid\n", fd); + continue; + } + A(0==epoll_ctl(ep->ep, EPOLL_CTL_DEL, fd, NULL), ""); + continue; + } + if (strlen(line)==0) continue; + fprintf(stderr, "unknown cmd:[%s]\n", line); } - getchar(); ep_destroy(ep); D(""); return 0; @@ -114,7 +170,7 @@ static void* routine(void* arg) { A(0==pthread_mutex_unlock(&ep->lock), ""); int r = epoll_wait(ep->ep, evs, sizeof(evs)/sizeof(evs[0]), -1); - A(r>0, "indefinite epoll_wait shall not timeout"); + A(r>0, "indefinite epoll_wait shall not timeout:[%d]", r); A(0==pthread_mutex_lock(&ep->lock), ""); A(ep->waiting==1, "internal logic error"); @@ -133,23 +189,50 @@ static void* routine(void* arg) { continue; } A(ev->data.ptr, "internal logic error"); - int skt = *(int*)ev->data.ptr; - if (ev->events & EPOLLIN) { - char buf[4]; - int n = recv(skt, buf, sizeof(buf)-1, 0); - A(n>=0 && nevents, buf); - } - if (ev->events & EPOLLRDHUP) { - A(0==epoll_ctl(ep->ep, EPOLL_CTL_DEL, skt, NULL), ""); - } + client_t *client = (client_t*)ev->data.ptr; + client->on_event(ep, ev, client); continue; } } return NULL; } +static int open_listen(unsigned short port) { + int r = 0; + int skt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (skt==-1) { + E("socket() failed"); + return -1; + } + do { + struct sockaddr_in si = {0}; + si.sin_family = AF_INET; + si.sin_addr.s_addr = inet_addr("127.0.0.1"); + si.sin_port = htons(port); + r = bind(skt, (struct sockaddr*)&si, sizeof(si)); + if (r) { + E("bind(%u) failed", port); + break; + } + r = listen(skt, 100); + if (r) { + E("listen() failed"); + break; + } + memset(&si, 0, sizeof(si)); + socklen_t len = sizeof(si); + r = getsockname(skt, (struct sockaddr *)&si, &len); + if (r) { + E("getsockname() failed"); + } + A(len==sizeof(si), "internal logic error"); + D("listenning at: %d", ntohs(si.sin_port)); + return skt; + } while (0); + close(skt); + return -1; +} + static int open_connect(unsigned short port) { int r = 0; int skt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); @@ -181,3 +264,41 @@ static int open_connect(unsigned short port) { return -1; } +static void echo_event(ep_t *ep, struct epoll_event *ev, client_t *client) { + if (ev->events & EPOLLIN) { + if (client->state==1) { + struct sockaddr_in si = {0}; + socklen_t silen = sizeof(si); + int skt = accept(client->skt, (struct sockaddr*)&si, &silen); + if (skt!=-1) { + client_t *server = (client_t*)calloc(1, sizeof(*server)); + if (server) { + server->skt = skt; + server->on_event = echo_event; + server->state = 2; + struct epoll_event ev = {0}; + ev.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP; + ev.data.ptr = server; + A(0==epoll_ctl(ep->ep, EPOLL_CTL_ADD, skt, &ev), ""); + } + } + } + if (client->state==2) { + char buf[4]; + int n = recv(client->skt, buf, sizeof(buf)-1, 0); + A(n>=0 && nevents, buf); + } + } + if (ev->events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) { + A(0==pthread_mutex_lock(&ep->lock), ""); + A(0==epoll_ctl(ep->ep, EPOLL_CTL_DEL, client->skt, NULL), ""); + A(0==pthread_mutex_unlock(&ep->lock), ""); + close(client->skt); + client->skt = -1; + client->on_event = NULL; + free(client); + } +} + From 985de782080ae45639aaf701df760c2a5ffcea1b Mon Sep 17 00:00:00 2001 From: freemine Date: Sun, 17 Jan 2021 00:47:16 +0800 Subject: [PATCH 05/17] with epoll over kqueue, taos can be built on MacOSX --- src/kit/shell/src/shellDarwin.c | 3 + src/os/inc/eok.h | 2 +- src/os/src/darwin/darwinEnv.c | 2 - src/os/src/darwin/eok.c | 176 +++++++++++++++++++++----------- src/os/src/detail/osSocket.c | 1 - src/rpc/src/rpcTcp.c | 14 --- src/sync/src/syncTcp.c | 6 -- src/tsdb/src/tsdbMain.c | 4 +- src/util/src/tsocket.c | 3 +- 9 files changed, 127 insertions(+), 84 deletions(-) diff --git a/src/kit/shell/src/shellDarwin.c b/src/kit/shell/src/shellDarwin.c index d6aed4401c..dbec3fdb05 100644 --- a/src/kit/shell/src/shellDarwin.c +++ b/src/kit/shell/src/shellDarwin.c @@ -350,6 +350,9 @@ void *shellLoopQuery(void *arg) { reset_terminal_mode(); } while (shellRunCommand(con, command) == 0); + tfree(command); + exitShell(); + pthread_cleanup_pop(1); return NULL; diff --git a/src/os/inc/eok.h b/src/os/inc/eok.h index 8892e50c35..a0fd5b5d8e 100644 --- a/src/os/inc/eok.h +++ b/src/os/inc/eok.h @@ -70,5 +70,5 @@ int epoll_close(int epfd); } #endif -#endif // _eok_h_fd274616_996c_400e_9023_ae70be881fa3_ +#endif // _eok_h_fd274616_996c_400e_9023_ae70be881fa3_ diff --git a/src/os/src/darwin/darwinEnv.c b/src/os/src/darwin/darwinEnv.c index 28388f24d2..da4b32139e 100644 --- a/src/os/src/darwin/darwinEnv.c +++ b/src/os/src/darwin/darwinEnv.c @@ -17,8 +17,6 @@ #include "os.h" #include "tglobal.h" -#include - void osInit() { if (configDir[0] == 0) { strcpy(configDir, "~/TDengine/cfg"); diff --git a/src/os/src/darwin/eok.c b/src/os/src/darwin/eok.c index f83846b734..3f7137f6a1 100644 --- a/src/os/src/darwin/eok.c +++ b/src/os/src/darwin/eok.c @@ -34,20 +34,31 @@ typedef struct eok_event_s eok_event_t; struct ep_over_kq_s { int kq; + + // !!! + // idx in the eoks list + // used as pseudo-file-desciptor + // must be 'closed' with epoll_close int idx; + ep_over_kq_t *next; int sv[2]; // 0 for read, 1 for write + // all registered 'epoll events, key by fd' int evs_count; eok_event_t *evs_head; eok_event_t *evs_tail; eok_event_t *evs_free; + // all kev changes list pending to be processed by kevent64 + // key by tuple (ident,filter), ident === fd in this case struct kevent64_s *kchanges; int nchanges; int ichanges; + // kev eventslist for kevent64 to store active events + // they remain alive among kevent64 calls struct kevent64_s *kevslist; int nevslist; @@ -76,6 +87,7 @@ struct eoks_s { int neoks; ep_over_kq_t *eoks_free; }; + static eoks_t eoks = { .lock = PTHREAD_MUTEX_INITIALIZER, .eoks = NULL, @@ -93,8 +105,9 @@ static const char* op_str(int op) { } static __thread char buf_slots[10][1024] = {0}; -static __thread int buf_slots_linelen = sizeof(buf_slots[0])/sizeof(buf_slots[0][0]); -static __thread int buf_slots_count = sizeof(buf_slots)/(sizeof(buf_slots[0])/sizeof(buf_slots[0][0])); +static __thread int buf_slots_linelen = sizeof(buf_slots[0])/sizeof(buf_slots[0][0]); +static __thread int buf_slots_count = sizeof(buf_slots)/(sizeof(buf_slots[0])/sizeof(buf_slots[0][0])); + static const char* events_str(uint32_t events, int slots) { A(slots>=0 && slots on linux // EPOLLIN = 0x001, // #define EPOLLIN EPOLLIN // EPOLLPRI = 0x002, @@ -164,6 +178,7 @@ static const char* kev_flags_str(uint16_t flags, int slots) { size_t len = buf_slots_linelen; int n = 0; buf[0] = '\0'; + // copied to // #define EV_ADD 0x0001 /* add event to kq (implies enable) */ // #define EV_DELETE 0x0002 /* delete event from kq */ // #define EV_ENABLE 0x0004 /* enable event */ @@ -233,10 +248,7 @@ int epoll_create(int size) { (void)size; int e = 0; ep_over_kq_t *eok = eoks_alloc(); - if (!eok) { - errno = ENOMEM; - return -1; - } + if (!eok) return -1; A(eok->kq==-1, "internal logic error"); A(eok->lock_valid, "internal logic error"); @@ -263,13 +275,14 @@ int epoll_create(int size) { struct epoll_event ev = {0}; ev.events = EPOLLIN; ev.data.ptr = &eok_dummy; + D("epoll_create epfd:[%d]", eok->idx); if (epoll_ctl(eok->idx, EPOLL_CTL_ADD, eok->sv[0], &ev)) { - A(0, "internal logic error"); + e = errno; epoll_close(eok->idx); + errno = e; return -1; } - D("epoll_create epfd:[%d]", eok->idx); return eok->idx; } @@ -309,36 +322,34 @@ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) { e = ENOENT; break; } + if (op!=EPOLL_CTL_DEL && !event) { + e = EINVAL; + break; + } + // prepare krev/kwev struct kevent64_s krev = {0}; struct kevent64_s kwev = {0}; krev.ident = -1; kwev.ident = -1; uint16_t flags = 0; + // prepare internal eok event eok_event_t ev = {0}; ev.fd = fd; if (event) ev.epev = *event; struct epoll_event *pev = event; switch (op) { case EPOLL_CTL_ADD: { - if (!event) { - e = EINVAL; - break; - } flags = EV_ADD; ev.changed = 1; } break; case EPOLL_CTL_MOD: { - if (!event) { - e = EINVAL; - break; - } flags = EV_ADD; ev.changed = 2; } break; case EPOLL_CTL_DEL: { - D("epoll_ctl adding..."); // event is ignored + // pev points to registered epoll_event pev = &oev->epev; flags = EV_DELETE; ev.changed = 3; @@ -350,6 +361,7 @@ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) { if (e) break; + // udata will be delayed to be set if (pev->events & (EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP)) { flags |= EV_EOF; EV_SET64(&krev, ev.fd, EVFILT_READ, flags, 0, 0, -1, 0, 0); @@ -358,6 +370,7 @@ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) { EV_SET64(&kwev, ev.fd, EVFILT_WRITE, flags, 0, 0, -1, 0, 0); } + // refresh registered evlist and changelist in a transaction way if (eok_chgs_refresh(eok, oev, &ev, &krev, &kwev)) { e = errno; A(e, "internal logic error"); @@ -380,11 +393,6 @@ static struct timespec do_timespec_diff(struct timespec *from, struct timespec * int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) { int e = 0; - if (epfd<0 || epfd>=eoks.neoks) { - errno = EBADF; - E("epoll_waiting epfd:[%d], maxevents:[%d], timeout:[%d] failed", epfd, maxevents, timeout); - return -1; - } if (!events) { errno = EINVAL; E("epoll_waiting epfd:[%d], maxevents:[%d], timeout:[%d] failed", epfd, maxevents, timeout); @@ -396,15 +404,6 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) return -1; } - int r = 0; - - ep_over_kq_t *eok = eoks_find(epfd); - if (!eok) { - errno = EBADF; - E("epoll_waiting epfd:[%d], maxevents:[%d], timeout:[%d] failed", epfd, maxevents, timeout); - return -1; - } - struct timespec abstime = {0}; A(TIME_UTC==timespec_get(&abstime, TIME_UTC), "internal logic error"); @@ -412,7 +411,17 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) if (timeout<0) timeout = 0; int64_t t = abstime.tv_nsec + timeout * 1000000; abstime.tv_sec += t / 1000000000; - abstime.tv_nsec %= 1000000000; + abstime.tv_nsec = t % 1000000000; + } + + int r = 0; + + ep_over_kq_t *eok = eoks_find(epfd); + if (!eok) { + errno = EBADF; + E("epoll_waiting epfd:[%d], maxevents:[%d], timeout:[%d] failed", epfd, maxevents, timeout); + errno = EBADF; + return -1; } int cnts = 0; @@ -431,43 +440,52 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) struct timespec now = {0}; A(TIME_UTC==timespec_get(&now, TIME_UTC), "internal logic error"); struct timespec to = do_timespec_diff(&now, &abstime); - struct timespec *pto = NULL; - if (timeout!=-1) { - pto = &to; + struct timespec *pto = &to; + if (timeout==-1) { + pto = NULL; } - eok->changed = 0; - eok->wakenup = 0; - eok->waiting = 1; - + // taking the changelist struct kevent64_s *kchanges = eok->kchanges; int nchanges = eok->nchanges; int ichanges = eok->ichanges; + // let outside world to add changes eok->kchanges = NULL; eok->nchanges = 0; eok->ichanges = 0; + eok->changed = 0; + eok->wakenup = 0; + eok->waiting = 1; + A(0==pthread_mutex_unlock(&eok->lock), ""); if (ichanges>0) { - D("kevent64 changing [%d] changes and waiting...", ichanges); + D("kevent64 epfd[%d] changing [%d] changes and waiting...", eok->idx, ichanges); } errno = 0; r = kevent64(eok->kq, kchanges, ichanges, eventslist, maxevents, 0, pto); e = errno; if (e) { - E("kevent64 waiting done, with r[%d]", r); + E("kevent64 epfd[%d] waiting done, with r[%d]", eok->idx, r); } A(0==pthread_mutex_lock(&eok->lock), ""); eok->waiting = 0; + if (kchanges) { - free(kchanges); - kchanges = NULL; + if (eok->kchanges==NULL) { + // reuse + A(eok->nchanges==0 && eok->ichanges==0, "internal logic error"); + eok->kchanges = kchanges; + eok->nchanges = nchanges; + } else { + free(kchanges); + kchanges = NULL; + } nchanges = 0; ichanges = 0; } - eok->waiting = 0; if (r==0) { A(timeout!=-1, "internal logic error"); } @@ -484,28 +502,37 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) case EVFILT_READ: { A((ev->epev.events & EPOLLIN), "internal logic errro"); if (ev->epev.data.ptr==&eok_dummy) { + // it's coming from wakeup socket pair char c = '\0'; A(1==recv(kev->ident, &c, 1, 0), "internal logic error"); A(0==memcmp(&c, "1", 1), "internal logic error"); D("wokenup"); + continue; } else { if (ev->changed==3) { D("already requested to delete for fd[%d]", ev->fd); + // TODO: write a unit test for this case // EV_DELETE? continue; } + // converting to epoll_event + // we shall collect all kevents for the uniq fd into one epoll_evnt + // but currently, taos never use EPOLLOUT + // just let it this way for the moment struct epoll_event pev = {0}; pev.data.ptr = ev->epev.data.ptr; pev.events = EPOLLIN; if (kev->flags & EV_EOF) { + // take all these as EOF for the moment pev.events |= (EPOLLHUP | EPOLLERR | EPOLLRDHUP); - D(".........."); } + // rounded to what user care pev.events = pev.events & ev->epev.events; D("events found for fd[%d]: [%04x:%s], which was registered: [%04x:%s], kev_flags: [%04x:%s]", ev->fd, pev.events, events_str(pev.events, 0), ev->epev.events, events_str(ev->epev.events, 1), kev->flags, kev_flags_str(kev->flags, 2)); + // now we get ev and store it events[cnts++] = pev; } } break; @@ -518,6 +545,8 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) } } if (r>=0) { + // we can safely rule out delete-requested-events from the regitered evlists + // if only changelist are correctly registered eok_event_t *p = eok->evs_head; while (p) { eok_event_t *next = p->next; @@ -528,6 +557,14 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) p = next; } } + if (cnts==0) { + // if no user-cared-events is up + // we check to see if time is up + A(TIME_UTC==timespec_get(&now, TIME_UTC), "internal logic error"); + to = do_timespec_diff(&now, &abstime); + if (to.tv_sec==0 && to.tv_nsec==0) break; + // time is not up yet, continue loop + } } while (cnts==0); if (cnts>0) { D("kevent64 waiting done with [%d] events", cnts); @@ -540,6 +577,7 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) return -1; } + // tell user how many events are valid return cnts; } @@ -547,19 +585,20 @@ static struct timespec do_timespec_diff(struct timespec *from, struct timespec * struct timespec delta; delta.tv_sec = to->tv_sec - from->tv_sec; delta.tv_nsec = to->tv_nsec - from->tv_nsec; + // norm and round up while (delta.tv_nsec<0) { delta.tv_sec -= 1; delta.tv_nsec += 1000000000; } + if (delta.tv_sec < 0) { + delta.tv_sec = 0; + delta.tv_nsec = 0; + } return delta; } int epoll_close(int epfd) { D("epoll_closing epfd: [%d]", epfd); - if (epfd<0 || epfd>=eoks.neoks) { - errno = EBADF; - return -1; - } ep_over_kq_t *eok = eoks_find(epfd); if (!eok) { errno = EBADF; @@ -568,9 +607,11 @@ int epoll_close(int epfd) { A(0==pthread_mutex_lock(&eok->lock), ""); do { + // panic if it would be double-closed A(eok->stopping==0, "internal logic error"); eok->stopping = 1; - A(eok->waiting, "internal logic error"); + // panic if epoll_wait is pending + A(eok->waiting==0, "internal logic error"); if (eok->kq!=-1) { close(eok->kq); @@ -608,9 +649,12 @@ static eok_event_t* eok_calloc_ev(ep_over_kq_t *eok) { p = eok->evs_free; eok->evs_free = p->next; p->next = NULL; + A(p->prev==NULL, "internal logic error"); } else { p = (eok_event_t*)calloc(1, sizeof(*p)); if (!p) return NULL; + A(p->next==NULL, "internal logic error"); + A(p->prev==NULL, "internal logic error"); } // dirty link p->prev = eok->evs_tail; @@ -626,9 +670,9 @@ static eok_event_t* eok_calloc_ev(ep_over_kq_t *eok) { static void eok_free_ev(ep_over_kq_t *eok, eok_event_t *ev) { if (ev->prev) ev->prev->next = ev->next; else eok->evs_head = ev->next; - ev->prev = NULL; if (ev->next) ev->next->prev = ev->prev; else eok->evs_tail = ev->prev; + ev->prev = NULL; ev->next = eok->evs_free; eok->evs_free = ev->next; @@ -639,7 +683,7 @@ static void eok_wakeup(ep_over_kq_t *eok) { if (!eok->waiting) return; if (eok->wakenup) return; eok->wakenup = 1; - send(eok->sv[1], "1", 1, 0); + A(1==send(eok->sv[1], "1", 1, 0), ""); } static int eok_chgs_refresh(ep_over_kq_t *eok, eok_event_t *oev, eok_event_t *ev, struct kevent64_s *krev, struct kevent64_s *kwev) { @@ -653,6 +697,8 @@ static int eok_chgs_refresh(ep_over_kq_t *eok, eok_event_t *oev, eok_event_t *ev struct kevent64_s *p = (struct kevent64_s*)realloc(eok->kchanges, sizeof(*p) * (eok->nchanges + 10)); if (!p) { if (ev->changed==1) { + // roll back + A(oev, "internal logic error"); eok_free_ev(eok, oev); } errno = ENOMEM; @@ -662,12 +708,15 @@ static int eok_chgs_refresh(ep_over_kq_t *eok, eok_event_t *oev, eok_event_t *ev eok->nchanges += 10; } + // copy to registered event slot oev->fd = ev->fd; if (ev->changed!=3) { + // if add/mod, copy epoll_event oev->epev = ev->epev; } oev->changed = ev->changed; + // copy to changes list n = 0; if (krev->ident==ev->fd) { krev->udata = (uint64_t)oev; @@ -679,7 +728,7 @@ static int eok_chgs_refresh(ep_over_kq_t *eok, eok_event_t *oev, eok_event_t *ev eok->kchanges[eok->ichanges++] = *kwev; ++n; } - D("add changes[%d] for fd[%d], and changes/registers [%d/%d]", n, ev->fd, eok->ichanges, eok->evs_count); + D("add #changes[%d] for fd[%d], and now #changes/registers [%d/%d]", n, ev->fd, eok->ichanges, eok->evs_count); return 0; } @@ -707,7 +756,10 @@ static ep_over_kq_t* eoks_alloc(void) { } while (0); A(0==pthread_mutex_unlock(&eoks.lock), ""); - if (!eok) return NULL; + if (!eok) { + errno = ENOMEM; + return NULL; + } if (eok->lock_valid) { return eok; } @@ -718,6 +770,7 @@ static ep_over_kq_t* eoks_alloc(void) { } eoks_free(eok); + errno = ENOMEM; return NULL; } @@ -725,16 +778,23 @@ static void eoks_free(ep_over_kq_t *eok) { A(0==pthread_mutex_lock(&eoks.lock), ""); do { A(eok->next==NULL, "internal logic error"); - A(eok->evs_head==NULL, "internal logic error"); + + // leave eok->kchanges as is + A(eok->ichanges==0, "internal logic error"); + A(eok->waiting==0, "internal logic error"); - if (eok->sv[0]!=-1) { + if (eok->evs_count==1) { + A(eok->evs_head && eok->evs_tail && eok->evs_head==eok->evs_tail, "internal logic error"); + A(eok->evs_head->fd==eok->sv[0] && eok->sv[0]!=-1 && eok->sv[1]!=-1, "internal logic error"); + // fd is critical system resource close(eok->sv[0]); eok->sv[0] = -1; - } - if (eok->sv[1]!=-1) { close(eok->sv[1]); eok->sv[1] = -1; + eok_free_ev(eok, eok->evs_head); } + A(eok->evs_head==NULL && eok->evs_tail==NULL && eok->evs_count==0, "internal logic error"); + A(eok->sv[0]==-1 && eok->sv[1]==-1, "internal logic error"); if (eok->kq!=-1) { close(eok->kq); eok->kq = -1; diff --git a/src/os/src/detail/osSocket.c b/src/os/src/detail/osSocket.c index c6cbbe6b83..659c8510dc 100644 --- a/src/os/src/detail/osSocket.c +++ b/src/os/src/detail/osSocket.c @@ -58,7 +58,6 @@ void taosBlockSIGPIPE() { #ifndef TAOS_OS_FUNC_SOCKET_SETSOCKETOPT int32_t taosSetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t optlen) { - fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); return setsockopt(socketfd, level, optname, optval, (socklen_t)optlen); } diff --git a/src/rpc/src/rpcTcp.c b/src/rpc/src/rpcTcp.c index ff2a5882f8..c65c0db66b 100644 --- a/src/rpc/src/rpcTcp.c +++ b/src/rpc/src/rpcTcp.c @@ -133,7 +133,6 @@ void *taosInitTcpServer(uint32_t ip, uint16_t port, char *label, int numOfThread } pThreadObj->pollFd = (int64_t)epoll_create(10); // size does not matter - fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); if (pThreadObj->pollFd < 0) { tError("%s failed to create TCP epoll", label); code = -1; @@ -294,7 +293,6 @@ void *taosInitTcpClient(uint32_t ip, uint16_t port, char *label, int num, void * } pThreadObj->pollFd = (SOCKET)epoll_create(10); // size does not matter - fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); if (pThreadObj->pollFd < 0) { tError("%s failed to create TCP client epoll", label); free(pThreadObj); @@ -343,10 +341,6 @@ void taosCleanUpTcpClient(void *chandle) { void *taosOpenTcpClientConnection(void *shandle, void *thandle, uint32_t ip, uint16_t port) { SThreadObj * pThreadObj = shandle; - fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); - fprintf(stderr, "pThreadObj->ip:%d\n", pThreadObj->ip); - fprintf(stderr, "PF_INET/AF_INET:%d/%d\n", PF_INET, AF_INET); - fprintf(stderr, "ip/port:%x/%d\n", ip, port); SOCKET fd = taosOpenTcpClientSocket(ip, port, pThreadObj->ip); if (fd < 0) return NULL; @@ -358,7 +352,6 @@ void *taosOpenTcpClientConnection(void *shandle, void *thandle, uint32_t ip, uin localPort = (uint16_t)ntohs(sin.sin_port); } - fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); SFdObj *pFdObj = taosMallocFdObj(pThreadObj, fd); if (pFdObj) { @@ -369,7 +362,6 @@ void *taosOpenTcpClientConnection(void *shandle, void *thandle, uint32_t ip, uin pThreadObj->label, thandle, ip, port, localPort, pFdObj, pThreadObj->numOfFds); } else { tError("%s failed to malloc client FdObj(%s)", pThreadObj->label, strerror(errno)); - fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); taosCloseSocket(fd); } @@ -492,32 +484,27 @@ static void *taosProcessTcpData(void *param) { if (fdNum < 0) continue; for (int i = 0; i < fdNum; ++i) { - fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); pFdObj = events[i].data.ptr; if (events[i].events & EPOLLERR) { tDebug("%s %p FD:%p epoll errors", pThreadObj->label, pFdObj->thandle, pFdObj); - fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); taosReportBrokenLink(pFdObj); continue; } if (events[i].events & EPOLLRDHUP) { tDebug("%s %p FD:%p RD hang up", pThreadObj->label, pFdObj->thandle, pFdObj); - fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); taosReportBrokenLink(pFdObj); continue; } if (events[i].events & EPOLLHUP) { tDebug("%s %p FD:%p hang up", pThreadObj->label, pFdObj->thandle, pFdObj); - fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); taosReportBrokenLink(pFdObj); continue; } if (taosReadTcpData(pFdObj, &recvInfo) < 0) { - fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); shutdown(pFdObj->fd, SHUT_WR); continue; } @@ -563,7 +550,6 @@ static SFdObj *taosMallocFdObj(SThreadObj *pThreadObj, SOCKET fd) { event.events = EPOLLIN | EPOLLRDHUP; event.data.ptr = pFdObj; - fprintf(stderr, "==%s[%d]%s()==\n", basename(__FILE__), __LINE__, __func__); if (epoll_ctl(pThreadObj->pollFd, EPOLL_CTL_ADD, fd, &event) < 0) { tfree(pFdObj); terrno = TAOS_SYSTEM_ERROR(errno); diff --git a/src/sync/src/syncTcp.c b/src/sync/src/syncTcp.c index be85853819..1e5761316a 100644 --- a/src/sync/src/syncTcp.c +++ b/src/sync/src/syncTcp.c @@ -136,15 +136,12 @@ void *syncAllocateTcpConn(void *param, int64_t rid, int32_t connFd) { event.events = EPOLLIN | EPOLLRDHUP; event.data.ptr = pConn; - fprintf(stderr, ">>>>>>>>>>>>>>>>>\n"); if (epoll_ctl(pThread->pollFd, EPOLL_CTL_ADD, connFd, &event) < 0) { - fprintf(stderr, "<<<<<<<<<<<<<<<<<\n"); sError("failed to add fd:%d since %s", connFd, strerror(errno)); terrno = TAOS_SYSTEM_ERROR(errno); tfree(pConn); pConn = NULL; } else { - fprintf(stderr, "<<<<<<<<<<<<<<<<<\n"); pThread->numOfFds++; sDebug("%p fd:%d is added to epoll thread, num:%d", pThread, connFd, pThread->numOfFds); } @@ -170,9 +167,7 @@ static void taosProcessBrokenLink(SConnObj *pConn) { (*pInfo->processBrokenLink)(pConn->handleId); pThread->numOfFds--; - fprintf(stderr, "<<<<<<<<<<<<<<<<<\n"); epoll_ctl(pThread->pollFd, EPOLL_CTL_DEL, pConn->fd, NULL); - fprintf(stderr, "<<<<<<<<<<<<<<<<<\n"); sDebug("%p fd:%d is removed from epoll thread, num:%d", pThread, pConn->fd, pThread->numOfFds); taosClose(pConn->fd); tfree(pConn); @@ -287,7 +282,6 @@ static SThreadObj *syncGetTcpThread(SPoolObj *pPool) { pThread->pPool = pPool; pThread->pollFd = epoll_create(10); // size does not matter - fprintf(stderr, "...............\n"); if (pThread->pollFd < 0) { tfree(pThread); return NULL; diff --git a/src/tsdb/src/tsdbMain.c b/src/tsdb/src/tsdbMain.c index 602aea70e3..5493188c09 100644 --- a/src/tsdb/src/tsdbMain.c +++ b/src/tsdb/src/tsdbMain.c @@ -649,13 +649,15 @@ static STsdbRepo *tsdbNewRepo(char *rootDir, STsdbAppH *pAppH, STsdbCfg *pCfg) { #ifdef __APPLE__ pRepo->readyToCommit = sem_open(NULL, O_CREAT, 0644, 1); - if (!pRepo->readyToCommit) { + if (pRepo->readyToCommit==SEM_FAILED) { + code = errno; terrno = TAOS_SYSTEM_ERROR(code); goto _err; } #else code = sem_init(&(pRepo->readyToCommit), 0, 1); if (code != 0) { + code = errno; terrno = TAOS_SYSTEM_ERROR(code); goto _err; } diff --git a/src/util/src/tsocket.c b/src/util/src/tsocket.c index 6b15d3677c..9fc10576ac 100644 --- a/src/util/src/tsocket.c +++ b/src/util/src/tsocket.c @@ -35,7 +35,7 @@ int32_t taosGetFqdn(char *fqdn) { hints.ai_flags = AI_CANONNAME; int32_t ret = getaddrinfo(hostname, NULL, &hints, &result); if (!result) { - uError("failed to get fqdn for hostname <%s>, code:%d, reason:%s", hostname, ret, gai_strerror(ret)); + uError("failed to get fqdn, code:%d, reason:%s", ret, gai_strerror(ret)); return -1; } @@ -342,6 +342,7 @@ int32_t taosKeepTcpAlive(SOCKET sockFd) { } #ifndef __APPLE__ + // all fails on macosx int32_t probes = 3; if (taosSetSockOpt(sockFd, SOL_TCP, TCP_KEEPCNT, (void *)&probes, sizeof(probes)) < 0) { uError("fd:%d setsockopt SO_KEEPCNT failed: %d (%s)", sockFd, errno, strerror(errno)); From 0c19d10929de5f598bdccb35c4295c72fcbdeecb Mon Sep 17 00:00:00 2001 From: freemine Date: Sun, 17 Jan 2021 01:04:09 +0800 Subject: [PATCH 06/17] add ENABLE_LOG macro to swith on/off D/E stuffs --- src/os/src/darwin/eok.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/os/src/darwin/eok.c b/src/os/src/darwin/eok.c index 3f7137f6a1..322aa7cf86 100644 --- a/src/os/src/darwin/eok.c +++ b/src/os/src/darwin/eok.c @@ -10,7 +10,19 @@ #include #include +#ifdef ENABLE_LOG #define D(fmt, ...) fprintf(stderr, "%s[%d]%s(): " fmt "\n", basename(__FILE__), __LINE__, __func__, ##__VA_ARGS__) +#define E(fmt, ...) do { \ + fprintf(stderr, "%s[%d]%s(): %d[%s]: " fmt "\n", \ + basename(__FILE__), __LINE__, __func__, \ + errno, strerror(errno), \ + ##__VA_ARGS__); \ +} while (0) +#else // !ENABLE_LOG +#define D(fmt, ...) (void)fmt +#define E(fmt, ...) (void)fmt +#endif // ENABLE_LOG + #define A(statement, fmt, ...) do { \ if (statement) break; \ fprintf(stderr, "%s[%d]%s(): assert [%s] failed: %d[%s]: " fmt "\n", \ @@ -20,13 +32,6 @@ abort(); \ } while (0) -#define E(fmt, ...) do { \ - fprintf(stderr, "%s[%d]%s(): %d[%s]: " fmt "\n", \ - basename(__FILE__), __LINE__, __func__, \ - errno, strerror(errno), \ - ##__VA_ARGS__); \ -} while (0) - static int eok_dummy = 0; typedef struct ep_over_kq_s ep_over_kq_t; @@ -95,6 +100,7 @@ static eoks_t eoks = { .eoks_free = NULL, }; +#ifdef ENABLE_LOG static const char* op_str(int op) { switch (op) { case EPOLL_CTL_ADD: return "EPOLL_CTL_ADD"; @@ -230,6 +236,7 @@ static const char* kev_flags_str(uint16_t flags, int slots) { #undef CHK_EV return buf; } +#endif // ENABLE_LOG static ep_over_kq_t* eoks_alloc(void); static void eoks_free(ep_over_kq_t *eok); From 54af96b6f06e04725eec3ea48ff876d10b76e06f Mon Sep 17 00:00:00 2001 From: freemine Date: Sun, 17 Jan 2021 01:37:58 +0800 Subject: [PATCH 07/17] adding TAOS license header --- src/CMakeLists.txt | 2 +- src/os/inc/eok.h | 15 +++++++++++++++ src/os/src/darwin/eok.c | 15 +++++++++++++++ tests/examples/c/epoll.c | 15 +++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 71ed16afa1..04bc61ed9e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -19,5 +19,5 @@ ADD_SUBDIRECTORY(wal) ADD_SUBDIRECTORY(cq) ADD_SUBDIRECTORY(dnode) #ADD_SUBDIRECTORY(connector/odbc) -#ADD_SUBDIRECTORY(connector/jdbc) +ADD_SUBDIRECTORY(connector/jdbc) diff --git a/src/os/inc/eok.h b/src/os/inc/eok.h index a0fd5b5d8e..4d38e8e632 100644 --- a/src/os/inc/eok.h +++ b/src/os/inc/eok.h @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + #ifndef _eok_h_fd274616_996c_400e_9023_ae70be881fa3_ #define _eok_h_fd274616_996c_400e_9023_ae70be881fa3_ diff --git a/src/os/src/darwin/eok.c b/src/os/src/darwin/eok.c index 322aa7cf86..f9465d29dd 100644 --- a/src/os/src/darwin/eok.c +++ b/src/os/src/darwin/eok.c @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + #include "eok.h" #include diff --git a/tests/examples/c/epoll.c b/tests/examples/c/epoll.c index 6047c9e4ea..1ef9b3eddf 100644 --- a/tests/examples/c/epoll.c +++ b/tests/examples/c/epoll.c @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + #ifdef __APPLE__ #include "eok.h" #else From 60a9725d031d36ff0f0903863080d434182555e4 Mon Sep 17 00:00:00 2001 From: freemine Date: Sun, 17 Jan 2021 02:07:25 +0800 Subject: [PATCH 08/17] replace array initializer with memset --- tests/examples/c/epoll.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/examples/c/epoll.c b/tests/examples/c/epoll.c index 1ef9b3eddf..eb66156c14 100644 --- a/tests/examples/c/epoll.c +++ b/tests/examples/c/epoll.c @@ -177,7 +177,8 @@ static void* routine(void* arg) { ep_t *ep = (ep_t*)arg; while (!ep->stopping) { - struct epoll_event evs[10] = {0}; + struct epoll_event evs[10]; + memset(evs, 0, sizeof(evs)); A(0==pthread_mutex_lock(&ep->lock), ""); A(ep->waiting==0, "internal logic error"); From 6530365e580e034615b4c550a9d0514a2b5f4be8 Mon Sep 17 00:00:00 2001 From: freemine Date: Sun, 17 Jan 2021 08:13:51 +0800 Subject: [PATCH 09/17] bugFix: check timeout as well --- src/os/src/darwin/eok.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/os/src/darwin/eok.c b/src/os/src/darwin/eok.c index f9465d29dd..e2f7c55284 100644 --- a/src/os/src/darwin/eok.c +++ b/src/os/src/darwin/eok.c @@ -25,6 +25,8 @@ #include #include +#define ENABLE_LOG + #ifdef ENABLE_LOG #define D(fmt, ...) fprintf(stderr, "%s[%d]%s(): " fmt "\n", basename(__FILE__), __LINE__, __func__, ##__VA_ARGS__) #define E(fmt, ...) do { \ @@ -582,9 +584,11 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) if (cnts==0) { // if no user-cared-events is up // we check to see if time is up - A(TIME_UTC==timespec_get(&now, TIME_UTC), "internal logic error"); - to = do_timespec_diff(&now, &abstime); - if (to.tv_sec==0 && to.tv_nsec==0) break; + if (timeout!=-1) { + A(TIME_UTC==timespec_get(&now, TIME_UTC), "internal logic error"); + to = do_timespec_diff(&now, &abstime); + if (to.tv_sec==0 && to.tv_nsec==0) break; + } // time is not up yet, continue loop } } while (cnts==0); From c5dfa590ec3bc5546e23d15357e92dda2156452c Mon Sep 17 00:00:00 2001 From: freemine Date: Sun, 17 Jan 2021 08:15:31 +0800 Subject: [PATCH 10/17] typo: by default, ENABLE_LOG is not defined --- src/os/src/darwin/eok.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/os/src/darwin/eok.c b/src/os/src/darwin/eok.c index e2f7c55284..a6dceb3df3 100644 --- a/src/os/src/darwin/eok.c +++ b/src/os/src/darwin/eok.c @@ -25,8 +25,6 @@ #include #include -#define ENABLE_LOG - #ifdef ENABLE_LOG #define D(fmt, ...) fprintf(stderr, "%s[%d]%s(): " fmt "\n", basename(__FILE__), __LINE__, __func__, ##__VA_ARGS__) #define E(fmt, ...) do { \ From 0c96f7653ec4d5c697ff6d504afc59bc40002fb2 Mon Sep 17 00:00:00 2001 From: freemine Date: Sun, 17 Jan 2021 21:40:39 +0800 Subject: [PATCH 11/17] 1. bugfix: eok.c: array of pointers rather than POD buffer 2. initial port of taosd for MacOSX --- cmake/define.inc | 2 + deps/CMakeLists.txt | 6 +- packaging/tools/install_client.sh | 2 +- packaging/tools/make_install.sh | 14 ++- src/balance/CMakeLists.txt | 4 + src/client/CMakeLists.txt | 22 +++++ src/cq/CMakeLists.txt | 12 ++- src/dnode/CMakeLists.txt | 41 +++++++- src/dnode/src/dnodeSystem.c | 4 + src/dnode/src/dnodeTelemetry.c | 7 ++ src/kit/shell/CMakeLists.txt | 13 ++- src/kit/taosdemo/CMakeLists.txt | 11 ++- src/kit/taosdemox/CMakeLists.txt | 34 +++++-- src/kit/taosdump/CMakeLists.txt | 12 ++- src/mnode/CMakeLists.txt | 13 ++- src/mnode/src/mnodeCluster.c | 7 ++ src/os/src/darwin/eok.c | 99 ++++++++++++++------ src/os/src/detail/CMakeLists.txt | 1 + src/plugins/http/CMakeLists.txt | 19 +++- src/plugins/http/src/httpServer.c | 46 +++++++++ src/plugins/monitor/CMakeLists.txt | 14 ++- src/plugins/mqtt/CMakeLists.txt | 12 +++ src/query/CMakeLists.txt | 5 + src/rpc/src/rpcTcp.c | 10 +- src/rpc/test/CMakeLists.txt | 14 +++ src/sync/CMakeLists.txt | 15 ++- src/sync/src/syncRetrieve.c | 2 +- src/sync/test/CMakeLists.txt | 1 - src/util/CMakeLists.txt | 3 +- src/vnode/CMakeLists.txt | 6 ++ src/wal/CMakeLists.txt | 8 +- src/wal/test/CMakeLists.txt | 8 ++ tests/comparisonTest/tdengine/CMakeLists.txt | 5 + tests/examples/c/CMakeLists.txt | 3 + tests/examples/c/demo.c | 2 +- 35 files changed, 413 insertions(+), 64 deletions(-) diff --git a/cmake/define.inc b/cmake/define.inc index 5d4d94ff42..ba12df6f0c 100755 --- a/cmake/define.inc +++ b/cmake/define.inc @@ -128,6 +128,8 @@ IF (TD_DARWIN_64) SET(COMMON_FLAGS "-std=gnu99 -Wall -Werror -Wno-missing-braces -fPIC -msse4.2 -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE") SET(DEBUG_FLAGS "-O0 -g3 -DDEBUG") SET(RELEASE_FLAGS "-Og") + INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/cJson/inc) + INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/lz4/inc) ENDIF () IF (TD_WINDOWS) diff --git a/deps/CMakeLists.txt b/deps/CMakeLists.txt index 1d725add21..1e59396c70 100644 --- a/deps/CMakeLists.txt +++ b/deps/CMakeLists.txt @@ -12,4 +12,8 @@ ADD_SUBDIRECTORY(MsvcLibX) IF (TD_LINUX AND TD_MQTT) ADD_SUBDIRECTORY(MQTT-C) -ENDIF () \ No newline at end of file +ENDIF () + +IF (TD_DARWIN AND TD_MQTT) + ADD_SUBDIRECTORY(MQTT-C) +ENDIF () diff --git a/packaging/tools/install_client.sh b/packaging/tools/install_client.sh index dd116e9bfb..84ca3b5131 100755 --- a/packaging/tools/install_client.sh +++ b/packaging/tools/install_client.sh @@ -21,7 +21,7 @@ else cd ${script_dir} script_dir="$(pwd)" data_dir="/var/lib/taos" - log_dir="~/TDengineLog" + log_dir=~/TDengineLog fi log_link_dir="/usr/local/taos/log" diff --git a/packaging/tools/make_install.sh b/packaging/tools/make_install.sh index 831012851a..0727fe2a1e 100755 --- a/packaging/tools/make_install.sh +++ b/packaging/tools/make_install.sh @@ -24,7 +24,7 @@ data_dir="/var/lib/taos" if [ "$osType" != "Darwin" ]; then log_dir="/var/log/taos" else - log_dir="~/TDengineLog" + log_dir=~/TDengineLog fi data_link_dir="/usr/local/taos/data" @@ -178,7 +178,9 @@ function install_bin() { function install_lib() { # Remove links ${csudo} rm -f ${lib_link_dir}/libtaos.* || : - ${csudo} rm -f ${lib64_link_dir}/libtaos.* || : + if [ "$osType" != "Darwin" ]; then + ${csudo} rm -f ${lib64_link_dir}/libtaos.* || : + fi if [ "$osType" != "Darwin" ]; then ${csudo} cp ${binary_dir}/build/lib/libtaos.so.${verNumber} ${install_main_dir}/driver && ${csudo} chmod 777 ${install_main_dir}/driver/* @@ -190,12 +192,14 @@ function install_lib() { ${csudo} ln -sf ${lib64_link_dir}/libtaos.so.1 ${lib64_link_dir}/libtaos.so fi else - ${csudo} cp ${binary_dir}/build/lib/libtaos.* ${install_main_dir}/driver && ${csudo} chmod 777 ${install_main_dir}/driver/* - ${csudo} ln -sf ${install_main_dir}/driver/libtaos.* ${lib_link_dir}/libtaos.1.dylib + ${csudo} cp -Rf ${binary_dir}/build/lib/libtaos.* ${install_main_dir}/driver && ${csudo} chmod 777 ${install_main_dir}/driver/* + ${csudo} ln -sf ${install_main_dir}/driver/libtaos.1.dylib ${lib_link_dir}/libtaos.1.dylib ${csudo} ln -sf ${lib_link_dir}/libtaos.1.dylib ${lib_link_dir}/libtaos.dylib fi - ${csudo} ldconfig + if [ "$osType" != "Darwin" ]; then + ${csudo} ldconfig + fi } function install_header() { diff --git a/src/balance/CMakeLists.txt b/src/balance/CMakeLists.txt index fdc43ea32f..a3ae74d825 100644 --- a/src/balance/CMakeLists.txt +++ b/src/balance/CMakeLists.txt @@ -11,3 +11,7 @@ AUX_SOURCE_DIRECTORY(src SRC) IF (TD_LINUX) ADD_LIBRARY(balance ${SRC}) ENDIF () + +IF (TD_DARWIN) + ADD_LIBRARY(balance ${SRC}) +ENDIF () diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index 3f8f9d2934..660ad564a5 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -28,6 +28,28 @@ IF (TD_LINUX) ADD_SUBDIRECTORY(tests) +ELSEIF (TD_DARWIN) + INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/jni/linux) + + # set the static lib name + ADD_LIBRARY(taos_static STATIC ${SRC}) + TARGET_LINK_LIBRARIES(taos_static common query trpc tutil pthread m) + SET_TARGET_PROPERTIES(taos_static PROPERTIES OUTPUT_NAME "taos_static") + SET_TARGET_PROPERTIES(taos_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) + + # generate dynamic library (*.dylib) + ADD_LIBRARY(taos SHARED ${SRC}) + TARGET_LINK_LIBRARIES(taos common query trpc tutil pthread m) + SET_TARGET_PROPERTIES(taos PROPERTIES CLEAN_DIRECT_OUTPUT 1) + + #set version of .dylib + #VERSION dylib version + #SOVERSION dylib version + #MESSAGE(STATUS "build version ${TD_VER_NUMBER}") + SET_TARGET_PROPERTIES(taos PROPERTIES VERSION ${TD_VER_NUMBER} SOVERSION 1) + + ADD_SUBDIRECTORY(tests) + ELSEIF (TD_WINDOWS) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/jni/windows) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/jni/windows/win32) diff --git a/src/cq/CMakeLists.txt b/src/cq/CMakeLists.txt index 9da831c9c1..34146241a5 100644 --- a/src/cq/CMakeLists.txt +++ b/src/cq/CMakeLists.txt @@ -10,7 +10,17 @@ IF (TD_LINUX) ADD_LIBRARY(tcq ${SRC}) IF (TD_SOMODE_STATIC) TARGET_LINK_LIBRARIES(tcq tutil common taos_static) - ELSE () + ELSE () + TARGET_LINK_LIBRARIES(tcq tutil common taos) + ENDIF () + ADD_SUBDIRECTORY(test) +ENDIF () + +IF (TD_DARWIN) + ADD_LIBRARY(tcq ${SRC}) + IF (TD_SOMODE_STATIC) + TARGET_LINK_LIBRARIES(tcq tutil common taos_static) + ELSE () TARGET_LINK_LIBRARIES(tcq tutil common taos) ENDIF () ADD_SUBDIRECTORY(test) diff --git a/src/dnode/CMakeLists.txt b/src/dnode/CMakeLists.txt index 699ca00a25..905ad9d920 100644 --- a/src/dnode/CMakeLists.txt +++ b/src/dnode/CMakeLists.txt @@ -23,7 +23,7 @@ IF (TD_LINUX) IF (TD_ACCOUNT) TARGET_LINK_LIBRARIES(taosd account) ENDIF () - + IF (TD_GRANT) TARGET_LINK_LIBRARIES(taosd grant) ENDIF () @@ -47,3 +47,42 @@ IF (TD_LINUX) COMMENT "prepare taosd environment") ADD_CUSTOM_TARGET(${PREPARE_ENV_TARGET} ALL WORKING_DIRECTORY ${TD_EXECUTABLE_OUTPUT_PATH} DEPENDS ${PREPARE_ENV_CMD}) ENDIF () + +IF (TD_DARWIN) + ADD_EXECUTABLE(taosd ${SRC}) + TARGET_LINK_LIBRARIES(taosd mnode monitor http tsdb twal vnode cJson lz4 balance sync) + + IF (TD_SOMODE_STATIC) + TARGET_LINK_LIBRARIES(taosd taos_static) + ELSE () + TARGET_LINK_LIBRARIES(taosd taos) + ENDIF () + + IF (TD_ACCOUNT) + TARGET_LINK_LIBRARIES(taosd account) + ENDIF () + + IF (TD_GRANT) + TARGET_LINK_LIBRARIES(taosd grant) + ENDIF () + + IF (TD_MQTT) + TARGET_LINK_LIBRARIES(taosd mqtt) + ENDIF () + + # SET(PREPARE_ENV_CMD "prepare_env_cmd") + # SET(PREPARE_ENV_TARGET "prepare_env_target") + # ADD_CUSTOM_COMMAND(OUTPUT ${PREPARE_ENV_CMD} + # POST_BUILD + # COMMAND echo "make test directory" + # DEPENDS taosd + # COMMAND ${CMAKE_COMMAND} -E make_directory ${TD_TESTS_OUTPUT_DIR}/cfg/ + # COMMAND ${CMAKE_COMMAND} -E make_directory ${TD_TESTS_OUTPUT_DIR}/log/ + # COMMAND ${CMAKE_COMMAND} -E make_directory ${TD_TESTS_OUTPUT_DIR}/data/ + # COMMAND ${CMAKE_COMMAND} -E echo dataDir ${TD_TESTS_OUTPUT_DIR}/data > ${TD_TESTS_OUTPUT_DIR}/cfg/taos.cfg + # COMMAND ${CMAKE_COMMAND} -E echo logDir ${TD_TESTS_OUTPUT_DIR}/log >> ${TD_TESTS_OUTPUT_DIR}/cfg/taos.cfg + # COMMAND ${CMAKE_COMMAND} -E echo charset UTF-8 >> ${TD_TESTS_OUTPUT_DIR}/cfg/taos.cfg + # COMMENT "prepare taosd environment") + # ADD_CUSTOM_TARGET(${PREPARE_ENV_TARGET} ALL WORKING_DIRECTORY ${TD_EXECUTABLE_OUTPUT_PATH} DEPENDS ${PREPARE_ENV_CMD}) +ENDIF () + diff --git a/src/dnode/src/dnodeSystem.c b/src/dnode/src/dnodeSystem.c index a4d7e791e6..893049df2e 100644 --- a/src/dnode/src/dnodeSystem.c +++ b/src/dnode/src/dnodeSystem.c @@ -160,7 +160,11 @@ static void signal_handler(int32_t signum, siginfo_t *sigInfo, void *context) { syslog(LOG_INFO, "Shut down signal is %d", signum); syslog(LOG_INFO, "Shutting down TDengine service..."); // clean the system. +#ifdef __APPLE__ + dInfo("shut down signal is %d, sender PID:%d", signum, sigInfo->si_pid); +#else // __APPLE__ dInfo("shut down signal is %d, sender PID:%d cmdline:%s", signum, sigInfo->si_pid, taosGetCmdlineByPID(sigInfo->si_pid)); +#endif // protect the application from receive another signal struct sigaction act = {{0}}; diff --git a/src/dnode/src/dnodeTelemetry.c b/src/dnode/src/dnodeTelemetry.c index ff9598ecc5..1f7084c3b5 100644 --- a/src/dnode/src/dnodeTelemetry.c +++ b/src/dnode/src/dnodeTelemetry.c @@ -236,6 +236,13 @@ static void sendTelemetryReport() { taosCloseSocket(fd); } +#ifdef __APPLE__ +static int sem_timedwait(tsem_t *sem, struct timespec *to) { + fprintf(stderr, "%s[%d]%s(): not implemented yet!\n", basename(__FILE__), __LINE__, __func__); + abort(); +} +#endif + static void* telemetryThread(void* param) { struct timespec end = {0}; clock_gettime(CLOCK_REALTIME, &end); diff --git a/src/kit/shell/CMakeLists.txt b/src/kit/shell/CMakeLists.txt index 45da99e572..c4f3cc5696 100644 --- a/src/kit/shell/CMakeLists.txt +++ b/src/kit/shell/CMakeLists.txt @@ -9,14 +9,14 @@ IF (TD_LINUX) AUX_SOURCE_DIRECTORY(./src SRC) LIST(REMOVE_ITEM SRC ./src/shellWindows.c) LIST(REMOVE_ITEM SRC ./src/shellDarwin.c) - ADD_EXECUTABLE(shell ${SRC}) - + ADD_EXECUTABLE(shell ${SRC}) + IF (TD_SOMODE_STATIC) TARGET_LINK_LIBRARIES(shell taos_static) ELSE () TARGET_LINK_LIBRARIES(shell taos) ENDIF () - + SET_TARGET_PROPERTIES(shell PROPERTIES OUTPUT_NAME taos) ELSEIF (TD_WINDOWS) LIST(APPEND SRC ./src/shellEngine.c) @@ -27,7 +27,7 @@ ELSEIF (TD_WINDOWS) IF (TD_POWER) SET_TARGET_PROPERTIES(shell PROPERTIES OUTPUT_NAME power) - ELSE () + ELSE () SET_TARGET_PROPERTIES(shell PROPERTIES OUTPUT_NAME taos) ENDIF () ELSEIF (TD_DARWIN) @@ -37,7 +37,10 @@ ELSEIF (TD_DARWIN) LIST(APPEND SRC ./src/shellCommand.c) LIST(APPEND SRC ./src/shellImport.c) ADD_EXECUTABLE(shell ${SRC}) - TARGET_LINK_LIBRARIES(shell taos_static) + # linking with dylib + TARGET_LINK_LIBRARIES(shell taos) + # linking taos statically + # TARGET_LINK_LIBRARIES(shell taos_static) SET_TARGET_PROPERTIES(shell PROPERTIES OUTPUT_NAME taos) ENDIF () diff --git a/src/kit/taosdemo/CMakeLists.txt b/src/kit/taosdemo/CMakeLists.txt index 91c743939c..f74dbc2de4 100644 --- a/src/kit/taosdemo/CMakeLists.txt +++ b/src/kit/taosdemo/CMakeLists.txt @@ -7,7 +7,7 @@ INCLUDE_DIRECTORIES(inc) IF (TD_LINUX) AUX_SOURCE_DIRECTORY(. SRC) ADD_EXECUTABLE(taosdemo ${SRC}) - + IF (TD_SOMODE_STATIC) TARGET_LINK_LIBRARIES(taosdemo taos_static) ELSE () @@ -17,4 +17,13 @@ ELSEIF (TD_WINDOWS) AUX_SOURCE_DIRECTORY(. SRC) ADD_EXECUTABLE(taosdemo ${SRC}) TARGET_LINK_LIBRARIES(taosdemo taos_static) +ELSEIF (TD_DARWIN) + AUX_SOURCE_DIRECTORY(. SRC) + ADD_EXECUTABLE(taosdemo ${SRC}) + + IF (TD_SOMODE_STATIC) + TARGET_LINK_LIBRARIES(taosdemo taos_static) + ELSE () + TARGET_LINK_LIBRARIES(taosdemo taos) + ENDIF () ENDIF () diff --git a/src/kit/taosdemox/CMakeLists.txt b/src/kit/taosdemox/CMakeLists.txt index 7db6c04b28..3993cb0feb 100644 --- a/src/kit/taosdemox/CMakeLists.txt +++ b/src/kit/taosdemox/CMakeLists.txt @@ -4,22 +4,44 @@ PROJECT(TDengine) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/client/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/libcurl/include) -IF (TD_LINUX) +IF (TD_LINUX) AUX_SOURCE_DIRECTORY(. SRC) ADD_EXECUTABLE(taosdemox ${SRC}) - - #find_program(HAVE_CURL NAMES curl) - IF ((NOT TD_ARM_64) AND (NOT TD_ARM_32)) + + #find_program(HAVE_CURL NAMES curl) + IF ((NOT TD_ARM_64) AND (NOT TD_ARM_32)) ADD_DEFINITIONS(-DTD_LOWA_CURL) LINK_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/libcurl/lib) ADD_LIBRARY(curl STATIC IMPORTED) SET_PROPERTY(TARGET curl PROPERTY IMPORTED_LOCATION ${TD_COMMUNITY_DIR}/deps/libcurl/lib/libcurl.a) - TARGET_LINK_LIBRARIES(taosdemox curl) + TARGET_LINK_LIBRARIES(taosdemox curl) ENDIF () - + IF (TD_SOMODE_STATIC) TARGET_LINK_LIBRARIES(taosdemox taos_static cJson) ELSE () TARGET_LINK_LIBRARIES(taosdemox taos cJson) ENDIF () ENDIF () + +IF (TD_DARWIN) + # missing a few dependencies, such as + # AUX_SOURCE_DIRECTORY(. SRC) + # ADD_EXECUTABLE(taosdemox ${SRC}) + # + # #find_program(HAVE_CURL NAMES curl) + # IF ((NOT TD_ARM_64) AND (NOT TD_ARM_32)) + # ADD_DEFINITIONS(-DTD_LOWA_CURL) + # LINK_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/libcurl/lib) + # ADD_LIBRARY(curl STATIC IMPORTED) + # SET_PROPERTY(TARGET curl PROPERTY IMPORTED_LOCATION ${TD_COMMUNITY_DIR}/deps/libcurl/lib/libcurl.a) + # TARGET_LINK_LIBRARIES(taosdemox curl) + # ENDIF () + # + # IF (TD_SOMODE_STATIC) + # TARGET_LINK_LIBRARIES(taosdemox taos_static cJson) + # ELSE () + # TARGET_LINK_LIBRARIES(taosdemox taos cJson) + # ENDIF () +ENDIF () + diff --git a/src/kit/taosdump/CMakeLists.txt b/src/kit/taosdump/CMakeLists.txt index dc9ac6d4a7..b50ad85c08 100644 --- a/src/kit/taosdump/CMakeLists.txt +++ b/src/kit/taosdump/CMakeLists.txt @@ -12,5 +12,15 @@ IF (TD_LINUX) TARGET_LINK_LIBRARIES(taosdump taos_static) ELSE () TARGET_LINK_LIBRARIES(taosdump taos) - ENDIF () + ENDIF () +ENDIF () + +IF (TD_DARWIN) + # missing for macosx + # ADD_EXECUTABLE(taosdump ${SRC}) + # IF (TD_SOMODE_STATIC) + # TARGET_LINK_LIBRARIES(taosdump taos_static) + # ELSE () + # TARGET_LINK_LIBRARIES(taosdump taos) + # ENDIF () ENDIF () diff --git a/src/mnode/CMakeLists.txt b/src/mnode/CMakeLists.txt index 4123098694..ae4a37320c 100644 --- a/src/mnode/CMakeLists.txt +++ b/src/mnode/CMakeLists.txt @@ -9,4 +9,15 @@ IF (TD_LINUX) AUX_SOURCE_DIRECTORY(src SRC) ADD_LIBRARY(mnode ${SRC}) -ENDIF () \ No newline at end of file +ENDIF () + +IF (TD_DARWIN) + INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/query/inc) + INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/dnode/inc) + + INCLUDE_DIRECTORIES(inc) + AUX_SOURCE_DIRECTORY(src SRC) + + ADD_LIBRARY(mnode ${SRC}) +ENDIF () + diff --git a/src/mnode/src/mnodeCluster.c b/src/mnode/src/mnodeCluster.c index a35e304810..08bf20e1a8 100644 --- a/src/mnode/src/mnodeCluster.c +++ b/src/mnode/src/mnodeCluster.c @@ -138,6 +138,13 @@ void mnodeDecClusterRef(SClusterObj *pCluster) { sdbDecRef(tsClusterSdb, pCluster); } +#ifdef __APPLE__ +bool taosGetSystemUid(char *uid) { + fprintf(stderr, "%s[%d]%s(): not implemented yet!\n", basename(__FILE__), __LINE__, __func__); + abort(); + return false; +} +#endif // __APPLE__ static int32_t mnodeCreateCluster() { int32_t numOfClusters = sdbGetNumOfRows(tsClusterSdb); if (numOfClusters != 0) return TSDB_CODE_SUCCESS; diff --git a/src/os/src/darwin/eok.c b/src/os/src/darwin/eok.c index a6dceb3df3..1be5ec4ac1 100644 --- a/src/os/src/darwin/eok.c +++ b/src/os/src/darwin/eok.c @@ -103,9 +103,9 @@ struct eok_event_s { typedef struct eoks_s eoks_t; struct eoks_s { pthread_mutex_t lock; - ep_over_kq_t *eoks; - int neoks; - ep_over_kq_t *eoks_free; + ep_over_kq_t **eoks; // note: this memory leaks when process terminates + int neoks; // we can add an extra api to let user clean + ep_over_kq_t *eoks_free; // currently, we just keep it simple stupid }; static eoks_t eoks = { @@ -297,7 +297,7 @@ int epoll_create(int size) { struct epoll_event ev = {0}; ev.events = EPOLLIN; ev.data.ptr = &eok_dummy; - D("epoll_create epfd:[%d]", eok->idx); + D("epoll_create epfd:[%d] and sv0[%d]", eok->idx, eok->sv[0]); if (epoll_ctl(eok->idx, EPOLL_CTL_ADD, eok->sv[0], &ev)) { e = errno; epoll_close(eok->idx); @@ -517,8 +517,8 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) eok_event_t *ev = (eok_event_t*)kev->udata; A(kev->ident == ev->fd, "internal logic error"); if (kev->flags & EV_ERROR) { - D("error when processing change list for fd[%d], error[%s], kev_flags:[%04x:%s]", - ev->fd, strerror(kev->data), kev->flags, kev_flags_str(kev->flags, 0)); + D("epfd[%d] error when processing change list for fd[%d], error[%s], kev_flags:[%04x:%s]", + epfd, ev->fd, strerror(kev->data), kev->flags, kev_flags_str(kev->flags, 0)); } switch (kev->filter) { case EVFILT_READ: { @@ -528,11 +528,11 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) char c = '\0'; A(1==recv(kev->ident, &c, 1, 0), "internal logic error"); A(0==memcmp(&c, "1", 1), "internal logic error"); - D("wokenup"); + D("epfd[%d] wokenup", epfd); continue; } else { if (ev->changed==3) { - D("already requested to delete for fd[%d]", ev->fd); + D("epfd[%d] already requested to delete for fd[%d]", epfd, ev->fd); // TODO: write a unit test for this case // EV_DELETE? continue; @@ -550,7 +550,8 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) } // rounded to what user care pev.events = pev.events & ev->epev.events; - D("events found for fd[%d]: [%04x:%s], which was registered: [%04x:%s], kev_flags: [%04x:%s]", + D("epfd[%d] events found for fd[%d]: [%04x:%s], which was registered: [%04x:%s], kev_flags: [%04x:%s]", + epfd, ev->fd, pev.events, events_str(pev.events, 0), ev->epev.events, events_str(ev->epev.events, 1), kev->flags, kev_flags_str(kev->flags, 2)); @@ -573,7 +574,7 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) while (p) { eok_event_t *next = p->next; if (p->changed==3) { - D("removing registered event for fd[%d]: [%04x:%s]", p->fd, p->epev.events, events_str(p->epev.events, 0)); + D("epfd[%d] removing registered event for fd[%d]: [%04x:%s]", epfd, p->fd, p->epev.events, events_str(p->epev.events, 0)); eok_free_ev(eok, p); } p = next; @@ -591,13 +592,13 @@ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) } } while (cnts==0); if (cnts>0) { - D("kevent64 waiting done with [%d] events", cnts); + D("kevent64 epfd[%d] waiting done with [%d] events", epfd, cnts); } A(0==pthread_mutex_unlock(&eok->lock), ""); if (e) { errno = e; - E("epoll_wait failed"); + E("epfd[%d] epoll_wait failed", epfd); return -1; } @@ -752,7 +753,7 @@ static int eok_chgs_refresh(ep_over_kq_t *eok, eok_event_t *oev, eok_event_t *ev eok->kchanges[eok->ichanges++] = *kwev; ++n; } - D("add #changes[%d] for fd[%d], and now #changes/registers [%d/%d]", n, ev->fd, eok->ichanges, eok->evs_count); + D("epfd[%d]: add #changes[%d] for fd[%d], and now #changes/registers [%d/%d]", eok->idx, n, ev->fd, eok->ichanges, eok->evs_count); return 0; } @@ -764,14 +765,21 @@ static ep_over_kq_t* eoks_alloc(void) { if (eoks.eoks_free) { eok = eoks.eoks_free; eoks.eoks_free = eok->next; + A(eoks.eoks, "internal logic error"); + A(eok->idx>=0 && eok->idxidx)==NULL, "internal logic error"); + *(eoks.eoks + eok->idx) = eok; eok->next = NULL; + eok->stopping = 0; break; } - ep_over_kq_t *p = (ep_over_kq_t*)realloc(eoks.eoks, sizeof(*p) * (eoks.neoks+1)); - if (!p) break; - eoks.eoks = p; - eok = eoks.eoks + eoks.neoks; - memset(eok, 0, sizeof(*eok)); + eok = (ep_over_kq_t*)calloc(1, sizeof(*eok)); + if (!eok) break; + eok->idx = -1; + ep_over_kq_t **ar = (ep_over_kq_t**)realloc(eoks.eoks, sizeof(**ar) * (eoks.neoks+1)); + if (!ar) break; + eoks.eoks = ar; + *(eoks.eoks + eoks.neoks) = eok; eok->idx = eoks.neoks; eok->kq = -1; eok->sv[0] = -1; @@ -784,6 +792,11 @@ static ep_over_kq_t* eoks_alloc(void) { errno = ENOMEM; return NULL; } + if (eok->idx==-1) { + free(eok); + errno = ENOMEM; + return NULL; + } if (eok->lock_valid) { return eok; } @@ -801,23 +814,50 @@ static ep_over_kq_t* eoks_alloc(void) { static void eoks_free(ep_over_kq_t *eok) { A(0==pthread_mutex_lock(&eoks.lock), ""); do { + A(eok->idx>=0 && eok->idxnext==NULL, "internal logic error"); // leave eok->kchanges as is A(eok->ichanges==0, "internal logic error"); A(eok->waiting==0, "internal logic error"); - if (eok->evs_count==1) { - A(eok->evs_head && eok->evs_tail && eok->evs_head==eok->evs_tail, "internal logic error"); - A(eok->evs_head->fd==eok->sv[0] && eok->sv[0]!=-1 && eok->sv[1]!=-1, "internal logic error"); - // fd is critical system resource - close(eok->sv[0]); - eok->sv[0] = -1; - close(eok->sv[1]); - eok->sv[1] = -1; - eok_free_ev(eok, eok->evs_head); + eok_event_t *ev = eok->evs_head; + while (ev) { + eok_event_t *next = ev->next; + if (ev->fd==eok->sv[0]) { + // fd is critical system resource + close(eok->sv[0]); + eok->sv[0] = -1; + close(eok->sv[1]); + eok->sv[1] = -1; + eok_free_ev(eok, ev); + } else { + // user forget calling epoll_ctl(EPOLL_CTL_DEL) before calling epoll_close/close? + // calling close(ev->fd) here smells really bad +#ifdef LET_IT_BE + // we just let it be and reclaim ev + eok_free_ev(eok, ev); +#else + // panic otherwise, if LET_IT_BE not defined + A(eok->evs_head==NULL && eok->evs_tail==NULL && eok->evs_count==0, + "epfd[%d] fd[%d]: internal logic error: have you epoll_ctl(EPOLL_CTL_DEL) everything before calling epoll_close?", + eok->idx, ev->fd); +#endif + } + ev = next; } - A(eok->evs_head==NULL && eok->evs_tail==NULL && eok->evs_count==0, "internal logic error"); + // if (eok->evs_count==1) { + // A(eok->evs_head && eok->evs_tail && eok->evs_head==eok->evs_tail, "internal logic error"); + // A(eok->evs_head->fd==eok->sv[0] && eok->sv[0]!=-1 && eok->sv[1]!=-1, "internal logic error"); + // // fd is critical system resource + // close(eok->sv[0]); + // eok->sv[0] = -1; + // close(eok->sv[1]); + // eok->sv[1] = -1; + // eok_free_ev(eok, eok->evs_head); + // } + A(eok->evs_head==NULL && eok->evs_tail==NULL && eok->evs_count==0, + "internal logic error: have you epoll_ctl(EPOLL_CTL_DEL) everything before calling epoll_close?"); A(eok->sv[0]==-1 && eok->sv[1]==-1, "internal logic error"); if (eok->kq!=-1) { close(eok->kq); @@ -825,6 +865,7 @@ static void eoks_free(ep_over_kq_t *eok) { } eok->next = eoks.eoks_free; eoks.eoks_free = eok; + *(eoks.eoks + eok->idx) = NULL; } while (0); A(0==pthread_mutex_unlock(&eoks.lock), ""); } @@ -837,7 +878,7 @@ static ep_over_kq_t* eoks_find(int epfd) { break; } A(eoks.eoks, "internal logic error"); - eok = eoks.eoks + epfd; + eok = *(eoks.eoks + epfd); A(eok->next==NULL, "internal logic error"); A(eok->lock_valid, "internal logic error"); } while (0); diff --git a/src/os/src/detail/CMakeLists.txt b/src/os/src/detail/CMakeLists.txt index e4052a4fb3..1c5e55a522 100644 --- a/src/os/src/detail/CMakeLists.txt +++ b/src/os/src/detail/CMakeLists.txt @@ -13,3 +13,4 @@ TARGET_LINK_LIBRARIES(osdetail os) IF (TD_ARM_32 OR TD_LINUX_32) TARGET_LINK_LIBRARIES(osdetail atomic) ENDIF () + diff --git a/src/plugins/http/CMakeLists.txt b/src/plugins/http/CMakeLists.txt index 56c3c25d7c..274f50d24a 100644 --- a/src/plugins/http/CMakeLists.txt +++ b/src/plugins/http/CMakeLists.txt @@ -8,14 +8,29 @@ INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/client/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/query/inc) INCLUDE_DIRECTORIES(inc) AUX_SOURCE_DIRECTORY(src SRC) - + IF (TD_LINUX) ADD_LIBRARY(http ${SRC}) TARGET_LINK_LIBRARIES(http z) IF (TD_SOMODE_STATIC) TARGET_LINK_LIBRARIES(http taos_static) - ELSE () + ELSE () + TARGET_LINK_LIBRARIES(http taos) + ENDIF () + + IF (TD_ADMIN) + TARGET_LINK_LIBRARIES(http admin) + ENDIF () +ENDIF () + +IF (TD_DARWIN) + ADD_LIBRARY(http ${SRC}) + TARGET_LINK_LIBRARIES(http z) + + IF (TD_SOMODE_STATIC) + TARGET_LINK_LIBRARIES(http taos_static) + ELSE () TARGET_LINK_LIBRARIES(http taos) ENDIF () diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index 1cc73aef06..97a5b4bc23 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -31,11 +31,36 @@ static bool httpReadData(HttpContext *pContext); +#ifdef __APPLE__ +static int sv_dummy = 0; +#endif + static void httpStopThread(HttpThread* pThread) { pThread->stop = true; // signal the thread to stop, try graceful method first, // and use pthread_cancel when failed +#ifdef __APPLE__ + int sv[2]; + sv[0] = sv[1] = -1; + int r = socketpair(PF_LOCAL, SOCK_STREAM, 0, sv); + do { + if (r) break; + struct epoll_event ev = {0}; + ev.events = EPOLLIN; + ev.data.ptr = &sv_dummy; + pThread->stop = true; + r = epoll_ctl(pThread->pollFd, EPOLL_CTL_ADD, sv[0], &ev); + if (r) break; + if (1!=send(sv[1], "1", 1, 0)) { + r = -1; + break; + } + } while (0); + if (r) { + pthread_cancel(pThread->thread); + } +#else // __APPLE__ struct epoll_event event = { .events = EPOLLIN }; eventfd_t fd = eventfd(1, 0); if (fd == -1) { @@ -46,11 +71,23 @@ static void httpStopThread(HttpThread* pThread) { httpError("%s, failed to call epoll_ctl, will call pthread_cancel instead, which may result in data corruption: %s", pThread->label, strerror(errno)); pthread_cancel(pThread->thread); } +#endif // __APPLE__ pthread_join(pThread->thread, NULL); +#ifdef __APPLE__ + if (sv[0]!=-1) { + close(sv[0]); + sv[0] = -1; + } + if (sv[1]!=-1) { + close(sv[1]); + sv[1] = -1; + } +#else // __APPLE__ if (fd != -1) { close(fd); } +#endif // __APPLE__ #ifdef __APPLE__ epoll_close(pThread->pollFd); @@ -97,6 +134,15 @@ static void httpProcessHttpData(void *param) { if (fdNum <= 0) continue; for (int32_t i = 0; i < fdNum; ++i) { +#ifdef __APPLE__ + if (events[i].data.ptr == &sv_dummy) { + // no need to drain the recv buffer of sv[0] + // since there's only one time to send at most 1 byte to sv[0] + // btw, pThread->stop shall be already set, thus never reached here + httpDebug("if you see this line, there's internal logic error"); + continue; + } +#endif // __APPLE__ pContext = httpGetContext(events[i].data.ptr); if (pContext == NULL) { httpError("context:%p, is already released, close connect", events[i].data.ptr); diff --git a/src/plugins/monitor/CMakeLists.txt b/src/plugins/monitor/CMakeLists.txt index 90189c9d75..6b26c0447a 100644 --- a/src/plugins/monitor/CMakeLists.txt +++ b/src/plugins/monitor/CMakeLists.txt @@ -8,10 +8,20 @@ AUX_SOURCE_DIRECTORY(./src SRC) IF (TD_LINUX) ADD_LIBRARY(monitor ${SRC}) - + IF (TD_SOMODE_STATIC) TARGET_LINK_LIBRARIES(monitor taos_static) ELSE () TARGET_LINK_LIBRARIES(monitor taos) ENDIF () -ENDIF () +ENDIF () + +IF (TD_DARWIN) + ADD_LIBRARY(monitor ${SRC}) + + IF (TD_SOMODE_STATIC) + TARGET_LINK_LIBRARIES(monitor taos_static) + ELSE () + TARGET_LINK_LIBRARIES(monitor taos) + ENDIF () +ENDIF () diff --git a/src/plugins/mqtt/CMakeLists.txt b/src/plugins/mqtt/CMakeLists.txt index 3761f70134..b6de421517 100644 --- a/src/plugins/mqtt/CMakeLists.txt +++ b/src/plugins/mqtt/CMakeLists.txt @@ -18,3 +18,15 @@ IF (TD_LINUX) TARGET_LINK_LIBRARIES(mqtt taos) ENDIF () ENDIF () + +IF (TD_DARWIN) + ADD_LIBRARY(mqtt ${SRC}) + TARGET_LINK_LIBRARIES(mqtt cJson mqttc) + + IF (TD_SOMODE_STATIC) + TARGET_LINK_LIBRARIES(mqtt taos_static) + ELSE () + TARGET_LINK_LIBRARIES(mqtt taos) + ENDIF () +ENDIF () + diff --git a/src/query/CMakeLists.txt b/src/query/CMakeLists.txt index e403251858..967e86de3c 100644 --- a/src/query/CMakeLists.txt +++ b/src/query/CMakeLists.txt @@ -14,3 +14,8 @@ IF (TD_LINUX) TARGET_LINK_LIBRARIES(query m rt) ADD_SUBDIRECTORY(tests) ENDIF () + +IF (TD_DARWIN) + TARGET_LINK_LIBRARIES(query m) + ADD_SUBDIRECTORY(tests) +ENDIF () diff --git a/src/rpc/src/rpcTcp.c b/src/rpc/src/rpcTcp.c index 2bdc58390d..ba90f93073 100644 --- a/src/rpc/src/rpcTcp.c +++ b/src/rpc/src/rpcTcp.c @@ -308,7 +308,10 @@ void *taosInitTcpClient(uint32_t ip, uint16_t port, char *label, int num, void * pthread_attr_destroy(&thattr); if (code != 0) { #ifdef __APPLE__ - epoll_close(pThreadObj->pollFd); + if (pThreadObj->pollFd!=-1) { + epoll_close(pThreadObj->pollFd); + pThreadObj->pollFd = -1; + } #else taosCloseSocket(pThreadObj->pollFd); #endif @@ -517,7 +520,10 @@ static void *taosProcessTcpData(void *param) { } #ifdef __APPLE__ - if (pThreadObj->pollFd >=0) epoll_close(pThreadObj->pollFd); + if (pThreadObj->pollFd >=0) { + epoll_close(pThreadObj->pollFd); + pThreadObj->pollFd = -1; + } #else if (pThreadObj->pollFd >=0) taosCloseSocket(pThreadObj->pollFd); #endif diff --git a/src/rpc/test/CMakeLists.txt b/src/rpc/test/CMakeLists.txt index 9a4bcc353d..e923105860 100644 --- a/src/rpc/test/CMakeLists.txt +++ b/src/rpc/test/CMakeLists.txt @@ -16,3 +16,17 @@ IF (TD_LINUX) ADD_EXECUTABLE(rserver ${SERVER_SRC}) TARGET_LINK_LIBRARIES(rserver trpc) ENDIF () + +IF (TD_DARWIN) + LIST(APPEND CLIENT_SRC ./rclient.c) + ADD_EXECUTABLE(rclient ${CLIENT_SRC}) + TARGET_LINK_LIBRARIES(rclient trpc) + + LIST(APPEND SCLIENT_SRC ./rsclient.c) + ADD_EXECUTABLE(rsclient ${SCLIENT_SRC}) + TARGET_LINK_LIBRARIES(rsclient trpc) + + LIST(APPEND SERVER_SRC ./rserver.c) + ADD_EXECUTABLE(rserver ${SERVER_SRC}) + TARGET_LINK_LIBRARIES(rserver trpc) +ENDIF () diff --git a/src/sync/CMakeLists.txt b/src/sync/CMakeLists.txt index 6a53380841..3721163f79 100644 --- a/src/sync/CMakeLists.txt +++ b/src/sync/CMakeLists.txt @@ -3,7 +3,7 @@ PROJECT(TDengine) INCLUDE_DIRECTORIES(inc) AUX_SOURCE_DIRECTORY(src SRC) - + IF (TD_LINUX) LIST(REMOVE_ITEM SRC src/syncArbitrator.c) ADD_LIBRARY(sync ${SRC}) @@ -16,3 +16,16 @@ IF (TD_LINUX) #ADD_SUBDIRECTORY(test) ENDIF () + +IF (TD_DARWIN) + LIST(REMOVE_ITEM SRC src/syncArbitrator.c) + ADD_LIBRARY(sync ${SRC}) + TARGET_LINK_LIBRARIES(sync tutil pthread common) + + LIST(APPEND BIN_SRC src/syncArbitrator.c) + LIST(APPEND BIN_SRC src/syncTcp.c) + ADD_EXECUTABLE(tarbitrator ${BIN_SRC}) + TARGET_LINK_LIBRARIES(tarbitrator sync common osdetail tutil) + + #ADD_SUBDIRECTORY(test) +ENDIF () diff --git a/src/sync/src/syncRetrieve.c b/src/sync/src/syncRetrieve.c index cb2379583f..7678a7d284 100644 --- a/src/sync/src/syncRetrieve.c +++ b/src/sync/src/syncRetrieve.c @@ -14,7 +14,7 @@ */ #define _DEFAULT_SOURCE -#include +// #include #include "os.h" #include "taoserror.h" #include "tlog.h" diff --git a/src/sync/test/CMakeLists.txt b/src/sync/test/CMakeLists.txt index 256e87580d..ab2e6c307b 100644 --- a/src/sync/test/CMakeLists.txt +++ b/src/sync/test/CMakeLists.txt @@ -13,4 +13,3 @@ IF (TD_LINUX) TARGET_LINK_LIBRARIES(syncServer sync trpc common) ENDIF () - diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 48b4d76561..3606aea76b 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -5,7 +5,7 @@ INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/rpc/inc) AUX_SOURCE_DIRECTORY(src SRC) ADD_LIBRARY(tutil ${SRC}) TARGET_LINK_LIBRARIES(tutil pthread osdetail lz4 z) - + IF (TD_LINUX) TARGET_LINK_LIBRARIES(tutil m rt) # ADD_SUBDIRECTORY(tests) @@ -28,5 +28,6 @@ IF (TD_LINUX) ELSEIF (TD_WINDOWS) TARGET_LINK_LIBRARIES(tutil iconv regex winmm IPHLPAPI ws2_32 wepoll) ELSEIF(TD_DARWIN) + TARGET_LINK_LIBRARIES(tutil m) TARGET_LINK_LIBRARIES(tutil iconv) ENDIF() diff --git a/src/vnode/CMakeLists.txt b/src/vnode/CMakeLists.txt index c953883361..3a26172411 100644 --- a/src/vnode/CMakeLists.txt +++ b/src/vnode/CMakeLists.txt @@ -13,3 +13,9 @@ IF (TD_LINUX) ADD_LIBRARY(vnode ${SRC}) TARGET_LINK_LIBRARIES(vnode tsdb tcq) ENDIF () + +IF (TD_DARWIN) + ADD_LIBRARY(vnode ${SRC}) + TARGET_LINK_LIBRARIES(vnode tsdb tcq) +ENDIF () + diff --git a/src/wal/CMakeLists.txt b/src/wal/CMakeLists.txt index 681bed5425..d3cd86df50 100644 --- a/src/wal/CMakeLists.txt +++ b/src/wal/CMakeLists.txt @@ -8,4 +8,10 @@ IF (TD_LINUX) ADD_LIBRARY(twal ${SRC}) TARGET_LINK_LIBRARIES(twal tutil common) ADD_SUBDIRECTORY(test) -ENDIF () +ENDIF () + +IF (TD_DARWIN) + ADD_LIBRARY(twal ${SRC}) + TARGET_LINK_LIBRARIES(twal tutil common) + ADD_SUBDIRECTORY(test) +ENDIF () diff --git a/src/wal/test/CMakeLists.txt b/src/wal/test/CMakeLists.txt index b8338b1738..aec0602ac0 100644 --- a/src/wal/test/CMakeLists.txt +++ b/src/wal/test/CMakeLists.txt @@ -10,4 +10,12 @@ IF (TD_LINUX) ENDIF () +IF (TD_DARWIN) + INCLUDE_DIRECTORIES(../inc) + + LIST(APPEND WALTEST_SRC ./waltest.c) + ADD_EXECUTABLE(waltest ${WALTEST_SRC}) + TARGET_LINK_LIBRARIES(waltest twal osdetail tutil) + +ENDIF () diff --git a/tests/comparisonTest/tdengine/CMakeLists.txt b/tests/comparisonTest/tdengine/CMakeLists.txt index aaa18592ed..a12e36ab6b 100644 --- a/tests/comparisonTest/tdengine/CMakeLists.txt +++ b/tests/comparisonTest/tdengine/CMakeLists.txt @@ -5,3 +5,8 @@ IF (TD_LINUX) add_executable(tdengineTest tdengineTest.c) target_link_libraries(tdengineTest taos_static tutil common pthread) ENDIF() + +IF (TD_DARWIN) + add_executable(tdengineTest tdengineTest.c) + target_link_libraries(tdengineTest taos_static tutil common pthread) +ENDIF() diff --git a/tests/examples/c/CMakeLists.txt b/tests/examples/c/CMakeLists.txt index 7cacefda57..5af7935c6b 100644 --- a/tests/examples/c/CMakeLists.txt +++ b/tests/examples/c/CMakeLists.txt @@ -10,6 +10,9 @@ IF (TD_LINUX) ENDIF () IF (TD_DARWIN) INCLUDE_DIRECTORIES(. ${TD_COMMUNITY_DIR}/src/inc ${TD_COMMUNITY_DIR}/src/client/inc ${TD_COMMUNITY_DIR}/inc) + AUX_SOURCE_DIRECTORY(. SRC) + ADD_EXECUTABLE(demo demo.c) + TARGET_LINK_LIBRARIES(demo taos_static trpc tutil pthread ) ADD_EXECUTABLE(epoll epoll.c) TARGET_LINK_LIBRARIES(epoll taos_static trpc tutil pthread ) ENDIF () diff --git a/tests/examples/c/demo.c b/tests/examples/c/demo.c index 54e81d33b9..f5ad37acef 100644 --- a/tests/examples/c/demo.c +++ b/tests/examples/c/demo.c @@ -86,7 +86,7 @@ void Test(TAOS *taos, char *qstr, int index) { int i = 0; for (i = 0; i < 10; ++i) { - sprintf(qstr, "insert into m1 values (%" PRId64 ", %d, %d, %d, %d, %f, %lf, '%s')", 1546300800000 + i * 1000, i, i, i, i*10000000, i*1.0, i*2.0, "hello"); + sprintf(qstr, "insert into m1 values (%" PRId64 ", %d, %d, %d, %d, %f, %lf, '%s')", 1546300800000LL + i * 1000, i, i, i, i*10000000, i*1.0, i*2.0, "hello"); printf("qstr: %s\n", qstr); // note: how do you wanna do if taos_query returns non-NULL From 0ef43ca365355fdc40c7c333a26bebc254593b63 Mon Sep 17 00:00:00 2001 From: freemine Date: Sun, 17 Jan 2021 21:53:02 +0800 Subject: [PATCH 12/17] literal big integer --- tests/examples/c/demo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/examples/c/demo.c b/tests/examples/c/demo.c index f5ad37acef..9ad3e6f902 100644 --- a/tests/examples/c/demo.c +++ b/tests/examples/c/demo.c @@ -86,7 +86,7 @@ void Test(TAOS *taos, char *qstr, int index) { int i = 0; for (i = 0; i < 10; ++i) { - sprintf(qstr, "insert into m1 values (%" PRId64 ", %d, %d, %d, %d, %f, %lf, '%s')", 1546300800000LL + i * 1000, i, i, i, i*10000000, i*1.0, i*2.0, "hello"); + sprintf(qstr, "insert into m1 values (%" PRId64 ", %d, %d, %d, %d, %f, %lf, '%s')", (uint64_t)(1546300800000 + i * 1000), i, i, i, i*10000000, i*1.0, i*2.0, "hello"); printf("qstr: %s\n", qstr); // note: how do you wanna do if taos_query returns non-NULL From d73cdd656dd6705d84efb275aae17e9010ff52ea Mon Sep 17 00:00:00 2001 From: freemine Date: Wed, 20 Jan 2021 10:05:53 +0800 Subject: [PATCH 13/17] 1. bugFix: typo in eok.c which results in memory leakage 2. for the sake of SIGALRM used in the base code, add taos_block_sigalrm and so forth 3. TODO: getaddrinfo would block a few unnecessary seconds when host ~ *.local better give warning/suggestion message to user, or just bypass getaddrinfo and fail the function --- src/os/inc/eok.h | 4 ++++ src/os/inc/osDarwin.h | 2 ++ src/os/src/darwin/darwinTimer.c | 13 +++++++++++++ src/os/src/darwin/eok.c | 16 +++++++--------- src/os/src/detail/osTimer.c | 6 +++++- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/os/inc/eok.h b/src/os/inc/eok.h index 4d38e8e632..0874ca975b 100644 --- a/src/os/inc/eok.h +++ b/src/os/inc/eok.h @@ -22,6 +22,8 @@ extern "C" { #endif +#ifdef __APPLE__ + enum EPOLL_EVENTS { EPOLLIN = 0x001, @@ -81,6 +83,8 @@ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout); int epoll_close(int epfd); +#endif // __APPLE__ + #ifdef __cplusplus } #endif diff --git a/src/os/inc/osDarwin.h b/src/os/inc/osDarwin.h index 52bb661f89..935708eb1b 100644 --- a/src/os/inc/osDarwin.h +++ b/src/os/inc/osDarwin.h @@ -104,6 +104,8 @@ int64_t tsosStr2int64(char *str); #include "eok.h" +void taos_block_sigalrm(void); + diff --git a/src/os/src/darwin/darwinTimer.c b/src/os/src/darwin/darwinTimer.c index 5fe65fb99e..121e2e9427 100644 --- a/src/os/src/darwin/darwinTimer.c +++ b/src/os/src/darwin/darwinTimer.c @@ -34,3 +34,16 @@ void taosUninitTimer() { setitimer(ITIMER_REAL, &tv, NULL); } +void taos_block_sigalrm(void) { + // since SIGALRM has been used + // consideration: any better solution? + static __thread int already_set = 0; + if (!already_set) { + sigset_t set; + sigemptyset(&set); + sigaddset(&set, SIGALRM); + pthread_sigmask(SIG_BLOCK, &set, NULL); + already_set = 1; + } +} + diff --git a/src/os/src/darwin/eok.c b/src/os/src/darwin/eok.c index 1be5ec4ac1..9c6a263a40 100644 --- a/src/os/src/darwin/eok.c +++ b/src/os/src/darwin/eok.c @@ -15,15 +15,11 @@ #include "eok.h" -#include -#include -#include -#include -#include -#include +#include "os.h" + #include -#include -#include + +#define LET_IT_BE #ifdef ENABLE_LOG #define D(fmt, ...) fprintf(stderr, "%s[%d]%s(): " fmt "\n", basename(__FILE__), __LINE__, __func__, ##__VA_ARGS__) @@ -414,6 +410,8 @@ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) { static struct timespec do_timespec_diff(struct timespec *from, struct timespec *to); int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) { + taos_block_sigalrm(); + int e = 0; if (!events) { errno = EINVAL; @@ -699,7 +697,7 @@ static void eok_free_ev(ep_over_kq_t *eok, eok_event_t *ev) { else eok->evs_tail = ev->prev; ev->prev = NULL; ev->next = eok->evs_free; - eok->evs_free = ev->next; + eok->evs_free = ev; eok->evs_count -= 1; } diff --git a/src/os/src/detail/osTimer.c b/src/os/src/detail/osTimer.c index 1d3ba30def..c1135ce292 100644 --- a/src/os/src/detail/osTimer.c +++ b/src/os/src/detail/osTimer.c @@ -116,6 +116,9 @@ void taosUninitTimer() { pthread_sigmask(SIG_BLOCK, &set, NULL); */ void taosMsleep(int mseconds) { +#ifdef __APPLE__ + taos_block_sigalrm(); +#endif // __APPLE__ #if 1 usleep(mseconds * 1000); #else @@ -136,6 +139,7 @@ void taosMsleep(int mseconds) { /* pthread_sigmask(SIG_UNBLOCK, &set, NULL); */ #endif + } -#endif \ No newline at end of file +#endif From 94eb0e1e71b713d66d00274242d7667408095154 Mon Sep 17 00:00:00 2001 From: freemine Date: Sat, 23 Jan 2021 08:44:32 +0800 Subject: [PATCH 14/17] 1. __APPLE__ 2. tsem_xxx for mac --- src/client/src/tscUtil.c | 4 ++-- src/dnode/src/dnodeSystem.c | 2 +- src/dnode/src/dnodeTelemetry.c | 2 +- src/mnode/src/mnodeCluster.c | 1 + src/os/inc/osDarwin.h | 10 +++++----- src/os/inc/osDef.h | 5 +++-- src/plugins/http/src/httpServer.c | 2 +- src/rpc/src/rpcTcp.c | 13 ++++++++----- src/sync/src/syncTcp.c | 8 ++++---- src/tsdb/inc/tsdbMain.h | 4 ++-- src/tsdb/src/tsdbCommit.c | 4 ++-- src/tsdb/src/tsdbMain.c | 12 ++++++------ src/tsdb/src/tsdbMemTable.c | 8 ++++---- src/util/src/tsocket.c | 2 +- tests/examples/c/epoll.c | 4 ++-- 15 files changed, 43 insertions(+), 38 deletions(-) diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index 19a1e99c33..bb2a81db66 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -2520,9 +2520,9 @@ bool tscSetSqlOwner(SSqlObj* pSql) { // set the sql object owner #ifdef __APPLE__ pthread_t threadId = (pthread_t)taosGetSelfPthreadId(); -#else +#else // __APPLE__ uint64_t threadId = taosGetSelfPthreadId(); -#endif +#endif // __APPLE__ if (atomic_val_compare_exchange_64(&pSql->owner, 0, threadId) != 0) { pRes->code = TSDB_CODE_QRY_IN_EXEC; return false; diff --git a/src/dnode/src/dnodeSystem.c b/src/dnode/src/dnodeSystem.c index 893049df2e..7a218f901e 100644 --- a/src/dnode/src/dnodeSystem.c +++ b/src/dnode/src/dnodeSystem.c @@ -164,7 +164,7 @@ static void signal_handler(int32_t signum, siginfo_t *sigInfo, void *context) { dInfo("shut down signal is %d, sender PID:%d", signum, sigInfo->si_pid); #else // __APPLE__ dInfo("shut down signal is %d, sender PID:%d cmdline:%s", signum, sigInfo->si_pid, taosGetCmdlineByPID(sigInfo->si_pid)); -#endif +#endif // __APPLE__ // protect the application from receive another signal struct sigaction act = {{0}}; diff --git a/src/dnode/src/dnodeTelemetry.c b/src/dnode/src/dnodeTelemetry.c index 1f7084c3b5..b2e8eac267 100644 --- a/src/dnode/src/dnodeTelemetry.c +++ b/src/dnode/src/dnodeTelemetry.c @@ -241,7 +241,7 @@ static int sem_timedwait(tsem_t *sem, struct timespec *to) { fprintf(stderr, "%s[%d]%s(): not implemented yet!\n", basename(__FILE__), __LINE__, __func__); abort(); } -#endif +#endif // __APPLE__ static void* telemetryThread(void* param) { struct timespec end = {0}; diff --git a/src/mnode/src/mnodeCluster.c b/src/mnode/src/mnodeCluster.c index 1b153247f0..5dee0e1c26 100644 --- a/src/mnode/src/mnodeCluster.c +++ b/src/mnode/src/mnodeCluster.c @@ -145,6 +145,7 @@ bool taosGetSystemUid(char *uid) { return false; } #endif // __APPLE__ + static int32_t mnodeCreateCluster() { int32_t numOfClusters = sdbGetNumOfRows(tsClusterSdb); if (numOfClusters != 0) return TSDB_CODE_SUCCESS; diff --git a/src/os/inc/osDarwin.h b/src/os/inc/osDarwin.h index 935708eb1b..0a77ed6895 100644 --- a/src/os/inc/osDarwin.h +++ b/src/os/inc/osDarwin.h @@ -75,11 +75,11 @@ extern "C" { #define TAOS_OS_FUNC_FILE_SENDIFLE #define TAOS_OS_FUNC_SEMPHONE - #define tsem_t dispatch_semaphore_t - int tsem_init(dispatch_semaphore_t *sem, int pshared, unsigned int value); - int tsem_wait(dispatch_semaphore_t *sem); - int tsem_post(dispatch_semaphore_t *sem); - int tsem_destroy(dispatch_semaphore_t *sem); + typedef struct tsem_s *tsem_t; + int tsem_init(tsem_t *sem, int pshared, unsigned int value); + int tsem_wait(tsem_t *sem); + int tsem_post(tsem_t *sem); + int tsem_destroy(tsem_t *sem); #define TAOS_OS_FUNC_SOCKET_SETSOCKETOPT #define TAOS_OS_FUNC_STRING_STR2INT64 diff --git a/src/os/inc/osDef.h b/src/os/inc/osDef.h index d718bef6da..f646940d3a 100644 --- a/src/os/inc/osDef.h +++ b/src/os/inc/osDef.h @@ -90,11 +90,12 @@ extern "C" { #ifdef _ISOC11_SOURCE #define threadlocal _Thread_local #elif defined(__APPLE__) - #define threadlocal + #define threadlocal __thread #elif defined(__GNUC__) && !defined(threadlocal) #define threadlocal __thread #else - #define threadlocal + // #define threadlocal + #error please follow with the target platform's thread-local implementation #endif #ifdef __cplusplus diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c index 97a5b4bc23..6a8eb556d8 100644 --- a/src/plugins/http/src/httpServer.c +++ b/src/plugins/http/src/httpServer.c @@ -33,7 +33,7 @@ static bool httpReadData(HttpContext *pContext); #ifdef __APPLE__ static int sv_dummy = 0; -#endif +#endif // __APPLE__ static void httpStopThread(HttpThread* pThread) { pThread->stop = true; diff --git a/src/rpc/src/rpcTcp.c b/src/rpc/src/rpcTcp.c index ba90f93073..d64bdb42aa 100644 --- a/src/rpc/src/rpcTcp.c +++ b/src/rpc/src/rpcTcp.c @@ -312,9 +312,9 @@ void *taosInitTcpClient(uint32_t ip, uint16_t port, char *label, int num, void * epoll_close(pThreadObj->pollFd); pThreadObj->pollFd = -1; } -#else +#else // __APPLE__ taosCloseSocket(pThreadObj->pollFd); -#endif +#endif // __APPLE__ free(pThreadObj); terrno = TAOS_SYSTEM_ERROR(errno); tError("%s failed to create TCP read data thread(%s)", label, strerror(errno)); @@ -477,7 +477,10 @@ static void *taosProcessTcpData(void *param) { SFdObj *pFdObj; struct epoll_event events[maxEvents]; SRecvInfo recvInfo; - + +#ifdef __APPLE__ + taos_block_sigalrm(); +#endif // __APPLE__ while (1) { int fdNum = epoll_wait(pThreadObj->pollFd, events, maxEvents, TAOS_EPOLL_WAIT_TIME); if (pThreadObj->stop) { @@ -524,9 +527,9 @@ static void *taosProcessTcpData(void *param) { epoll_close(pThreadObj->pollFd); pThreadObj->pollFd = -1; } -#else +#else // __APPLE__ if (pThreadObj->pollFd >=0) taosCloseSocket(pThreadObj->pollFd); -#endif +#endif // __APPLE__ while (pThreadObj->pHead) { SFdObj *pFdObj = pThreadObj->pHead; diff --git a/src/sync/src/syncTcp.c b/src/sync/src/syncTcp.c index 1e5761316a..e15f71e905 100644 --- a/src/sync/src/syncTcp.c +++ b/src/sync/src/syncTcp.c @@ -235,9 +235,9 @@ static void *syncProcessTcpData(void *param) { #ifdef __APPLE__ epoll_close(pThread->pollFd); -#else +#else // __APPLE__ close(pThread->pollFd); -#endif +#endif // __APPLE__ tfree(pThread); tfree(buffer); return NULL; @@ -296,9 +296,9 @@ static SThreadObj *syncGetTcpThread(SPoolObj *pPool) { if (ret != 0) { #ifdef __APPLE__ epoll_close(pThread->pollFd); -#else +#else // __APPLE__ close(pThread->pollFd); -#endif +#endif // __APPLE__ tfree(pThread); return NULL; } diff --git a/src/tsdb/inc/tsdbMain.h b/src/tsdb/inc/tsdbMain.h index 984839162e..f3e69d9210 100644 --- a/src/tsdb/inc/tsdbMain.h +++ b/src/tsdb/inc/tsdbMain.h @@ -235,9 +235,9 @@ typedef struct { STsdbFileH* tsdbFileH; #ifdef __APPLE__ sem_t *readyToCommit; -#else +#else // __APPLE__ sem_t readyToCommit; -#endif +#endif // __APPLE__ pthread_mutex_t mutex; bool repoLocked; int32_t code; // Commit code diff --git a/src/tsdb/src/tsdbCommit.c b/src/tsdb/src/tsdbCommit.c index 9551d1b3a5..1c8a661c0a 100644 --- a/src/tsdb/src/tsdbCommit.c +++ b/src/tsdb/src/tsdbCommit.c @@ -168,9 +168,9 @@ static void tsdbEndCommit(STsdbRepo *pRepo, int eno) { tsdbUnRefMemTable(pRepo, pIMem); #ifdef __APPLE__ sem_post(pRepo->readyToCommit); -#else +#else // __APPLE__ sem_post(&(pRepo->readyToCommit)); -#endif +#endif // __APPLE__ } static int tsdbHasDataToCommit(SCommitIter *iters, int nIters, TSKEY minKey, TSKEY maxKey) { diff --git a/src/tsdb/src/tsdbMain.c b/src/tsdb/src/tsdbMain.c index 5493188c09..c770f0ff42 100644 --- a/src/tsdb/src/tsdbMain.c +++ b/src/tsdb/src/tsdbMain.c @@ -148,9 +148,9 @@ int tsdbCloseRepo(TSDB_REPO_T *repo, int toCommit) { tsdbAsyncCommit(pRepo); #ifdef __APPLE__ sem_wait(pRepo->readyToCommit); -#else +#else // __APPLE__ sem_wait(&(pRepo->readyToCommit)); -#endif +#endif // __APPLE__ terrno = pRepo->code; } tsdbUnRefMemTable(pRepo, pRepo->mem); @@ -654,14 +654,14 @@ static STsdbRepo *tsdbNewRepo(char *rootDir, STsdbAppH *pAppH, STsdbCfg *pCfg) { terrno = TAOS_SYSTEM_ERROR(code); goto _err; } -#else +#else // __APPLE__ code = sem_init(&(pRepo->readyToCommit), 0, 1); if (code != 0) { code = errno; terrno = TAOS_SYSTEM_ERROR(code); goto _err; } -#endif +#endif // __APPLE__ pRepo->repoLocked = false; @@ -709,9 +709,9 @@ static void tsdbFreeRepo(STsdbRepo *pRepo) { tfree(pRepo->rootDir); #ifdef __APPLE__ sem_close(pRepo->readyToCommit); -#else +#else // __APPLE__ sem_destroy(&(pRepo->readyToCommit)); -#endif +#endif // __APPLE__ pthread_mutex_destroy(&pRepo->mutex); free(pRepo); } diff --git a/src/tsdb/src/tsdbMemTable.c b/src/tsdb/src/tsdbMemTable.c index 202405df00..206a3332d5 100644 --- a/src/tsdb/src/tsdbMemTable.c +++ b/src/tsdb/src/tsdbMemTable.c @@ -209,9 +209,9 @@ int tsdbAsyncCommit(STsdbRepo *pRepo) { #ifdef __APPLE__ sem_wait(pRepo->readyToCommit); -#else +#else // __APPLE__ sem_wait(&(pRepo->readyToCommit)); -#endif +#endif // __APPLE__ ASSERT(pRepo->imem == NULL); @@ -236,10 +236,10 @@ int tsdbSyncCommit(TSDB_REPO_T *repo) { #ifdef __APPLE__ sem_wait(pRepo->readyToCommit); sem_post(pRepo->readyToCommit); -#else +#else // __APPLE__ sem_wait(&(pRepo->readyToCommit)); sem_post(&(pRepo->readyToCommit)); -#endif +#endif // __APPLE__ if (pRepo->code != TSDB_CODE_SUCCESS) { terrno = pRepo->code; diff --git a/src/util/src/tsocket.c b/src/util/src/tsocket.c index cfd948265b..4bfd7a855b 100644 --- a/src/util/src/tsocket.c +++ b/src/util/src/tsocket.c @@ -376,7 +376,7 @@ int32_t taosKeepTcpAlive(SOCKET sockFd) { taosCloseSocket(sockFd); return -1; } -#endif +#endif // __APPLE__ int32_t nodelay = 1; if (taosSetSockOpt(sockFd, IPPROTO_TCP, TCP_NODELAY, (void *)&nodelay, sizeof(nodelay)) < 0) { diff --git a/tests/examples/c/epoll.c b/tests/examples/c/epoll.c index eb66156c14..4f65ea478e 100644 --- a/tests/examples/c/epoll.c +++ b/tests/examples/c/epoll.c @@ -15,9 +15,9 @@ #ifdef __APPLE__ #include "eok.h" -#else +#else // __APPLE__ #include -#endif +#endif // __APPLE__ #include #include #include From 4e774660b6de2f0035f8735c3a16c2f3b705e5a2 Mon Sep 17 00:00:00 2001 From: freemine Date: Sat, 23 Jan 2021 08:45:44 +0800 Subject: [PATCH 15/17] tsem_xxx for mac --- src/os/src/darwin/darwinSemphone.c | 88 ++++++++++++++++++++++++------ 1 file changed, 72 insertions(+), 16 deletions(-) diff --git a/src/os/src/darwin/darwinSemphone.c b/src/os/src/darwin/darwinSemphone.c index 97ff543789..d8e60cd2d5 100644 --- a/src/os/src/darwin/darwinSemphone.c +++ b/src/os/src/darwin/darwinSemphone.c @@ -16,25 +16,81 @@ #define _DEFAULT_SOURCE #include "os.h" -int tsem_init(dispatch_semaphore_t *sem, int pshared, unsigned int value) { - *sem = dispatch_semaphore_create(value); - if (*sem == NULL) { - return -1; - } else { +struct tsem_s { + dispatch_semaphore_t sem; + volatile unsigned int valid:1; +}; + +int tsem_wait(tsem_t *sem); +int tsem_post(tsem_t *sem); +int tsem_destroy(tsem_t *sem); +int tsem_init(tsem_t *sem, int pshared, unsigned int value) { + fprintf(stderr, "==%s[%d]%s():[%p]==creating\n", basename(__FILE__), __LINE__, __func__, sem); + if (*sem) { + fprintf(stderr, "==%s[%d]%s():[%p]==already initialized\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } + struct tsem_s *p = (struct tsem_s*)calloc(1, sizeof(*p)); + if (!p) { + fprintf(stderr, "==%s[%d]%s():[%p]==out of memory\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } + + p->sem = dispatch_semaphore_create(value); + if (p->sem == NULL) { + fprintf(stderr, "==%s[%d]%s():[%p]==not created\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } + p->valid = 1; + + *sem = p; + + return 0; +} + +int tsem_wait(tsem_t *sem) { + if (!*sem) { + fprintf(stderr, "==%s[%d]%s():[%p]==not initialized\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } + struct tsem_s *p = *sem; + if (!p->valid) { + fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } + return dispatch_semaphore_wait(p->sem, DISPATCH_TIME_FOREVER); +} + +int tsem_post(tsem_t *sem) { + if (!*sem) { + fprintf(stderr, "==%s[%d]%s():[%p]==not initialized\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } + struct tsem_s *p = *sem; + if (!p->valid) { + fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } + return dispatch_semaphore_signal(p->sem); +} + +int tsem_destroy(tsem_t *sem) { + fprintf(stderr, "==%s[%d]%s():[%p]==destroying\n", basename(__FILE__), __LINE__, __func__, sem); + if (!*sem) { + fprintf(stderr, "==%s[%d]%s():[%p]==not initialized\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); return 0; } -} + struct tsem_s *p = *sem; + if (!p->valid) { + fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } -int tsem_wait(dispatch_semaphore_t *sem) { - dispatch_semaphore_wait(*sem, DISPATCH_TIME_FOREVER); + p->valid = 0; + free(p); + + *sem = NULL; return 0; } -int tsem_post(dispatch_semaphore_t *sem) { - dispatch_semaphore_signal(*sem); - return 0; -} - -int tsem_destroy(dispatch_semaphore_t *sem) { - return 0; -} From ce50e131bc35b23f9db111736c0a55c59135f958 Mon Sep 17 00:00:00 2001 From: freemine Date: Sun, 24 Jan 2021 09:29:13 +0800 Subject: [PATCH 16/17] 1. kqueue rather than SIGALRM for the timer 2. semaphore rather than dispatch_semaphore for tsem_xxx 3. fail-fast comments 4. niche --- src/client/src/tscSql.c | 4 + src/client/src/tscUtil.c | 4 + src/os/src/darwin/darwinSemphone.c | 201 +++++++++++++++++++++++++++-- src/os/src/darwin/darwinTimer.c | 74 +++++++++++ src/os/src/darwin/eok.c | 37 +++--- 5 files changed, 297 insertions(+), 23 deletions(-) diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index fa7c1f83ba..ef9b079cfc 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -295,6 +295,10 @@ void taos_close(TAOS *taos) { tscDebug("%p HB is freed", pHb); taosReleaseRef(tscObjRef, pHb->self); +#ifdef __APPLE__ + // to satisfy later tsem_destroy in taos_free_result + tsem_init(&pHb->rspSem, 0, 0); +#endif // __APPLE__ taos_free_result(pHb); } } diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index 8dcb7425a8..e1521b4aa1 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -1933,6 +1933,10 @@ SSqlObj* createSimpleSubObj(SSqlObj* pSql, __async_cb_func_t fp, void* param, in } if (tscAddSubqueryInfo(pCmd) != TSDB_CODE_SUCCESS) { +#ifdef __APPLE__ + // to satisfy later tsem_destroy in taos_free_result + tsem_init(&pNew->rspSem, 0, 0); +#endif // __APPLE__ tscFreeSqlObj(pNew); return NULL; } diff --git a/src/os/src/darwin/darwinSemphone.c b/src/os/src/darwin/darwinSemphone.c index d8e60cd2d5..a9e470bd70 100644 --- a/src/os/src/darwin/darwinSemphone.c +++ b/src/os/src/darwin/darwinSemphone.c @@ -13,19 +13,76 @@ * along with this program. If not, see . */ +// fail-fast or let-it-crash philosophy +// https://en.wikipedia.org/wiki/Fail-fast +// https://stackoverflow.com/questions/4393197/erlangs-let-it-crash-philosophy-applicable-elsewhere +// experimentally, we follow log-and-crash here + #define _DEFAULT_SOURCE #include "os.h" +// #define SEM_USE_PTHREAD +// #define SEM_USE_POSIX +#define SEM_USE_SEM + +#ifdef SEM_USE_SEM +#include +#include +#include +#include + +static pthread_t sem_thread; +static pthread_once_t sem_once; +static task_t sem_port; +static volatile int sem_inited = 0; +static semaphore_t sem_exit; + +static void* sem_thread_routine(void *arg) { + (void)arg; + sem_port = mach_task_self(); + kern_return_t ret = semaphore_create(sem_port, &sem_exit, SYNC_POLICY_FIFO, 0); + if (ret != KERN_SUCCESS) { + fprintf(stderr, "==%s[%d]%s()==failed to create sem_exit\n", basename(__FILE__), __LINE__, __func__); + sem_inited = -1; + return NULL; + } + sem_inited = 1; + semaphore_wait(sem_exit); + return NULL; +} + +static void once_init(void) { + int r = 0; + r = pthread_create(&sem_thread, NULL, sem_thread_routine, NULL); + if (r) { + fprintf(stderr, "==%s[%d]%s()==failed to create thread\n", basename(__FILE__), __LINE__, __func__); + return; + } + while (sem_inited==0) { + ; + } +} +#endif + struct tsem_s { +#ifdef SEM_USE_PTHREAD + pthread_mutex_t lock; + pthread_cond_t cond; + volatile int64_t val; +#elif defined(SEM_USE_POSIX) + size_t id; + sem_t *sem; +#elif defined(SEM_USE_SEM) + semaphore_t sem; +#else // SEM_USE_PTHREAD dispatch_semaphore_t sem; +#endif // SEM_USE_PTHREAD + volatile unsigned int valid:1; }; -int tsem_wait(tsem_t *sem); -int tsem_post(tsem_t *sem); -int tsem_destroy(tsem_t *sem); int tsem_init(tsem_t *sem, int pshared, unsigned int value) { - fprintf(stderr, "==%s[%d]%s():[%p]==creating\n", basename(__FILE__), __LINE__, __func__, sem); + // fprintf(stderr, "==%s[%d]%s():[%p]==creating\n", basename(__FILE__), __LINE__, __func__, sem); if (*sem) { fprintf(stderr, "==%s[%d]%s():[%p]==already initialized\n", basename(__FILE__), __LINE__, __func__, sem); abort(); @@ -36,11 +93,61 @@ int tsem_init(tsem_t *sem, int pshared, unsigned int value) { abort(); } +#ifdef SEM_USE_PTHREAD + int r = pthread_mutex_init(&p->lock, NULL); + do { + if (r) break; + r = pthread_cond_init(&p->cond, NULL); + if (r) { + pthread_mutex_destroy(&p->lock); + break; + } + p->val = value; + } while (0); + if (r) { + fprintf(stderr, "==%s[%d]%s():[%p]==not created\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } +#elif defined(SEM_USE_POSIX) + static size_t tick = 0; + do { + size_t id = atomic_add_fetch_64(&tick, 1); + if (id==SEM_VALUE_MAX) { + atomic_store_64(&tick, 0); + id = 0; + } + char name[NAME_MAX-4]; + snprintf(name, sizeof(name), "/t%ld", id); + p->sem = sem_open(name, O_CREAT|O_EXCL, pshared, value); + p->id = id; + if (p->sem!=SEM_FAILED) break; + int e = errno; + if (e==EEXIST) continue; + if (e==EINTR) continue; + fprintf(stderr, "==%s[%d]%s():[%p]==not created[%d]%s\n", basename(__FILE__), __LINE__, __func__, sem, e, strerror(e)); + abort(); + } while (p->sem==SEM_FAILED); +#elif defined(SEM_USE_SEM) + pthread_once(&sem_once, once_init); + if (sem_inited!=1) { + fprintf(stderr, "==%s[%d]%s():[%p]==internal resource init failed\n", basename(__FILE__), __LINE__, __func__, sem); + errno = ENOMEM; + return -1; + } + kern_return_t ret = semaphore_create(sem_port, &p->sem, SYNC_POLICY_FIFO, 0); + if (ret != KERN_SUCCESS) { + fprintf(stderr, "==%s[%d]%s():[%p]==semophore_create failed\n", basename(__FILE__), __LINE__, __func__, sem); + // we fail-fast here, because we have less-doc about semaphore_create for the moment + abort(); + } +#else // SEM_USE_PTHREAD p->sem = dispatch_semaphore_create(value); if (p->sem == NULL) { fprintf(stderr, "==%s[%d]%s():[%p]==not created\n", basename(__FILE__), __LINE__, __func__, sem); abort(); } +#endif // SEM_USE_PTHREAD + p->valid = 1; *sem = p; @@ -58,7 +165,30 @@ int tsem_wait(tsem_t *sem) { fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", basename(__FILE__), __LINE__, __func__, sem); abort(); } +#ifdef SEM_USE_PTHREAD + if (pthread_mutex_lock(&p->lock)) { + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } + p->val -= 1; + if (p->val < 0) { + if (pthread_cond_wait(&p->cond, &p->lock)) { + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } + } + if (pthread_mutex_unlock(&p->lock)) { + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } + return 0; +#elif defined(SEM_USE_POSIX) + return sem_wait(p->sem); +#elif defined(SEM_USE_SEM) + return semaphore_wait(p->sem); +#else // SEM_USE_PTHREAD return dispatch_semaphore_wait(p->sem, DISPATCH_TIME_FOREVER); +#endif // SEM_USE_PTHREAD } int tsem_post(tsem_t *sem) { @@ -71,21 +201,76 @@ int tsem_post(tsem_t *sem) { fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", basename(__FILE__), __LINE__, __func__, sem); abort(); } +#ifdef SEM_USE_PTHREAD + if (pthread_mutex_lock(&p->lock)) { + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } + p->val += 1; + if (p->val <= 0) { + if (pthread_cond_signal(&p->cond)) { + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } + } + if (pthread_mutex_unlock(&p->lock)) { + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } + return 0; +#elif defined(SEM_USE_POSIX) + return sem_post(p->sem); +#elif defined(SEM_USE_SEM) + return semaphore_signal(p->sem); +#else // SEM_USE_PTHREAD return dispatch_semaphore_signal(p->sem); +#endif // SEM_USE_PTHREAD } int tsem_destroy(tsem_t *sem) { - fprintf(stderr, "==%s[%d]%s():[%p]==destroying\n", basename(__FILE__), __LINE__, __func__, sem); + // fprintf(stderr, "==%s[%d]%s():[%p]==destroying\n", basename(__FILE__), __LINE__, __func__, sem); if (!*sem) { - fprintf(stderr, "==%s[%d]%s():[%p]==not initialized\n", basename(__FILE__), __LINE__, __func__, sem); - abort(); + // fprintf(stderr, "==%s[%d]%s():[%p]==not initialized\n", basename(__FILE__), __LINE__, __func__, sem); + // abort(); return 0; } struct tsem_s *p = *sem; if (!p->valid) { - fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", basename(__FILE__), __LINE__, __func__, sem); + // fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", basename(__FILE__), __LINE__, __func__, sem); + // abort(); + return 0; + } +#ifdef SEM_USE_PTHREAD + if (pthread_mutex_lock(&p->lock)) { + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); abort(); } + p->valid = 0; + if (pthread_cond_destroy(&p->cond)) { + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } + if (pthread_mutex_unlock(&p->lock)) { + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } + if (pthread_mutex_destroy(&p->lock)) { + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", basename(__FILE__), __LINE__, __func__, sem); + abort(); + } +#elif defined(SEM_USE_POSIX) + char name[NAME_MAX-4]; + snprintf(name, sizeof(name), "/t%ld", p->id); + int r = sem_unlink(name); + if (r) { + int e = errno; + fprintf(stderr, "==%s[%d]%s():[%p]==unlink failed[%d]%s\n", basename(__FILE__), __LINE__, __func__, sem, e, strerror(e)); + abort(); + } +#elif defined(SEM_USE_SEM) + semaphore_destroy(sem_port, p->sem); +#else // SEM_USE_PTHREAD +#endif // SEM_USE_PTHREAD p->valid = 0; free(p); diff --git a/src/os/src/darwin/darwinTimer.c b/src/os/src/darwin/darwinTimer.c index 121e2e9427..ee1becc91a 100644 --- a/src/os/src/darwin/darwinTimer.c +++ b/src/os/src/darwin/darwinTimer.c @@ -13,9 +13,82 @@ * along with this program. If not, see . */ +// fail-fast or let-it-crash philosophy +// https://en.wikipedia.org/wiki/Fail-fast +// https://stackoverflow.com/questions/4393197/erlangs-let-it-crash-philosophy-applicable-elsewhere +// experimentally, we follow log-and-crash here + #define _DEFAULT_SOURCE #include "os.h" +#if 1 +#include + +static void (*timer_callback)(int); +static int timer_ms = 0; +static pthread_t timer_thread; +static int timer_kq = -1; +static volatile int timer_stop = 0; + +static void* timer_routine(void *arg) { + (void)arg; + + int r = 0; + struct timespec to = {0}; + to.tv_sec = timer_ms / 1000; + to.tv_nsec = (timer_ms % 1000) * 1000000; + while (!timer_stop) { + struct kevent64_s kev[10] = {0}; + r = kevent64(timer_kq, NULL, 0, kev, sizeof(kev)/sizeof(kev[0]), 0, &to); + if (r!=0) { + fprintf(stderr, "==%s[%d]%s()==kevent64 failed\n", basename(__FILE__), __LINE__, __func__); + abort(); + } + timer_callback(SIGALRM); // just mock + } + + return NULL; +} + +int taosInitTimer(void (*callback)(int), int ms) { + int r = 0; + timer_ms = ms; + timer_callback = callback; + + timer_kq = kqueue(); + if (timer_kq==-1) { + fprintf(stderr, "==%s[%d]%s()==failed to create timer kq\n", basename(__FILE__), __LINE__, __func__); + // since no caller of this func checks the return value for the moment + abort(); + } + + r = pthread_create(&timer_thread, NULL, timer_routine, NULL); + if (r) { + fprintf(stderr, "==%s[%d]%s()==failed to create timer thread\n", basename(__FILE__), __LINE__, __func__); + // since no caller of this func checks the return value for the moment + abort(); + } + return 0; +} + +void taosUninitTimer() { + int r = 0; + timer_stop = 1; + r = pthread_join(timer_thread, NULL); + if (r) { + fprintf(stderr, "==%s[%d]%s()==failed to join timer thread\n", basename(__FILE__), __LINE__, __func__); + // since no caller of this func checks the return value for the moment + abort(); + } + close(timer_kq); + timer_kq = -1; +} + +void taos_block_sigalrm(void) { + // we don't know if there's any specific API for SIGALRM to deliver to specific thread + // this implementation relies on kqueue rather than SIGALRM +} +#else int taosInitTimer(void (*callback)(int), int ms) { signal(SIGALRM, callback); @@ -46,4 +119,5 @@ void taos_block_sigalrm(void) { already_set = 1; } } +#endif diff --git a/src/os/src/darwin/eok.c b/src/os/src/darwin/eok.c index 9c6a263a40..f731aaf330 100644 --- a/src/os/src/darwin/eok.c +++ b/src/os/src/darwin/eok.c @@ -13,13 +13,18 @@ * along with this program. If not, see . */ +// fail-fast or let-it-crash philosophy +// https://en.wikipedia.org/wiki/Fail-fast +// https://stackoverflow.com/questions/4393197/erlangs-let-it-crash-philosophy-applicable-elsewhere +// experimentally, we follow log-and-crash here + #include "eok.h" #include "os.h" #include -#define LET_IT_BE +// #define BALANCE_CHECK_WHEN_CLOSE #ifdef ENABLE_LOG #define D(fmt, ...) fprintf(stderr, "%s[%d]%s(): " fmt "\n", basename(__FILE__), __LINE__, __func__, ##__VA_ARGS__) @@ -99,16 +104,16 @@ struct eok_event_s { typedef struct eoks_s eoks_t; struct eoks_s { pthread_mutex_t lock; - ep_over_kq_t **eoks; // note: this memory leaks when process terminates - int neoks; // we can add an extra api to let user clean - ep_over_kq_t *eoks_free; // currently, we just keep it simple stupid + ep_over_kq_t **eoks; // note: this memory leaks when process terminates + int neoks; // we can add an extra api to let user clean + ep_over_kq_t *eoks_free_list; // currently, we just keep it simple stupid }; static eoks_t eoks = { - .lock = PTHREAD_MUTEX_INITIALIZER, - .eoks = NULL, - .neoks = 0, - .eoks_free = NULL, + .lock = PTHREAD_MUTEX_INITIALIZER, + .eoks = NULL, + .neoks = 0, + .eoks_free_list = NULL, }; #ifdef ENABLE_LOG @@ -760,9 +765,9 @@ static ep_over_kq_t* eoks_alloc(void) { A(0==pthread_mutex_lock(&eoks.lock), ""); do { - if (eoks.eoks_free) { - eok = eoks.eoks_free; - eoks.eoks_free = eok->next; + if (eoks.eoks_free_list) { + eok = eoks.eoks_free_list; + eoks.eoks_free_list = eok->next; A(eoks.eoks, "internal logic error"); A(eok->idx>=0 && eok->idxidx)==NULL, "internal logic error"); @@ -820,10 +825,12 @@ static void eoks_free(ep_over_kq_t *eok) { A(eok->waiting==0, "internal logic error"); eok_event_t *ev = eok->evs_head; + int sv_closed = 0; while (ev) { eok_event_t *next = ev->next; if (ev->fd==eok->sv[0]) { // fd is critical system resource + A(sv_closed==0, "internal logic error"); close(eok->sv[0]); eok->sv[0] = -1; close(eok->sv[1]); @@ -832,11 +839,11 @@ static void eoks_free(ep_over_kq_t *eok) { } else { // user forget calling epoll_ctl(EPOLL_CTL_DEL) before calling epoll_close/close? // calling close(ev->fd) here smells really bad -#ifdef LET_IT_BE +#ifndef BALANCE_CHECK_WHEN_CLOSE // we just let it be and reclaim ev eok_free_ev(eok, ev); #else - // panic otherwise, if LET_IT_BE not defined + // panic otherwise, if BALANCE_CHECK_WHEN_CLOSE is defined A(eok->evs_head==NULL && eok->evs_tail==NULL && eok->evs_count==0, "epfd[%d] fd[%d]: internal logic error: have you epoll_ctl(EPOLL_CTL_DEL) everything before calling epoll_close?", eok->idx, ev->fd); @@ -861,8 +868,8 @@ static void eoks_free(ep_over_kq_t *eok) { close(eok->kq); eok->kq = -1; } - eok->next = eoks.eoks_free; - eoks.eoks_free = eok; + eok->next = eoks.eoks_free_list; + eoks.eoks_free_list = eok; *(eoks.eoks + eok->idx) = NULL; } while (0); A(0==pthread_mutex_unlock(&eoks.lock), ""); From 15b7973e48caf52b7a22e621fe9fa654bfc6a8a6 Mon Sep 17 00:00:00 2001 From: freemine Date: Sun, 24 Jan 2021 09:44:47 +0800 Subject: [PATCH 17/17] typo --- src/os/inc/osDef.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/os/inc/osDef.h b/src/os/inc/osDef.h index f646940d3a..e15c45ad14 100644 --- a/src/os/inc/osDef.h +++ b/src/os/inc/osDef.h @@ -95,7 +95,7 @@ extern "C" { #define threadlocal __thread #else // #define threadlocal - #error please follow with the target platform's thread-local implementation + #error please follow with the thread-local implementation on the target platform #endif #ifdef __cplusplus