Adopt owning_lock in builtin_commandline

This commit is contained in:
ridiculousfish 2017-01-29 18:08:00 -08:00
parent 10a2275c34
commit 3569987c81

View file

@ -50,46 +50,37 @@ static const wchar_t *get_buffer() { return current_buffer; }
/// Returns the position of the cursor. /// Returns the position of the cursor.
static size_t get_cursor_pos() { return current_cursor_pos; } static size_t get_cursor_pos() { return current_cursor_pos; }
static pthread_mutex_t transient_commandline_lock = PTHREAD_MUTEX_INITIALIZER; static owning_lock<wcstring_list_t> &get_transient_stack() {
static wcstring_list_t *get_transient_stack() {
ASSERT_IS_MAIN_THREAD(); ASSERT_IS_MAIN_THREAD();
ASSERT_IS_LOCKED(transient_commandline_lock); static owning_lock<wcstring_list_t> s_transient_stack;
// A pointer is a little more efficient than an object as a static because we can elide the return s_transient_stack;
// thread-safe initialization.
static wcstring_list_t *result = NULL;
if (!result) {
result = new wcstring_list_t();
}
return result;
} }
static bool get_top_transient(wcstring *out_result) { static bool get_top_transient(wcstring *out_result) {
ASSERT_IS_MAIN_THREAD(); auto locked = get_transient_stack().acquire();
bool result = false; wcstring_list_t &stack = locked.value;
scoped_lock locker(transient_commandline_lock); if (stack.empty()) {
const wcstring_list_t *stack = get_transient_stack(); return false;
if (!stack->empty()) {
out_result->assign(stack->back());
result = true;
} }
return result; out_result->assign(stack.back());
return true;
} }
builtin_commandline_scoped_transient_t::builtin_commandline_scoped_transient_t( builtin_commandline_scoped_transient_t::builtin_commandline_scoped_transient_t(
const wcstring &cmd) { const wcstring &cmd) {
ASSERT_IS_MAIN_THREAD(); ASSERT_IS_MAIN_THREAD();
scoped_lock locker(transient_commandline_lock); auto locked = get_transient_stack().acquire();
wcstring_list_t *stack = get_transient_stack(); wcstring_list_t &stack = locked.value;
stack->push_back(cmd); stack.push_back(cmd);
this->token = stack->size(); this->token = stack.size();
} }
builtin_commandline_scoped_transient_t::~builtin_commandline_scoped_transient_t() { builtin_commandline_scoped_transient_t::~builtin_commandline_scoped_transient_t() {
ASSERT_IS_MAIN_THREAD(); ASSERT_IS_MAIN_THREAD();
scoped_lock locker(transient_commandline_lock); auto locked = get_transient_stack().acquire();
wcstring_list_t *stack = get_transient_stack(); wcstring_list_t &stack = locked.value;
assert(this->token == stack->size()); assert(this->token == stack.size());
stack->pop_back(); stack.pop_back();
} }
/// Replace/append/insert the selection with/at/after the specified string. /// Replace/append/insert the selection with/at/after the specified string.