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
549d499e
Commit
549d499e
authored
May 06, 2022
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move and split deque.hpp to fundamental
parent
c57ef80c
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
189 additions
and
3 deletions
+189
-3
rt/include/typon/fundamental/deque.hpp
rt/include/typon/fundamental/deque.hpp
+108
-0
rt/include/typon/fundamental/ring_buffer.hpp
rt/include/typon/fundamental/ring_buffer.hpp
+78
-0
rt/include/typon/scheduler.hpp
rt/include/typon/scheduler.hpp
+3
-3
No files found.
rt/include/typon/deque.hpp
→
rt/include/typon/
fundamental/
deque.hpp
View file @
549d499e
#ifndef TYPON_DEQUE_HPP_INCLUDED
#define TYPON_DEQUE_HPP_INCLUDED
#ifndef TYPON_
FUNDAMENTAL_
DEQUE_HPP_INCLUDED
#define TYPON_
FUNDAMENTAL_
DEQUE_HPP_INCLUDED
#include <atomic>
#include <cstdint>
...
...
@@ -8,99 +8,36 @@
#include <utility>
#include <typon/fundamental/optional.hpp>
#include <typon/fundamental/ring_buffer.hpp>
#include <typon/fundamental/scope.hpp>
namespace
typon
namespace
typon
::
fdt
::
lock_free
{
template
<
typename
T
>
requires
std
::
is_trivially_copyable_v
<
T
>
struct
RingBuffer
struct
deque
{
using
u8
=
std
::
uint_least8_t
;
using
u64
=
std
::
uint_fast64_t
;
using
array_type
=
ring_buffer
<
T
>
;
using
pop_type
=
fdt
::
optional
<
T
,
2
>
;
using
enum
std
::
memory_order
;
const
u8
_bits
;
const
u64
_mask
;
RingBuffer
*
const
_next
;
std
::
atomic
<
T
>
*
const
_array
;
RingBuffer
(
u8
bits
,
RingBuffer
*
next
=
nullptr
)
noexcept
:
_bits
(
bits
)
,
_mask
(
this
->
capacity
()
-
1
)
,
_next
(
next
)
,
_array
(
new
std
::
atomic
<
T
>
[
this
->
capacity
()])
{}
~
RingBuffer
()
{
delete
[]
_array
;
if
(
_next
)
{
delete
_next
;
}
}
u64
capacity
()
noexcept
{
return
u64
(
1
)
<<
_bits
;
}
void
put
(
u64
index
,
T
object
)
noexcept
{
_array
[
index
&
_mask
].
store
(
std
::
move
(
object
),
relaxed
);
}
T
get
(
u64
index
)
noexcept
{
return
_array
[
index
&
_mask
].
load
(
relaxed
);
}
RingBuffer
*
fill
(
RingBuffer
*
sink
,
u64
start
,
u64
end
)
noexcept
{
for
(
u64
i
=
start
;
i
<
end
;
i
++
)
{
sink
->
put
(
i
,
get
(
i
));
}
return
sink
;
}
using
u8
=
typename
ring_buffer
<
T
>::
u8
;
using
u64
=
typename
ring_buffer
<
T
>::
u64
;
RingBuffer
*
grow
(
u64
start
,
u64
end
)
noexcept
{
return
fill
(
new
RingBuffer
(
_bits
+
1
,
this
),
start
,
end
);
}
RingBuffer
*
shrink
(
u64
start
,
u64
end
)
noexcept
{
return
fill
(
std
::
exchange
(
_next
,
nullptr
),
start
,
end
);
}
};
template
<
typename
T
>
struct
Deque
{
using
u8
=
typename
RingBuffer
<
T
>::
u8
;
using
u64
=
typename
RingBuffer
<
T
>::
u64
;
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
{};
static
constexpr
typename
pop_type
::
template
state
<
0
>
Empty
{};
static
constexpr
typename
pop_type
::
template
state
<
1
>
Abort
{};
using
enum
std
::
memory_order
;
std
::
atomic
<
u64
>
_top
{
1
};
std
::
atomic
<
u64
>
_bottom
{
1
};
std
::
atomic
<
Array
*>
_array
;
std
::
atomic
<
array_type
*>
_array
;
D
eque
(
u8
bits
=
2
)
noexcept
:
_array
(
new
Array
(
bits
))
d
eque
(
u8
bits
=
2
)
noexcept
:
_array
(
new
array_type
(
bits
))
{}
~
D
eque
()
~
d
eque
()
{
delete
_array
;
}
...
...
@@ -109,7 +46,7 @@ namespace typon
{
u64
bottom
=
_bottom
.
load
(
relaxed
);
u64
top
=
_top
.
load
(
acquire
);
Array
*
array
=
_array
.
load
(
relaxed
);
array_type
*
array
=
_array
.
load
(
relaxed
);
if
(
bottom
-
top
>
array
->
capacity
()
-
1
)
{
array
=
array
->
grow
(
top
,
bottom
);
...
...
@@ -120,10 +57,10 @@ namespace typon
_bottom
.
store
(
bottom
+
1
,
relaxed
);
}
Pop
pop
()
noexcept
pop_type
pop
()
noexcept
{
u64
bottom
=
_bottom
.
load
(
relaxed
)
-
1
;
Array
*
array
=
_array
.
load
(
relaxed
);
array_type
*
array
=
_array
.
load
(
relaxed
);
_bottom
.
store
(
bottom
,
relaxed
);
std
::
atomic_thread_fence
(
seq_cst
);
u64
top
=
_top
.
load
(
relaxed
);
...
...
@@ -137,23 +74,23 @@ namespace typon
{
return
{
x
};
}
if
(
!
_top
.
compare_exchange_strong
(
top
,
top
+
1
,
seq_cst
,
relaxed
))
{
_bottom
.
store
(
top
+
1
,
relaxed
);
return
{
Empty
};
}
bool
win
=
_top
.
compare_exchange_strong
(
top
,
top
+
1
,
seq_cst
,
relaxed
);
_bottom
.
store
(
top
+
1
,
relaxed
);
if
(
win
)
{
return
{
x
};
}
return
{
Empty
};
}
Pop
steal
()
noexcept
pop_type
steal
()
noexcept
{
u64
top
=
_top
.
load
(
acquire
);
std
::
atomic_thread_fence
(
seq_cst
);
u64
bottom
=
_bottom
.
load
(
acquire
);
if
(
top
<
bottom
)
{
Array
*
array
=
_array
.
load
(
consume
);
array_type
*
array
=
_array
.
load
(
consume
);
T
x
=
array
->
get
(
top
);
if
(
!
_top
.
compare_exchange_strong
(
top
,
top
+
1
,
seq_cst
,
relaxed
))
{
...
...
@@ -168,4 +105,4 @@ namespace typon
}
#endif // TYPON_DEQUE_HPP_INCLUDED
#endif // TYPON_
FUNDAMENTAL_
DEQUE_HPP_INCLUDED
rt/include/typon/fundamental/ring_buffer.hpp
0 → 100644
View file @
549d499e
#ifndef TYPON_FUNDAMENTAL_RINGBUFFER_HPP_INCLUDED
#define TYPON_FUNDAMENTAL_RINGBUFFER_HPP_INCLUDED
#include <atomic>
namespace
typon
::
fdt
::
lock_free
{
template
<
typename
T
>
requires
std
::
is_trivially_copyable_v
<
T
>
struct
ring_buffer
{
using
u8
=
std
::
uint_least8_t
;
using
u64
=
std
::
uint_fast64_t
;
using
enum
std
::
memory_order
;
const
u8
_bits
;
const
u64
_mask
;
ring_buffer
*
const
_next
;
std
::
atomic
<
T
>
*
const
_array
;
ring_buffer
(
u8
bits
,
ring_buffer
*
next
=
nullptr
)
noexcept
:
_bits
(
bits
)
,
_mask
(
this
->
capacity
()
-
1
)
,
_next
(
next
)
,
_array
(
new
std
::
atomic
<
T
>
[
this
->
capacity
()])
{}
~
ring_buffer
()
{
delete
[]
_array
;
if
(
_next
)
{
delete
_next
;
}
}
u64
capacity
()
noexcept
{
return
u64
(
1
)
<<
_bits
;
}
void
put
(
u64
index
,
T
object
)
noexcept
{
_array
[
index
&
_mask
].
store
(
std
::
move
(
object
),
relaxed
);
}
T
get
(
u64
index
)
noexcept
{
return
_array
[
index
&
_mask
].
load
(
relaxed
);
}
ring_buffer
*
fill
(
ring_buffer
*
sink
,
u64
start
,
u64
end
)
noexcept
{
for
(
u64
i
=
start
;
i
<
end
;
i
++
)
{
sink
->
put
(
i
,
get
(
i
));
}
return
sink
;
}
ring_buffer
*
grow
(
u64
start
,
u64
end
)
noexcept
{
return
fill
(
new
ring_buffer
(
_bits
+
1
,
this
),
start
,
end
);
}
ring_buffer
*
shrink
(
u64
start
,
u64
end
)
noexcept
{
return
fill
(
std
::
exchange
(
_next
,
nullptr
),
start
,
end
);
}
};
}
#endif // TYPON_FUNDAMENTAL_RINGBUFFER_HPP_INCLUDED
rt/include/typon/scheduler.hpp
View file @
549d499e
...
...
@@ -7,12 +7,12 @@
#include <thread>
#include <vector>
#include <typon/fundamental/deque.hpp>
#include <typon/fundamental/event_count.hpp>
#include <typon/fundamental/optional.hpp>
#include <typon/fundamental/random.hpp>
#include <typon/continuation.hpp>
#include <typon/deque.hpp>
namespace
typon
...
...
@@ -21,8 +21,8 @@ namespace typon
struct
Scheduler
{
using
uint
=
unsigned
int
;
using
Deque
=
D
eque
<
continuation_handle
>
;
using
Task
=
typename
Deque
::
Pop
;
using
Deque
=
fdt
::
lock_free
::
d
eque
<
continuation_handle
>
;
using
Task
=
typename
Deque
::
pop_type
;
static
inline
thread_local
uint
thread_id
;
...
...
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