Skip to content

Commit 13a6d87

Browse files
committed
Add "scheduler" / "locking_spin_count" (int) config parameter.
1 parent 24e049c commit 13a6d87

File tree

6 files changed

+43
-3
lines changed

6 files changed

+43
-3
lines changed

asio/include/asio/detail/conditionally_enabled_mutex.hpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ class conditionally_enabled_mutex
7070
{
7171
if (mutex_.enabled_ && !locked_)
7272
{
73+
for (int n = mutex_.spin_count_; n != 0; n -= (n > 0) ? 1 : 0)
74+
{
75+
if (mutex_.mutex_.try_lock())
76+
{
77+
locked_ = true;
78+
return;
79+
}
80+
}
7381
mutex_.mutex_.lock();
7482
locked_ = true;
7583
}
@@ -104,8 +112,9 @@ class conditionally_enabled_mutex
104112
};
105113

106114
// Constructor.
107-
explicit conditionally_enabled_mutex(bool enabled)
108-
: enabled_(enabled)
115+
explicit conditionally_enabled_mutex(bool enabled, int spin_count = 0)
116+
: spin_count_(spin_count),
117+
enabled_(enabled)
109118
{
110119
}
111120

@@ -124,7 +133,12 @@ class conditionally_enabled_mutex
124133
void lock()
125134
{
126135
if (enabled_)
136+
{
137+
for (int n = spin_count_; n != 0; n -= (n > 0) ? 1 : 0)
138+
if (mutex_.try_lock())
139+
return;
127140
mutex_.lock();
141+
}
128142
}
129143

130144
// Unlock the mutex.
@@ -138,6 +152,7 @@ class conditionally_enabled_mutex
138152
friend class scoped_lock;
139153
friend class conditionally_enabled_event;
140154
asio::detail::mutex mutex_;
155+
const int spin_count_;
141156
const bool enabled_;
142157
};
143158

asio/include/asio/detail/impl/scheduler.ipp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ scheduler::scheduler(asio::execution_context& ctx,
112112
bool own_thread, get_task_func_type get_task)
113113
: asio::detail::execution_context_service_base<scheduler>(ctx),
114114
one_thread_(config(ctx).get("scheduler", "concurrency_hint", 0) == 1),
115-
mutex_(config(ctx).get("scheduler", "locking", true)),
115+
mutex_(config(ctx).get("scheduler", "locking", true),
116+
config(ctx).get("scheduler", "locking_spin_count", 0)),
116117
task_(0),
117118
get_task_(get_task),
118119
task_interrupted_(true),

asio/include/asio/detail/null_static_mutex.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ struct null_static_mutex
3535
{
3636
}
3737

38+
// Try to lock the mutex without blocking.
39+
bool try_lock()
40+
{
41+
return true;
42+
}
43+
3844
// Lock the mutex.
3945
void lock()
4046
{

asio/include/asio/detail/posix_mutex.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ class posix_mutex
4545
::pthread_mutex_destroy(&mutex_); // Ignore EBUSY.
4646
}
4747

48+
// Try to lock the mutex.
49+
bool try_lock()
50+
{
51+
return ::pthread_mutex_trylock(&mutex_) == 0; // Ignore EINVAL.
52+
}
53+
4854
// Lock the mutex.
4955
void lock()
5056
{

asio/include/asio/detail/std_mutex.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ class std_mutex
4343
{
4444
}
4545

46+
// Try to lock the mutex.
47+
bool try_lock()
48+
{
49+
return mutex_.try_lock();
50+
}
51+
4652
// Lock the mutex.
4753
void lock()
4854
{

asio/include/asio/detail/win_mutex.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ class win_mutex
4343
::DeleteCriticalSection(&crit_section_);
4444
}
4545

46+
// Try to lock the mutex.
47+
bool try_lock()
48+
{
49+
return ::TryEnterCriticalSection(&crit_section_) != 0;
50+
}
51+
4652
// Lock the mutex.
4753
void lock()
4854
{

0 commit comments

Comments
 (0)