From cede738575ec01a7bbd3559a1c564609b949bebf Mon Sep 17 00:00:00 2001 From: "Konstantin S. Matveyev" Date: Mon, 26 Nov 2018 16:03:02 +0300 Subject: 'Linking error while embedding Lanes' on Linux fix --- src/threading.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/threading.c b/src/threading.c index c0e6a55..d5c127f 100644 --- a/src/threading.c +++ b/src/threading.c @@ -34,6 +34,12 @@ THE SOFTWARE. =============================================================================== */ +#include "platform.h" +#if defined(PLATFORM_LINUX) +# define _GNU_SOURCE /* must be defined before */ +# include +#endif + #include #include #include -- cgit v1.2.3-55-g6feb From 1e8656f3d9f920b23a5106de2dd6167471ccbdb2 Mon Sep 17 00:00:00 2001 From: valid-ptr Date: Mon, 26 Nov 2018 18:27:26 +0300 Subject: Compilation of 'threading.c' fixed for macos --- src/threading.c | 4 ++++ src/threading_osx.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/threading_osx.h diff --git a/src/threading.c b/src/threading.c index c0e6a55..7b75986 100644 --- a/src/threading.c +++ b/src/threading.c @@ -58,6 +58,10 @@ THE SOFTWARE. volatile bool_t sudo; #endif +#ifdef PLATFORM_OSX +# include "threading_osx.h" +#endif + /* Linux with older glibc (such as Debian) don't have pthread_setname_np, but have prctl */ #if defined PLATFORM_LINUX diff --git a/src/threading_osx.h b/src/threading_osx.h new file mode 100644 index 0000000..93da8c3 --- /dev/null +++ b/src/threading_osx.h @@ -0,0 +1,59 @@ +/* + * THREADING_OSX.H + * http://yyshen.github.io/2015/01/18/binding_threads_to_cores_osx.html + */ +#ifndef __threading_osx_h__ +#define __threading_osx_h__ 1 + +#include +#include +#include + +#define SYSCTL_CORE_COUNT "machdep.cpu.core_count" + +typedef struct cpu_set { + uint32_t count; +} cpu_set_t; + +static inline void CPU_ZERO(cpu_set_t *cs) { cs->count = 0; } +static inline void CPU_SET(int num, cpu_set_t *cs) { cs->count |= (1 << num); } +static inline int CPU_ISSET(int num, cpu_set_t *cs) { return (cs->count & (1 << num)); } + +int sched_getaffinity(pid_t pid, size_t cpu_size, cpu_set_t *cpu_set) +{ + int32_t core_count = 0; + size_t len = sizeof(core_count); + int ret = sysctlbyname(SYSCTL_CORE_COUNT, &core_count, &len, 0, 0); + if (ret) + { +// printf("error while get core count %d\n", ret); + return -1; + } + cpu_set->count = 0; + for (int i = 0; i < core_count; i++) + { + cpu_set->count |= (1 << i); + } + + return 0; +} + +int pthread_setaffinity_np(pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set) +{ + thread_port_t mach_thread; + int core = 0; + + for (core = 0; core < 8 * cpu_size; core++) + { + if (CPU_ISSET(core, cpu_set)) + break; + } +// printf("binding to core %d\n", core); + thread_affinity_policy_data_t policy = { core }; + mach_thread = pthread_mach_thread_np(thread); + thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 1); + + return 0; +} + +#endif -- cgit v1.2.3-55-g6feb From 24f8f79350725f1b75e550c85246288506365a13 Mon Sep 17 00:00:00 2001 From: "Konstantin S. Matveyev" Date: Mon, 26 Nov 2018 18:53:39 +0300 Subject: 'Linking error while embedding Lanes' on Linux fix simplified --- src/threading.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/threading.c b/src/threading.c index d5c127f..9aa04af 100644 --- a/src/threading.c +++ b/src/threading.c @@ -34,10 +34,8 @@ THE SOFTWARE. =============================================================================== */ -#include "platform.h" -#if defined(PLATFORM_LINUX) -# define _GNU_SOURCE /* must be defined before */ -# include +#if defined(__linux__) +# define _GNU_SOURCE /* must be defined before any include */ #endif #include -- cgit v1.2.3-55-g6feb From b9375bf2b9f59250dc46187b705c9abd90c5d3bc Mon Sep 17 00:00:00 2001 From: valid-ptr Date: Tue, 27 Nov 2018 11:24:57 +0300 Subject: Trailing whitespaces deleted from lanes.lua --- src/lanes.lua | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/lanes.lua b/src/lanes.lua index 7f2560b..ba9da81 100644 --- a/src/lanes.lua +++ b/src/lanes.lua @@ -55,7 +55,7 @@ lanes.configure = function( settings_) end -- Configure called so remove metatable from lanes setmetatable( lanes, nil) - -- + -- -- Cache globals for code that might run under sandboxing -- local assert = assert( assert) @@ -372,7 +372,7 @@ lanes.configure = function( settings_) -- timer tables and sleep in between the timer events. All interaction with -- the timer lane happens via a 'timer_gateway' Linda, which is common to -- all that 'require "lanes"'. - -- + -- -- Linda protocol to timer lane: -- -- TGW_KEY: linda_h, key, [wakeup_at_secs], [repeat_secs] @@ -403,7 +403,7 @@ lanes.configure = function( settings_) local timer_body = function() set_debug_threadname( "LanesTimer") -- - -- { [deep_linda_lightuserdata]= { [deep_linda_lightuserdata]=linda_h, + -- { [deep_linda_lightuserdata]= { [deep_linda_lightuserdata]=linda_h, -- [key]= { wakeup_secs [,period_secs] } [, ...] }, -- } -- @@ -452,7 +452,7 @@ lanes.configure = function( settings_) t1 = { [linda_deep] = linda} -- proxy to use the Linda collection[linda_deep] = t1 end - + if wakeup_at == nil then -- Clear the timer -- @@ -482,7 +482,7 @@ lanes.configure = function( settings_) t2= {} t1[key]= t2 end - + t2[1] = wakeup_at t2[2] = period -- can be 'nil' end @@ -507,12 +507,12 @@ lanes.configure = function( settings_) local wakeup_at= t2[1] local period= t2[2] -- may be 'nil' - if wakeup_at <= now then + if wakeup_at <= now then local linda= t1[linda_deep] assert(linda) - + linda:set( key, now ) - + -- 'pairs()' allows the values to be modified (and even -- removed) as far as keys are not touched @@ -531,10 +531,10 @@ lanes.configure = function( settings_) t2[1]= wakeup_at end end - + if wakeup_at and ((not next_wakeup) or (wakeup_at < next_wakeup)) then next_wakeup= wakeup_at - end + end end end -- t2 loop end -- t1 loop -- cgit v1.2.3-55-g6feb From da2a4c3f2160b2a3103609f8c593b4f7af2752d2 Mon Sep 17 00:00:00 2001 From: valid-ptr Date: Tue, 27 Nov 2018 13:39:03 +0300 Subject: Threads compilation for Android fixed; Thread can't be killed to date (warning in logcat). --- src/threading.c | 18 ++++++++++++++++++ src/threading.h | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/threading.c b/src/threading.c index ffd063f..1eab52f 100644 --- a/src/threading.c +++ b/src/threading.c @@ -36,6 +36,10 @@ THE SOFTWARE. */ #if defined(__linux__) # define _GNU_SOURCE /* must be defined before any include */ +# ifdef __ANDROID__ +# include +# define LOG_TAG "LuaLanes" +# endif #endif #include @@ -797,7 +801,9 @@ void THREAD_CREATE( THREAD_T* ref, THREAD_RETURN_T (*func)( void*), void* data, // "The specified scheduling parameters are only used if the scheduling // parameter inheritance attribute is PTHREAD_EXPLICIT_SCHED." // +#if !defined __ANDROID__ || ( defined __ANDROID__ && __ANDROID_API__ >= 28 ) PT_CALL( pthread_attr_setinheritsched( &a, PTHREAD_EXPLICIT_SCHED)); +#endif #ifdef _PRIO_SCOPE PT_CALL( pthread_attr_setscope( &a, _PRIO_SCOPE)); @@ -892,7 +898,11 @@ void THREAD_SET_AFFINITY( unsigned int aff) ++ bit; aff >>= 1; } +#ifdef __ANDROID__ + PT_CALL( sched_setaffinity( pthread_self(), sizeof(cpu_set_t), &cpuset)); +#else PT_CALL( pthread_setaffinity_np( pthread_self(), sizeof(cpu_set_t), &cpuset)); +#endif } /* @@ -963,15 +973,23 @@ bool_t THREAD_WAIT( THREAD_T *ref, double secs , SIGNAL_T *signal_ref, MUTEX_T * } // void THREAD_KILL( THREAD_T *ref ) { +#ifdef __ANDROID__ + __android_log_print(ANDROID_LOG_WARN, LOG_TAG, "Cannot kill thread!"); +#else pthread_cancel( *ref ); +#endif } void THREAD_MAKE_ASYNCH_CANCELLABLE() { +#ifdef __ANDROID__ + __android_log_print(ANDROID_LOG_WARN, LOG_TAG, "Cannot make thread async cancellable!"); +#else // that's the default, but just in case... pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); // we want cancellation to take effect immediately if possible, instead of waiting for a cancellation point (which is the default) pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NULL); +#endif } void THREAD_SETNAME( char const* _name) diff --git a/src/threading.h b/src/threading.h index 8ebe805..063cc82 100644 --- a/src/threading.h +++ b/src/threading.h @@ -143,7 +143,7 @@ enum e_status { PENDING, RUNNING, WAITING, DONE, ERROR_ST, CANCELLED }; // #if defined( PLATFORM_OSX) #define YIELD() pthread_yield_np() - #elif defined( PLATFORM_WIN32) || defined( PLATFORM_POCKETPC) // no PTHREAD for PLATFORM_XBOX +#elif defined( PLATFORM_WIN32) || defined( PLATFORM_POCKETPC) || defined(__ANDROID__) // no PTHREAD for PLATFORM_XBOX // for some reason win32-pthread doesn't have pthread_yield(), but sched_yield() #define YIELD() sched_yield() #else -- cgit v1.2.3-55-g6feb