diff options
-rw-r--r-- | CHANGES | 4 | ||||
-rw-r--r-- | docs/index.html | 16 | ||||
-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 |
6 files changed, 60 insertions, 2 deletions
@@ -1,5 +1,9 @@ | |||
1 | CHANGES: | 1 | CHANGES: |
2 | 2 | ||
3 | CHANGE 134: BGe 3-Dec-13 | ||
4 | * new API lanes.set_thread_affinity() | ||
5 | * set_debug_threadname implemented with win32 pthread | ||
6 | |||
3 | CHANGE 133: BGe 8-Nov-18 | 7 | CHANGE 133: BGe 8-Nov-18 |
4 | * Make sure any linda operation that can raise an error won't ever leave a mutex unreleased | 8 | * Make sure any linda operation that can raise an error won't ever leave a mutex unreleased |
5 | * lane:join() now returns nil, "timeout" in case of timeout | 9 | * lane:join() now returns nil, "timeout" in case of timeout |
diff --git a/docs/index.html b/docs/index.html index ef227c8..1b7477f 100644 --- a/docs/index.html +++ b/docs/index.html | |||
@@ -709,6 +709,22 @@ | |||
709 | </p> | 709 | </p> |
710 | 710 | ||
711 | 711 | ||
712 | <!-- affinity +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
713 | <hr/> | ||
714 | <h2 id="affinity">Affinity</h2> | ||
715 | |||
716 | <table border="1" bgcolor="#E0E0FF" cellpadding="10" style="width:50%"> | ||
717 | <tr> | ||
718 | <td> | ||
719 | <pre> lanes.set_thread_affinity( affinity)</pre> | ||
720 | </td> | ||
721 | </tr> | ||
722 | </table> | ||
723 | <p> | ||
724 | Each thread can change its own affinity at will. This is also true for the main Lua state. | ||
725 | </p> | ||
726 | |||
727 | |||
712 | <!-- status +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 728 | <!-- status +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
713 | <hr/> | 729 | <hr/> |
714 | <h2 id="status">Status</h2> | 730 | <h2 id="status">Status</h2> |
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__ |