refactor(lua): cleanup and docs for threads

This commit is contained in:
bfredl
2022-02-26 11:03:39 +01:00
parent acf38245d8
commit 850b3e19c9
7 changed files with 79 additions and 78 deletions

View File

@@ -474,6 +474,9 @@ static int nlua_stricmp(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
void nlua_state_add_stdlib(lua_State *const lstate, bool is_thread)
{
if (!is_thread) {
// TODO(bfredl): some of basic string functions should already be
// (or be easy to make) threadsafe
// stricmp
lua_pushcfunction(lstate, &nlua_stricmp);
lua_setfield(lstate, -2, "stricmp");

View File

@@ -157,7 +157,7 @@ void early_init(mparm_T *paramp)
eval_init(); // init global variables
init_path(argv0 ? argv0 : "nvim");
init_normal_cmds(); // Init the table of Normal mode commands.
runtime_search_path_init();
runtime_init();
highlight_init();
#ifdef WIN32

View File

@@ -64,6 +64,9 @@ void fs_init(void)
uv_mutex_init_recursive(&fs_loop_mutex);
}
/// TODO(bfredl): some of these operations should
/// be possible to do the private libuv loop of the
/// thread, instead of contending the global fs loop
void fs_loop_lock(void)
{
uv_mutex_lock(&fs_loop_mutex);

View File

@@ -27,21 +27,11 @@ static RuntimeSearchPath runtime_search_path;
static RuntimeSearchPath runtime_search_path_thread;
static uv_mutex_t runtime_search_path_mutex;
void runtime_search_path_init(void)
void runtime_init(void)
{
uv_mutex_init(&runtime_search_path_mutex);
}
void runtime_search_path_lock(void)
{
uv_mutex_lock(&runtime_search_path_mutex);
}
void runtime_search_path_unlock(void)
{
uv_mutex_unlock(&runtime_search_path_mutex);
}
/// ":runtime [what] {name}"
void ex_runtime(exarg_T *eap)
{
@@ -330,8 +320,9 @@ ArrayOf(String) runtime_get_named(bool lua, Array pat, bool all)
{
int ref;
RuntimeSearchPath path = runtime_search_path_get_cached(&ref);
static char buf[MAXPATHL];
ArrayOf(String) rv = runtime_get_named_common(lua, pat, all, path);
ArrayOf(String) rv = runtime_get_named_common(lua, pat, all, path, buf, sizeof buf);
runtime_search_path_unref(path, &ref);
return rv;
@@ -339,18 +330,19 @@ ArrayOf(String) runtime_get_named(bool lua, Array pat, bool all)
ArrayOf(String) runtime_get_named_thread(bool lua, Array pat, bool all)
{
runtime_search_path_lock();
ArrayOf(String) rv = runtime_get_named_common(lua, pat, all, runtime_search_path_thread);
runtime_search_path_unlock();
// TODO(bfredl): avoid contention between multiple worker threads?
uv_mutex_lock(&runtime_search_path_mutex);
static char buf[MAXPATHL];
ArrayOf(String) rv = runtime_get_named_common(lua, pat, all, runtime_search_path_thread,
buf, sizeof buf);
uv_mutex_unlock(&runtime_search_path_mutex);
return rv;
}
ArrayOf(String) runtime_get_named_common(bool lua, Array pat, bool all,
RuntimeSearchPath path)
RuntimeSearchPath path, char *buf, size_t buf_len)
{
ArrayOf(String) rv = ARRAY_DICT_INIT;
size_t buf_len = MAXPATHL;
char *buf = xmalloc(MAXPATHL);
for (size_t i = 0; i < kv_size(path); i++) {
SearchPathItem *item = &kv_A(path, i);
if (lua) {
@@ -380,7 +372,6 @@ ArrayOf(String) runtime_get_named_common(bool lua, Array pat, bool all,
}
}
done:
xfree(buf);
return rv;
}
@@ -614,10 +605,10 @@ void runtime_search_path_validate(void)
runtime_search_path = runtime_search_path_build();
runtime_search_path_valid = true;
runtime_search_path_ref = NULL; // initially unowned
runtime_search_path_lock();
uv_mutex_lock(&runtime_search_path_mutex);
runtime_search_path_free(runtime_search_path_thread);
runtime_search_path_thread = copy_runtime_search_path(runtime_search_path);
runtime_search_path_unlock();
uv_mutex_unlock(&runtime_search_path_mutex);
}
}