Skip to content

Commit baf5414

Browse files
Make allocator threadsafe by default when running LuaJIT, because LuaJIT allocator is not
1 parent a147fa3 commit baf5414

File tree

5 files changed

+25
-21
lines changed

5 files changed

+25
-21
lines changed

src/keeper.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -612,14 +612,14 @@ void close_keepers( Universe* U, lua_State* L)
612612
// free the keeper bookkeeping structure
613613
{
614614
// 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 LUAJIT_FLAVOR == 0
615+
#if USE_LUA_STATE_ALLOCATOR
616616
{
617617
AllocatorDefinition* const allocD = &U->protected_allocator.definition;
618-
allocD->allocF( allocUD, U->keepers, sizeof( Keepers) + (nbKeepers - 1) * sizeof( Keeper), 0);
618+
allocD->allocF( allocD->allocUD, U->keepers, sizeof( Keepers) + (nbKeepers - 1) * sizeof( Keeper), 0);
619619
}
620-
#else // LUAJIT_FLAVOR
620+
#else // USE_LUA_STATE_ALLOCATOR
621621
free(U->keepers);
622-
#endif // LUAJIT_FLAVOR
622+
#endif // USE_LUA_STATE_ALLOCATOR
623623
U->keepers = NULL;
624624
}
625625
}
@@ -654,14 +654,14 @@ void init_keepers( Universe* U, lua_State* L)
654654
{
655655
size_t const bytes = sizeof( Keepers) + (nb_keepers - 1) * sizeof( Keeper);
656656
// 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 LUAJIT_FLAVOR == 0
657+
#if USE_LUA_STATE_ALLOCATOR
658658
{
659659
AllocatorDefinition* const allocD = &U->protected_allocator.definition;
660-
U->keepers = (Keepers*) allocD->allocF( allocUD, NULL, 0, bytes);
660+
U->keepers = (Keepers*) allocD->allocF( allocD->allocUD, NULL, 0, bytes);
661661
}
662-
#else // LUAJIT_FLAVOR
662+
#else // USE_LUA_STATE_ALLOCATOR
663663
U->keepers = (Keepers*)malloc(bytes);
664-
#endif // LUAJIT_FLAVOR
664+
#endif // USE_LUA_STATE_ALLOCATOR
665665
if( U->keepers == NULL)
666666
{
667667
(void) luaL_error( L, "init_keepers() failed while creating keeper array; out of memory");

src/lanes.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,14 @@ static void lane_cleanup( Lane* s)
254254
#endif // HAVE_LANE_TRACKING
255255

256256
// 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 LUAJIT_FLAVOR == 0
257+
#if USE_LUA_STATE_ALLOCATOR
258258
{
259259
AllocatorDefinition* const allocD = &s->U->protected_allocator.definition;
260260
allocD->allocF(allocD->allocUD, s, sizeof(Lane), 0);
261261
}
262-
#else // LUAJIT_FLAVOR
262+
#else // USE_LUA_STATE_ALLOCATOR
263263
free(s);
264-
#endif // LUAJIT_FLAVOR
264+
#endif // USE_LUA_STATE_ALLOCATOR
265265
}
266266

267267
/*
@@ -1231,14 +1231,14 @@ LUAG_FUNC( lane_new)
12311231
// a Lane full userdata needs a single uservalue
12321232
ud = lua_newuserdatauv( L, sizeof( Lane*), 1); // func libs priority globals package required gc_cb lane
12331233
// 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 LUAJIT_FLAVOR == 0
1234+
#if USE_LUA_STATE_ALLOCATOR
12351235
{
12361236
AllocatorDefinition* const allocD = &U->protected_allocator.definition;
12371237
s = *ud = (Lane*)allocD->allocF(allocD->allocUD, NULL, 0, sizeof(Lane));
12381238
}
1239-
#else // LUAJIT_FLAVOR
1239+
#else // USE_LUA_STATE_ALLOCATOR
12401240
s = *ud = (Lane*) malloc(sizeof(Lane));
1241-
#endif // LUAJIT_FLAVOR
1241+
#endif // USE_LUA_STATE_ALLOCATOR
12421242
if( s == NULL)
12431243
{
12441244
return luaL_error( L, "could not create lane: out of memory");

src/lanes.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ lanes.configure = function( settings_)
7676
track_lanes = false,
7777
demote_full_userdata = nil,
7878
verbose_errors = false,
79-
allocator = nil
79+
-- LuaJIT provides a thread-unsafe allocator by default, so we need to protect it when used in parallel lanes
80+
allocator = (package.loaded.jit and jit.version) and "protected" or nil
8081
}
8182
local boolean_param_checker = function( val_)
8283
-- non-'boolean-false' should be 'boolean-true' or nil

src/linda.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -795,16 +795,16 @@ static void* linda_id( lua_State* L, DeepOp op_)
795795
* just don't use L's allocF because we don't know which state will get the honor of GCing the linda
796796
*/
797797
// 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 LUAJIT_FLAVOR == 0
798+
#if USE_LUA_STATE_ALLOCATOR
799799
{
800800
Universe* const U = universe_get(L);
801801
AllocatorDefinition* const allocD = &U->protected_allocator.definition;
802802

803803
s = (struct s_Linda*)allocD->allocF(allocD->allocUD, NULL, 0, sizeof(struct s_Linda) + name_len); // terminating 0 is already included
804804
}
805-
#else // LUAJIT_FLAVOR
805+
#else // USE_LUA_STATE_ALLOCATOR
806806
s = (struct s_Linda*)malloc(sizeof(struct s_Linda) + name_len); // terminating 0 is already included
807-
#endif // LUAJIT_FLAVOR
807+
#endif // USE_LUA_STATE_ALLOCATOR
808808
if( s)
809809
{
810810
s->prelude.magic.value = DEEP_VERSION.value;
@@ -838,16 +838,16 @@ static void* linda_id( lua_State* L, DeepOp op_)
838838
SIGNAL_FREE( &linda->read_happened);
839839
SIGNAL_FREE( &linda->write_happened);
840840
// 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 LUAJIT_FLAVOR == 0
841+
#if USE_LUA_STATE_ALLOCATOR
842842
{
843843
Universe* const U = universe_get(L);
844844
AllocatorDefinition* const allocD = &U->protected_allocator.definition;
845845

846846
allocD->allocF(allocD->allocUD, linda, sizeof(struct s_Linda) + strlen(linda->name), 0);
847847
}
848-
#else // LUAJIT_FLAVOR
848+
#else // USE_LUA_STATE_ALLOCATOR
849849
free(linda);
850-
#endif // LUAJIT_FLAVOR
850+
#endif // USE_LUA_STATE_ALLOCATOR
851851
return NULL;
852852
}
853853

src/macros_and_utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,7 @@ extern char const* debugspew_indent;
109109
#define LUAJIT_FLAVOR 0
110110
#endif // LUA_JITLIBNAME
111111

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
113+
#define USE_LUA_STATE_ALLOCATOR 1 // (LUAJIT_FLAVOR==0)
114+
112115
#endif // MACROS_AND_UTILS_H

0 commit comments

Comments
 (0)