Skip to content

Commit

Permalink
Add "scheduler" / "locking_spin_count" (int) config parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskohlhoff committed Nov 5, 2024
1 parent e07d96a commit 82d1be5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
19 changes: 17 additions & 2 deletions asio/include/asio/detail/conditionally_enabled_mutex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ class conditionally_enabled_mutex
{
if (mutex_.enabled_ && !locked_)
{
for (int n = mutex_.spin_count_; n != 0; n -= (n > 0) ? 1 : 0)
{
if (mutex_.mutex_.try_lock())
{
locked_ = true;
return;
}
}
mutex_.mutex_.lock();
locked_ = true;
}
Expand Down Expand Up @@ -104,8 +112,9 @@ class conditionally_enabled_mutex
};

// Constructor.
explicit conditionally_enabled_mutex(bool enabled)
: enabled_(enabled)
explicit conditionally_enabled_mutex(bool enabled, int spin_count = 0)
: spin_count_(spin_count),
enabled_(enabled)
{
}

Expand All @@ -124,7 +133,12 @@ class conditionally_enabled_mutex
void lock()
{
if (enabled_)
{
for (int n = spin_count_; n != 0; n -= (n > 0) ? 1 : 0)
if (mutex_.try_lock())
return;
mutex_.lock();
}
}

// Unlock the mutex.
Expand All @@ -138,6 +152,7 @@ class conditionally_enabled_mutex
friend class scoped_lock;
friend class conditionally_enabled_event;
asio::detail::mutex mutex_;
const int spin_count_;
const bool enabled_;
};

Expand Down
3 changes: 2 additions & 1 deletion asio/include/asio/detail/impl/scheduler.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ scheduler::scheduler(asio::execution_context& ctx,
bool own_thread, get_task_func_type get_task)
: asio::detail::execution_context_service_base<scheduler>(ctx),
one_thread_(config(ctx).get("scheduler", "concurrency_hint", 0) == 1),
mutex_(config(ctx).get("scheduler", "locking", true)),
mutex_(config(ctx).get("scheduler", "locking", true),
config(ctx).get("scheduler", "locking_spin_count", 0)),
task_(0),
get_task_(get_task),
task_interrupted_(true),
Expand Down
6 changes: 6 additions & 0 deletions asio/include/asio/detail/null_static_mutex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ struct null_static_mutex
{
}

// Try to lock the mutex without blocking.
bool try_lock()
{
return true;
}

// Lock the mutex.
void lock()
{
Expand Down
6 changes: 6 additions & 0 deletions asio/include/asio/detail/posix_mutex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ class posix_mutex
::pthread_mutex_destroy(&mutex_); // Ignore EBUSY.
}

// Try to lock the mutex.
bool try_lock()
{
return ::pthread_mutex_trylock(&mutex_) == 0; // Ignore EINVAL.
}

// Lock the mutex.
void lock()
{
Expand Down
6 changes: 6 additions & 0 deletions asio/include/asio/detail/std_mutex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class std_mutex
{
}

// Try to lock the mutex.
bool try_lock()
{
return mutex_.try_lock();
}

// Lock the mutex.
void lock()
{
Expand Down
6 changes: 6 additions & 0 deletions asio/include/asio/detail/win_mutex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class win_mutex
::DeleteCriticalSection(&crit_section_);
}

// Try to lock the mutex.
bool try_lock()
{
return ::TryEnterCriticalSection(&crit_section_) != 0;
}

// Lock the mutex.
void lock()
{
Expand Down

0 comments on commit 82d1be5

Please sign in to comment.