aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBenoit Germain <bnt.germain@gmail.com>2022-03-09 14:11:21 +0100
committerBenoit Germain <bnt.germain@gmail.com>2022-03-09 14:11:21 +0100
commit49ef9d50d475921aab0c50b13b857f8cb990fcc0 (patch)
tree0b5e4deefd63481e99557dd326352a4bb8cb5dc0 /src
parentfe44f7e83fc0b3264533caaa3085938d78c3750b (diff)
downloadlanes-49ef9d50d475921aab0c50b13b857f8cb990fcc0.tar.gz
lanes-49ef9d50d475921aab0c50b13b857f8cb990fcc0.tar.bz2
lanes-49ef9d50d475921aab0c50b13b857f8cb990fcc0.zip
moonjit support
Diffstat (limited to 'src')
-rw-r--r--src/compat.h25
-rw-r--r--src/keeper.c12
-rw-r--r--src/lanes.c44
-rw-r--r--src/lanes_private.h5
-rw-r--r--src/linda.c12
-rw-r--r--src/macros_and_utils.h20
-rw-r--r--src/state.c15
-rw-r--r--src/tools.c4
-rw-r--r--src/uniquekey.h8
-rw-r--r--src/universe.h11
10 files changed, 87 insertions, 69 deletions
diff --git a/src/compat.h b/src/compat.h
index eaa9f85..e44f827 100644
--- a/src/compat.h
+++ b/src/compat.h
@@ -5,13 +5,29 @@
5#include "lualib.h" 5#include "lualib.h"
6#include "lauxlib.h" 6#include "lauxlib.h"
7 7
8// try to detect if we are building against LuaJIT or MoonJIT
9#if defined(LUA_JITLIBNAME)
10#include "luajit.h"
11#if (defined(__x86_64__) || defined(_M_X64) || defined(__LP64__))
12#define LUAJIT_FLAVOR() 64
13#else // 64 bits
14#define LUAJIT_FLAVOR() 32
15#endif // 64 bits
16#else // LUA_JITLIBNAME
17#define LUAJIT_FLAVOR() 0
18#endif // LUA_JITLIBNAME
19
20
8// code is now preferring Lua 5.4 API 21// code is now preferring Lua 5.4 API
9 22
10// add some Lua 5.3-style API when building for Lua 5.1 23// add some Lua 5.3-style API when building for Lua 5.1
11#if LUA_VERSION_NUM == 501 24#if LUA_VERSION_NUM == 501
25
12#define lua501_equal lua_equal 26#define lua501_equal lua_equal
13#define lua_absindex( L, idx) (((idx) >= 0 || (idx) <= LUA_REGISTRYINDEX) ? (idx) : lua_gettop(L) + (idx) +1) 27#define lua_absindex( L, idx) (((idx) >= 0 || (idx) <= LUA_REGISTRYINDEX) ? (idx) : lua_gettop(L) + (idx) +1)
28#if LUAJIT_VERSION_NUM < 20200 // moonjit is 5.1 plus bits of 5.2 that we don't need to wrap
14#define lua_pushglobaltable(L) lua_pushvalue( L, LUA_GLOBALSINDEX) 29#define lua_pushglobaltable(L) lua_pushvalue( L, LUA_GLOBALSINDEX)
30#endif // LUAJIT_VERSION_NUM
15#define lua_setuservalue lua_setfenv 31#define lua_setuservalue lua_setfenv
16#define lua_getuservalue lua_getfenv 32#define lua_getuservalue lua_getfenv
17#define lua_rawlen lua_objlen 33#define lua_rawlen lua_objlen
@@ -21,10 +37,12 @@
21void luaL_requiref (lua_State* L, const char* modname, lua_CFunction openf, int glb); // implementation copied from Lua 5.2 sources 37void luaL_requiref (lua_State* L, const char* modname, lua_CFunction openf, int glb); // implementation copied from Lua 5.2 sources
22#define lua504_dump( L, writer, data, strip) lua_dump( L, writer, data) 38#define lua504_dump( L, writer, data, strip) lua_dump( L, writer, data)
23#define LUA_LOADED_TABLE "_LOADED" // // doesn't exist in Lua 5.1 39#define LUA_LOADED_TABLE "_LOADED" // // doesn't exist in Lua 5.1
40
24#endif // LUA_VERSION_NUM == 501 41#endif // LUA_VERSION_NUM == 501
25 42
26// wrap Lua 5.2 calls under Lua 5.1 API when it is simpler that way 43// wrap Lua 5.2 calls under Lua 5.1 API when it is simpler that way
27#if LUA_VERSION_NUM == 502 44#if LUA_VERSION_NUM == 502
45
28#ifndef lua501_equal // already defined when compatibility is active in luaconf.h 46#ifndef lua501_equal // already defined when compatibility is active in luaconf.h
29#define lua501_equal( L, a, b) lua_compare( L, a, b, LUA_OPEQ) 47#define lua501_equal( L, a, b) lua_compare( L, a, b, LUA_OPEQ)
30#endif // lua501_equal 48#endif // lua501_equal
@@ -34,10 +52,12 @@ void luaL_requiref (lua_State* L, const char* modname, lua_CFunction openf, int
34#define luaG_registerlibfuncs( L, _funcs) luaL_setfuncs( L, _funcs, 0) 52#define luaG_registerlibfuncs( L, _funcs) luaL_setfuncs( L, _funcs, 0)
35#define lua504_dump( L, writer, data, strip) lua_dump( L, writer, data) 53#define lua504_dump( L, writer, data, strip) lua_dump( L, writer, data)
36#define LUA_LOADED_TABLE "_LOADED" // // doesn't exist in Lua 5.2 54#define LUA_LOADED_TABLE "_LOADED" // // doesn't exist in Lua 5.2
55
37#endif // LUA_VERSION_NUM == 502 56#endif // LUA_VERSION_NUM == 502
38 57
39// wrap Lua 5.3 calls under Lua 5.1 API when it is simpler that way 58// wrap Lua 5.3 calls under Lua 5.1 API when it is simpler that way
40#if LUA_VERSION_NUM == 503 59#if LUA_VERSION_NUM == 503
60
41#ifndef lua501_equal // already defined when compatibility is active in luaconf.h 61#ifndef lua501_equal // already defined when compatibility is active in luaconf.h
42#define lua501_equal( L, a, b) lua_compare( L, a, b, LUA_OPEQ) 62#define lua501_equal( L, a, b) lua_compare( L, a, b, LUA_OPEQ)
43#endif // lua501_equal 63#endif // lua501_equal
@@ -47,16 +67,20 @@ void luaL_requiref (lua_State* L, const char* modname, lua_CFunction openf, int
47#define luaG_registerlibfuncs( L, _funcs) luaL_setfuncs( L, _funcs, 0) 67#define luaG_registerlibfuncs( L, _funcs) luaL_setfuncs( L, _funcs, 0)
48#define lua504_dump lua_dump 68#define lua504_dump lua_dump
49#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 69#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
70
50#endif // LUA_VERSION_NUM == 503 71#endif // LUA_VERSION_NUM == 503
51 72
52#if LUA_VERSION_NUM < 504 73#if LUA_VERSION_NUM < 504
74
53void *lua_newuserdatauv( lua_State* L, size_t sz, int nuvalue); 75void *lua_newuserdatauv( lua_State* L, size_t sz, int nuvalue);
54int lua_getiuservalue( lua_State* L, int idx, int n); 76int lua_getiuservalue( lua_State* L, int idx, int n);
55int lua_setiuservalue( lua_State* L, int idx, int n); 77int lua_setiuservalue( lua_State* L, int idx, int n);
78
56#endif // LUA_VERSION_NUM < 504 79#endif // LUA_VERSION_NUM < 504
57 80
58// wrap Lua 5.4 calls under Lua 5.1 API when it is simpler that way 81// wrap Lua 5.4 calls under Lua 5.1 API when it is simpler that way
59#if LUA_VERSION_NUM == 504 82#if LUA_VERSION_NUM == 504
83
60#ifndef lua501_equal // already defined when compatibility is active in luaconf.h 84#ifndef lua501_equal // already defined when compatibility is active in luaconf.h
61#define lua501_equal( L, a, b) lua_compare( L, a, b, LUA_OPEQ) 85#define lua501_equal( L, a, b) lua_compare( L, a, b, LUA_OPEQ)
62#endif // lua501_equal 86#endif // lua501_equal
@@ -66,6 +90,7 @@ int lua_setiuservalue( lua_State* L, int idx, int n);
66#define luaG_registerlibfuncs( L, _funcs) luaL_setfuncs( L, _funcs, 0) 90#define luaG_registerlibfuncs( L, _funcs) luaL_setfuncs( L, _funcs, 0)
67#define lua504_dump lua_dump 91#define lua504_dump lua_dump
68#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 92#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
93
69#endif // LUA_VERSION_NUM == 504 94#endif // LUA_VERSION_NUM == 504
70 95
71#endif // __COMPAT_H__ 96#endif // __COMPAT_H__
diff --git a/src/keeper.c b/src/keeper.c
index 3211c1b..19b9e1a 100644
--- a/src/keeper.c
+++ b/src/keeper.c
@@ -612,14 +612,14 @@ void close_keepers( Universe* U, lua_State* L)
612 // free the keeper bookkeeping structure 612 // free the keeper bookkeeping structure
613 { 613 {
614 // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly 614 // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly
615#if USE_LUA_STATE_ALLOCATOR 615#if USE_LUA_STATE_ALLOCATOR()
616 { 616 {
617 AllocatorDefinition* const allocD = &U->protected_allocator.definition; 617 AllocatorDefinition* const allocD = &U->protected_allocator.definition;
618 allocD->allocF( allocD->allocUD, U->keepers, sizeof( Keepers) + (nbKeepers - 1) * sizeof( Keeper), 0); 618 allocD->allocF( allocD->allocUD, U->keepers, sizeof( Keepers) + (nbKeepers - 1) * sizeof( Keeper), 0);
619 } 619 }
620#else // USE_LUA_STATE_ALLOCATOR 620#else // USE_LUA_STATE_ALLOCATOR()
621 free(U->keepers); 621 free(U->keepers);
622#endif // USE_LUA_STATE_ALLOCATOR 622#endif // USE_LUA_STATE_ALLOCATOR()
623 U->keepers = NULL; 623 U->keepers = NULL;
624 } 624 }
625 } 625 }
@@ -654,14 +654,14 @@ void init_keepers( Universe* U, lua_State* L)
654 { 654 {
655 size_t const bytes = sizeof( Keepers) + (nb_keepers - 1) * sizeof( Keeper); 655 size_t const bytes = sizeof( Keepers) + (nb_keepers - 1) * sizeof( Keeper);
656 // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly 656 // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly
657#if USE_LUA_STATE_ALLOCATOR 657#if USE_LUA_STATE_ALLOCATOR()
658 { 658 {
659 AllocatorDefinition* const allocD = &U->protected_allocator.definition; 659 AllocatorDefinition* const allocD = &U->protected_allocator.definition;
660 U->keepers = (Keepers*) allocD->allocF( allocD->allocUD, NULL, 0, bytes); 660 U->keepers = (Keepers*) allocD->allocF( allocD->allocUD, NULL, 0, bytes);
661 } 661 }
662#else // USE_LUA_STATE_ALLOCATOR 662#else // USE_LUA_STATE_ALLOCATOR()
663 U->keepers = (Keepers*)malloc(bytes); 663 U->keepers = (Keepers*)malloc(bytes);
664#endif // USE_LUA_STATE_ALLOCATOR 664#endif // USE_LUA_STATE_ALLOCATOR()
665 if( U->keepers == NULL) 665 if( U->keepers == NULL)
666 { 666 {
667 (void) luaL_error( L, "init_keepers() failed while creating keeper array; out of memory"); 667 (void) luaL_error( L, "init_keepers() failed while creating keeper array; out of memory");
diff --git a/src/lanes.c b/src/lanes.c
index f702685..cd1d4f1 100644
--- a/src/lanes.c
+++ b/src/lanes.c
@@ -174,7 +174,7 @@ static bool_t push_registry_table( lua_State* L, UniqueKey key, bool_t create)
174 return TRUE; // table pushed 174 return TRUE; // table pushed
175} 175}
176 176
177#if HAVE_LANE_TRACKING 177#if HAVE_LANE_TRACKING()
178 178
179// The chain is ended by '(Lane*)(-1)', not NULL: 179// The chain is ended by '(Lane*)(-1)', not NULL:
180// 'tracking_first -> ... -> ... -> (-1)' 180// 'tracking_first -> ... -> ... -> (-1)'
@@ -231,7 +231,7 @@ static bool_t tracking_remove( Lane* s)
231 return found; 231 return found;
232} 232}
233 233
234#endif // HAVE_LANE_TRACKING 234#endif // HAVE_LANE_TRACKING()
235 235
236//--- 236//---
237// low-level cleanup 237// low-level cleanup
@@ -245,23 +245,23 @@ static void lane_cleanup( Lane* s)
245 MUTEX_FREE( &s->done_lock); 245 MUTEX_FREE( &s->done_lock);
246#endif // THREADWAIT_METHOD == THREADWAIT_CONDVAR 246#endif // THREADWAIT_METHOD == THREADWAIT_CONDVAR
247 247
248#if HAVE_LANE_TRACKING 248#if HAVE_LANE_TRACKING()
249 if( s->U->tracking_first != NULL) 249 if( s->U->tracking_first != NULL)
250 { 250 {
251 // Lane was cleaned up, no need to handle at process termination 251 // Lane was cleaned up, no need to handle at process termination
252 tracking_remove( s); 252 tracking_remove( s);
253 } 253 }
254#endif // HAVE_LANE_TRACKING 254#endif // HAVE_LANE_TRACKING()
255 255
256 // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly 256 // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly
257#if USE_LUA_STATE_ALLOCATOR 257#if USE_LUA_STATE_ALLOCATOR()
258 { 258 {
259 AllocatorDefinition* const allocD = &s->U->protected_allocator.definition; 259 AllocatorDefinition* const allocD = &s->U->protected_allocator.definition;
260 allocD->allocF(allocD->allocUD, s, sizeof(Lane), 0); 260 allocD->allocF(allocD->allocUD, s, sizeof(Lane), 0);
261 } 261 }
262#else // USE_LUA_STATE_ALLOCATOR 262#else // USE_LUA_STATE_ALLOCATOR()
263 free(s); 263 free(s);
264#endif // USE_LUA_STATE_ALLOCATOR 264#endif // USE_LUA_STATE_ALLOCATOR()
265} 265}
266 266
267/* 267/*
@@ -589,9 +589,9 @@ static int selfdestruct_gc( lua_State* L)
589 // remove the protected allocator, if any 589 // remove the protected allocator, if any
590 cleanup_allocator_function( U, L); 590 cleanup_allocator_function( U, L);
591 591
592#if HAVE_LANE_TRACKING 592#if HAVE_LANE_TRACKING()
593 MUTEX_FREE( &U->tracking_cs); 593 MUTEX_FREE( &U->tracking_cs);
594#endif // HAVE_LANE_TRACKING 594#endif // HAVE_LANE_TRACKING()
595 // Linked chains handling 595 // Linked chains handling
596 MUTEX_FREE( &U->selfdestruct_cs); 596 MUTEX_FREE( &U->selfdestruct_cs);
597 MUTEX_FREE( &U->require_cs); 597 MUTEX_FREE( &U->require_cs);
@@ -844,7 +844,7 @@ LUAG_FUNC( set_thread_affinity)
844 return 0; 844 return 0;
845} 845}
846 846
847#if USE_DEBUG_SPEW 847#if USE_DEBUG_SPEW()
848// can't use direct LUA_x errcode indexing because the sequence is not the same between Lua 5.1 and 5.2 :-( 848// can't use direct LUA_x errcode indexing because the sequence is not the same between Lua 5.1 and 5.2 :-(
849// LUA_ERRERR doesn't have the same value 849// LUA_ERRERR doesn't have the same value
850struct errcode_name 850struct errcode_name
@@ -875,7 +875,7 @@ static char const* get_errcode_name( int _code)
875 } 875 }
876 return "<NULL>"; 876 return "<NULL>";
877} 877}
878#endif // USE_DEBUG_SPEW 878#endif // USE_DEBUG_SPEW()
879 879
880#if THREADWAIT_METHOD == THREADWAIT_CONDVAR // implies THREADAPI == THREADAPI_PTHREAD 880#if THREADWAIT_METHOD == THREADWAIT_CONDVAR // implies THREADAPI == THREADAPI_PTHREAD
881static void thread_cleanup_handler( void* opaque) 881static void thread_cleanup_handler( void* opaque)
@@ -1231,14 +1231,14 @@ LUAG_FUNC( lane_new)
1231 // a Lane full userdata needs a single uservalue 1231 // a Lane full userdata needs a single uservalue
1232 ud = lua_newuserdatauv( L, sizeof( Lane*), 1); // func libs priority globals package required gc_cb lane 1232 ud = lua_newuserdatauv( L, sizeof( Lane*), 1); // func libs priority globals package required gc_cb lane
1233 // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly 1233 // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly
1234#if USE_LUA_STATE_ALLOCATOR 1234#if USE_LUA_STATE_ALLOCATOR()
1235 { 1235 {
1236 AllocatorDefinition* const allocD = &U->protected_allocator.definition; 1236 AllocatorDefinition* const allocD = &U->protected_allocator.definition;
1237 s = *ud = (Lane*)allocD->allocF(allocD->allocUD, NULL, 0, sizeof(Lane)); 1237 s = *ud = (Lane*)allocD->allocF(allocD->allocUD, NULL, 0, sizeof(Lane));
1238 } 1238 }
1239#else // USE_LUA_STATE_ALLOCATOR 1239#else // USE_LUA_STATE_ALLOCATOR()
1240 s = *ud = (Lane*) malloc(sizeof(Lane)); 1240 s = *ud = (Lane*) malloc(sizeof(Lane));
1241#endif // USE_LUA_STATE_ALLOCATOR 1241#endif // USE_LUA_STATE_ALLOCATOR()
1242 if( s == NULL) 1242 if( s == NULL)
1243 { 1243 {
1244 return luaL_error( L, "could not create lane: out of memory"); 1244 return luaL_error( L, "could not create lane: out of memory");
@@ -1257,13 +1257,13 @@ LUAG_FUNC( lane_new)
1257#endif // THREADWAIT_METHOD == THREADWAIT_CONDVAR 1257#endif // THREADWAIT_METHOD == THREADWAIT_CONDVAR
1258 s->mstatus = NORMAL; 1258 s->mstatus = NORMAL;
1259 s->selfdestruct_next = NULL; 1259 s->selfdestruct_next = NULL;
1260#if HAVE_LANE_TRACKING 1260#if HAVE_LANE_TRACKING()
1261 s->tracking_next = NULL; 1261 s->tracking_next = NULL;
1262 if( s->U->tracking_first) 1262 if( s->U->tracking_first)
1263 { 1263 {
1264 tracking_add( s); 1264 tracking_add( s);
1265 } 1265 }
1266#endif // HAVE_LANE_TRACKING 1266#endif // HAVE_LANE_TRACKING()
1267 1267
1268 // Set metatable for the userdata 1268 // Set metatable for the userdata
1269 // 1269 //
@@ -1662,7 +1662,7 @@ LUAG_FUNC( thread_index)
1662 return 0; 1662 return 0;
1663} 1663}
1664 1664
1665#if HAVE_LANE_TRACKING 1665#if HAVE_LANE_TRACKING()
1666//--- 1666//---
1667// threads() -> {}|nil 1667// threads() -> {}|nil
1668// 1668//
@@ -1695,7 +1695,7 @@ LUAG_FUNC( threads)
1695 MUTEX_UNLOCK( &U->tracking_cs); 1695 MUTEX_UNLOCK( &U->tracking_cs);
1696 return lua_gettop( L) - top; // 0 or 1 1696 return lua_gettop( L) - top; // 0 or 1
1697} 1697}
1698#endif // HAVE_LANE_TRACKING 1698#endif // HAVE_LANE_TRACKING()
1699 1699
1700/* 1700/*
1701 * ############################################################################################### 1701 * ###############################################################################################
@@ -1893,12 +1893,12 @@ LUAG_FUNC( configure)
1893 lua_getfield( L, 1, "demote_full_userdata"); // settings demote_full_userdata 1893 lua_getfield( L, 1, "demote_full_userdata"); // settings demote_full_userdata
1894 U->demoteFullUserdata = lua_toboolean( L, -1); 1894 U->demoteFullUserdata = lua_toboolean( L, -1);
1895 lua_pop( L, 1); // settings 1895 lua_pop( L, 1); // settings
1896#if HAVE_LANE_TRACKING 1896#if HAVE_LANE_TRACKING()
1897 MUTEX_INIT( &U->tracking_cs); 1897 MUTEX_INIT( &U->tracking_cs);
1898 lua_getfield( L, 1, "track_lanes"); // settings track_lanes 1898 lua_getfield( L, 1, "track_lanes"); // settings track_lanes
1899 U->tracking_first = lua_toboolean( L, -1) ? TRACKING_END : NULL; 1899 U->tracking_first = lua_toboolean( L, -1) ? TRACKING_END : NULL;
1900 lua_pop( L, 1); // settings 1900 lua_pop( L, 1); // settings
1901#endif // HAVE_LANE_TRACKING 1901#endif // HAVE_LANE_TRACKING()
1902 // Linked chains handling 1902 // Linked chains handling
1903 MUTEX_INIT( &U->selfdestruct_cs); 1903 MUTEX_INIT( &U->selfdestruct_cs);
1904 MUTEX_RECURSIVE_INIT( &U->require_cs); 1904 MUTEX_RECURSIVE_INIT( &U->require_cs);
@@ -1935,14 +1935,14 @@ LUAG_FUNC( configure)
1935 lua_setfield( L, -2, "configure"); // settings M 1935 lua_setfield( L, -2, "configure"); // settings M
1936 // add functions to the module's table 1936 // add functions to the module's table
1937 luaG_registerlibfuncs( L, lanes_functions); 1937 luaG_registerlibfuncs( L, lanes_functions);
1938#if HAVE_LANE_TRACKING 1938#if HAVE_LANE_TRACKING()
1939 // register core.threads() only if settings say it should be available 1939 // register core.threads() only if settings say it should be available
1940 if( U->tracking_first != NULL) 1940 if( U->tracking_first != NULL)
1941 { 1941 {
1942 lua_pushcfunction( L, LG_threads); // settings M LG_threads() 1942 lua_pushcfunction( L, LG_threads); // settings M LG_threads()
1943 lua_setfield( L, -2, "threads"); // settings M 1943 lua_setfield( L, -2, "threads"); // settings M
1944 } 1944 }
1945#endif // HAVE_LANE_TRACKING 1945#endif // HAVE_LANE_TRACKING()
1946 STACK_MID( L, 2); 1946 STACK_MID( L, 2);
1947 1947
1948 { 1948 {
diff --git a/src/lanes_private.h b/src/lanes_private.h
index 7da3286..6717fe0 100644
--- a/src/lanes_private.h
+++ b/src/lanes_private.h
@@ -3,6 +3,7 @@
3 3
4#include "uniquekey.h" 4#include "uniquekey.h"
5#include "cancel.h" 5#include "cancel.h"
6#include "universe.h"
6 7
7// NOTE: values to be changed by either thread, during execution, without 8// NOTE: values to be changed by either thread, during execution, without
8// locking, are marked "volatile" 9// locking, are marked "volatile"
@@ -63,9 +64,9 @@ struct s_Lane
63 // is still running 64 // is still running
64 // S: cleans up after itself if non-NULL at lane exit 65 // S: cleans up after itself if non-NULL at lane exit
65 66
66#if HAVE_LANE_TRACKING 67#if HAVE_LANE_TRACKING()
67 struct s_Lane* volatile tracking_next; 68 struct s_Lane* volatile tracking_next;
68#endif // HAVE_LANE_TRACKING 69#endif // HAVE_LANE_TRACKING()
69 // 70 //
70 // For tracking only 71 // For tracking only
71}; 72};
diff --git a/src/linda.c b/src/linda.c
index 42cda51..637f909 100644
--- a/src/linda.c
+++ b/src/linda.c
@@ -795,16 +795,16 @@ static void* linda_id( lua_State* L, DeepOp op_)
795 * just don't use L's allocF because we don't know which state will get the honor of GCing the linda 795 * just don't use L's allocF because we don't know which state will get the honor of GCing the linda
796 */ 796 */
797 // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly 797 // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly
798#if USE_LUA_STATE_ALLOCATOR 798#if USE_LUA_STATE_ALLOCATOR()
799 { 799 {
800 Universe* const U = universe_get(L); 800 Universe* const U = universe_get(L);
801 AllocatorDefinition* const allocD = &U->protected_allocator.definition; 801 AllocatorDefinition* const allocD = &U->protected_allocator.definition;
802 802
803 s = (struct s_Linda*)allocD->allocF(allocD->allocUD, NULL, 0, sizeof(struct s_Linda) + name_len); // terminating 0 is already included 803 s = (struct s_Linda*)allocD->allocF(allocD->allocUD, NULL, 0, sizeof(struct s_Linda) + name_len); // terminating 0 is already included
804 } 804 }
805#else // USE_LUA_STATE_ALLOCATOR 805#else // USE_LUA_STATE_ALLOCATOR()
806 s = (struct s_Linda*)malloc(sizeof(struct s_Linda) + name_len); // terminating 0 is already included 806 s = (struct s_Linda*)malloc(sizeof(struct s_Linda) + name_len); // terminating 0 is already included
807#endif // USE_LUA_STATE_ALLOCATOR 807#endif // USE_LUA_STATE_ALLOCATOR()
808 if( s) 808 if( s)
809 { 809 {
810 s->prelude.magic.value = DEEP_VERSION.value; 810 s->prelude.magic.value = DEEP_VERSION.value;
@@ -838,16 +838,16 @@ static void* linda_id( lua_State* L, DeepOp op_)
838 SIGNAL_FREE( &linda->read_happened); 838 SIGNAL_FREE( &linda->read_happened);
839 SIGNAL_FREE( &linda->write_happened); 839 SIGNAL_FREE( &linda->write_happened);
840 // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly 840 // don't hijack the state allocator when running LuaJIT because it looks like LuaJIT does not expect it and might invalidate the memory unexpectedly
841#if USE_LUA_STATE_ALLOCATOR 841#if USE_LUA_STATE_ALLOCATOR()
842 { 842 {
843 Universe* const U = universe_get(L); 843 Universe* const U = universe_get(L);
844 AllocatorDefinition* const allocD = &U->protected_allocator.definition; 844 AllocatorDefinition* const allocD = &U->protected_allocator.definition;
845 845
846 allocD->allocF(allocD->allocUD, linda, sizeof(struct s_Linda) + strlen(linda->name), 0); 846 allocD->allocF(allocD->allocUD, linda, sizeof(struct s_Linda) + strlen(linda->name), 0);
847 } 847 }
848#else // USE_LUA_STATE_ALLOCATOR 848#else // USE_LUA_STATE_ALLOCATOR()
849 free(linda); 849 free(linda);
850#endif // USE_LUA_STATE_ALLOCATOR 850#endif // USE_LUA_STATE_ALLOCATOR()
851 return NULL; 851 return NULL;
852 } 852 }
853 853
diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h
index e8e725b..3ed234a 100644
--- a/src/macros_and_utils.h
+++ b/src/macros_and_utils.h
@@ -12,19 +12,19 @@
12#define inline __inline 12#define inline __inline
13#endif 13#endif
14 14
15#define USE_DEBUG_SPEW 0 15#define USE_DEBUG_SPEW() 0
16#if USE_DEBUG_SPEW 16#if USE_DEBUG_SPEW()
17extern char const* debugspew_indent; 17extern char const* debugspew_indent;
18#define INDENT_BEGIN "%.*s " 18#define INDENT_BEGIN "%.*s "
19#define INDENT_END , (U ? U->debugspew_indent_depth : 0), debugspew_indent 19#define INDENT_END , (U ? U->debugspew_indent_depth : 0), debugspew_indent
20#define DEBUGSPEW_CODE(_code) _code 20#define DEBUGSPEW_CODE(_code) _code
21#define DEBUGSPEW_PARAM_COMMA( param_) param_, 21#define DEBUGSPEW_PARAM_COMMA( param_) param_,
22#define DEBUGSPEW_COMMA_PARAM( param_) , param_ 22#define DEBUGSPEW_COMMA_PARAM( param_) , param_
23#else // USE_DEBUG_SPEW 23#else // USE_DEBUG_SPEW()
24#define DEBUGSPEW_CODE(_code) 24#define DEBUGSPEW_CODE(_code)
25#define DEBUGSPEW_PARAM_COMMA( param_) 25#define DEBUGSPEW_PARAM_COMMA( param_)
26#define DEBUGSPEW_COMMA_PARAM( param_) 26#define DEBUGSPEW_COMMA_PARAM( param_)
27#endif // USE_DEBUG_SPEW 27#endif // USE_DEBUG_SPEW()
28 28
29#ifdef NDEBUG 29#ifdef NDEBUG
30 30
@@ -99,17 +99,7 @@ extern char const* debugspew_indent;
99 99
100#define LUAG_FUNC( func_name) int LG_##func_name( lua_State* L) 100#define LUAG_FUNC( func_name) int LG_##func_name( lua_State* L)
101 101
102#if defined(LUA_JITLIBNAME)
103#if (defined(__x86_64__) || defined(_M_X64) || defined(__LP64__))
104#define LUAJIT_FLAVOR 64
105#else // 64 bits
106#define LUAJIT_FLAVOR 32
107#endif // 64 bits
108#else // LUA_JITLIBNAME
109#define LUAJIT_FLAVOR 0
110#endif // LUA_JITLIBNAME
111
112// after all, it looks like we can use the state allocator for our own usage when running LuaJIT, as long as we mutex-protect it 102// after all, it looks like we can use the state allocator for our own usage when running LuaJIT, as long as we mutex-protect it
113#define USE_LUA_STATE_ALLOCATOR 1 // (LUAJIT_FLAVOR==0) 103#define USE_LUA_STATE_ALLOCATOR() 1 // (LUAJIT_FLAVOR()==0)
114 104
115#endif // MACROS_AND_UTILS_H 105#endif // MACROS_AND_UTILS_H
diff --git a/src/state.c b/src/state.c
index a2de5cb..ef70842 100644
--- a/src/state.c
+++ b/src/state.c
@@ -41,6 +41,7 @@ THE SOFTWARE.
41#endif // __APPLE__ 41#endif // __APPLE__
42 42
43#include "compat.h" 43#include "compat.h"
44#include "macros_and_utils.h"
44#include "universe.h" 45#include "universe.h"
45#include "tools.h" 46#include "tools.h"
46#include "lanes.h" 47#include "lanes.h"
@@ -148,12 +149,12 @@ static const luaL_Reg libs[] =
148 { LUA_COLIBNAME, NULL}, // Lua 5.1: part of base package 149 { LUA_COLIBNAME, NULL}, // Lua 5.1: part of base package
149#endif // LUA_VERSION_NUM 150#endif // LUA_VERSION_NUM
150 { LUA_DBLIBNAME, luaopen_debug}, 151 { LUA_DBLIBNAME, luaopen_debug},
151#if LUAJIT_FLAVOR != 0 // building against LuaJIT headers, add some LuaJIT-specific libs 152#if LUAJIT_FLAVOR() != 0 // building against LuaJIT headers, add some LuaJIT-specific libs
152//#pragma message( "supporting JIT base libs") 153//#pragma message( "supporting JIT base libs")
153 { LUA_BITLIBNAME, luaopen_bit}, 154 { LUA_BITLIBNAME, luaopen_bit},
154 { LUA_JITLIBNAME, luaopen_jit}, 155 { LUA_JITLIBNAME, luaopen_jit},
155 { LUA_FFILIBNAME, luaopen_ffi}, 156 { LUA_FFILIBNAME, luaopen_ffi},
156#endif // LUAJIT_FLAVOR 157#endif // LUAJIT_FLAVOR()
157 158
158{ LUA_DBLIBNAME, luaopen_debug}, 159{ LUA_DBLIBNAME, luaopen_debug},
159{ "lanes.core", require_lanes_core}, // So that we can open it like any base library (possible since we have access to the init function) 160{ "lanes.core", require_lanes_core}, // So that we can open it like any base library (possible since we have access to the init function)
@@ -249,10 +250,10 @@ void initialize_on_state_create( Universe* U, lua_State* L)
249lua_State* create_state( Universe* U, lua_State* from_) 250lua_State* create_state( Universe* U, lua_State* from_)
250{ 251{
251 lua_State* L; 252 lua_State* L;
252#if LUAJIT_FLAVOR == 64 253#if LUAJIT_FLAVOR() == 64
253 // for some reason, LuaJIT 64 bits does not support creating a state with lua_newstate... 254 // for some reason, LuaJIT 64 bits does not support creating a state with lua_newstate...
254 L = luaL_newstate(); 255 L = luaL_newstate();
255#else // LUAJIT_FLAVOR == 64 256#else // LUAJIT_FLAVOR() == 64
256 if( U->provide_allocator != NULL) // we have a function we can call to obtain an allocator 257 if( U->provide_allocator != NULL) // we have a function we can call to obtain an allocator
257 { 258 {
258 lua_pushcclosure( from_, U->provide_allocator, 0); 259 lua_pushcclosure( from_, U->provide_allocator, 0);
@@ -268,7 +269,7 @@ lua_State* create_state( Universe* U, lua_State* from_)
268 // reuse the allocator provided when the master state was created 269 // reuse the allocator provided when the master state was created
269 L = lua_newstate( U->protected_allocator.definition.allocF, U->protected_allocator.definition.allocUD); 270 L = lua_newstate( U->protected_allocator.definition.allocF, U->protected_allocator.definition.allocUD);
270 } 271 }
271#endif // LUAJIT_FLAVOR == 64 272#endif // LUAJIT_FLAVOR() == 64
272 273
273 if( L == NULL) 274 if( L == NULL)
274 { 275 {
@@ -418,7 +419,7 @@ lua_State* luaG_newstate( Universe* U, lua_State* from_, char const* libs_)
418 lua_pushglobaltable( L); // Lua 5.2 no longer has LUA_GLOBALSINDEX: we must push globals table on the stack 419 lua_pushglobaltable( L); // Lua 5.2 no longer has LUA_GLOBALSINDEX: we must push globals table on the stack
419 populate_func_lookup_table( L, -1, NULL); 420 populate_func_lookup_table( L, -1, NULL);
420 421
421#if 0 && USE_DEBUG_SPEW 422#if 0 && USE_DEBUG_SPEW()
422 // dump the lookup database contents 423 // dump the lookup database contents
423 lua_getfield( L, LUA_REGISTRYINDEX, LOOKUP_REGKEY); // {} 424 lua_getfield( L, LUA_REGISTRYINDEX, LOOKUP_REGKEY); // {}
424 lua_pushnil( L); // {} nil 425 lua_pushnil( L); // {} nil
@@ -432,7 +433,7 @@ lua_State* luaG_newstate( Universe* U, lua_State* from_, char const* libs_)
432 lua_pop( L, 1); // {} k 433 lua_pop( L, 1); // {} k
433 } 434 }
434 lua_pop( L, 1); // {} 435 lua_pop( L, 1); // {}
435#endif // USE_DEBUG_SPEW 436#endif // USE_DEBUG_SPEW()
436 437
437 lua_pop( L, 1); 438 lua_pop( L, 1);
438 STACK_END( L, 0); 439 STACK_END( L, 0);
diff --git a/src/tools.c b/src/tools.c
index 2c8a9f0..626da2b 100644
--- a/src/tools.c
+++ b/src/tools.c
@@ -1117,7 +1117,7 @@ static void lookup_native_func( lua_State* L2, lua_State* L, uint_t i, LookupMod
1117 * L2 has the cache key for this function at the top of the stack 1117 * L2 has the cache key for this function at the top of the stack
1118*/ 1118*/
1119 1119
1120#if USE_DEBUG_SPEW 1120#if USE_DEBUG_SPEW()
1121static char const* lua_type_names[] = 1121static char const* lua_type_names[] =
1122{ 1122{
1123 "LUA_TNIL" 1123 "LUA_TNIL"
@@ -1138,7 +1138,7 @@ static char const* vt_names[] =
1138 , "VT_KEY" 1138 , "VT_KEY"
1139 , "VT_METATABLE" 1139 , "VT_METATABLE"
1140}; 1140};
1141#endif // USE_DEBUG_SPEW 1141#endif // USE_DEBUG_SPEW()
1142 1142
1143// Lua 5.4.3 style of dumping (see lstrlib.c) 1143// Lua 5.4.3 style of dumping (see lstrlib.c)
1144// we have to do it that way because we can't unbalance the stack between buffer operations 1144// we have to do it that way because we can't unbalance the stack between buffer operations
diff --git a/src/uniquekey.h b/src/uniquekey.h
index a72426c..015fbf2 100644
--- a/src/uniquekey.h
+++ b/src/uniquekey.h
@@ -1,7 +1,7 @@
1#if !defined __LANES_UNIQUEKEY_H__ 1#if !defined __LANES_UNIQUEKEY_H__
2#define __LANES_UNIQUEKEY_H__ 1 2#define __LANES_UNIQUEKEY_H__ 1
3 3
4#include "macros_and_utils.h" 4#include "compat.h"
5 5
6// Lua light userdata can hold a pointer. 6// Lua light userdata can hold a pointer.
7struct s_UniqueKey 7struct s_UniqueKey
@@ -10,11 +10,11 @@ struct s_UniqueKey
10}; 10};
11typedef struct s_UniqueKey UniqueKey; 11typedef struct s_UniqueKey UniqueKey;
12 12
13#if LUAJIT_FLAVOR == 64 // building against LuaJIT headers for 64 bits, light userdata is restricted to 47 significant bits, because LuaJIT uses the other bits for internal optimizations 13#if LUAJIT_FLAVOR() == 64 // building against LuaJIT headers for 64 bits, light userdata is restricted to 47 significant bits, because LuaJIT uses the other bits for internal optimizations
14#define MAKE_UNIQUE_KEY( p_) ((void*)((ptrdiff_t)(p_) & 0x7fffffffffffull)) 14#define MAKE_UNIQUE_KEY( p_) ((void*)((ptrdiff_t)(p_) & 0x7fffffffffffull))
15#else // LUAJIT_FLAVOR 15#else // LUAJIT_FLAVOR()
16#define MAKE_UNIQUE_KEY( p_) ((void*)(ptrdiff_t)(p_)) 16#define MAKE_UNIQUE_KEY( p_) ((void*)(ptrdiff_t)(p_))
17#endif // LUAJIT_FLAVOR 17#endif // LUAJIT_FLAVOR()
18 18
19#define DECLARE_UNIQUE_KEY( name_) UniqueKey name_ 19#define DECLARE_UNIQUE_KEY( name_) UniqueKey name_
20#define DECLARE_CONST_UNIQUE_KEY( name_, p_) UniqueKey const name_ = { MAKE_UNIQUE_KEY( p_)} 20#define DECLARE_CONST_UNIQUE_KEY( name_, p_) UniqueKey const name_ = { MAKE_UNIQUE_KEY( p_)}
diff --git a/src/universe.h b/src/universe.h
index e4c1191..ba00e87 100644
--- a/src/universe.h
+++ b/src/universe.h
@@ -6,6 +6,7 @@
6 6
7#include "lua.h" 7#include "lua.h"
8#include "threading.h" 8#include "threading.h"
9#include "macros_and_utils.h"
9 10
10// forwards 11// forwards
11struct s_DeepPrelude; 12struct s_DeepPrelude;
@@ -20,7 +21,7 @@ typedef struct s_Lane Lane;
20/* 21/*
21* Do we want to activate full lane tracking feature? (EXPERIMENTAL) 22* Do we want to activate full lane tracking feature? (EXPERIMENTAL)
22*/ 23*/
23#define HAVE_LANE_TRACKING 1 24#define HAVE_LANE_TRACKING() 1
24 25
25// ################################################################################################ 26// ################################################################################################
26 27
@@ -68,10 +69,10 @@ struct s_Universe
68 // used for timers (each lane will get a proxy to this) 69 // used for timers (each lane will get a proxy to this)
69 volatile DeepPrelude* timer_deep; // = NULL 70 volatile DeepPrelude* timer_deep; // = NULL
70 71
71#if HAVE_LANE_TRACKING 72#if HAVE_LANE_TRACKING()
72 MUTEX_T tracking_cs; 73 MUTEX_T tracking_cs;
73 Lane* volatile tracking_first; // will change to TRACKING_END if we want to activate tracking 74 Lane* volatile tracking_first; // will change to TRACKING_END if we want to activate tracking
74#endif // HAVE_LANE_TRACKING 75#endif // HAVE_LANE_TRACKING()
75 76
76 MUTEX_T selfdestruct_cs; 77 MUTEX_T selfdestruct_cs;
77 78
@@ -84,9 +85,9 @@ struct s_Universe
84 85
85 lua_Integer last_mt_id; 86 lua_Integer last_mt_id;
86 87
87#if USE_DEBUG_SPEW 88#if USE_DEBUG_SPEW()
88 int debugspew_indent_depth; 89 int debugspew_indent_depth;
89#endif // USE_DEBUG_SPEW 90#endif // USE_DEBUG_SPEW()
90 91
91 Lane* volatile selfdestruct_first; 92 Lane* volatile selfdestruct_first;
92 // After a lane has removed itself from the chain, it still performs some processing. 93 // After a lane has removed itself from the chain, it still performs some processing.