Skip to content

Commit

Permalink
Add workaround for LuaJIT not supporting >47bit pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
daurnimator committed Jul 21, 2019
1 parent 12bee2f commit 1e00a82
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/cqueues.c
Original file line number Diff line number Diff line change
Expand Up @@ -1875,7 +1875,7 @@ static void thread_add(lua_State *L, struct cqueue *Q, struct callinfo *I, int i
/* anchor thread context to cqueue object */
cqs_getuservalue(L, I->self);
lua_pushvalue(L, -2);
lua_rawsetp(L, -2, T);
lua_rawsetp(L, -2, CQS_UNIQUE_LIGHTUSERDATA_MASK(T));
lua_pop(L, 2);

LIST_INSERT_HEAD(&Q->thread.pending, T, le);
Expand Down Expand Up @@ -1906,15 +1906,15 @@ static void thread_del(lua_State *L, struct cqueue *Q, struct callinfo *I, struc
cqs_getuservalue(L, I->self);

/* set thread's uservalue (it's thread) to nil */
lua_rawgetp(L, -1, T);
lua_rawgetp(L, -1, CQS_UNIQUE_LIGHTUSERDATA_MASK(T));
lua_pushnil(L);
cqs_setuservalue(L, -2);
lua_pop(L, 1);
T->L = NULL;

/* remove thread from cqueues's thread table */
lua_pushnil(L);
lua_rawsetp(L, -2, T);
lua_rawsetp(L, -2, CQS_UNIQUE_LIGHTUSERDATA_MASK(T));
lua_pop(L, 1);
} /* thread_del() */

Expand Down Expand Up @@ -2717,7 +2717,7 @@ static struct cstack *cstack_self(lua_State *L) {
static const int index = 47;
struct cstack *CS;

lua_rawgetp(L, LUA_REGISTRYINDEX, &index);
lua_rawgetp(L, LUA_REGISTRYINDEX, CQS_UNIQUE_LIGHTUSERDATA_MASK(&index));

CS = lua_touserdata(L, -1);

Expand All @@ -2731,7 +2731,7 @@ static struct cstack *cstack_self(lua_State *L) {

LIST_INIT(&CS->cqueues);

lua_rawsetp(L, LUA_REGISTRYINDEX, &index);
lua_rawsetp(L, LUA_REGISTRYINDEX, CQS_UNIQUE_LIGHTUSERDATA_MASK(&index));

return CS;
} /* cstack_self() */
Expand Down
18 changes: 17 additions & 1 deletion src/cqueues.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,23 @@
#define CQS_NOTIFY "CQS Notify"
#define CQS_CONDITION "CQS Condition"

#define CQUEUE__POLL ((void *)&cqueue__poll)
#ifndef CQS_USE_47BIT_LIGHTUSERDATA_HACK
/* LuaJIT only supports pointers with the low 47 bits set */
#if defined(LUA_JITLIBNAME) && (defined(_LP64) || defined(_LLP64) || defined(__arch64__) || defined (__arm64__) || defined (__aarch64__) || defined(_WIN64))
#define CQS_USE_47BIT_LIGHTUSERDATA_HACK 1
#else
#define CQS_USE_47BIT_LIGHTUSERDATA_HACK 0
#endif
#endif

#if CQS_USE_47BIT_LIGHTUSERDATA_HACK
#define CQS_UNIQUE_LIGHTUSERDATA_MASK(p) ((void *)((intptr_t)(p) & ((1UL<<47)-1)))
#else
#define CQS_UNIQUE_LIGHTUSERDATA_MASK(p) ((void *)(p))
#endif

#define CQUEUE__POLL CQS_UNIQUE_LIGHTUSERDATA_MASK(&cqueue__poll)

const char *cqueue__poll; // signals multilevel yield

cqs_nargs_t luaopen__cqueues(lua_State *);
Expand Down
4 changes: 2 additions & 2 deletions src/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,11 @@ static struct so_options lso_checkopts(lua_State *L, int index) {
luaL_argcheck(L, path != NULL || addr != NULL, index, "no bind address specified");

if (path) {
sa = lso_singleton(L, &regindex, NULL, sizeof(struct sockaddr_un));
sa = lso_singleton(L, CQS_UNIQUE_LIGHTUSERDATA_MASK(&regindex), NULL, sizeof(struct sockaddr_un));
sa->sa_family = AF_UNIX;
memcpy(((struct sockaddr_un*)sa)->sun_path, path, MIN(plen, sizeof(((struct sockaddr_un*)sa)->sun_path)));
} else {
sa = lso_singleton(L, &regindex, NULL, sizeof(struct sockaddr_storage));
sa = lso_singleton(L, CQS_UNIQUE_LIGHTUSERDATA_MASK(&regindex), NULL, sizeof(struct sockaddr_storage));
if (!sa_pton(sa, sizeof(struct sockaddr_storage), addr, NULL, &error))
luaL_argerror(L, index, lua_pushfstring(L, "%s: unable to parse bind address (%s)", addr, cqs_strerror(error)));

Expand Down
4 changes: 2 additions & 2 deletions src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ static void *ct_enter(void *arg) {
*ud = ct;

lua_pushvalue(L, -1);
lua_rawsetp(L, LUA_REGISTRYINDEX, &selfindex);
lua_rawsetp(L, LUA_REGISTRYINDEX, CQS_UNIQUE_LIGHTUSERDATA_MASK(&selfindex));

if ((error = cqs_socket_fdopen(L, ct->tmp.fd[1], NULL)))
goto error;
Expand Down Expand Up @@ -797,7 +797,7 @@ static int ct_interpose(lua_State *L) {


static int ct_self(lua_State *L) {
lua_rawgetp(L, LUA_REGISTRYINDEX, &selfindex);
lua_rawgetp(L, LUA_REGISTRYINDEX, CQS_UNIQUE_LIGHTUSERDATA_MASK(&selfindex));

return 1;
} /* ct_self() */
Expand Down

0 comments on commit 1e00a82

Please sign in to comment.