Skip to content

Commit 1615578

Browse files
committed
ast: remove code to support reentrancy
1 parent 946f3b2 commit 1615578

File tree

1 file changed

+33
-103
lines changed

1 file changed

+33
-103
lines changed

src/ast.c

Lines changed: 33 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -116,29 +116,6 @@ static const uint8_t flisp_system_image[] = {
116116
#include <julia_flisp.boot.inc>
117117
};
118118

119-
typedef struct _jl_ast_context_list_t {
120-
struct _jl_ast_context_list_t *next;
121-
struct _jl_ast_context_list_t **prev;
122-
} jl_ast_context_list_t;
123-
124-
STATIC_INLINE void jl_ast_context_list_insert(jl_ast_context_list_t **head,
125-
jl_ast_context_list_t *node) JL_NOTSAFEPOINT
126-
{
127-
jl_ast_context_list_t *next = *head;
128-
if (next)
129-
next->prev = &node->next;
130-
node->next = next;
131-
node->prev = head;
132-
*head = node;
133-
}
134-
135-
STATIC_INLINE void jl_ast_context_list_delete(jl_ast_context_list_t *node) JL_NOTSAFEPOINT
136-
{
137-
if (node->next)
138-
node->next->prev = node->prev;
139-
*node->prev = node->next;
140-
}
141-
142119
typedef struct _jl_ast_context_t {
143120
fl_context_t fl;
144121
fltype_t *jvtype;
@@ -149,10 +126,8 @@ typedef struct _jl_ast_context_t {
149126
value_t null_sym;
150127
value_t ssavalue_sym;
151128
value_t slot_sym;
152-
jl_ast_context_list_t list;
153-
int ref;
154-
jl_task_t *task; // the current owner (user) of this jl_ast_context_t
155129
jl_module_t *module; // context module for `current-julia-module-counter`
130+
struct _jl_ast_context_t *next; // invasive list pointer for getting free contexts
156131
} jl_ast_context_t;
157132

158133
static jl_ast_context_t jl_ast_main_ctx;
@@ -162,20 +137,12 @@ jl_ast_context_t *jl_ast_ctx(fl_context_t *fl) JL_GLOBALLY_ROOTED JL_NOTSAFEPOIN
162137
#else
163138
#define jl_ast_ctx(fl_ctx) container_of(fl_ctx, jl_ast_context_t, fl)
164139
#endif
165-
#define jl_ast_context_list_item(node) \
166-
container_of(node, jl_ast_context_t, list)
167140

168141
struct macroctx_stack {
169142
jl_module_t *m;
170143
struct macroctx_stack *parent;
171144
};
172145

173-
#define JL_AST_PRESERVE_PUSH(ctx, old, inmodule) \
174-
jl_module_t *(old) = ctx->module; \
175-
ctx->module = (inmodule)
176-
#define JL_AST_PRESERVE_POP(ctx, old) \
177-
ctx->module = (old)
178-
179146
static jl_value_t *scm_to_julia(fl_context_t *fl_ctx, value_t e, jl_module_t *mod);
180147
static value_t julia_to_scm(fl_context_t *fl_ctx, jl_value_t *v);
181148
static jl_value_t *jl_expand_macros(jl_value_t *expr, jl_module_t *inmodule, struct macroctx_stack *macroctx, int onelevel, size_t world, int throw_load_error);
@@ -235,9 +202,9 @@ static const builtinspec_t julia_flisp_ast_ext[] = {
235202
{ NULL, NULL }
236203
};
237204

238-
static void jl_init_ast_ctx(jl_ast_context_t *ast_ctx) JL_NOTSAFEPOINT
205+
static void jl_init_ast_ctx(jl_ast_context_t *ctx) JL_NOTSAFEPOINT
239206
{
240-
fl_context_t *fl_ctx = &ast_ctx->fl;
207+
fl_context_t *fl_ctx = &ctx->fl;
241208
fl_init(fl_ctx, 4*1024*1024);
242209

243210
if (fl_load_system_image_str(fl_ctx, (char*)flisp_system_image,
@@ -247,7 +214,6 @@ static void jl_init_ast_ctx(jl_ast_context_t *ast_ctx) JL_NOTSAFEPOINT
247214

248215
fl_applyn(fl_ctx, 0, symbol_value(symbol(fl_ctx, "__init_globals")));
249216

250-
jl_ast_context_t *ctx = jl_ast_ctx(fl_ctx);
251217
ctx->jvtype = define_opaque_type(fl_ctx->jl_sym, sizeof(void*), NULL, NULL);
252218
assign_global_builtins(fl_ctx, julia_flisp_ast_ext);
253219
ctx->true_sym = symbol(fl_ctx, "true");
@@ -256,76 +222,48 @@ static void jl_init_ast_ctx(jl_ast_context_t *ast_ctx) JL_NOTSAFEPOINT
256222
ctx->null_sym = symbol(fl_ctx, "null");
257223
ctx->ssavalue_sym = symbol(fl_ctx, "ssavalue");
258224
ctx->slot_sym = symbol(fl_ctx, "slot");
259-
ctx->task = NULL;
260225
ctx->module = NULL;
261226
set(symbol(fl_ctx, "*scopewarn-opt*"), fixnum(jl_options.warn_scope));
262227
}
263228

264229
// There should be no GC allocation while holding this lock
265230
static uv_mutex_t flisp_lock;
266-
static jl_ast_context_list_t *jl_ast_ctx_using = NULL;
267-
static jl_ast_context_list_t *jl_ast_ctx_freed = NULL;
231+
static jl_ast_context_t *jl_ast_ctx_freed = NULL;
268232

269-
static jl_ast_context_t *jl_ast_ctx_enter(void) JL_GLOBALLY_ROOTED JL_NOTSAFEPOINT
233+
static jl_ast_context_t *jl_ast_ctx_enter(jl_module_t *m) JL_GLOBALLY_ROOTED JL_NOTSAFEPOINT
270234
{
271-
jl_task_t *ct = jl_current_task;
272235
JL_SIGATOMIC_BEGIN();
273236
uv_mutex_lock(&flisp_lock);
274-
jl_ast_context_list_t *node;
275-
jl_ast_context_t *ctx;
276-
// First check if the current task is using one of the contexts
277-
for (node = jl_ast_ctx_using;node;(node = node->next)) {
278-
ctx = jl_ast_context_list_item(node);
279-
if (ctx->task == ct) {
280-
ctx->ref++;
281-
uv_mutex_unlock(&flisp_lock);
282-
return ctx;
283-
}
284-
}
285-
// If not, grab one from the free list
286-
if ((node = jl_ast_ctx_freed)) {
287-
jl_ast_context_list_delete(node);
288-
jl_ast_context_list_insert(&jl_ast_ctx_using, node);
289-
ctx = jl_ast_context_list_item(node);
290-
ctx->ref = 1;
291-
ctx->task = ct;
292-
ctx->module = NULL;
293-
uv_mutex_unlock(&flisp_lock);
294-
return ctx;
237+
jl_ast_context_t *ctx = jl_ast_ctx_freed;
238+
if (ctx != NULL) {
239+
jl_ast_ctx_freed = ctx->next;
240+
ctx->next = NULL;
295241
}
296-
// Construct a new one if we can't find any
297-
ctx = (jl_ast_context_t*)calloc(1, sizeof(jl_ast_context_t));
298-
ctx->ref = 1;
299-
ctx->task = ct;
300-
node = &ctx->list;
301-
jl_ast_context_list_insert(&jl_ast_ctx_using, node);
302242
uv_mutex_unlock(&flisp_lock);
303-
jl_init_ast_ctx(ctx);
243+
if (ctx == NULL) {
244+
// Construct a new one if we can't find any
245+
ctx = (jl_ast_context_t*)calloc(1, sizeof(jl_ast_context_t));
246+
jl_init_ast_ctx(ctx);
247+
}
248+
ctx->module = m;
304249
return ctx;
305250
}
306251

307252
static void jl_ast_ctx_leave(jl_ast_context_t *ctx)
308253
{
309-
JL_SIGATOMIC_END();
310-
if (--ctx->ref)
311-
return;
312254
uv_mutex_lock(&flisp_lock);
313-
ctx->task = NULL;
314-
jl_ast_context_list_t *node = &ctx->list;
315-
jl_ast_context_list_delete(node);
316-
jl_ast_context_list_insert(&jl_ast_ctx_freed, node);
255+
ctx->module = NULL;
256+
ctx->next = jl_ast_ctx_freed;
257+
jl_ast_ctx_freed = ctx;
317258
uv_mutex_unlock(&flisp_lock);
259+
JL_SIGATOMIC_END();
318260
}
319261

320262
void jl_init_flisp(void)
321263
{
322-
jl_task_t *ct = jl_current_task;
323-
if (jl_ast_ctx_using || jl_ast_ctx_freed)
264+
if (jl_ast_ctx_freed)
324265
return;
325266
uv_mutex_init(&flisp_lock);
326-
jl_ast_main_ctx.ref = 1;
327-
jl_ast_main_ctx.task = ct;
328-
jl_ast_context_list_insert(&jl_ast_ctx_using, &jl_ast_main_ctx.list);
329267
jl_init_ast_ctx(&jl_ast_main_ctx);
330268
// To match the one in jl_ast_ctx_leave
331269
JL_SIGATOMIC_BEGIN();
@@ -432,33 +370,31 @@ JL_DLLEXPORT void jl_lisp_prompt(void)
432370
// We don't have our signal handler registered in that case anyway...
433371
JL_SIGATOMIC_BEGIN();
434372
jl_init_flisp();
435-
jl_ast_context_t *ctx = jl_ast_ctx_enter();
436-
JL_AST_PRESERVE_PUSH(ctx, old_roots, jl_main_module);
373+
jl_ast_context_t *ctx = jl_ast_ctx_enter(jl_main_module);
437374
fl_context_t *fl_ctx = &ctx->fl;
438375
fl_applyn(fl_ctx, 1, symbol_value(symbol(fl_ctx, "__start")), fl_cons(fl_ctx, fl_ctx->NIL,fl_ctx->NIL));
439-
JL_AST_PRESERVE_POP(ctx, old_roots);
440376
jl_ast_ctx_leave(ctx);
441377
}
442378

443379
JL_DLLEXPORT void fl_show_profile(void)
444380
{
445-
jl_ast_context_t *ctx = jl_ast_ctx_enter();
381+
jl_ast_context_t *ctx = jl_ast_ctx_enter(NULL);
446382
fl_context_t *fl_ctx = &ctx->fl;
447383
fl_applyn(fl_ctx, 0, symbol_value(symbol(fl_ctx, "show-profiles")));
448384
jl_ast_ctx_leave(ctx);
449385
}
450386

451387
JL_DLLEXPORT void fl_clear_profile(void)
452388
{
453-
jl_ast_context_t *ctx = jl_ast_ctx_enter();
389+
jl_ast_context_t *ctx = jl_ast_ctx_enter(NULL);
454390
fl_context_t *fl_ctx = &ctx->fl;
455391
fl_applyn(fl_ctx, 0, symbol_value(symbol(fl_ctx, "clear-profiles")));
456392
jl_ast_ctx_leave(ctx);
457393
}
458394

459395
JL_DLLEXPORT void fl_profile(const char *fname)
460396
{
461-
jl_ast_context_t *ctx = jl_ast_ctx_enter();
397+
jl_ast_context_t *ctx = jl_ast_ctx_enter(NULL);
462398
fl_context_t *fl_ctx = &ctx->fl;
463399
fl_applyn(fl_ctx, 1, symbol_value(symbol(fl_ctx, "profile-e")), symbol(fl_ctx, fname));
464400
jl_ast_ctx_leave(ctx);
@@ -843,7 +779,7 @@ JL_DLLEXPORT jl_value_t *jl_fl_parse(const char *text, size_t text_len,
843779
jl_error("Parse `all`: offset not supported");
844780
}
845781

846-
jl_ast_context_t *ctx = jl_ast_ctx_enter();
782+
jl_ast_context_t *ctx = jl_ast_ctx_enter(NULL);
847783
fl_context_t *fl_ctx = &ctx->fl;
848784
value_t fl_text = cvalue_static_cstrn(fl_ctx, text, text_len);
849785
fl_gc_handle(fl_ctx, &fl_text);
@@ -881,14 +817,12 @@ JL_DLLEXPORT jl_value_t *jl_fl_parse(const char *text, size_t text_len,
881817
// returns either an expression or a thunk
882818
jl_value_t *jl_call_scm_on_ast(const char *funcname, jl_value_t *expr, jl_module_t *inmodule)
883819
{
884-
jl_ast_context_t *ctx = jl_ast_ctx_enter();
820+
jl_ast_context_t *ctx = jl_ast_ctx_enter(inmodule);
885821
fl_context_t *fl_ctx = &ctx->fl;
886-
JL_AST_PRESERVE_PUSH(ctx, old_roots, inmodule);
887822
value_t arg = julia_to_scm(fl_ctx, expr);
888823
value_t e = fl_applyn(fl_ctx, 1, symbol_value(symbol(fl_ctx, funcname)), arg);
889824
jl_value_t *result = scm_to_julia(fl_ctx, e, inmodule);
890825
JL_GC_PUSH1(&result);
891-
JL_AST_PRESERVE_POP(ctx, old_roots);
892826
jl_ast_ctx_leave(ctx);
893827
JL_GC_POP();
894828
return result;
@@ -897,15 +831,13 @@ jl_value_t *jl_call_scm_on_ast(const char *funcname, jl_value_t *expr, jl_module
897831
static jl_value_t *jl_call_scm_on_ast_and_loc(const char *funcname, jl_value_t *expr,
898832
jl_module_t *inmodule, const char *file, int line)
899833
{
900-
jl_ast_context_t *ctx = jl_ast_ctx_enter();
834+
jl_ast_context_t *ctx = jl_ast_ctx_enter(inmodule);
901835
fl_context_t *fl_ctx = &ctx->fl;
902-
JL_AST_PRESERVE_PUSH(ctx, old_roots, inmodule);
903836
value_t arg = julia_to_scm(fl_ctx, expr);
904837
value_t e = fl_applyn(fl_ctx, 3, symbol_value(symbol(fl_ctx, funcname)), arg,
905838
symbol(fl_ctx, file), fixnum(line));
906839
jl_value_t *result = scm_to_julia(fl_ctx, e, inmodule);
907840
JL_GC_PUSH1(&result);
908-
JL_AST_PRESERVE_POP(ctx, old_roots);
909841
jl_ast_ctx_leave(ctx);
910842
JL_GC_POP();
911843
return result;
@@ -989,7 +921,7 @@ JL_DLLEXPORT jl_value_t *jl_copy_ast(jl_value_t *expr)
989921

990922
JL_DLLEXPORT int jl_is_operator(char *sym)
991923
{
992-
jl_ast_context_t *ctx = jl_ast_ctx_enter();
924+
jl_ast_context_t *ctx = jl_ast_ctx_enter(NULL);
993925
fl_context_t *fl_ctx = &ctx->fl;
994926
int res = fl_applyn(fl_ctx, 1, symbol_value(symbol(fl_ctx, "operator?")), symbol(fl_ctx, sym)) == fl_ctx->T;
995927
jl_ast_ctx_leave(ctx);
@@ -998,7 +930,7 @@ JL_DLLEXPORT int jl_is_operator(char *sym)
998930

999931
JL_DLLEXPORT int jl_is_unary_operator(char *sym)
1000932
{
1001-
jl_ast_context_t *ctx = jl_ast_ctx_enter();
933+
jl_ast_context_t *ctx = jl_ast_ctx_enter(NULL);
1002934
fl_context_t *fl_ctx = &ctx->fl;
1003935
int res = fl_applyn(fl_ctx, 1, symbol_value(symbol(fl_ctx, "unary-op?")), symbol(fl_ctx, sym)) == fl_ctx->T;
1004936
jl_ast_ctx_leave(ctx);
@@ -1007,7 +939,7 @@ JL_DLLEXPORT int jl_is_unary_operator(char *sym)
1007939

1008940
JL_DLLEXPORT int jl_is_unary_and_binary_operator(char *sym)
1009941
{
1010-
jl_ast_context_t *ctx = jl_ast_ctx_enter();
942+
jl_ast_context_t *ctx = jl_ast_ctx_enter(NULL);
1011943
fl_context_t *fl_ctx = &ctx->fl;
1012944
int res = fl_applyn(fl_ctx, 1, symbol_value(symbol(fl_ctx, "unary-and-binary-op?")), symbol(fl_ctx, sym)) == fl_ctx->T;
1013945
jl_ast_ctx_leave(ctx);
@@ -1016,7 +948,7 @@ JL_DLLEXPORT int jl_is_unary_and_binary_operator(char *sym)
1016948

1017949
JL_DLLEXPORT int jl_is_syntactic_operator(char *sym)
1018950
{
1019-
jl_ast_context_t *ctx = jl_ast_ctx_enter();
951+
jl_ast_context_t *ctx = jl_ast_ctx_enter(NULL);
1020952
fl_context_t *fl_ctx = &ctx->fl;
1021953
int res = fl_applyn(fl_ctx, 1, symbol_value(symbol(fl_ctx, "syntactic-op?")), symbol(fl_ctx, sym)) == fl_ctx->T;
1022954
jl_ast_ctx_leave(ctx);
@@ -1025,7 +957,7 @@ JL_DLLEXPORT int jl_is_syntactic_operator(char *sym)
1025957

1026958
JL_DLLEXPORT int jl_operator_precedence(char *sym)
1027959
{
1028-
jl_ast_context_t *ctx = jl_ast_ctx_enter();
960+
jl_ast_context_t *ctx = jl_ast_ctx_enter(NULL);
1029961
fl_context_t *fl_ctx = &ctx->fl;
1030962
int res = numval(fl_applyn(fl_ctx, 1, symbol_value(symbol(fl_ctx, "operator-precedence")), symbol(fl_ctx, sym)));
1031963
jl_ast_ctx_leave(ctx);
@@ -1244,14 +1176,12 @@ JL_DLLEXPORT jl_value_t *jl_expand_with_loc_warn(jl_value_t *expr, jl_module_t *
12441176
JL_GC_PUSH2(&expr, &kwargs);
12451177
expr = jl_copy_ast(expr);
12461178
expr = jl_expand_macros(expr, inmodule, NULL, 0, ~(size_t)0, 1);
1247-
jl_ast_context_t *ctx = jl_ast_ctx_enter();
1179+
jl_ast_context_t *ctx = jl_ast_ctx_enter(inmodule);
12481180
fl_context_t *fl_ctx = &ctx->fl;
1249-
JL_AST_PRESERVE_PUSH(ctx, old_roots, inmodule);
12501181
value_t arg = julia_to_scm(fl_ctx, expr);
12511182
value_t e = fl_applyn(fl_ctx, 4, symbol_value(symbol(fl_ctx, "jl-expand-to-thunk-warn")), arg,
12521183
symbol(fl_ctx, file), fixnum(line), fl_ctx->F);
12531184
expr = scm_to_julia(fl_ctx, e, inmodule);
1254-
JL_AST_PRESERVE_POP(ctx, old_roots);
12551185
jl_ast_ctx_leave(ctx);
12561186
jl_sym_t *warn_sym = jl_symbol("warn");
12571187
if (jl_is_expr(expr) && ((jl_expr_t*)expr)->head == warn_sym) {

0 commit comments

Comments
 (0)