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
6c2e5c5d
Commit
6c2e5c5d
authored
Sep 21, 2011
by
neverlord
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
future_send and delayed_reply
parent
77b1e3d0
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
68 additions
and
7 deletions
+68
-7
cppa/cppa.hpp
cppa/cppa.hpp
+22
-0
cppa/scheduler.hpp
cppa/scheduler.hpp
+4
-3
src/yielding_message_queue.cpp
src/yielding_message_queue.cpp
+3
-2
unit_testing/test__spawn.cpp
unit_testing/test__spawn.cpp
+39
-2
No files found.
cppa/cppa.hpp
View file @
6c2e5c5d
...
...
@@ -297,6 +297,28 @@ void reply(const Arg0& arg0, const Args&... args)
if
(
whom
)
whom
->
enqueue
(
message
(
sptr
,
whom
,
arg0
,
args
...));
}
/**
* @brief Send a message that is delayed by @p rel_time.
*/
template
<
typename
Duration
,
typename
...
Data
>
void
future_send
(
actor_ptr
whom
,
const
Duration
&
rel_time
,
const
Data
&
...
data
)
{
get_scheduler
()
->
future_send
(
self
(),
whom
,
rel_time
,
data
...);
}
/**
*
*/
template
<
typename
Duration
,
typename
...
Data
>
void
delayed_reply
(
const
Duration
&
rel_time
,
const
Data
&
...
data
)
{
auto
whom
=
last_received
().
sender
();
if
(
whom
)
{
get_scheduler
()
->
future_send
(
self
(),
whom
,
rel_time
,
data
...);
}
}
/**
* @brief Blocks execution of this actor until all
* other actors finished execution.
...
...
cppa/scheduler.hpp
View file @
6c2e5c5d
...
...
@@ -86,11 +86,12 @@ class scheduler
virtual
void
await_others_done
();
template
<
typename
Duration
,
typename
...
Data
>
void
future_send
(
actor_ptr
whom
,
const
Duration
&
d
,
const
Data
&
...
data
)
void
future_send
(
actor_ptr
from
,
actor_ptr
to
,
const
Duration
&
rel_time
,
const
Data
&
...
data
)
{
static_assert
(
sizeof
...(
Data
)
>
0
,
"no message to send"
);
any_tuple
tup
=
make_tuple
(
util
::
duration
(
d
),
data
...);
future_send_helper
()
->
enqueue
(
message
(
whom
,
whom
,
tup
));
any_tuple
tup
=
make_tuple
(
util
::
duration
(
rel_time
),
data
...);
future_send_helper
()
->
enqueue
(
message
(
from
,
to
,
tup
));
}
};
...
...
src/yielding_message_queue.cpp
View file @
6c2e5c5d
...
...
@@ -4,6 +4,7 @@
#include <atomic>
#include <iostream>
#include "cppa/cppa.hpp"
#include "cppa/match.hpp"
#include "cppa/context.hpp"
#include "cppa/scheduler.hpp"
...
...
@@ -180,7 +181,7 @@ bool yielding_message_queue_impl::dequeue_impl(timed_invoke_rules& rules, queue_
{
if
(
m_queue
.
empty
()
&&
!
m_has_pending_timeout_request
)
{
get_scheduler
()
->
future_send
(
self
(),
rules
.
timeout
(),
future_send
(
self
(),
rules
.
timeout
(),
atom
(
":Timeout"
),
++
m_active_timeout_id
);
m_has_pending_timeout_request
=
true
;
}
...
...
unit_testing/test__spawn.cpp
View file @
6c2e5c5d
...
...
@@ -50,6 +50,24 @@ void testee2(actor_ptr other)
);
}
void
testee3
(
actor_ptr
parent
)
{
// test a future_send / delayed_reply based loop
future_send
(
self
(),
std
::
chrono
::
milliseconds
(
50
),
atom
(
"Poll"
));
int
polls
=
0
;
receive_while
([
&
polls
]()
{
return
++
polls
<=
5
;
})
(
on
(
atom
(
"Poll"
))
>>
[
&
]()
{
if
(
polls
<
5
)
{
delayed_reply
(
std
::
chrono
::
milliseconds
(
50
),
atom
(
"Poll"
));
}
send
(
parent
,
atom
(
"Push"
),
polls
);
}
);
}
size_t
test__spawn
()
{
CPPA_TEST
(
test__spawn
);
...
...
@@ -65,8 +83,7 @@ size_t test__spawn()
monitor
(
spawn
(
testee2
,
spawn
(
testee1
)));
int
i
=
0
;
int
flags
=
0
;
get_scheduler
()
->
future_send
(
self
(),
std
::
chrono
::
seconds
(
1
),
atom
(
"FooBar"
));
future_send
(
self
(),
std
::
chrono
::
seconds
(
1
),
atom
(
"FooBar"
));
// wait for :Down and :Exit messages of pong
receive_while
([
&
i
]()
{
return
++
i
<=
4
;
})
(
...
...
@@ -115,5 +132,25 @@ size_t test__spawn()
}
// verify pong messages
CPPA_CHECK_EQUAL
(
pongs
(),
5
);
spawn
(
testee3
,
self
());
i
=
0
;
// testee3 sends 5 { "Push", int } messages in a 50 milliseconds interval;
// allow for a maximum error of 5ms
receive_while
([
&
i
]()
{
return
++
i
<=
5
;
})
(
on
<
atom
(
"Push"
),
int
>
()
>>
[
&
](
int
val
)
{
CPPA_CHECK_EQUAL
(
i
,
val
);
//cout << "{ Push, " << val << " } ..." << endl;
},
after
(
std
::
chrono
::
milliseconds
(
55
))
>>
[
&
]()
{
cout
<<
"Push "
<<
i
<<
" was delayed more than 55 milliseconds"
<<
endl
;
CPPA_CHECK
(
false
);
}
);
spawn
(
dancing_kirby
);
await_all_others_done
();
return
CPPA_TEST_RESULT
;
}
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