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
c57ef80c
Commit
c57ef80c
authored
May 06, 2022
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move optional to fundamental and adapt / arrange deque
parent
da6a6cde
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
98 additions
and
68 deletions
+98
-68
rt/include/typon/deque.hpp
rt/include/typon/deque.hpp
+27
-65
rt/include/typon/fork.hpp
rt/include/typon/fork.hpp
+1
-1
rt/include/typon/fundamental/optional.hpp
rt/include/typon/fundamental/optional.hpp
+67
-0
rt/include/typon/scheduler.hpp
rt/include/typon/scheduler.hpp
+3
-2
No files found.
rt/include/typon/deque.hpp
View file @
c57ef80c
...
@@ -4,13 +4,17 @@
...
@@ -4,13 +4,17 @@
#include <atomic>
#include <atomic>
#include <cstdint>
#include <cstdint>
#include <memory>
#include <memory>
#include <type_traits>
#include <utility>
#include <utility>
#include <typon/fundamental/optional.hpp>
namespace
typon
namespace
typon
{
{
template
<
typename
T
>
template
<
typename
T
>
requires
std
::
is_trivially_copyable_v
<
T
>
struct
RingBuffer
struct
RingBuffer
{
{
using
u8
=
std
::
uint_least8_t
;
using
u8
=
std
::
uint_least8_t
;
...
@@ -75,57 +79,16 @@ namespace typon
...
@@ -75,57 +79,16 @@ namespace typon
};
};
template
<
typename
T
>
struct
Optional
{
enum
State
:
unsigned
char
{
empty
=
0
,
abort
=
1
,
engaged
=
2
};
State
_state
;
union
{
T
_value
;
};
Optional
(
State
state
=
empty
)
noexcept
:
_state
(
state
)
{}
Optional
(
T
value
)
noexcept
:
_state
(
engaged
),
_value
(
value
)
{}
~
Optional
()
{
if
(
_state
==
engaged
)
{
std
::
destroy_at
(
std
::
addressof
(
_value
));
}
}
operator
bool
()
noexcept
{
return
_state
==
engaged
;
}
State
state
()
noexcept
{
return
_state
;
}
T
*
operator
->
()
noexcept
{
return
std
::
addressof
(
_value
);
}
T
&
operator
*
()
noexcept
{
return
_value
;
}
};
template
<
typename
T
>
template
<
typename
T
>
struct
Deque
struct
Deque
{
{
using
u8
=
typename
RingBuffer
<
T
>::
u8
;
using
u8
=
typename
RingBuffer
<
T
>::
u8
;
using
u64
=
typename
RingBuffer
<
T
>::
u64
;
using
u64
=
typename
RingBuffer
<
T
>::
u64
;
using
Array
=
RingBuffer
<
T
>
;
using
Array
=
RingBuffer
<
T
>
;
using
Pop
=
fdt
::
optional
<
T
,
2
>
;
static
constexpr
typename
Pop
::
template
state
<
0
>
Empty
{};
static
constexpr
typename
Pop
::
template
state
<
1
>
Abort
{};
using
enum
std
::
memory_order
;
using
enum
std
::
memory_order
;
...
@@ -157,49 +120,48 @@ namespace typon
...
@@ -157,49 +120,48 @@ namespace typon
_bottom
.
store
(
bottom
+
1
,
relaxed
);
_bottom
.
store
(
bottom
+
1
,
relaxed
);
}
}
Optional
<
T
>
pop
()
noexcept
Pop
pop
()
noexcept
{
{
u64
bottom
=
_bottom
.
load
(
relaxed
)
-
1
;
u64
bottom
=
_bottom
.
load
(
relaxed
)
-
1
;
Array
*
array
=
_array
.
load
(
relaxed
);
Array
*
array
=
_array
.
load
(
relaxed
);
_bottom
.
store
(
bottom
,
relaxed
);
_bottom
.
store
(
bottom
,
relaxed
);
std
::
atomic_thread_fence
(
seq_cst
);
std
::
atomic_thread_fence
(
seq_cst
);
u64
top
=
_top
.
load
(
relaxed
);
u64
top
=
_top
.
load
(
relaxed
);
Optional
<
T
>
x
{};
if
(
top
>
bottom
)
if
(
top
<=
bottom
)
{
{
x
=
array
->
get
(
bottom
);
_bottom
.
store
(
top
,
relaxed
);
if
(
top
==
bottom
)
return
{
Empty
};
{
if
(
!
_top
.
compare_exchange_strong
(
top
,
top
+
1
,
seq_cst
,
relaxed
))
{
x
=
{};
}
_bottom
.
store
(
bottom
+
1
,
relaxed
);
}
}
T
x
=
array
->
get
(
bottom
);
if
(
top
<
bottom
)
{
return
{
x
};
}
}
else
if
(
!
_top
.
compare_exchange_strong
(
top
,
top
+
1
,
seq_cst
,
relaxed
))
{
{
_bottom
.
store
(
bottom
+
1
,
relaxed
);
_bottom
.
store
(
top
+
1
,
relaxed
);
return
{
Empty
};
}
}
return
x
;
_bottom
.
store
(
top
+
1
,
relaxed
);
return
{
x
};
}
}
Optional
<
T
>
steal
()
noexcept
Pop
steal
()
noexcept
{
{
u64
top
=
_top
.
load
(
acquire
);
u64
top
=
_top
.
load
(
acquire
);
std
::
atomic_thread_fence
(
seq_cst
);
std
::
atomic_thread_fence
(
seq_cst
);
u64
bottom
=
_bottom
.
load
(
acquire
);
u64
bottom
=
_bottom
.
load
(
acquire
);
Optional
<
T
>
x
{};
if
(
top
<
bottom
)
if
(
top
<
bottom
)
{
{
Array
*
array
=
_array
.
load
(
consume
);
Array
*
array
=
_array
.
load
(
consume
);
x
=
array
->
get
(
top
);
T
x
=
array
->
get
(
top
);
if
(
!
_top
.
compare_exchange_strong
(
top
,
top
+
1
,
seq_cst
,
relaxed
))
if
(
!
_top
.
compare_exchange_strong
(
top
,
top
+
1
,
seq_cst
,
relaxed
))
{
{
return
{
Optional
<
T
>::
abort
};
return
{
Abort
};
}
}
return
{
x
};
}
}
return
x
;
return
{
Empty
}
;
}
}
};
};
...
...
rt/include/typon/fork.hpp
View file @
c57ef80c
...
@@ -51,7 +51,7 @@ namespace typon
...
@@ -51,7 +51,7 @@ namespace typon
{
{
std
::
coroutine_handle
<>
await_suspend
(
std
::
coroutine_handle
<
promise_type
>
coroutine
)
noexcept
std
::
coroutine_handle
<>
await_suspend
(
std
::
coroutine_handle
<
promise_type
>
coroutine
)
noexcept
{
{
if
(
Optional
continuation
=
Scheduler
::
pop
())
if
(
auto
continuation
=
Scheduler
::
pop
())
{
{
return
*
continuation
;
return
*
continuation
;
}
}
...
...
rt/include/typon/fundamental/optional.hpp
0 → 100644
View file @
c57ef80c
#ifndef TYPON_FUNDAMENTAL_OPTIONAL_HPP_INCLUDED
#define TYPON_FUNDAMENTAL_OPTIONAL_HPP_INCLUDED
#include <type_traits>
namespace
typon
::
fdt
{
template
<
typename
T
,
unsigned
char
N
>
requires
std
::
is_trivially_copyable_v
<
T
>
struct
optional
{
static_assert
(
N
>
0
,
"N must be greater than 0"
);
template
<
unsigned
char
I
>
requires
(
I
<=
N
)
using
state
=
std
::
integral_constant
<
unsigned
char
,
I
>
;
unsigned
char
_state
;
union
{
T
_value
;
};
optional
()
noexcept
:
_state
(
0
)
{}
template
<
unsigned
char
I
>
requires
(
I
<
N
)
optional
(
state
<
I
>
state
)
noexcept
:
_state
(
state
)
{}
optional
(
T
value
)
noexcept
:
_state
(
N
),
_value
(
value
)
{}
~
optional
()
{
if
(
_state
==
N
)
{
std
::
destroy_at
(
std
::
addressof
(
_value
));
}
}
operator
bool
()
noexcept
{
return
_state
==
N
;
}
template
<
unsigned
char
I
>
bool
match
(
state
<
I
>
state
)
noexcept
{
return
state
()
==
_state
;
}
T
*
operator
->
()
noexcept
{
return
std
::
addressof
(
_value
);
}
T
&
operator
*
()
noexcept
{
return
_value
;
}
};
}
#endif // TYPON_FUNDAMENTAL_OPTIONAL_HPP_INCLUDED
rt/include/typon/scheduler.hpp
View file @
c57ef80c
...
@@ -8,6 +8,7 @@
...
@@ -8,6 +8,7 @@
#include <vector>
#include <vector>
#include <typon/fundamental/event_count.hpp>
#include <typon/fundamental/event_count.hpp>
#include <typon/fundamental/optional.hpp>
#include <typon/fundamental/random.hpp>
#include <typon/fundamental/random.hpp>
#include <typon/continuation.hpp>
#include <typon/continuation.hpp>
...
@@ -20,8 +21,8 @@ namespace typon
...
@@ -20,8 +21,8 @@ namespace typon
struct
Scheduler
struct
Scheduler
{
{
using
uint
=
unsigned
int
;
using
uint
=
unsigned
int
;
using
Task
=
Optional
<
continuation_handle
>
;
using
Deque
=
Deque
<
continuation_handle
>
;
using
Deque
=
Deque
<
continuation_handle
>
;
using
Task
=
typename
Deque
::
Pop
;
static
inline
thread_local
uint
thread_id
;
static
inline
thread_local
uint
thread_id
;
...
@@ -151,7 +152,7 @@ namespace typon
...
@@ -151,7 +152,7 @@ namespace typon
}
}
auto
key
=
_notifyer
.
prepare_wait
();
auto
key
=
_notifyer
.
prepare_wait
();
task
=
_deque
.
back
().
steal
();
task
=
_deque
.
back
().
steal
();
if
(
task
.
state
()
!=
Task
::
empty
)
if
(
!
task
.
match
(
Deque
::
Empty
)
)
{
{
_notifyer
.
cancel_wait
();
_notifyer
.
cancel_wait
();
if
(
task
)
if
(
task
)
...
...
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