Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
typon-concurrency
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
typon
typon-concurrency
Commits
f074f70a
Commit
f074f70a
authored
Jul 18, 2022
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mutex.hpp: Change the mutex implementation
parent
d70c7609
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
29 deletions
+38
-29
rt/include/typon/mutex.hpp
rt/include/typon/mutex.hpp
+38
-29
No files found.
rt/include/typon/mutex.hpp
View file @
f074f70a
...
...
@@ -3,6 +3,7 @@
#include <atomic>
#include <coroutine>
#include <cstdint>
#include <typon/scheduler.hpp>
#include <typon/stack.hpp>
...
...
@@ -13,42 +14,45 @@ namespace typon
/* An asynchronous mutex.
Based on the MCS lock, but without the spinning. This means it should be
easy to implement spinning before suspension.
Disadvantage: each lock acquisition incurs an allocation. The alternative
is to potentially spin when unlocking.
Inspired by the implementation in CppCoro by Lewis Baker.
https://github.com/lewissbaker/cppcoro
*/
struct
Mutex
{
struct
Node
{
std
::
atomic
<
Node
*>
_next
{
nullptr
}
;
Stack
*
_stack
;
Node
*
_next
;
std
::
atomic
<
std
::
uintptr_t
>
_stack
{
0
}
;
};
std
::
atomic
<
Node
*>
_state
{
nullptr
};
Node
*
_waiters
{
nullptr
};
[[
nodiscard
]]
auto
lock
()
noexcept
{
struct
awaitable
struct
awaitable
:
Node
{
std
::
atomic
<
Node
*>
&
_state
;
Node
*
_prev
;
Node
*
_node
;
Mutex
*
_mutex
;
bool
await_ready
()
noexcept
{
return
!
_prev
;
Node
*
next
=
nullptr
;
for
(;;)
{
// _next must be properly set before updating _state
_next
=
next
;
if
(
_mutex
->
_state
.
compare_exchange_weak
(
next
,
this
))
{
return
!
next
;
}
}
}
void
await_suspend
(
std
::
coroutine_handle
<>
coroutine
)
noexcept
{
auto
stack
=
_node
->
_stack
=
Scheduler
::
suspend
(
coroutine
);
auto
ready
=
_prev
->
_next
.
exchange
(
_node
);
if
(
ready
)
auto
stack
=
Scheduler
::
suspend
(
coroutine
);
if
(
_stack
.
exchange
(
reinterpret_cast
<
std
::
uintptr_t
>
(
stack
)))
{
delete
_prev
;
Scheduler
::
enable
(
stack
);
}
}
...
...
@@ -57,28 +61,33 @@ namespace typon
~
awaitable
()
{
Node
*
node
=
_node
;
bool
waiters
=
!
_state
.
compare_exchange_strong
(
node
,
nullptr
);
if
(
waiters
)
Node
*
waiter
=
_mutex
->
_waiters
;
if
(
!
waiter
)
{
node
=
_node
;
auto
next
=
node
->
_next
.
exchange
(
_node
);
if
(
next
)
Node
*
state
=
this
;
if
(
_mutex
->
_state
.
compare_exchange_strong
(
state
,
nullptr
))
{
return
;
}
auto
next
=
state
;
while
(
next
!=
this
)
{
delete
node
;
Scheduler
::
enable
(
next
->
_stack
);
auto
tmp
=
next
->
_next
;
next
->
_next
=
waiter
;
waiter
=
next
;
next
=
tmp
;
}
}
else
_mutex
->
_waiters
=
waiter
->
_next
;
auto
stack
=
waiter
->
_stack
.
exchange
(
1
);
if
(
stack
)
{
delete
node
;
Scheduler
::
enable
(
reinterpret_cast
<
Stack
*>
(
stack
))
;
}
}
};
auto
node
=
new
Node
();
auto
prev
=
_state
.
exchange
(
node
);
return
awaitable
{
this
->
_state
,
prev
,
node
};
return
awaitable
{
{},
this
};
}
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment