Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
L
libzmq
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
libzmq
Commits
0b32fb36
Commit
0b32fb36
authored
Apr 13, 2020
by
Gudmundur Adalsteinsson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Problem: poller item lookup can be simplified
Solution: Extract generic find function
parent
7b1fef28
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
55 deletions
+52
-55
socket_poller.cpp
src/socket_poller.cpp
+32
-44
socket_poller.hpp
src/socket_poller.hpp
+20
-11
No files found.
src/socket_poller.cpp
View file @
0b32fb36
...
@@ -41,6 +41,18 @@ static bool is_thread_safe (const zmq::socket_base_t &socket_)
...
@@ -41,6 +41,18 @@ static bool is_thread_safe (const zmq::socket_base_t &socket_)
return
socket_
.
is_thread_safe
();
return
socket_
.
is_thread_safe
();
}
}
// compare elements to value
template
<
class
It
,
class
T
,
class
Pred
>
static
It
find_if2
(
It
b_
,
It
e_
,
const
T
&
value
,
Pred
pred
)
{
for
(;
b_
!=
e_
;
++
b_
)
{
if
(
pred
(
*
b_
,
value
))
{
break
;
}
}
return
b_
;
}
zmq
::
socket_poller_t
::
socket_poller_t
()
:
zmq
::
socket_poller_t
::
socket_poller_t
()
:
_tag
(
0xCAFEBABE
),
_tag
(
0xCAFEBABE
),
_signaler
(
NULL
)
_signaler
(
NULL
)
...
@@ -101,12 +113,10 @@ int zmq::socket_poller_t::add (socket_base_t *socket_,
...
@@ -101,12 +113,10 @@ int zmq::socket_poller_t::add (socket_base_t *socket_,
void
*
user_data_
,
void
*
user_data_
,
short
events_
)
short
events_
)
{
{
for
(
items_t
::
iterator
it
=
_items
.
begin
(),
end
=
_items
.
end
();
it
!=
end
;
if
(
find_if2
(
_items
.
begin
(),
_items
.
end
(),
socket_
,
&
is_socket
)
++
it
)
{
!=
_items
.
end
())
{
if
(
it
->
socket
==
socket_
)
{
errno
=
EINVAL
;
errno
=
EINVAL
;
return
-
1
;
return
-
1
;
}
}
}
if
(
is_thread_safe
(
*
socket_
))
{
if
(
is_thread_safe
(
*
socket_
))
{
...
@@ -151,12 +161,10 @@ int zmq::socket_poller_t::add (socket_base_t *socket_,
...
@@ -151,12 +161,10 @@ int zmq::socket_poller_t::add (socket_base_t *socket_,
int
zmq
::
socket_poller_t
::
add_fd
(
fd_t
fd_
,
void
*
user_data_
,
short
events_
)
int
zmq
::
socket_poller_t
::
add_fd
(
fd_t
fd_
,
void
*
user_data_
,
short
events_
)
{
{
for
(
items_t
::
iterator
it
=
_items
.
begin
(),
end
=
_items
.
end
();
it
!=
end
;
if
(
find_if2
(
_items
.
begin
(),
_items
.
end
(),
fd_
,
&
is_fd
)
++
it
)
{
!=
_items
.
end
())
{
if
(
!
it
->
socket
&&
it
->
fd
==
fd_
)
{
errno
=
EINVAL
;
errno
=
EINVAL
;
return
-
1
;
return
-
1
;
}
}
}
const
item_t
item
=
{
const
item_t
item
=
{
...
@@ -183,15 +191,10 @@ int zmq::socket_poller_t::add_fd (fd_t fd_, void *user_data_, short events_)
...
@@ -183,15 +191,10 @@ int zmq::socket_poller_t::add_fd (fd_t fd_, void *user_data_, short events_)
int
zmq
::
socket_poller_t
::
modify
(
const
socket_base_t
*
socket_
,
short
events_
)
int
zmq
::
socket_poller_t
::
modify
(
const
socket_base_t
*
socket_
,
short
events_
)
{
{
const
items_t
::
iterator
end
=
_items
.
end
();
const
items_t
::
iterator
it
=
items_t
::
iterator
it
;
find_if2
(
_items
.
begin
(),
_items
.
end
(),
socket_
,
&
is_socket
);
for
(
it
=
_items
.
begin
();
it
!=
end
;
++
it
)
{
if
(
it
->
socket
==
socket_
)
break
;
}
if
(
it
==
end
)
{
if
(
it
==
_items
.
end
()
)
{
errno
=
EINVAL
;
errno
=
EINVAL
;
return
-
1
;
return
-
1
;
}
}
...
@@ -205,15 +208,10 @@ int zmq::socket_poller_t::modify (const socket_base_t *socket_, short events_)
...
@@ -205,15 +208,10 @@ int zmq::socket_poller_t::modify (const socket_base_t *socket_, short events_)
int
zmq
::
socket_poller_t
::
modify_fd
(
fd_t
fd_
,
short
events_
)
int
zmq
::
socket_poller_t
::
modify_fd
(
fd_t
fd_
,
short
events_
)
{
{
const
items_t
::
iterator
end
=
_items
.
end
();
const
items_t
::
iterator
it
=
items_t
::
iterator
it
;
find_if2
(
_items
.
begin
(),
_items
.
end
(),
fd_
,
&
is_fd
)
;
for
(
it
=
_items
.
begin
();
it
!=
end
;
++
it
)
{
if
(
it
==
_items
.
end
())
{
if
(
!
it
->
socket
&&
it
->
fd
==
fd_
)
break
;
}
if
(
it
==
end
)
{
errno
=
EINVAL
;
errno
=
EINVAL
;
return
-
1
;
return
-
1
;
}
}
...
@@ -227,15 +225,10 @@ int zmq::socket_poller_t::modify_fd (fd_t fd_, short events_)
...
@@ -227,15 +225,10 @@ int zmq::socket_poller_t::modify_fd (fd_t fd_, short events_)
int
zmq
::
socket_poller_t
::
remove
(
socket_base_t
*
socket_
)
int
zmq
::
socket_poller_t
::
remove
(
socket_base_t
*
socket_
)
{
{
const
items_t
::
iterator
end
=
_items
.
end
();
const
items_t
::
iterator
it
=
items_t
::
iterator
it
;
find_if2
(
_items
.
begin
(),
_items
.
end
(),
socket_
,
&
is_socket
);
for
(
it
=
_items
.
begin
();
it
!=
end
;
++
it
)
{
if
(
it
->
socket
==
socket_
)
break
;
}
if
(
it
==
end
)
{
if
(
it
==
_items
.
end
()
)
{
errno
=
EINVAL
;
errno
=
EINVAL
;
return
-
1
;
return
-
1
;
}
}
...
@@ -252,15 +245,10 @@ int zmq::socket_poller_t::remove (socket_base_t *socket_)
...
@@ -252,15 +245,10 @@ int zmq::socket_poller_t::remove (socket_base_t *socket_)
int
zmq
::
socket_poller_t
::
remove_fd
(
fd_t
fd_
)
int
zmq
::
socket_poller_t
::
remove_fd
(
fd_t
fd_
)
{
{
const
items_t
::
iterator
end
=
_items
.
end
();
const
items_t
::
iterator
it
=
items_t
::
iterator
it
;
find_if2
(
_items
.
begin
(),
_items
.
end
(),
fd_
,
&
is_fd
);
for
(
it
=
_items
.
begin
();
it
!=
end
;
++
it
)
{
if
(
!
it
->
socket
&&
it
->
fd
==
fd_
)
break
;
}
if
(
it
==
end
)
{
if
(
it
==
_items
.
end
()
)
{
errno
=
EINVAL
;
errno
=
EINVAL
;
return
-
1
;
return
-
1
;
}
}
...
...
src/socket_poller.hpp
View file @
0b32fb36
...
@@ -86,6 +86,17 @@ class socket_poller_t
...
@@ -86,6 +86,17 @@ class socket_poller_t
bool
check_tag
()
const
;
bool
check_tag
()
const
;
private
:
private
:
typedef
struct
item_t
{
socket_base_t
*
socket
;
fd_t
fd
;
void
*
user_data
;
short
events
;
#if defined ZMQ_POLL_BASED_ON_POLL
int
pollfd_index
;
#endif
}
item_t
;
static
void
zero_trail_events
(
zmq
::
socket_poller_t
::
event_t
*
events_
,
static
void
zero_trail_events
(
zmq
::
socket_poller_t
::
event_t
*
events_
,
int
n_events_
,
int
n_events_
,
int
found_
);
int
found_
);
...
@@ -103,6 +114,15 @@ class socket_poller_t
...
@@ -103,6 +114,15 @@ class socket_poller_t
uint64_t
&
now_
,
uint64_t
&
now_
,
uint64_t
&
end_
,
uint64_t
&
end_
,
bool
&
first_pass_
);
bool
&
first_pass_
);
static
bool
is_socket
(
const
item_t
&
item
,
const
socket_base_t
*
socket_
)
{
return
item
.
socket
==
socket_
;
}
static
bool
is_fd
(
const
item_t
&
item
,
fd_t
fd_
)
{
return
!
item
.
socket
&&
item
.
fd
==
fd_
;
}
int
rebuild
();
int
rebuild
();
// Used to check whether the object is a socket_poller.
// Used to check whether the object is a socket_poller.
...
@@ -111,17 +131,6 @@ class socket_poller_t
...
@@ -111,17 +131,6 @@ class socket_poller_t
// Signaler used for thread safe sockets polling
// Signaler used for thread safe sockets polling
signaler_t
*
_signaler
;
signaler_t
*
_signaler
;
typedef
struct
item_t
{
socket_base_t
*
socket
;
fd_t
fd
;
void
*
user_data
;
short
events
;
#if defined ZMQ_POLL_BASED_ON_POLL
int
pollfd_index
;
#endif
}
item_t
;
// List of sockets
// List of sockets
typedef
std
::
vector
<
item_t
>
items_t
;
typedef
std
::
vector
<
item_t
>
items_t
;
items_t
_items
;
items_t
_items
;
...
...
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