mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-12 21:18:53 +00:00
Adopt owning_lock in builtin_commandline
This commit is contained in:
parent
10a2275c34
commit
3569987c81
1 changed files with 17 additions and 26 deletions
|
@ -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<wcstring_list_t> &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<wcstring_list_t> 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.
|
||||
|
|
Loading…
Reference in a new issue