Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
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
Operations
Operations
Metrics
Environments
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
cpp-libs
Actor Framework
Commits
9f5db87e
Commit
9f5db87e
authored
Jun 23, 2015
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Consistently use policy factories in actor pool
parent
d8b30f1c
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
73 additions
and
75 deletions
+73
-75
libcaf_core/caf/actor_pool.hpp
libcaf_core/caf/actor_pool.hpp
+21
-40
libcaf_core/src/actor_pool.cpp
libcaf_core/src/actor_pool.cpp
+48
-31
libcaf_core/test/actor_pool.cpp
libcaf_core/test/actor_pool.cpp
+4
-4
No files found.
libcaf_core/caf/actor_pool.hpp
View file @
9f5db87e
...
@@ -20,8 +20,6 @@
...
@@ -20,8 +20,6 @@
#ifndef CAF_ACTOR_POOL_HPP
#ifndef CAF_ACTOR_POOL_HPP
#define CAF_ACTOR_POOL_HPP
#define CAF_ACTOR_POOL_HPP
#include <atomic>
#include <random>
#include <vector>
#include <vector>
#include <functional>
#include <functional>
...
@@ -63,44 +61,27 @@ public:
...
@@ -63,44 +61,27 @@ public:
using
policy
=
std
::
function
<
void
(
uplock
&
,
const
actor_vec
&
,
using
policy
=
std
::
function
<
void
(
uplock
&
,
const
actor_vec
&
,
mailbox_element_ptr
&
,
execution_unit
*
)
>
;
mailbox_element_ptr
&
,
execution_unit
*
)
>
;
/// Default policy class implementing simple round robin dispatching.
/// Returns a simple round robin dispatching policy.
class
round_robin
{
static
policy
round_robin
();
public:
round_robin
();
/// Returns a broadcast dispatching policy.
round_robin
(
const
round_robin
&
);
static
policy
broadcast
();
void
operator
()(
uplock
&
,
const
actor_vec
&
,
mailbox_element_ptr
&
,
execution_unit
*
);
/// Returns a random dispatching policy.
static
policy
random
();
private:
std
::
atomic
<
size_t
>
pos_
;
/// Returns a split/join dispatching policy. The function object `sf`
};
/// distributes a work item to all workers (split step) and the function
/// object `jf` joins individual results into a single one with `init`
/// Default policy class implementing broadcast dispatching.
/// as initial value of the operation.
class
broadcast
{
/// @tparam T Result type of the join step.
public:
/// @tparam Join Function object with signature `void (T&, message&)`.
void
operator
()(
uplock
&
,
const
actor_vec
&
,
/// @tparam Split Function object with signature
mailbox_element_ptr
&
,
execution_unit
*
);
/// `void (vector<pair<actor, message>>&, message&)`. The first
};
/// argument is a mapping from actors (workers) to tasks
/// (messages). The second argument is the input message.
/// Default policy class implementing random dispatching.
/// The default split policy broadcasts the work item to all
class
random
{
/// workers.
public:
random
();
random
(
const
random
&
);
void
operator
()(
uplock
&
,
const
actor_vec
&
,
mailbox_element_ptr
&
,
execution_unit
*
);
private:
std
::
random_device
rd_
;
};
/// Default policy class implementing broadcast dispatching (split step)
/// followed by a join operation `F` combining all individual results to
/// a single result of type `T`.
/// @tparam T Result type received by the original sender.
/// @tparam Join Functor with signature `void (T&, message&)`.
/// @tparam Split Functor with signature
/// `void (vector<pair<actor, message>>&, message&)`.
template
<
class
T
,
class
Join
,
class
Split
=
detail
::
nop_split
>
template
<
class
T
,
class
Join
,
class
Split
=
detail
::
nop_split
>
static
policy
split_join
(
Join
jf
,
Split
sf
=
Split
(),
T
init
=
T
())
{
static
policy
split_join
(
Join
jf
,
Split
sf
=
Split
(),
T
init
=
T
())
{
using
impl
=
detail
::
split_join
<
T
,
Split
,
Join
>
;
using
impl
=
detail
::
split_join
<
T
,
Split
,
Join
>
;
...
...
libcaf_core/src/actor_pool.cpp
View file @
9f5db87e
...
@@ -19,6 +19,9 @@
...
@@ -19,6 +19,9 @@
#include "caf/actor_pool.hpp"
#include "caf/actor_pool.hpp"
#include <atomic>
#include <random>
#include "caf/send.hpp"
#include "caf/send.hpp"
#include "caf/default_attachable.hpp"
#include "caf/default_attachable.hpp"
...
@@ -26,49 +29,63 @@
...
@@ -26,49 +29,63 @@
namespace
caf
{
namespace
caf
{
actor_pool
::
round_robin
::
round_robin
()
:
pos_
(
0
)
{
actor_pool
::
policy
actor_pool
::
round_robin
()
{
struct
impl
{
impl
()
:
pos_
(
0
)
{
// nop
// nop
}
}
impl
(
const
impl
&
)
:
pos_
(
0
)
{
actor_pool
::
round_robin
::
round_robin
(
const
round_robin
&
)
:
pos_
(
0
)
{
// nop
// nop
}
}
void
operator
()(
uplock
&
guard
,
const
actor_vec
&
vec
,
void
actor_pool
::
round_robin
::
operator
()(
uplock
&
guard
,
const
actor_vec
&
vec
,
mailbox_element_ptr
&
ptr
,
execution_unit
*
host
)
{
mailbox_element_ptr
&
ptr
,
CAF_ASSERT
(
!
vec
.
empty
());
execution_unit
*
host
)
{
CAF_ASSERT
(
!
vec
.
empty
());
actor
selected
=
vec
[
pos_
++
%
vec
.
size
()];
actor
selected
=
vec
[
pos_
++
%
vec
.
size
()];
guard
.
unlock
();
guard
.
unlock
();
selected
->
enqueue
(
std
::
move
(
ptr
),
host
);
selected
->
enqueue
(
std
::
move
(
ptr
),
host
);
}
std
::
atomic
<
size_t
>
pos_
;
};
return
impl
{};
}
}
void
actor_pool
::
broadcast
::
operator
()(
uplock
&
,
const
actor_vec
&
vec
,
namespace
{
mailbox_element_ptr
&
ptr
,
execution_unit
*
host
)
{
void
broadcast_dispatch
(
actor_pool
::
uplock
&
,
const
actor_pool
::
actor_vec
&
vec
,
CAF_ASSERT
(
!
vec
.
empty
());
mailbox_element_ptr
&
ptr
,
execution_unit
*
host
)
{
CAF_ASSERT
(
!
vec
.
empty
());
for
(
size_t
i
=
1
;
i
<
vec
.
size
();
++
i
)
{
for
(
size_t
i
=
1
;
i
<
vec
.
size
();
++
i
)
{
vec
[
i
]
->
enqueue
(
ptr
->
sender
,
ptr
->
mid
,
ptr
->
msg
,
host
);
vec
[
i
]
->
enqueue
(
ptr
->
sender
,
ptr
->
mid
,
ptr
->
msg
,
host
);
}
}
vec
.
front
()
->
enqueue
(
std
::
move
(
ptr
),
host
);
vec
.
front
()
->
enqueue
(
std
::
move
(
ptr
),
host
);
}
}
actor_pool
::
random
::
random
()
{
}
// namespace <anonymous>
// nop
}
actor_pool
::
random
::
random
(
const
random
&
)
{
actor_pool
::
policy
actor_pool
::
broadcast
(
)
{
// nop
return
broadcast_dispatch
;
}
}
void
actor_pool
::
random
::
operator
()(
uplock
&
guard
,
const
actor_vec
&
vec
,
mailbox_element_ptr
&
ptr
,
actor_pool
::
policy
actor_pool
::
random
()
{
execution_unit
*
host
)
{
struct
impl
{
impl
()
{
// nop
}
impl
(
const
impl
&
)
{
// nop
}
void
operator
()(
uplock
&
guard
,
const
actor_vec
&
vec
,
mailbox_element_ptr
&
ptr
,
execution_unit
*
host
)
{
std
::
uniform_int_distribution
<
size_t
>
dis
(
0
,
vec
.
size
()
-
1
);
std
::
uniform_int_distribution
<
size_t
>
dis
(
0
,
vec
.
size
()
-
1
);
upgrade_to_unique_lock
<
detail
::
shared_spinlock
>
unique_guard
{
guard
};
upgrade_to_unique_lock
<
detail
::
shared_spinlock
>
unique_guard
{
guard
};
actor
selected
=
vec
[
dis
(
rd_
)];
actor
selected
=
vec
[
dis
(
rd_
)];
unique_guard
.
unlock
();
unique_guard
.
unlock
();
selected
->
enqueue
(
std
::
move
(
ptr
),
host
);
selected
->
enqueue
(
std
::
move
(
ptr
),
host
);
}
std
::
random_device
rd_
;
};
return
impl
{};
}
}
actor_pool
::~
actor_pool
()
{
actor_pool
::~
actor_pool
()
{
...
...
libcaf_core/test/actor_pool.cpp
View file @
9f5db87e
...
@@ -73,7 +73,7 @@ CAF_TEST_FIXTURE_SCOPE(actor_pool_tests, fixture)
...
@@ -73,7 +73,7 @@ CAF_TEST_FIXTURE_SCOPE(actor_pool_tests, fixture)
CAF_TEST
(
round_robin_actor_pool
)
{
CAF_TEST
(
round_robin_actor_pool
)
{
scoped_actor
self
;
scoped_actor
self
;
auto
w
=
actor_pool
::
make
(
5
,
spawn_worker
,
actor_pool
::
round_robin
{}
);
auto
w
=
actor_pool
::
make
(
5
,
spawn_worker
,
actor_pool
::
round_robin
()
);
self
->
monitor
(
w
);
self
->
monitor
(
w
);
self
->
send
(
w
,
sys_atom
::
value
,
put_atom
::
value
,
spawn_worker
());
self
->
send
(
w
,
sys_atom
::
value
,
put_atom
::
value
,
spawn_worker
());
std
::
vector
<
actor_addr
>
workers
;
std
::
vector
<
actor_addr
>
workers
;
...
@@ -148,9 +148,9 @@ CAF_TEST(round_robin_actor_pool) {
...
@@ -148,9 +148,9 @@ CAF_TEST(round_robin_actor_pool) {
CAF_TEST
(
broadcast_actor_pool
)
{
CAF_TEST
(
broadcast_actor_pool
)
{
scoped_actor
self
;
scoped_actor
self
;
auto
spawn5
=
[]()
{
auto
spawn5
=
[]()
{
return
actor_pool
::
make
(
5
,
spawn_worker
,
actor_pool
::
broadcast
{}
);
return
actor_pool
::
make
(
5
,
spawn_worker
,
actor_pool
::
broadcast
()
);
};
};
auto
w
=
actor_pool
::
make
(
5
,
spawn5
,
actor_pool
::
broadcast
{}
);
auto
w
=
actor_pool
::
make
(
5
,
spawn5
,
actor_pool
::
broadcast
()
);
self
->
send
(
w
,
1
,
2
);
self
->
send
(
w
,
1
,
2
);
std
::
vector
<
int
>
results
;
std
::
vector
<
int
>
results
;
int
i
=
0
;
int
i
=
0
;
...
@@ -170,7 +170,7 @@ CAF_TEST(broadcast_actor_pool) {
...
@@ -170,7 +170,7 @@ CAF_TEST(broadcast_actor_pool) {
CAF_TEST
(
random_actor_pool
)
{
CAF_TEST
(
random_actor_pool
)
{
scoped_actor
self
;
scoped_actor
self
;
auto
w
=
actor_pool
::
make
(
5
,
spawn_worker
,
actor_pool
::
random
{}
);
auto
w
=
actor_pool
::
make
(
5
,
spawn_worker
,
actor_pool
::
random
()
);
for
(
int
i
=
0
;
i
<
5
;
++
i
)
{
for
(
int
i
=
0
;
i
<
5
;
++
i
)
{
self
->
sync_send
(
w
,
1
,
2
).
await
(
self
->
sync_send
(
w
,
1
,
2
).
await
(
[
&
](
int
res
)
{
[
&
](
int
res
)
{
...
...
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