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