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
Show 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 @@
...
@@ -3,6 +3,7 @@
#include <atomic>
#include <atomic>
#include <coroutine>
#include <coroutine>
#include <cstdint>
#include <typon/scheduler.hpp>
#include <typon/scheduler.hpp>
#include <typon/stack.hpp>
#include <typon/stack.hpp>
...
@@ -13,42 +14,45 @@ namespace typon
...
@@ -13,42 +14,45 @@ namespace typon
/* An asynchronous mutex.
/* An asynchronous mutex.
Based on the MCS lock, but without the spinning. This means it should be
Inspired by the implementation in CppCoro by Lewis Baker.
easy to implement spinning before suspension.
https://github.com/lewissbaker/cppcoro
Disadvantage: each lock acquisition incurs an allocation. The alternative
is to potentially spin when unlocking.
*/
*/
struct
Mutex
struct
Mutex
{
{
struct
Node
struct
Node
{
{
std
::
atomic
<
Node
*>
_next
{
nullptr
}
;
Node
*
_next
;
Stack
*
_stack
;
std
::
atomic
<
std
::
uintptr_t
>
_stack
{
0
}
;
};
};
std
::
atomic
<
Node
*>
_state
{
nullptr
};
std
::
atomic
<
Node
*>
_state
{
nullptr
};
Node
*
_waiters
{
nullptr
};
[[
nodiscard
]]
auto
lock
()
noexcept
[[
nodiscard
]]
auto
lock
()
noexcept
{
{
struct
awaitable
struct
awaitable
:
Node
{
{
std
::
atomic
<
Node
*>
&
_state
;
Mutex
*
_mutex
;
Node
*
_prev
;
Node
*
_node
;
bool
await_ready
()
noexcept
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
void
await_suspend
(
std
::
coroutine_handle
<>
coroutine
)
noexcept
{
{
auto
stack
=
_node
->
_stack
=
Scheduler
::
suspend
(
coroutine
);
auto
stack
=
Scheduler
::
suspend
(
coroutine
);
auto
ready
=
_prev
->
_next
.
exchange
(
_node
);
if
(
_stack
.
exchange
(
reinterpret_cast
<
std
::
uintptr_t
>
(
stack
)))
if
(
ready
)
{
{
delete
_prev
;
Scheduler
::
enable
(
stack
);
Scheduler
::
enable
(
stack
);
}
}
}
}
...
@@ -57,28 +61,33 @@ namespace typon
...
@@ -57,28 +61,33 @@ namespace typon
~
awaitable
()
~
awaitable
()
{
{
Node
*
node
=
_node
;
Node
*
waiter
=
_mutex
->
_waiters
;
bool
waiters
=
!
_state
.
compare_exchange_strong
(
node
,
nullptr
);
if
(
!
waiter
)
if
(
waiters
)
{
Node
*
state
=
this
;
if
(
_mutex
->
_state
.
compare_exchange_strong
(
state
,
nullptr
))
{
{
node
=
_node
;
return
;
auto
next
=
node
->
_next
.
exchange
(
_node
);
}
if
(
next
)
auto
next
=
state
;
while
(
next
!=
this
)
{
{
delete
node
;
auto
tmp
=
next
->
_next
;
Scheduler
::
enable
(
next
->
_stack
);
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
();
return
awaitable
{
{},
this
};
auto
prev
=
_state
.
exchange
(
node
);
return
awaitable
{
this
->
_state
,
prev
,
node
};
}
}
};
};
...
...
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