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
b82cce1b
Commit
b82cce1b
authored
Oct 08, 2017
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Prevent multiplexer from writing into its own pipe
parent
65c300db
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
28 deletions
+84
-28
libcaf_io/caf/io/network/default_multiplexer.hpp
libcaf_io/caf/io/network/default_multiplexer.hpp
+28
-3
libcaf_io/src/default_multiplexer.cpp
libcaf_io/src/default_multiplexer.cpp
+56
-25
No files found.
libcaf_io/caf/io/network/default_multiplexer.hpp
View file @
b82cce1b
...
@@ -347,6 +347,8 @@ public:
...
@@ -347,6 +347,8 @@ public:
supervisor_ptr
make_supervisor
()
override
;
supervisor_ptr
make_supervisor
()
override
;
/// Tries to run one or more events.
/// @returns `true` if at least one event occurred, otherwise `false`.
bool
poll_once
(
bool
block
);
bool
poll_once
(
bool
block
);
bool
try_run_once
()
override
;
bool
try_run_once
()
override
;
...
@@ -359,7 +361,13 @@ public:
...
@@ -359,7 +361,13 @@ public:
void
del
(
operation
op
,
native_socket
fd
,
event_handler
*
ptr
);
void
del
(
operation
op
,
native_socket
fd
,
event_handler
*
ptr
);
/// Calls `ptr->resume`.
void
resume
(
intrusive_ptr
<
resumable
>
ptr
);
private:
private:
/// Calls `epoll`, `kqueue`, or `poll` with or without blocking.
bool
poll_once_impl
(
bool
block
);
// platform-dependent additional initialization code
// platform-dependent additional initialization code
void
init
();
void
init
();
...
@@ -410,14 +418,31 @@ private:
...
@@ -410,14 +418,31 @@ private:
void
wr_dispatch_request
(
resumable
*
ptr
);
void
wr_dispatch_request
(
resumable
*
ptr
);
//
resumable* rd_dispatch_request();
//
/ Socket handle to an OS-level event loop such as `epoll`. Unused in the
/// `poll` implementation.
native_socket
epollfd_
;
// unused in poll() implementation
native_socket
epollfd_
;
// unused in poll() implementation
/// Platform-dependent bookkeeping data, e.g., `pollfd` or `epoll_event`.
std
::
vector
<
multiplexer_data
>
pollset_
;
std
::
vector
<
multiplexer_data
>
pollset_
;
std
::
vector
<
event
>
events_
;
// always sorted by .fd
/// Insertion and deletion events. This vector is always sorted by `.fd`.
std
::
vector
<
event
>
events_
;
/// Platform-dependent meta data for `pollset_`. This allows O(1) lookup of
/// event handlers from `pollfd`.
multiplexer_poll_shadow_data
shadow_
;
multiplexer_poll_shadow_data
shadow_
;
/// Pipe for pushing events and callbacks into the multiplexer's thread.
std
::
pair
<
native_socket
,
native_socket
>
pipe_
;
std
::
pair
<
native_socket
,
native_socket
>
pipe_
;
/// Special-purpose event handler for the pipe.
pipe_reader
pipe_reader_
;
pipe_reader
pipe_reader_
;
/// Events posted from the multiplexer's own thread are cached in this vector
/// in order to prevent the multiplexer from writing into its own pipe. This
/// avoids a possible deadlock where the multiplexer is blocked in
/// `wr_dispatch_request` when the pipe's buffer is full.
std
::
vector
<
intrusive_ptr
<
resumable
>>
internally_posted_
;
};
};
inline
connection_handle
conn_hdl_from_socket
(
native_socket
fd
)
{
inline
connection_handle
conn_hdl_from_socket
(
native_socket
fd
)
{
...
...
libcaf_io/src/default_multiplexer.cpp
View file @
b82cce1b
...
@@ -296,8 +296,9 @@ namespace network {
...
@@ -296,8 +296,9 @@ namespace network {
}
}
}
}
bool
default_multiplexer
::
poll_once
(
bool
block
)
{
bool
default_multiplexer
::
poll_once
_impl
(
bool
block
)
{
CAF_LOG_TRACE
(
"epoll()-based multiplexer"
);
CAF_LOG_TRACE
(
"epoll()-based multiplexer"
);
CAF_ASSERT
(
block
==
false
||
internally_posted_
.
empty
());
// Keep running in case of `EINTR`.
// Keep running in case of `EINTR`.
for
(;;)
{
for
(;;)
{
int
presult
=
epoll_wait
(
epollfd_
,
pollset_
.
data
(),
int
presult
=
epoll_wait
(
epollfd_
,
pollset_
.
data
(),
...
@@ -437,8 +438,9 @@ namespace network {
...
@@ -437,8 +438,9 @@ namespace network {
shadow_
.
push_back
(
&
pipe_reader_
);
shadow_
.
push_back
(
&
pipe_reader_
);
}
}
bool
default_multiplexer
::
poll_once
(
bool
block
)
{
bool
default_multiplexer
::
poll_once
_impl
(
bool
block
)
{
CAF_LOG_TRACE
(
"poll()-based multiplexer"
);
CAF_LOG_TRACE
(
"poll()-based multiplexer"
);
CAF_ASSERT
(
block
==
false
||
internally_posted_
.
empty
());
// we store the results of poll() in a separate vector , because
// we store the results of poll() in a separate vector , because
// altering the pollset while traversing it is not exactly a
// altering the pollset while traversing it is not exactly a
// bright idea ...
// bright idea ...
...
@@ -786,6 +788,46 @@ void default_multiplexer::init() {
...
@@ -786,6 +788,46 @@ void default_multiplexer::init() {
# endif
# endif
}
}
bool
default_multiplexer
::
poll_once
(
bool
block
)
{
CAF_LOG_TRACE
(
CAF_ARG
(
block
));
if
(
!
internally_posted_
.
empty
())
{
// Don't iterate internally_posted_ directly, because resumables can
// enqueue new elements into it.
std
::
vector
<
intrusive_ptr
<
resumable
>>
xs
;
internally_posted_
.
swap
(
xs
);
for
(
auto
&
ptr
:
xs
)
resume
(
std
::
move
(
ptr
));
for
(
auto
&
me
:
events_
)
handle
(
me
);
events_
.
clear
();
// Try to swap back to internall_posted_ to re-use allocated memory.
if
(
internally_posted_
.
empty
())
{
xs
.
swap
(
internally_posted_
);
internally_posted_
.
clear
();
}
poll_once_impl
(
false
);
return
true
;
}
return
poll_once_impl
(
block
);
}
void
default_multiplexer
::
resume
(
intrusive_ptr
<
resumable
>
ptr
)
{
CAF_LOG_TRACE
(
""
);
auto
mt
=
system
().
config
().
scheduler_max_throughput
;
switch
(
ptr
->
resume
(
this
,
mt
))
{
case
resumable
:
:
resume_later
:
// Delay resumable until next cycle.
internally_posted_
.
emplace_back
(
ptr
.
release
(),
false
);
break
;
case
resumable
:
:
shutdown_execution_unit
:
// Don't touch reference count of shutdown helpers.
ptr
.
release
();
break
;
default:
;
// Done. Release reference to resumable.
}
}
default_multiplexer
::~
default_multiplexer
()
{
default_multiplexer
::~
default_multiplexer
()
{
if
(
epollfd_
!=
invalid_native_socket
)
if
(
epollfd_
!=
invalid_native_socket
)
closesocket
(
epollfd_
);
closesocket
(
epollfd_
);
...
@@ -807,14 +849,18 @@ default_multiplexer::~default_multiplexer() {
...
@@ -807,14 +849,18 @@ default_multiplexer::~default_multiplexer() {
}
}
void
default_multiplexer
::
exec_later
(
resumable
*
ptr
)
{
void
default_multiplexer
::
exec_later
(
resumable
*
ptr
)
{
CAF_ASSERT
(
ptr
);
CAF_LOG_TRACE
(
CAF_ARG
(
ptr
));
CAF_ASSERT
(
ptr
!=
nullptr
);
switch
(
ptr
->
subtype
())
{
switch
(
ptr
->
subtype
())
{
case
resumable
:
:
io_actor
:
case
resumable
:
:
io_actor
:
case
resumable
:
:
function_object
:
case
resumable
:
:
function_object
:
wr_dispatch_request
(
ptr
);
if
(
std
::
this_thread
::
get_id
()
!=
thread_id
())
wr_dispatch_request
(
ptr
);
else
internally_posted_
.
emplace_back
(
ptr
,
false
);
break
;
break
;
default:
default:
system
().
scheduler
().
enqueue
(
ptr
);
system
().
scheduler
().
enqueue
(
ptr
);
}
}
}
}
...
@@ -909,27 +955,12 @@ resumable* pipe_reader::try_read_next() {
...
@@ -909,27 +955,12 @@ resumable* pipe_reader::try_read_next() {
void
pipe_reader
::
handle_event
(
operation
op
)
{
void
pipe_reader
::
handle_event
(
operation
op
)
{
CAF_LOG_TRACE
(
CAF_ARG
(
op
));
CAF_LOG_TRACE
(
CAF_ARG
(
op
));
auto
mt
=
backend
().
system
().
config
().
scheduler_max_throughput
;
if
(
op
==
operation
::
read
)
{
switch
(
op
)
{
auto
ptr
=
try_read_next
();
case
operation
:
:
read
:
{
if
(
ptr
!=
nullptr
)
auto
cb
=
try_read_next
();
backend
().
resume
({
ptr
,
false
});
switch
(
cb
->
resume
(
&
backend
(),
mt
))
{
case
resumable
:
:
resume_later
:
backend
().
exec_later
(
cb
);
break
;
case
resumable
:
:
done
:
case
resumable
:
:
awaiting_message
:
intrusive_ptr_release
(
cb
);
break
;
default:
break
;
// ignored
}
break
;
}
default:
// nop (simply ignore errors)
break
;
}
}
// else: ignore errors
}
}
void
pipe_reader
::
init
(
native_socket
sock_fd
)
{
void
pipe_reader
::
init
(
native_socket
sock_fd
)
{
...
...
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