diff --git a/env_universal.cpp b/env_universal.cpp index b5756dfd3..6387677d4 100644 --- a/env_universal.cpp +++ b/env_universal.cpp @@ -289,7 +289,10 @@ void env_universal_init(wchar_t * p, { if (! synchronizes_via_fishd()) { + external_callback = cb; + env_universal_common_init(&callback); env_universal_read_from_file(); + s_env_univeral_inited = true; } else { @@ -378,6 +381,12 @@ void env_universal_barrier() ASSERT_IS_MAIN_THREAD(); message_t *msg; fd_set fds; + + if (! synchronizes_via_fishd()) + { + env_universal_common_sync(); + return; + } if (!s_env_univeral_inited || is_dead()) return; @@ -443,7 +452,7 @@ void env_universal_set(const wcstring &name, const wcstring &value, bool exportv debug(3, L"env_universal_set( \"%ls\", \"%ls\" )", name.c_str(), value.c_str()); - if (is_dead()) + if (! synchronizes_via_fishd() || is_dead()) { env_universal_common_set(name.c_str(), value.c_str(), exportv); } @@ -480,7 +489,7 @@ int env_universal_remove(const wchar_t *name) L"env_universal_remove( \"%ls\" )", name); - if (is_dead()) + if (! synchronizes_via_fishd() || is_dead()) { env_universal_common_remove(name_str); } diff --git a/env_universal_common.cpp b/env_universal_common.cpp index f7fa394f7..9913c8c1d 100644 --- a/env_universal_common.cpp +++ b/env_universal_common.cpp @@ -234,6 +234,11 @@ void env_universal_common_set(const wchar_t *key, const wchar_t *val, bool expor } } +void env_universal_common_sync() +{ + default_universal_vars().sync(); +} + /** Attempt to send the specified message to the specified file descriptor @@ -859,7 +864,7 @@ bool env_universal_t::open_and_acquire_lock(const wcstring &path, int *out_fd) return result_fd >= 0; } -bool env_universal_t::save() +bool env_universal_t::sync() { scoped_lock locker(lock); /* Our saving strategy: @@ -884,6 +889,13 @@ bool env_universal_t::save() Permission denied / other errors: log to the console (once) and then give up */ const wcstring vars_path = explicit_vars_path.empty() ? default_vars_path() : explicit_vars_path; + + /* If we have no changes, just load */ + if (modified.empty()) + { + return this->load_from_path(vars_path); + } + const wcstring directory = wdirname(vars_path); bool success = false; int vars_fd = -1; diff --git a/env_universal_common.h b/env_universal_common.h index 8c9edeb84..26e5c6332 100644 --- a/env_universal_common.h +++ b/env_universal_common.h @@ -189,6 +189,9 @@ env_var_t env_universal_common_get(const wcstring &name); */ bool env_universal_common_get_export(const wcstring &name); +/** Synchronizes all changse: writes everything out, reads stuff in */ +void env_universal_common_sync(); + /** Add messages about all existing variables to the specified connection */ @@ -260,8 +263,8 @@ public: /** Loads variables at the correct path */ bool load(); - /** Writes variables at the correct path */ - bool save(); + /** Reads and writes variables at the correct path */ + bool sync(); /* Internal use */ void read_message(connection_t *src); diff --git a/fish_tests.cpp b/fish_tests.cpp index 37991aa11..7c76fb1c1 100644 --- a/fish_tests.cpp +++ b/fish_tests.cpp @@ -2160,7 +2160,7 @@ static void test_input() } } -#define UVARS_PER_THREAD 100 +#define UVARS_PER_THREAD 8 static int test_universal_helper(int *x) { @@ -2170,10 +2170,10 @@ static int test_universal_helper(int *x) const wcstring key = format_string(L"key_%d_%d", *x, j); const wcstring val = format_string(L"val_%d_%d", *x, j); uvars.set(key, val, false); - bool saved = uvars.save(); - if (! saved) + bool synced = uvars.sync(); + if (! synced) { - err(L"Failed to save universal variables"); + err(L"Failed to sync universal variables"); } fputc('.', stderr); fflush(stderr); @@ -2186,7 +2186,7 @@ static void test_universal() say(L"Testing universal variables"); if (system("mkdir -p /tmp/fish_uvars_test/")) err(L"mkdir failed"); - const int threads = 32; + const int threads = 16; for (int i=0; i < threads; i++) { iothread_perform(test_universal_helper, (void (*)(int *, int))NULL, new int(i)); @@ -2214,6 +2214,7 @@ static void test_universal() } system("rm -Rf /tmp/fish_uvars_test"); + putc('\n', stderr); } class history_tests_t