aboutsummaryrefslogtreecommitdiff
path: root/src/lanes.c
diff options
context:
space:
mode:
authorBenoit Germain <bnt.germain@gmail.com>2018-10-25 14:03:32 +0200
committerBenoit Germain <bnt.germain@gmail.com>2018-10-25 14:03:32 +0200
commit9645a86fbcb16ca83c5f4ff0dae0925ac71bb46b (patch)
treec47a5f4ea54490ed969b5ebdf1a2174c29e49ff5 /src/lanes.c
parent1ec3f220f345f1c090a18adbaa90c0ead61e8ed3 (diff)
downloadlanes-9645a86fbcb16ca83c5f4ff0dae0925ac71bb46b.tar.gz
lanes-9645a86fbcb16ca83c5f4ff0dae0925ac71bb46b.tar.bz2
lanes-9645a86fbcb16ca83c5f4ff0dae0925ac71bb46b.zip
Fix Lanes build by reorganizing types around a bit
Diffstat (limited to 'src/lanes.c')
-rw-r--r--src/lanes.c134
1 files changed, 68 insertions, 66 deletions
diff --git a/src/lanes.c b/src/lanes.c
index 3268c8b..0a04d88 100644
--- a/src/lanes.c
+++ b/src/lanes.c
@@ -85,13 +85,14 @@ THE SOFTWARE.
85#include <stdio.h> 85#include <stdio.h>
86#include <stdlib.h> 86#include <stdlib.h>
87#include <ctype.h> 87#include <ctype.h>
88#include <assert.h>
88 89
90#include "lanes.h"
89#include "threading.h" 91#include "threading.h"
90#include "compat.h" 92#include "compat.h"
91#include "tools.h" 93#include "tools.h"
92#include "universe.h" 94#include "universe.h"
93#include "keeper.h" 95#include "keeper.h"
94#include "lanes.h"
95 96
96#if !(defined( PLATFORM_XBOX) || defined( PLATFORM_WIN32) || defined( PLATFORM_POCKETPC)) 97#if !(defined( PLATFORM_XBOX) || defined( PLATFORM_WIN32) || defined( PLATFORM_POCKETPC))
97# include <sys/time.h> 98# include <sys/time.h>
@@ -122,7 +123,7 @@ enum e_cancel_request
122// NOTE: values to be changed by either thread, during execution, without 123// NOTE: values to be changed by either thread, during execution, without
123// locking, are marked "volatile" 124// locking, are marked "volatile"
124// 125//
125struct s_lane 126struct s_Lane
126{ 127{
127 THREAD_T thread; 128 THREAD_T thread;
128 // 129 //
@@ -132,7 +133,7 @@ struct s_lane
132 char const* debug_name; 133 char const* debug_name;
133 134
134 lua_State* L; 135 lua_State* L;
135 struct s_Universe* U; 136 Universe* U;
136 // 137 //
137 // M: prepares the state, and reads results 138 // M: prepares the state, and reads results
138 // S: while S is running, M must keep out of modifying the state 139 // S: while S is running, M must keep out of modifying the state
@@ -172,29 +173,30 @@ struct s_lane
172 // M: sets to NORMAL, if issued a kill changes to KILLED 173 // M: sets to NORMAL, if issued a kill changes to KILLED
173 // S: not used 174 // S: not used
174 175
175 struct s_lane* volatile selfdestruct_next; 176 struct s_Lane* volatile selfdestruct_next;
176 // 177 //
177 // M: sets to non-NULL if facing lane handle '__gc' cycle but the lane 178 // M: sets to non-NULL if facing lane handle '__gc' cycle but the lane
178 // is still running 179 // is still running
179 // S: cleans up after itself if non-NULL at lane exit 180 // S: cleans up after itself if non-NULL at lane exit
180 181
181#if HAVE_LANE_TRACKING 182#if HAVE_LANE_TRACKING
182 struct s_lane* volatile tracking_next; 183 struct s_Lane* volatile tracking_next;
183#endif // HAVE_LANE_TRACKING 184#endif // HAVE_LANE_TRACKING
184 // 185 //
185 // For tracking only 186 // For tracking only
186}; 187};
188typedef struct s_Lane Lane;
187 189
188// To allow free-running threads (longer lifespan than the handle's) 190// To allow free-running threads (longer lifespan than the handle's)
189// 'struct s_lane' are malloc/free'd and the handle only carries a pointer. 191// 'Lane' are malloc/free'd and the handle only carries a pointer.
190// This is not deep userdata since the handle's not portable among lanes. 192// This is not deep userdata since the handle's not portable among lanes.
191// 193//
192#define lua_toLane( L, i) (*((struct s_lane**) luaL_checkudata( L, i, "Lane"))) 194#define lua_toLane( L, i) (*((Lane**) luaL_checkudata( L, i, "Lane")))
193 195
194#define CANCEL_TEST_KEY ((void*)get_lane_from_registry) // used as registry key 196#define CANCEL_TEST_KEY ((void*)get_lane_from_registry) // used as registry key
195static inline struct s_lane* get_lane_from_registry( lua_State* L) 197static inline Lane* get_lane_from_registry( lua_State* L)
196{ 198{
197 struct s_lane* s; 199 Lane* s;
198 STACK_GROW( L, 1); 200 STACK_GROW( L, 1);
199 STACK_CHECK( L); 201 STACK_CHECK( L);
200 lua_pushlightuserdata( L, CANCEL_TEST_KEY); 202 lua_pushlightuserdata( L, CANCEL_TEST_KEY);
@@ -206,7 +208,7 @@ static inline struct s_lane* get_lane_from_registry( lua_State* L)
206} 208}
207 209
208// intern the debug name in the specified lua state so that the pointer remains valid when the lane's state is closed 210// intern the debug name in the specified lua state so that the pointer remains valid when the lane's state is closed
209static void securize_debug_threadname( lua_State* L, struct s_lane* s) 211static void securize_debug_threadname( lua_State* L, Lane* s)
210{ 212{
211 STACK_CHECK( L); 213 STACK_CHECK( L);
212 STACK_GROW( L, 3); 214 STACK_GROW( L, 3);
@@ -231,7 +233,7 @@ static void securize_debug_threadname( lua_State* L, struct s_lane* s)
231*/ 233*/
232static inline enum e_cancel_request cancel_test( lua_State* L) 234static inline enum e_cancel_request cancel_test( lua_State* L)
233{ 235{
234 struct s_lane* const s = get_lane_from_registry( L); 236 Lane* const s = get_lane_from_registry( L);
235 // 's' is NULL for the original main state (and no-one can cancel that) 237 // 's' is NULL for the original main state (and no-one can cancel that)
236 return s ? s->cancel_request : CANCEL_NONE; 238 return s ? s->cancel_request : CANCEL_NONE;
237} 239}
@@ -320,15 +322,15 @@ static bool_t push_registry_table( lua_State* L, void* key, bool_t create)
320 322
321#if HAVE_LANE_TRACKING 323#if HAVE_LANE_TRACKING
322 324
323// The chain is ended by '(struct s_lane*)(-1)', not NULL: 325// The chain is ended by '(Lane*)(-1)', not NULL:
324// 'tracking_first -> ... -> ... -> (-1)' 326// 'tracking_first -> ... -> ... -> (-1)'
325#define TRACKING_END ((struct s_lane *)(-1)) 327#define TRACKING_END ((Lane *)(-1))
326 328
327/* 329/*
328 * Add the lane to tracking chain; the ones still running at the end of the 330 * Add the lane to tracking chain; the ones still running at the end of the
329 * whole process will be cancelled. 331 * whole process will be cancelled.
330 */ 332 */
331static void tracking_add( struct s_lane* s) 333static void tracking_add( Lane* s)
332{ 334{
333 335
334 MUTEX_LOCK( &s->U->tracking_cs); 336 MUTEX_LOCK( &s->U->tracking_cs);
@@ -344,7 +346,7 @@ static void tracking_add( struct s_lane* s)
344/* 346/*
345 * A free-running lane has ended; remove it from tracking chain 347 * A free-running lane has ended; remove it from tracking chain
346 */ 348 */
347static bool_t tracking_remove( struct s_lane* s) 349static bool_t tracking_remove( Lane* s)
348{ 350{
349 bool_t found = FALSE; 351 bool_t found = FALSE;
350 MUTEX_LOCK( &s->U->tracking_cs); 352 MUTEX_LOCK( &s->U->tracking_cs);
@@ -355,7 +357,7 @@ static bool_t tracking_remove( struct s_lane* s)
355 // 357 //
356 if( s->tracking_next != NULL) 358 if( s->tracking_next != NULL)
357 { 359 {
358 struct s_lane** ref = (struct s_lane**) &s->U->tracking_first; 360 Lane** ref = (Lane**) &s->U->tracking_first;
359 361
360 while( *ref != TRACKING_END) 362 while( *ref != TRACKING_END)
361 { 363 {
@@ -366,7 +368,7 @@ static bool_t tracking_remove( struct s_lane* s)
366 found = TRUE; 368 found = TRUE;
367 break; 369 break;
368 } 370 }
369 ref = (struct s_lane**) &((*ref)->tracking_next); 371 ref = (Lane**) &((*ref)->tracking_next);
370 } 372 }
371 assert( found); 373 assert( found);
372 } 374 }
@@ -380,7 +382,7 @@ static bool_t tracking_remove( struct s_lane* s)
380//--- 382//---
381// low-level cleanup 383// low-level cleanup
382 384
383static void lane_cleanup( struct s_lane* s) 385static void lane_cleanup( Lane* s)
384{ 386{
385 // Clean up after a (finished) thread 387 // Clean up after a (finished) thread
386 // 388 //
@@ -414,7 +416,7 @@ struct s_Linda
414{ 416{
415 SIGNAL_T read_happened; 417 SIGNAL_T read_happened;
416 SIGNAL_T write_happened; 418 SIGNAL_T write_happened;
417 struct s_Universe* U; // the universe this linda belongs to 419 Universe* U; // the universe this linda belongs to
418 ptrdiff_t group; // a group to control keeper allocation between lindas 420 ptrdiff_t group; // a group to control keeper allocation between lindas
419 enum e_cancel_request simulate_cancel; 421 enum e_cancel_request simulate_cancel;
420 char name[1]; 422 char name[1];
@@ -504,8 +506,8 @@ LUAG_FUNC( linda_send)
504 506
505 { 507 {
506 bool_t try_again = TRUE; 508 bool_t try_again = TRUE;
507 struct s_lane* const s = get_lane_from_registry( L); 509 Lane* const s = get_lane_from_registry( L);
508 struct s_Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); 510 Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda));
509 lua_State* KL = K ? K->L : NULL; // need to do this for 'STACK_CHECK' 511 lua_State* KL = K ? K->L : NULL; // need to do this for 'STACK_CHECK'
510 if( KL == NULL) return 0; 512 if( KL == NULL) return 0;
511 STACK_CHECK( KL); 513 STACK_CHECK( KL);
@@ -666,8 +668,8 @@ LUAG_FUNC( linda_receive)
666 668
667 { 669 {
668 bool_t try_again = TRUE; 670 bool_t try_again = TRUE;
669 struct s_lane* const s = get_lane_from_registry( L); 671 Lane* const s = get_lane_from_registry( L);
670 struct s_Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); 672 Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda));
671 if( K == NULL) return 0; 673 if( K == NULL) return 0;
672 for( ;;) 674 for( ;;)
673 { 675 {
@@ -770,7 +772,7 @@ LUAG_FUNC( linda_set)
770 check_key_types( L, 2, 2); 772 check_key_types( L, 2, 2);
771 773
772 { 774 {
773 struct s_Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); 775 Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda));
774 if( K == NULL) return 0; 776 if( K == NULL) return 0;
775 777
776 if( linda->simulate_cancel == CANCEL_NONE) 778 if( linda->simulate_cancel == CANCEL_NONE)
@@ -826,7 +828,7 @@ LUAG_FUNC( linda_count)
826 check_key_types( L, 2, lua_gettop( L)); 828 check_key_types( L, 2, lua_gettop( L));
827 829
828 { 830 {
829 struct s_Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); 831 Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda));
830 if( K == NULL) return 0; 832 if( K == NULL) return 0;
831 pushed = keeper_call( linda->U, K->L, KEEPER_API( count), L, linda, 2); 833 pushed = keeper_call( linda->U, K->L, KEEPER_API( count), L, linda, 2);
832 keeper_release( K); 834 keeper_release( K);
@@ -855,7 +857,7 @@ LUAG_FUNC( linda_get)
855 // make sure the key is of a valid type (throws an error if not the case) 857 // make sure the key is of a valid type (throws an error if not the case)
856 check_key_types( L, 2, 2); 858 check_key_types( L, 2, 2);
857 { 859 {
858 struct s_Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); 860 Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda));
859 if( K == NULL) return 0; 861 if( K == NULL) return 0;
860 862
861 if( linda->simulate_cancel == CANCEL_NONE) 863 if( linda->simulate_cancel == CANCEL_NONE)
@@ -904,7 +906,7 @@ LUAG_FUNC( linda_limit)
904 check_key_types( L, 2, 2); 906 check_key_types( L, 2, 2);
905 907
906 { 908 {
907 struct s_Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); 909 Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda));
908 if( K == NULL) return 0; 910 if( K == NULL) return 0;
909 911
910 if( linda->simulate_cancel == CANCEL_NONE) 912 if( linda->simulate_cancel == CANCEL_NONE)
@@ -939,7 +941,7 @@ LUAG_FUNC( linda_cancel)
939{ 941{
940 struct s_Linda* linda = lua_toLinda( L, 1); 942 struct s_Linda* linda = lua_toLinda( L, 1);
941 char const* who = luaL_optstring( L, 2, "both"); 943 char const* who = luaL_optstring( L, 2, "both");
942 struct s_Keeper* K; 944 Keeper* K;
943 945
944 // make sure we got 3 arguments: the linda, a key and a limit 946 // make sure we got 3 arguments: the linda, a key and a limit
945 luaL_argcheck( L, lua_gettop( L) <= 2, 2, "wrong number of arguments"); 947 luaL_argcheck( L, lua_gettop( L) <= 2, 2, "wrong number of arguments");
@@ -1171,7 +1173,7 @@ static void* linda_id( lua_State* L, enum eDeepOp op_)
1171 1173
1172 case eDO_delete: 1174 case eDO_delete:
1173 { 1175 {
1174 struct s_Keeper* K; 1176 Keeper* K;
1175 struct s_Linda* linda = lua_touserdata( L, 1); 1177 struct s_Linda* linda = lua_touserdata( L, 1);
1176 ASSERT_L( linda); 1178 ASSERT_L( linda);
1177 1179
@@ -1435,7 +1437,7 @@ typedef enum
1435 CR_Killed 1437 CR_Killed
1436} cancel_result; 1438} cancel_result;
1437 1439
1438static cancel_result thread_cancel( lua_State* L, struct s_lane* s, double secs, bool_t force, double waitkill_timeout_) 1440static cancel_result thread_cancel( lua_State* L, Lane* s, double secs, bool_t force, double waitkill_timeout_)
1439{ 1441{
1440 cancel_result result; 1442 cancel_result result;
1441 1443
@@ -1512,16 +1514,16 @@ static cancel_result thread_cancel( lua_State* L, struct s_lane* s, double secs,
1512 // 1514 //
1513 // Protects modifying the selfdestruct chain 1515 // Protects modifying the selfdestruct chain
1514 1516
1515#define SELFDESTRUCT_END ((struct s_lane*)(-1)) 1517#define SELFDESTRUCT_END ((Lane*)(-1))
1516 // 1518 //
1517 // The chain is ended by '(struct s_lane*)(-1)', not NULL: 1519 // The chain is ended by '(Lane*)(-1)', not NULL:
1518 // 'selfdestruct_first -> ... -> ... -> (-1)' 1520 // 'selfdestruct_first -> ... -> ... -> (-1)'
1519 1521
1520/* 1522/*
1521 * Add the lane to selfdestruct chain; the ones still running at the end of the 1523 * Add the lane to selfdestruct chain; the ones still running at the end of the
1522 * whole process will be cancelled. 1524 * whole process will be cancelled.
1523 */ 1525 */
1524static void selfdestruct_add( struct s_lane* s) 1526static void selfdestruct_add( Lane* s)
1525{ 1527{
1526 MUTEX_LOCK( &s->U->selfdestruct_cs); 1528 MUTEX_LOCK( &s->U->selfdestruct_cs);
1527 assert( s->selfdestruct_next == NULL); 1529 assert( s->selfdestruct_next == NULL);
@@ -1534,7 +1536,7 @@ static void selfdestruct_add( struct s_lane* s)
1534/* 1536/*
1535 * A free-running lane has ended; remove it from selfdestruct chain 1537 * A free-running lane has ended; remove it from selfdestruct chain
1536 */ 1538 */
1537static bool_t selfdestruct_remove( struct s_lane* s) 1539static bool_t selfdestruct_remove( Lane* s)
1538{ 1540{
1539 bool_t found = FALSE; 1541 bool_t found = FALSE;
1540 MUTEX_LOCK( &s->U->selfdestruct_cs); 1542 MUTEX_LOCK( &s->U->selfdestruct_cs);
@@ -1545,7 +1547,7 @@ static bool_t selfdestruct_remove( struct s_lane* s)
1545 // 1547 //
1546 if( s->selfdestruct_next != NULL) 1548 if( s->selfdestruct_next != NULL)
1547 { 1549 {
1548 struct s_lane** ref = (struct s_lane**) &s->U->selfdestruct_first; 1550 Lane** ref = (Lane**) &s->U->selfdestruct_first;
1549 1551
1550 while( *ref != SELFDESTRUCT_END ) 1552 while( *ref != SELFDESTRUCT_END )
1551 { 1553 {
@@ -1558,7 +1560,7 @@ static bool_t selfdestruct_remove( struct s_lane* s)
1558 found = TRUE; 1560 found = TRUE;
1559 break; 1561 break;
1560 } 1562 }
1561 ref = (struct s_lane**) &((*ref)->selfdestruct_next); 1563 ref = (Lane**) &((*ref)->selfdestruct_next);
1562 } 1564 }
1563 assert( found); 1565 assert( found);
1564 } 1566 }
@@ -1591,7 +1593,7 @@ void * protected_lua_Alloc( void *ud, void *ptr, size_t osize, size_t nsize)
1591*/ 1593*/
1592static int selfdestruct_gc( lua_State* L) 1594static int selfdestruct_gc( lua_State* L)
1593{ 1595{
1594 struct s_Universe* U = (struct s_Universe*) lua_touserdata( L, 1); 1596 Universe* U = (Universe*) lua_touserdata( L, 1);
1595 1597
1596 while( U->selfdestruct_first != SELFDESTRUCT_END) // true at most once! 1598 while( U->selfdestruct_first != SELFDESTRUCT_END) // true at most once!
1597 { 1599 {
@@ -1599,7 +1601,7 @@ static int selfdestruct_gc( lua_State* L)
1599 // 1601 //
1600 MUTEX_LOCK( &U->selfdestruct_cs); 1602 MUTEX_LOCK( &U->selfdestruct_cs);
1601 { 1603 {
1602 struct s_lane* s = U->selfdestruct_first; 1604 Lane* s = U->selfdestruct_first;
1603 while( s != SELFDESTRUCT_END) 1605 while( s != SELFDESTRUCT_END)
1604 { 1606 {
1605 // attempt a regular unforced hard cancel with a small timeout 1607 // attempt a regular unforced hard cancel with a small timeout
@@ -1645,7 +1647,7 @@ static int selfdestruct_gc( lua_State* L)
1645 double t_now = 0.0; 1647 double t_now = 0.0;
1646 MUTEX_LOCK( &U->selfdestruct_cs); 1648 MUTEX_LOCK( &U->selfdestruct_cs);
1647 { 1649 {
1648 struct s_lane* s = U->selfdestruct_first; 1650 Lane* s = U->selfdestruct_first;
1649 while( s != SELFDESTRUCT_END) 1651 while( s != SELFDESTRUCT_END)
1650 { 1652 {
1651 if( s->cancel_request == CANCEL_HARD) 1653 if( s->cancel_request == CANCEL_HARD)
@@ -1689,10 +1691,10 @@ static int selfdestruct_gc( lua_State* L)
1689 // these are not running, and the state can be closed 1691 // these are not running, and the state can be closed
1690 MUTEX_LOCK( &U->selfdestruct_cs); 1692 MUTEX_LOCK( &U->selfdestruct_cs);
1691 { 1693 {
1692 struct s_lane* s = U->selfdestruct_first; 1694 Lane* s = U->selfdestruct_first;
1693 while( s != SELFDESTRUCT_END) 1695 while( s != SELFDESTRUCT_END)
1694 { 1696 {
1695 struct s_lane* next_s = s->selfdestruct_next; 1697 Lane* next_s = s->selfdestruct_next;
1696 s->selfdestruct_next = NULL; // detach from selfdestruct chain 1698 s->selfdestruct_next = NULL; // detach from selfdestruct chain
1697 if( !THREAD_ISNULL( s->thread)) // can be NULL if previous 'soft' termination succeeded 1699 if( !THREAD_ISNULL( s->thread)) // can be NULL if previous 'soft' termination succeeded
1698 { 1700 {
@@ -1719,7 +1721,7 @@ static int selfdestruct_gc( lua_State* L)
1719 lua_settop( L, 0); 1721 lua_settop( L, 0);
1720 // no need to mutex-protect this as all threads in the universe are gone at that point 1722 // no need to mutex-protect this as all threads in the universe are gone at that point
1721 -- U->timer_deep->refcount; // should be 0 now 1723 -- U->timer_deep->refcount; // should be 0 now
1722 free_deep_prelude( L, (struct DEEP_PRELUDE*) U->timer_deep); 1724 free_deep_prelude( L, (DeepPrelude*) U->timer_deep);
1723 U->timer_deep = NULL; 1725 U->timer_deep = NULL;
1724 1726
1725 close_keepers( U, L); 1727 close_keepers( U, L);
@@ -1959,7 +1961,7 @@ static void push_stack_trace( lua_State* L, int rc_, int stk_base_)
1959LUAG_FUNC( set_debug_threadname) 1961LUAG_FUNC( set_debug_threadname)
1960{ 1962{
1961 // C s_lane structure is a light userdata upvalue 1963 // C s_lane structure is a light userdata upvalue
1962 struct s_lane* s = lua_touserdata( L, lua_upvalueindex( 1)); 1964 Lane* s = lua_touserdata( L, lua_upvalueindex( 1));
1963 luaL_checktype( L, -1, LUA_TSTRING); // "name" 1965 luaL_checktype( L, -1, LUA_TSTRING); // "name"
1964 // store a hidden reference in the registry to make sure the string is kept around even if a lane decides to manually change the "decoda_name" global... 1966 // store a hidden reference in the registry to make sure the string is kept around even if a lane decides to manually change the "decoda_name" global...
1965 lua_pushlightuserdata( L, LG_set_debug_threadname); // "name" lud 1967 lua_pushlightuserdata( L, LG_set_debug_threadname); // "name" lud
@@ -1975,7 +1977,7 @@ LUAG_FUNC( set_debug_threadname)
1975 1977
1976LUAG_FUNC( get_debug_threadname) 1978LUAG_FUNC( get_debug_threadname)
1977{ 1979{
1978 struct s_lane* const s = lua_toLane( L, 1); 1980 Lane* const s = lua_toLane( L, 1);
1979 luaL_argcheck( L, lua_gettop( L) == 1, 2, "too many arguments"); 1981 luaL_argcheck( L, lua_gettop( L) == 1, 2, "too many arguments");
1980 lua_pushstring( L, s->debug_name); 1982 lua_pushstring( L, s->debug_name);
1981 return 1; 1983 return 1;
@@ -2031,7 +2033,7 @@ static char const* get_errcode_name( int _code)
2031#if THREADWAIT_METHOD == THREADWAIT_CONDVAR // implies THREADAPI == THREADAPI_PTHREAD 2033#if THREADWAIT_METHOD == THREADWAIT_CONDVAR // implies THREADAPI == THREADAPI_PTHREAD
2032static void thread_cleanup_handler( void* opaque) 2034static void thread_cleanup_handler( void* opaque)
2033{ 2035{
2034 struct s_lane* s= (struct s_lane*) opaque; 2036 Lane* s= (Lane*) opaque;
2035 MUTEX_LOCK( &s->done_lock); 2037 MUTEX_LOCK( &s->done_lock);
2036 s->status = CANCELLED; 2038 s->status = CANCELLED;
2037 SIGNAL_ONE( &s->done_signal); // wake up master (while 's->done_lock' is on) 2039 SIGNAL_ONE( &s->done_signal); // wake up master (while 's->done_lock' is on)
@@ -2041,12 +2043,12 @@ static void thread_cleanup_handler( void* opaque)
2041 2043
2042static THREAD_RETURN_T THREAD_CALLCONV lane_main( void* vs) 2044static THREAD_RETURN_T THREAD_CALLCONV lane_main( void* vs)
2043{ 2045{
2044 struct s_lane* s = (struct s_lane*) vs; 2046 Lane* s = (Lane*) vs;
2045 int rc, rc2; 2047 int rc, rc2;
2046 lua_State* L = s->L; 2048 lua_State* L = s->L;
2047 // Called with the lane function and arguments on the stack 2049 // Called with the lane function and arguments on the stack
2048 int const nargs = lua_gettop( L) - 1; 2050 int const nargs = lua_gettop( L) - 1;
2049 DEBUGSPEW_CODE( struct s_Universe* U = universe_get( L)); 2051 DEBUGSPEW_CODE( Universe* U = universe_get( L));
2050 THREAD_MAKE_ASYNCH_CANCELLABLE(); 2052 THREAD_MAKE_ASYNCH_CANCELLABLE();
2051 THREAD_CLEANUP_PUSH( thread_cleanup_handler, s); 2053 THREAD_CLEANUP_PUSH( thread_cleanup_handler, s);
2052 s->status = RUNNING; // PENDING -> RUNNING 2054 s->status = RUNNING; // PENDING -> RUNNING
@@ -2144,7 +2146,7 @@ LUAG_FUNC( require)
2144{ 2146{
2145 char const* name = lua_tostring( L, 1); 2147 char const* name = lua_tostring( L, 1);
2146 int const nargs = lua_gettop( L); 2148 int const nargs = lua_gettop( L);
2147 DEBUGSPEW_CODE( struct s_Universe* U = universe_get( L)); 2149 DEBUGSPEW_CODE( Universe* U = universe_get( L));
2148 STACK_CHECK( L); 2150 STACK_CHECK( L);
2149 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lanes.require %s BEGIN\n" INDENT_END, name)); 2151 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lanes.require %s BEGIN\n" INDENT_END, name));
2150 DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); 2152 DEBUGSPEW_CODE( ++ U->debugspew_indent_depth);
@@ -2170,7 +2172,7 @@ LUAG_FUNC( register)
2170 // ignore extra parameters, just in case 2172 // ignore extra parameters, just in case
2171 lua_settop( L, 2); 2173 lua_settop( L, 2);
2172 luaL_argcheck( L, (mod_type == LUA_TTABLE) || (mod_type == LUA_TFUNCTION), 2, "unexpected module type"); 2174 luaL_argcheck( L, (mod_type == LUA_TTABLE) || (mod_type == LUA_TFUNCTION), 2, "unexpected module type");
2173 DEBUGSPEW_CODE( struct s_Universe* U = universe_get( L)); 2175 DEBUGSPEW_CODE( Universe* U = universe_get( L));
2174 STACK_CHECK( L); // "name" mod_table 2176 STACK_CHECK( L); // "name" mod_table
2175 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lanes.register %s BEGIN\n" INDENT_END, name)); 2177 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lanes.register %s BEGIN\n" INDENT_END, name));
2176 DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); 2178 DEBUGSPEW_CODE( ++ U->debugspew_indent_depth);
@@ -2200,8 +2202,8 @@ LUAG_FUNC( thread_gc);
2200LUAG_FUNC( lane_new) 2202LUAG_FUNC( lane_new)
2201{ 2203{
2202 lua_State* L2; 2204 lua_State* L2;
2203 struct s_lane* s; 2205 Lane* s;
2204 struct s_lane** ud; 2206 Lane** ud;
2205 2207
2206 char const* libs_str = lua_tostring( L, 2); 2208 char const* libs_str = lua_tostring( L, 2);
2207 uint_t cancelstep_idx = luaG_optunsigned( L, 3, 0); 2209 uint_t cancelstep_idx = luaG_optunsigned( L, 3, 0);
@@ -2213,7 +2215,7 @@ LUAG_FUNC( lane_new)
2213 2215
2214#define FIXED_ARGS 8 2216#define FIXED_ARGS 8
2215 int const nargs = lua_gettop(L) - FIXED_ARGS; 2217 int const nargs = lua_gettop(L) - FIXED_ARGS;
2216 struct s_Universe* U = universe_get( L); 2218 Universe* U = universe_get( L);
2217 ASSERT_L( nargs >= 0); 2219 ASSERT_L( nargs >= 0);
2218 2220
2219 // public Lanes API accepts a generic range -3/+3 2221 // public Lanes API accepts a generic range -3/+3
@@ -2386,8 +2388,8 @@ LUAG_FUNC( lane_new)
2386 2388
2387 // 's' is allocated from heap, not Lua, since its life span may surpass the handle's (if free running thread) 2389 // 's' is allocated from heap, not Lua, since its life span may surpass the handle's (if free running thread)
2388 // 2390 //
2389 ud = lua_newuserdata( L, sizeof( struct s_lane*)); // func libs cancelstep priority globals package required gc_cb lane 2391 ud = lua_newuserdata( L, sizeof( Lane*)); // func libs cancelstep priority globals package required gc_cb lane
2390 s = *ud = (struct s_lane*) malloc( sizeof( struct s_lane)); 2392 s = *ud = (Lane*) malloc( sizeof( Lane));
2391 if( s == NULL) 2393 if( s == NULL)
2392 { 2394 {
2393 return luaL_error( L, "could not create lane: out of memory"); 2395 return luaL_error( L, "could not create lane: out of memory");
@@ -2470,7 +2472,7 @@ LUAG_FUNC( lane_new)
2470LUAG_FUNC( thread_gc) 2472LUAG_FUNC( thread_gc)
2471{ 2473{
2472 bool_t have_gc_cb = FALSE; 2474 bool_t have_gc_cb = FALSE;
2473 struct s_lane* s = lua_toLane( L, 1); // ud 2475 Lane* s = lua_toLane( L, 1); // ud
2474 2476
2475 // if there a gc callback? 2477 // if there a gc callback?
2476 lua_getuservalue( L, 1); // ud uservalue 2478 lua_getuservalue( L, 1); // ud uservalue
@@ -2547,7 +2549,7 @@ LUAG_FUNC( thread_gc)
2547// lane_h:cancel( [timeout] [, force [, forcekill_timeout]]) 2549// lane_h:cancel( [timeout] [, force [, forcekill_timeout]])
2548LUAG_FUNC( thread_cancel) 2550LUAG_FUNC( thread_cancel)
2549{ 2551{
2550 struct s_lane* s = lua_toLane( L, 1); 2552 Lane* s = lua_toLane( L, 1);
2551 double secs = 0.0; 2553 double secs = 0.0;
2552 int force_i = 2; 2554 int force_i = 2;
2553 int forcekill_timeout_i = 3; 2555 int forcekill_timeout_i = 3;
@@ -2604,7 +2606,7 @@ LUAG_FUNC( thread_cancel)
2604// / "error" finished at an error, error value is there 2606// / "error" finished at an error, error value is there
2605// / "cancelled" execution cancelled by M (state gone) 2607// / "cancelled" execution cancelled by M (state gone)
2606// 2608//
2607static char const * thread_status_string( struct s_lane* s) 2609static char const * thread_status_string( Lane* s)
2608{ 2610{
2609 enum e_status st = s->status; // read just once (volatile) 2611 enum e_status st = s->status; // read just once (volatile)
2610 char const* str = 2612 char const* str =
@@ -2618,7 +2620,7 @@ static char const * thread_status_string( struct s_lane* s)
2618 return str; 2620 return str;
2619} 2621}
2620 2622
2621static int push_thread_status( lua_State* L, struct s_lane* s) 2623static int push_thread_status( lua_State* L, Lane* s)
2622{ 2624{
2623 char const* const str = thread_status_string( s); 2625 char const* const str = thread_status_string( s);
2624 ASSERT_L( str); 2626 ASSERT_L( str);
@@ -2638,7 +2640,7 @@ static int push_thread_status( lua_State* L, struct s_lane* s)
2638// 2640//
2639LUAG_FUNC( thread_join) 2641LUAG_FUNC( thread_join)
2640{ 2642{
2641 struct s_lane* const s = lua_toLane( L, 1); 2643 Lane* const s = lua_toLane( L, 1);
2642 double wait_secs = luaL_optnumber( L, 2, -1.0); 2644 double wait_secs = luaL_optnumber( L, 2, -1.0);
2643 lua_State* L2 = s->L; 2645 lua_State* L2 = s->L;
2644 int ret; 2646 int ret;
@@ -2661,7 +2663,7 @@ LUAG_FUNC( thread_join)
2661 } 2663 }
2662 else 2664 else
2663 { 2665 {
2664 struct s_Universe* U = universe_get( L); 2666 Universe* U = universe_get( L);
2665 // debug_name is a pointer to string possibly interned in the lane's state, that no longer exists when the state is closed 2667 // debug_name is a pointer to string possibly interned in the lane's state, that no longer exists when the state is closed
2666 // so store it in the userdata uservalue at a key that can't possibly collide 2668 // so store it in the userdata uservalue at a key that can't possibly collide
2667 securize_debug_threadname( L, s); 2669 securize_debug_threadname( L, s);
@@ -2722,7 +2724,7 @@ LUAG_FUNC( thread_index)
2722 int const UD = 1; 2724 int const UD = 1;
2723 int const KEY = 2; 2725 int const KEY = 2;
2724 int const USR = 3; 2726 int const USR = 3;
2725 struct s_lane* const s = lua_toLane( L, UD); 2727 Lane* const s = lua_toLane( L, UD);
2726 ASSERT_L( lua_gettop( L) == 2); 2728 ASSERT_L( lua_gettop( L) == 2);
2727 2729
2728 STACK_GROW( L, 8); // up to 8 positions are needed in case of error propagation 2730 STACK_GROW( L, 8); // up to 8 positions are needed in case of error propagation
@@ -2873,14 +2875,14 @@ LUAG_FUNC( thread_index)
2873LUAG_FUNC( threads) 2875LUAG_FUNC( threads)
2874{ 2876{
2875 int const top = lua_gettop( L); 2877 int const top = lua_gettop( L);
2876 struct s_Universe* U = universe_get( L); 2878 Universe* U = universe_get( L);
2877 2879
2878 // List _all_ still running threads 2880 // List _all_ still running threads
2879 // 2881 //
2880 MUTEX_LOCK( &U->tracking_cs); 2882 MUTEX_LOCK( &U->tracking_cs);
2881 if( U->tracking_first && U->tracking_first != TRACKING_END) 2883 if( U->tracking_first && U->tracking_first != TRACKING_END)
2882 { 2884 {
2883 struct s_lane* s = U->tracking_first; 2885 Lane* s = U->tracking_first;
2884 lua_newtable( L); // {} 2886 lua_newtable( L); // {}
2885 while( s != TRACKING_END) 2887 while( s != TRACKING_END)
2886 { 2888 {
@@ -3025,7 +3027,7 @@ static volatile long s_initCount = 0;
3025// param 1: settings table 3027// param 1: settings table
3026LUAG_FUNC( configure) 3028LUAG_FUNC( configure)
3027{ 3029{
3028 struct s_Universe* U = universe_get( L); 3030 Universe* U = universe_get( L);
3029 bool_t const from_master_state = (U == NULL); 3031 bool_t const from_master_state = (U == NULL);
3030 char const* name = luaL_checkstring( L, lua_upvalueindex( 1)); 3032 char const* name = luaL_checkstring( L, lua_upvalueindex( 1));
3031 _ASSERT_L( L, lua_type( L, 1) == LUA_TTABLE); 3033 _ASSERT_L( L, lua_type( L, 1) == LUA_TTABLE);
@@ -3129,7 +3131,7 @@ LUAG_FUNC( configure)
3129 STACK_MID( L, 1); 3131 STACK_MID( L, 1);
3130 3132
3131 // Proxy userdata contents is only a 'DEEP_PRELUDE*' pointer 3133 // Proxy userdata contents is only a 'DEEP_PRELUDE*' pointer
3132 U->timer_deep = *(struct DEEP_PRELUDE**) lua_touserdata( L, -1); 3134 U->timer_deep = *(DeepPrelude**) lua_touserdata( L, -1);
3133 ASSERT_L( U->timer_deep && (U->timer_deep->refcount == 1) && U->timer_deep->deep && U->timer_deep->idfunc == linda_id); 3135 ASSERT_L( U->timer_deep && (U->timer_deep->refcount == 1) && U->timer_deep->deep && U->timer_deep->idfunc == linda_id);
3134 // increment refcount that this linda remains alive as long as the universe is. 3136 // increment refcount that this linda remains alive as long as the universe is.
3135 ++ U->timer_deep->refcount; 3137 ++ U->timer_deep->refcount;
@@ -3159,7 +3161,7 @@ LUAG_FUNC( configure)
3159 3161
3160 { 3162 {
3161 char const* errmsg; 3163 char const* errmsg;
3162 errmsg = push_deep_proxy( U, L, (struct DEEP_PRELUDE*) U->timer_deep, eLM_LaneBody);// settings M timer_deep 3164 errmsg = push_deep_proxy( U, L, (DeepPrelude*) U->timer_deep, eLM_LaneBody);// settings M timer_deep
3163 if( errmsg != NULL) 3165 if( errmsg != NULL)
3164 { 3166 {
3165 return luaL_error( L, errmsg); 3167 return luaL_error( L, errmsg);