diff options
author | Benoit Germain <bnt.germain@gmail.com> | 2018-11-09 17:36:35 +0100 |
---|---|---|
committer | Benoit Germain <bnt.germain@gmail.com> | 2018-11-09 17:36:35 +0100 |
commit | a3ec8e0b6b5cc88063893fd7226599727a41dd29 (patch) | |
tree | 8b7f5304b55a49c60bf0f1b4b540143373e00c42 /src | |
parent | b899b53286e9125f34fd7522f4a173ff12643b68 (diff) | |
download | lanes-a3ec8e0b6b5cc88063893fd7226599727a41dd29.tar.gz lanes-a3ec8e0b6b5cc88063893fd7226599727a41dd29.tar.bz2 lanes-a3ec8e0b6b5cc88063893fd7226599727a41dd29.zip |
new API lanes.set_thread_affinity(), and et_debug_threadname implemented with win32 pthread
Diffstat (limited to 'src')
-rw-r--r-- | src/lanes.c | 12 | ||||
-rw-r--r-- | src/lanes.lua | 3 | ||||
-rw-r--r-- | src/threading.c | 26 | ||||
-rw-r--r-- | src/threading.h | 1 |
4 files changed, 40 insertions, 2 deletions
diff --git a/src/lanes.c b/src/lanes.c index 72f9dd6..f43a2e6 100644 --- a/src/lanes.c +++ b/src/lanes.c | |||
@@ -2022,6 +2022,17 @@ LUAG_FUNC( set_thread_priority) | |||
2022 | return 0; | 2022 | return 0; |
2023 | } | 2023 | } |
2024 | 2024 | ||
2025 | LUAG_FUNC( set_thread_affinity) | ||
2026 | { | ||
2027 | lua_Integer affinity = luaL_checkinteger( L, 1); | ||
2028 | if( affinity <= 0) | ||
2029 | { | ||
2030 | return luaL_error( L, "invalid affinity (%d)", affinity); | ||
2031 | } | ||
2032 | THREAD_SET_AFFINITY( (unsigned int) affinity); | ||
2033 | return 0; | ||
2034 | } | ||
2035 | |||
2025 | #if USE_DEBUG_SPEW | 2036 | #if USE_DEBUG_SPEW |
2026 | // can't use direct LUA_x errcode indexing because the sequence is not the same between Lua 5.1 and 5.2 :-( | 2037 | // can't use direct LUA_x errcode indexing because the sequence is not the same between Lua 5.1 and 5.2 :-( |
2027 | // LUA_ERRERR doesn't have the same value | 2038 | // LUA_ERRERR doesn't have the same value |
@@ -2995,6 +3006,7 @@ static const struct luaL_Reg lanes_functions [] = { | |||
2995 | {"now_secs", LG_now_secs}, | 3006 | {"now_secs", LG_now_secs}, |
2996 | {"wakeup_conv", LG_wakeup_conv}, | 3007 | {"wakeup_conv", LG_wakeup_conv}, |
2997 | {"set_thread_priority", LG_set_thread_priority}, | 3008 | {"set_thread_priority", LG_set_thread_priority}, |
3009 | {"set_thread_affinity", LG_set_thread_affinity}, | ||
2998 | {"nameof", luaG_nameof}, | 3010 | {"nameof", luaG_nameof}, |
2999 | {"register", LG_register}, | 3011 | {"register", LG_register}, |
3000 | {"set_singlethreaded", LG_set_singlethreaded}, | 3012 | {"set_singlethreaded", LG_set_singlethreaded}, |
diff --git a/src/lanes.lua b/src/lanes.lua index b82ae2a..6779095 100644 --- a/src/lanes.lua +++ b/src/lanes.lua | |||
@@ -190,7 +190,7 @@ lanes.configure = function( settings_) | |||
190 | -- | 190 | -- |
191 | -- 'opt': .priority: int (-3..+3) smaller is lower priority (0 = default) | 191 | -- 'opt': .priority: int (-3..+3) smaller is lower priority (0 = default) |
192 | -- | 192 | -- |
193 | -- .cancelstep: bool | uint | 193 | -- .cancelstep: bool | uint |
194 | -- false: cancellation check only at pending Linda operations | 194 | -- false: cancellation check only at pending Linda operations |
195 | -- (send/receive) so no runtime performance penalty (default) | 195 | -- (send/receive) so no runtime performance penalty (default) |
196 | -- true: adequate cancellation check (same as 100) | 196 | -- true: adequate cancellation check (same as 100) |
@@ -723,6 +723,7 @@ lanes.configure = function( settings_) | |||
723 | lanes.set_singlethreaded = core.set_singlethreaded | 723 | lanes.set_singlethreaded = core.set_singlethreaded |
724 | lanes.threads = core.threads or function() error "lane tracking is not available" end -- core.threads isn't registered if settings.track_lanes is false | 724 | lanes.threads = core.threads or function() error "lane tracking is not available" end -- core.threads isn't registered if settings.track_lanes is false |
725 | lanes.set_thread_priority = core.set_thread_priority | 725 | lanes.set_thread_priority = core.set_thread_priority |
726 | lanes.set_thread_affinity = core.set_thread_affinity | ||
726 | lanes.timer = timer | 727 | lanes.timer = timer |
727 | lanes.timer_lane = timer_lane | 728 | lanes.timer_lane = timer_lane |
728 | lanes.timers = timers | 729 | lanes.timers = timers |
diff --git a/src/threading.c b/src/threading.c index 0a4c62a..c0e6a55 100644 --- a/src/threading.c +++ b/src/threading.c | |||
@@ -324,6 +324,13 @@ void THREAD_SET_PRIORITY( int prio) | |||
324 | } | 324 | } |
325 | } | 325 | } |
326 | 326 | ||
327 | void THREAD_SET_AFFINITY( unsigned int aff) | ||
328 | { | ||
329 | if( !SetThreadAffinityMask( GetCurrentThread(), aff)); | ||
330 | { | ||
331 | FAIL( "THREAD_SET_AFFINITY", GetLastError()); | ||
332 | } | ||
333 | } | ||
327 | 334 | ||
328 | bool_t THREAD_WAIT_IMPL( THREAD_T *ref, double secs) | 335 | bool_t THREAD_WAIT_IMPL( THREAD_T *ref, double secs) |
329 | { | 336 | { |
@@ -546,6 +553,7 @@ bool_t THREAD_WAIT_IMPL( THREAD_T *ref, double secs) | |||
546 | // On Linux, SCHED_RR and su privileges are required.. !-( | 553 | // On Linux, SCHED_RR and su privileges are required.. !-( |
547 | // | 554 | // |
548 | #include <errno.h> | 555 | #include <errno.h> |
556 | #include <sched.h> | ||
549 | 557 | ||
550 | # if (defined(__MINGW32__) || defined(__MINGW64__)) && defined pthread_attr_setschedpolicy | 558 | # if (defined(__MINGW32__) || defined(__MINGW64__)) && defined pthread_attr_setschedpolicy |
551 | # if pthread_attr_setschedpolicy( A, S) == ENOTSUP | 559 | # if pthread_attr_setschedpolicy( A, S) == ENOTSUP |
@@ -862,6 +870,22 @@ void THREAD_SET_PRIORITY( int prio) | |||
862 | } | 870 | } |
863 | } | 871 | } |
864 | 872 | ||
873 | void THREAD_SET_AFFINITY( unsigned int aff) | ||
874 | { | ||
875 | cpu_set_t cpuset; | ||
876 | int bit = 0; | ||
877 | CPU_ZERO( &cpuset); | ||
878 | while( aff != 0) | ||
879 | { | ||
880 | if( aff & 1) | ||
881 | { | ||
882 | CPU_SET( bit, &cpuset); | ||
883 | } | ||
884 | ++ bit; | ||
885 | aff >>= 1; | ||
886 | } | ||
887 | PT_CALL( pthread_setaffinity_np( pthread_self(), sizeof(cpu_set_t), &cpuset)); | ||
888 | } | ||
865 | 889 | ||
866 | /* | 890 | /* |
867 | * Wait for a thread to finish. | 891 | * Wait for a thread to finish. |
@@ -959,7 +983,7 @@ bool_t THREAD_WAIT( THREAD_T *ref, double secs , SIGNAL_T *signal_ref, MUTEX_T * | |||
959 | #elif defined PLATFORM_OSX | 983 | #elif defined PLATFORM_OSX |
960 | pthread_setname_np(_name); | 984 | pthread_setname_np(_name); |
961 | #elif defined PLATFORM_WIN32 || defined PLATFORM_POCKETPC | 985 | #elif defined PLATFORM_WIN32 || defined PLATFORM_POCKETPC |
962 | // no API in win32-pthread yet :-( | 986 | PT_CALL( pthread_setname_np( pthread_self(), _name)); |
963 | #endif | 987 | #endif |
964 | } | 988 | } |
965 | #endif // THREADAPI == THREADAPI_PTHREAD | 989 | #endif // THREADAPI == THREADAPI_PTHREAD |
diff --git a/src/threading.h b/src/threading.h index 4114dba..8ebe805 100644 --- a/src/threading.h +++ b/src/threading.h | |||
@@ -255,5 +255,6 @@ void THREAD_KILL( THREAD_T* ref); | |||
255 | void THREAD_SETNAME( char const* _name); | 255 | void THREAD_SETNAME( char const* _name); |
256 | void THREAD_MAKE_ASYNCH_CANCELLABLE(); | 256 | void THREAD_MAKE_ASYNCH_CANCELLABLE(); |
257 | void THREAD_SET_PRIORITY( int prio); | 257 | void THREAD_SET_PRIORITY( int prio); |
258 | void THREAD_SET_AFFINITY( unsigned int aff); | ||
258 | 259 | ||
259 | #endif // __threading_h__ | 260 | #endif // __threading_h__ |