From c593893ec5805bf47949138d9f96ea4222900f4e Mon Sep 17 00:00:00 2001
From: naeper <naeper@Mac-mini-naeper.local>
Date: Tue, 2 Mar 2021 17:01:46 +0700
Subject: [PATCH] :q

---
 CMakeLists.txt                          |  2 +-
 dap-sdk/core/libdap.pri                 |  7 +++-
 dap-sdk/core/src/dap_common.c           |  8 ++--
 dap-sdk/core/src/darwin/macos/macos.pri |  2 +-
 dap-sdk/net/core/dap_events.c           | 51 +++++++++++++++++++------
 dap-sdk/net/core/dap_events_socket.c    | 26 +++++++++++--
 dap-sdk/net/core/dap_proc_thread.c      | 30 ++++++++++-----
 dap-sdk/net/core/dap_timerfd.c          |  5 +++
 dap-sdk/net/core/dap_worker.c           | 24 +++++++++---
 9 files changed, 118 insertions(+), 37 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index da537d9eb8..161246a8f4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,7 +2,7 @@ project(cellframe-sdk C)
 cmake_minimum_required(VERSION 3.0)
 
 set(CMAKE_C_STANDARD 11)
-set(CELLFRAME_SDK_NATIVE_VERSION "2.8-22")
+set(CELLFRAME_SDK_NATIVE_VERSION "2.8-23")
 add_definitions ("-DCELLFRAME_SDK_VERSION=\"${CELLFRAME_SDK_NATIVE_VERSION}\"")
 set(DAPSDK_MODULES "")
 
diff --git a/dap-sdk/core/libdap.pri b/dap-sdk/core/libdap.pri
index 67de4803b2..1103b77d27 100755
--- a/dap-sdk/core/libdap.pri
+++ b/dap-sdk/core/libdap.pri
@@ -4,6 +4,7 @@
 QMAKE_CFLAGS_DEBUG += -DDAP_DEBUG
 QMAKE_CXXFLAGS +=  -std=c++11
 QMAKE_CFLAGS +=  -std=gnu11
+
 unix {
     include(src/unix/unix.pri)
     DEFINES += DAP_OS_UNIX
@@ -12,15 +13,17 @@ android {
     DEFINES += DAP_OS_ANDROID DAP_OS_LINUX DAP_OS_UNIX
 }
 
-unix: !android {
+unix: !android : ! darwin {
     QMAKE_CFLAGS_DEBUG += -Wall -Wno-deprecated-declarations -Wno-unused-local-typedefs -Wno-unused-function -Wno-implicit-fallthrough -Wno-unused-variable -Wno-unused-parameter -Wno-unused-but-set-variable -pg -g3 -ggdb -fno-eliminate-unused-debug-symbols -fno-strict-aliasing
     QMAKE_LFLAGS_DEBUG += -pg
     DEFINES += _GNU_SOURCE
     LIBS += -lrt -ljson-c -lmagic
 }
 darwin {
+    QMAKE_CFLAGS_DEBUG += -Wall -Wno-deprecated-declarations -Wno-unused-local-typedefs -Wno-unused-function -Wno-implicit-fallthrough -Wno-unused-variable -Wno-unused-parameter -Wno-unused-but-set-variable -g3 -ggdb -fno-eliminate-unused-debug-symbols -fno-strict-aliasing
+    DEFINES += _GNU_SOURCE
     include(src/darwin/darwin.pri)
-    DEFINES += DAP_OS_DARWIN
+    DEFINES += DAP_OS_DARWIN DAP_OS_BSD
     LIBS -= -lrt
 }
 
diff --git a/dap-sdk/core/src/dap_common.c b/dap-sdk/core/src/dap_common.c
index a69ac36d00..13814e2736 100755
--- a/dap-sdk/core/src/dap_common.c
+++ b/dap-sdk/core/src/dap_common.c
@@ -969,12 +969,14 @@ int dap_interval_timer_delete(void *a_timer)
     if (s_timers_count == 0) {
         pthread_mutex_destroy(&s_timers_lock);
     }
-#ifdef DAP_OS_UNIX
-    return timer_delete((timer_t)a_timer);
-#else
+#ifdef DAP_OS_DARWIN
     dispatch_source_cancel(a_timer);
     return 0;
+#elif defined(DAP_OS_UNIX)
+    // POSIX timer delete
+    return timer_delete((timer_t)a_timer);
 #endif  // DAP_OS_UNIX
+
 #endif  // _WIN32
 }
 
diff --git a/dap-sdk/core/src/darwin/macos/macos.pri b/dap-sdk/core/src/darwin/macos/macos.pri
index 1fb87f8d02..e3bf4c4525 100755
--- a/dap-sdk/core/src/darwin/macos/macos.pri
+++ b/dap-sdk/core/src/darwin/macos/macos.pri
@@ -3,7 +3,7 @@ HEADERS += $$PWD/pthread_barrier.h
 
 SOURCES += $$PWD/dap_network_monitor.c
 
-INCLUDEPATH += $$PWD
+INCLUDEPATH += $$PWD /usr/local/include
 
 LIBS += -framework CoreFoundation
 LIBS += -framework SystemConfiguration
diff --git a/dap-sdk/net/core/dap_events.c b/dap-sdk/net/core/dap_events.c
index 80e5ff6277..2b3cfbd9d4 100644
--- a/dap-sdk/net/core/dap_events.c
+++ b/dap-sdk/net/core/dap_events.c
@@ -32,6 +32,8 @@
 #include <stdint.h>
 
 #ifdef DAP_OS_UNIX
+#include <pthread.h>
+#include <fcntl.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
@@ -46,22 +48,29 @@
 #include <signal.h>
 #include <sched.h>
 
-#ifdef DAP_OS_LINUX
+#if defined(DAP_OS_LINUX)
 #include <sys/timerfd.h>
 #endif
 
+#if defined(DAP_OS_BSD)
+#include <sys/event.h>
+#include <err.h>
+#endif
 
-#include <pthread.h>
+#if defined(DAP_OS_DARWIN)
+#include <sys/types.h>
+#include <sys/sysctl.h>
 
-#ifdef DAP_OS_BSD
+#include <mach/thread_policy.h>
+#include <mach/thread_act.h>
+#elif defined (DAP_OS_BSD)
 #include <pthread_np.h>
-#include <sys/event.h>
-#include <err.h>
-#include <fcntl.h>
 typedef cpuset_t cpu_set_t; // Adopt BSD CPU setstructure to POSIX variant
-
 #endif
 
+
+
+
 #if defined(DAP_OS_ANDROID)
 #define NO_POSIX_SHED
 #define NO_TIMER
@@ -78,6 +87,7 @@ typedef cpuset_t cpu_set_t; // Adopt BSD CPU setstructure to POSIX variant
 #endif
 
 #include <utlist.h>
+#include <pthread.h>
 
 #include "dap_common.h"
 #include "dap_strfuncs.h"
@@ -103,20 +113,31 @@ uint32_t dap_get_cpu_count( )
   return si.dwNumberOfProcessors;
 #else
 #ifndef NO_POSIX_SHED
+#ifndef DAP_OS_DARWIN
   cpu_set_t cs;
   CPU_ZERO( &cs );
+#endif
+
 #if defined (DAP_OS_ANDROID) 
   sched_getaffinity( 0, sizeof(cs), &cs );
+#elif defined (DAP_OS_DARWIN)
+  int count=0;
+  size_t count_len = sizeof(count);
+  sysctlbyname("hw.logicalcpu", &count, &count_len, NULL, 0);
+
 #else
   pthread_getaffinity_np(pthread_self(), sizeof(cs), &cs);
 #endif
 
+#ifndef DAP_OS_DARWIN
   uint32_t count = 0;
   for ( int i = 0; i < 32; i++ ){
     if ( CPU_ISSET(i, &cs) )
     count ++;
   }
+#endif
   return count;
+
 #else
   return 1;
 #endif
@@ -127,15 +148,23 @@ void dap_cpu_assign_thread_on(uint32_t a_cpu_id)
 {
 #ifndef DAP_OS_WINDOWS
 #ifndef NO_POSIX_SHED
+
+#ifdef DAP_OS_DARWIN
+    pthread_t l_pthread_id = pthread_self();
+    mach_port_t l_pthread_mach_port = pthread_mach_thread_np(l_pthread_id);
+#else
     cpu_set_t mask;
     CPU_ZERO(&mask);
     CPU_SET(a_cpu_id, &mask);
-
+#endif
     int l_retcode;
-#ifndef __ANDROID__
-    l_retcode = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &mask);
+#ifdef DAP_OS_DARWIN
+    thread_affinity_policy_data_t l_policy_data={.affinity_tag = a_cpu_id};
+    thread_policy_set(l_pthread_mach_port , THREAD_AFFINITY_POLICY, (thread_policy_t)&l_policy_data , 1);
+#elif defined(DAP_OS_ANDROID)
+    err = sched_setaffinity(pthread_self(), sizeof(cpu_set_t), &mask);
 #else
-  err = sched_setaffinity(pthread_self(), sizeof(cpu_set_t), &mask);
+    l_retcode = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &mask);
 #endif
     if(l_retcode != 0)
     {
diff --git a/dap-sdk/net/core/dap_events_socket.c b/dap-sdk/net/core/dap_events_socket.c
index 15c010b78c..077360faf5 100644
--- a/dap-sdk/net/core/dap_events_socket.c
+++ b/dap-sdk/net/core/dap_events_socket.c
@@ -59,10 +59,17 @@
 #endif
 
 #ifdef DAP_OS_BSD
-#include <pthread_np.h>
 #include <sys/event.h>
 #include <err.h>
+
+#ifndef DAP_OS_DARWIN
+#include <pthread_np.h>
 typedef cpuset_t cpu_set_t; // Adopt BSD CPU setstructure to POSIX variant
+#else
+#define NOTE_READ NOTE_LOWAT
+
+#endif
+
 #endif
 
 
@@ -176,7 +183,11 @@ dap_events_socket_t *dap_events_socket_wrap_no_add( dap_events_t *a_events,
     l_ret->poll_base_flags = POLLERR | POLLRDHUP | POLLHUP;
     #elif defined(DAP_EVENTS_CAPS_KQUEUE)
     l_ret->kqueue_base_flags = EV_ADD | EV_ENABLE | EV_CLEAR;
-    l_ret->kqueue_base_fflags = NOTE_CLOSE | NOTE_CLOSE_WRITE | NOTE_DELETE | NOTE_REVOKE ;
+
+    l_ret->kqueue_base_fflags =  NOTE_DELETE | NOTE_REVOKE ;
+#if !defined(DAP_OS_DARWIN)
+    l_ret->kqueue_base_fflags |= NOTE_CLOSE | NOTE_CLOSE_WRITE ;
+#endif
     l_ret->kqueue_base_filter = EVFILT_VNODE;
     #endif
 
@@ -278,7 +289,10 @@ dap_events_socket_t * s_create_type_pipe(dap_worker_t * a_w, dap_events_socket_c
     l_es->poll_base_flags = POLLIN | POLLERR | POLLRDHUP | POLLHUP;
 #elif defined(DAP_EVENTS_CAPS_KQUEUE)
     l_es->kqueue_base_flags = EV_ADD | EV_ENABLE | EV_CLEAR;
-    l_es->kqueue_base_fflags = NOTE_CLOSE | NOTE_CLOSE_WRITE | NOTE_DELETE | NOTE_REVOKE ;
+    l_es->kqueue_base_fflags = NOTE_DELETE | NOTE_REVOKE ;
+#if !defined(DAP_OS_DARWIN)
+    l_es->kqueue_base_fflags |= NOTE_CLOSE | NOTE_CLOSE_WRITE ;
+#endif
     l_es->kqueue_base_filter = EVFILT_VNODE;
 #else
 #error "Not defined s_create_type_pipe for your platform"
@@ -361,7 +375,11 @@ dap_events_socket_t * dap_events_socket_create(dap_events_desc_type_t a_type, da
     if (ioctlsocket((SOCKET)l_sock, (long)FIONBIO, &l_socket_flags))
         log_it(L_ERROR, "Error ioctl %d", WSAGetLastError());
 #else
-    int l_sock = socket(l_sock_class, l_sock_type | SOCK_NONBLOCK , 0);
+    int l_sock = socket(l_sock_class, l_sock_type, 0);
+    int l_sock_flags = fcntl( l_sock, F_GETFL);
+    l_sock_flags |= O_NONBLOCK;
+    fcntl( l_sock, F_SETFL, l_sock_flags);
+
     if (l_sock == INVALID_SOCKET) {
         log_it(L_ERROR, "Socket create error");
         return NULL;
diff --git a/dap-sdk/net/core/dap_proc_thread.c b/dap-sdk/net/core/dap_proc_thread.c
index 0780378170..cc6a4ad277 100644
--- a/dap-sdk/net/core/dap_proc_thread.c
+++ b/dap-sdk/net/core/dap_proc_thread.c
@@ -32,10 +32,17 @@
 #elif defined (DAP_EVENTS_CAPS_POLL)
 #include <poll.h>
 #elif defined (DAP_EVENTS_CAPS_KQUEUE)
-#include <pthread_np.h>
+
 #include <sys/event.h>
 #include <err.h>
+
+#ifndef DAP_OS_DARWIN
+#include <pthread_np.h>
 typedef cpuset_t cpu_set_t; // Adopt BSD CPU setstructure to POSIX variant
+#else
+#define NOTE_READ NOTE_LOWAT
+#endif
+
 #else
 #error "Unimplemented poll for this platform"
 #endif
@@ -557,21 +564,24 @@ static void * s_proc_thread_function(void * a_arg)
             l_flag_pri = l_cur_events & POLLPRI;
             l_flag_msg = l_cur_events & POLLMSG;
 #elif defined (DAP_EVENTS_CAPS_KQUEUE)
-	    l_cur = (dap_events_socket_t*) l_thread->kqueue_events[n].udata;
-            if (!l_cur)
-        	continue;
+            l_cur = (dap_events_socket_t*) l_thread->kqueue_events[n].udata;
+            assert(l_cur);
             l_cur->kqueue_event_catched = &l_thread->kqueue_events[n];
-	    u_int l_cur_events = l_thread->kqueue_events[n].fflags;
+#ifndef DAP_OS_DARWIN
+            u_int l_cur_events = l_thread->kqueue_events[n].flags;
+#else
+            uint32_t l_cur_events = l_thread->kqueue_events[n].fflags;
+#endif
+
             l_flag_write = l_cur_events & EVFILT_WRITE;
             l_flag_read  = l_cur_events & EVFILT_READ;
+            l_flag_error = l_cur_events & EVFILT_EXCEPT;
+            l_flag_nval = l_flag_pri = l_flag_msg = l_flag_hup=  0;
+            l_flag_rdhup = l_cur_events & EVFILT_EXCEPT & NOTE_DELETE;
 #else
 #error "Unimplemented fetch esocket after poll"
 #endif
-
-            if(!l_cur) {
-                log_it(L_ERROR, "dap_events_socket NULL");
-                continue;
-            }
+            assert(l_cur);
             if(s_debug_reactor)
                 log_it(L_DEBUG, "Proc thread #%u esocket %p fd=%d type=%d flags=0x%0X (%s:%s:%s:%s:%s:%s:%s:%s)", l_thread->cpu_id, l_cur, l_cur->socket,
                     l_cur->type, l_cur_events, l_flag_read?"read":"", l_flag_write?"write":"", l_flag_error?"error":"",
diff --git a/dap-sdk/net/core/dap_timerfd.c b/dap-sdk/net/core/dap_timerfd.c
index f92a4af3a8..008bde9777 100644
--- a/dap-sdk/net/core/dap_timerfd.c
+++ b/dap-sdk/net/core/dap_timerfd.c
@@ -156,8 +156,13 @@ dap_timerfd_t* dap_timerfd_create(uint64_t a_timeout_ms, dap_timerfd_callback_t
 #elif defined (DAP_OS_BSD)
     l_events_socket->kqueue_base_flags = EV_ADD | EV_ONESHOT | EV_DISPATCH;
     l_events_socket->kqueue_base_filter = EVFILT_TIMER;
+#ifdef DAP_OS_DARWIN
+    l_events_socket->kqueue_base_fflags = NOTE_USECONDS;
+    l_events_socket->kqueue_data =(int64_t) a_timeout_ms*1000;
+#else
     l_events_socket->kqueue_base_fflags = NOTE_MSECONDS;
     l_events_socket->kqueue_data =(int64_t) a_timeout_ms;
+#endif
     l_events_socket->socket = rand();
 #elif defined (DAP_OS_WINDOWS)
     HANDLE l_th = CreateWaitableTimer(NULL, true, NULL);
diff --git a/dap-sdk/net/core/dap_worker.c b/dap-sdk/net/core/dap_worker.c
index c705989ffa..d4adb1437e 100644
--- a/dap-sdk/net/core/dap_worker.c
+++ b/dap-sdk/net/core/dap_worker.c
@@ -35,6 +35,11 @@
 #elif defined DAP_OS_WINDOWS
 #include <ws2tcpip.h>
 #endif
+
+#ifdef DAP_OS_DARWIN
+#define NOTE_READ NOTE_LOWAT
+#endif
+
 #include "dap_common.h"
 #include "dap_config.h"
 #include "dap_math_ops.h"
@@ -214,13 +219,22 @@ void *dap_worker_thread(void *arg)
             l_cur = l_worker->poll_esocket[n];
             //log_it(L_DEBUG, "flags: returned events 0x%0X requested events 0x%0X",l_worker->poll[n].revents,l_worker->poll[n].events );
 #elif defined (DAP_EVENTS_CAPS_KQUEUE)
-	    l_cur = (dap_events_socket_t*) l_worker->kqueue_events[n].udata;
-	    u_int l_cur_flags = l_worker->kqueue_events[n].fflags;
+        struct kevent * l_kevent_selected = l_cur->kqueue_event_catched = &l_worker->kqueue_events[n];
+        l_cur = (dap_events_socket_t*) l_kevent_selected->udata;
+        assert(l_cur);
+#ifndef DAP_OS_DARWIN
+            u_int l_cur_flags = l_kevent_selected->flags;
+#else
+            uint32_t l_cur_flags = l_kevent_selected->fflags;
+#endif
+
+
             l_flag_write = l_cur_flags & EVFILT_WRITE;
             l_flag_read  = l_cur_flags & EVFILT_READ;
-            if( !l_cur)
-        	continue;
-    	    l_cur->kqueue_event_catched = &l_worker->kqueue_events[n];
+            l_flag_error = l_cur_flags & EVFILT_EXCEPT;
+            l_flag_nval = l_flag_pri = l_flag_msg = l_flag_hup=  0;
+            l_flag_rdhup = l_cur_flags & EVFILT_EXCEPT & NOTE_DELETE;
+
 
 #else
 #error "Unimplemented fetch esocket after poll"
-- 
GitLab