@@ -136,7 +136,7 @@ typedef struct _jl_ast_context_t {
136136 value_t ssavalue_sym ;
137137 value_t slot_sym ;
138138 jl_module_t * module ; // context module for `current-julia-module-counter`
139- struct _jl_ast_context_t * next ; // invasive list pointer for getting free contexts
139+ arraylist_t pinned_objects ;
140140} jl_ast_context_t ;
141141
142142static jl_ast_context_t jl_ast_main_ctx ;
@@ -279,27 +279,29 @@ static void jl_init_ast_ctx(jl_ast_context_t *ctx) JL_NOTSAFEPOINT
279279 ctx -> slot_sym = symbol (fl_ctx , "slot" );
280280 ctx -> module = NULL ;
281281 set (symbol (fl_ctx , "*scopewarn-opt*" ), fixnum (jl_options .warn_scope ));
282+ arraylist_new (& ctx -> pinned_objects , 0 );
282283}
283284
284285// There should be no GC allocation while holding this lock
285286static uv_mutex_t flisp_lock ;
286- static jl_ast_context_t * jl_ast_ctx_freed = NULL ;
287+ int flisp_initialized = 0 ;
288+ arraylist_t jl_ast_ctx_freed ;
289+ arraylist_t jl_ast_ctx_used ;
287290
288291static jl_ast_context_t * jl_ast_ctx_enter (jl_module_t * m ) JL_GLOBALLY_ROOTED JL_NOTSAFEPOINT
289292{
290293 JL_SIGATOMIC_BEGIN ();
291294 uv_mutex_lock (& flisp_lock );
292- jl_ast_context_t * ctx = jl_ast_ctx_freed ;
293- if (ctx != NULL ) {
294- jl_ast_ctx_freed = ctx -> next ;
295- ctx -> next = NULL ;
296- }
295+ jl_ast_context_t * ctx = (jl_ast_context_t * )arraylist_pop (& jl_ast_ctx_freed );
297296 uv_mutex_unlock (& flisp_lock );
298297 if (ctx == NULL ) {
299298 // Construct a new one if we can't find any
300299 ctx = (jl_ast_context_t * )calloc (1 , sizeof (jl_ast_context_t ));
301300 jl_init_ast_ctx (ctx );
302301 }
302+ uv_mutex_lock (& flisp_lock );
303+ arraylist_push (& jl_ast_ctx_used , ctx );
304+ uv_mutex_unlock (& flisp_lock );
303305 ctx -> module = m ;
304306 return ctx ;
305307}
@@ -308,16 +310,20 @@ static void jl_ast_ctx_leave(jl_ast_context_t *ctx)
308310{
309311 uv_mutex_lock (& flisp_lock );
310312 ctx -> module = NULL ;
311- ctx -> next = jl_ast_ctx_freed ;
312- jl_ast_ctx_freed = ctx ;
313+ ctx -> pinned_objects .len = 0 ; // clear pinned objects
314+ arraylist_pop (& jl_ast_ctx_used );
315+ arraylist_push (& jl_ast_ctx_freed , ctx );
313316 uv_mutex_unlock (& flisp_lock );
314317 JL_SIGATOMIC_END ();
315318}
316319
317320void jl_init_flisp (void )
318321{
319- if (jl_ast_ctx_freed )
322+ if (flisp_initialized )
320323 return ;
324+ flisp_initialized = 1 ;
325+ arraylist_new (& jl_ast_ctx_freed , 0 );
326+ arraylist_new (& jl_ast_ctx_used , 0 );
321327 uv_mutex_init (& flisp_lock );
322328 jl_init_ast_ctx (& jl_ast_main_ctx );
323329 // To match the one in jl_ast_ctx_leave
0 commit comments