Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
hiredis
Commits
2b61fd1a
Commit
2b61fd1a
authored
Aug 14, 2012
by
Pieter Noordhuis
Browse files
Replace select(2) with poll(2)
Like
f8debbfd
.
parent
1f6a4d8c
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/handle.c
View file @
2b61fd1a
...
@@ -7,10 +7,8 @@
...
@@ -7,10 +7,8 @@
#include <errno.h>
#include <errno.h>
#include <assert.h>
#include <assert.h>
/* select */
/* poll */
#include <sys/select.h>
#include <poll.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <unistd.h>
/* local */
/* local */
...
@@ -124,51 +122,64 @@ int redis_handle_connect_gai(redis_handle *h,
...
@@ -124,51 +122,64 @@ int redis_handle_connect_gai(redis_handle *h,
return
redis__finish_connect
(
h
,
fd
);
return
redis__finish_connect
(
h
,
fd
);
}
}
static
int
redis__
select
(
int
mode
,
int
fd
,
struct
timeval
timeout
)
{
static
int
redis__
poll
(
int
mode
,
int
fd
,
struct
timeval
timeout
)
{
fd_set
rfd
,
w
fd
;
struct
pollfd
p
fd
;
fd_set
*
_set
=
NULL
,
*
_rfd
=
NULL
,
*
_wfd
=
NULL
;
int
msec
;
int
so_erro
r
;
int
r
v
;
assert
(
fd
<
FD_SETSIZE
)
;
pfd
.
fd
=
fd
;
pfd
.
events
=
0
;
switch
(
mode
)
{
pfd
.
revents
=
0
;
case
REDIS__READABLE
:
FD_ZERO
(
&
rfd
);
if
(
mode
&
REDIS__READABLE
)
{
FD_SET
(
fd
,
&
rfd
)
;
pfd
.
events
|=
POLLIN
;
_rfd
=
_set
=
&
rfd
;
}
break
;
case
REDIS__WRITABLE
:
if
(
mode
&
REDIS__WRITABLE
)
{
FD_ZERO
(
&
wfd
)
;
pfd
.
events
|=
POLLOUT
;
FD_SET
(
fd
,
&
wfd
);
}
_wfd
=
_set
=
&
wfd
;
break
;
msec
=
(
timeout
.
tv_sec
*
1000
)
+
((
timeout
.
tv_usec
+
999
)
/
1000
)
;
default:
if
(
msec
<
0
)
{
assert
(
NULL
&&
"invalid mode"
)
;
msec
=
0
;
}
}
if
(
select
(
FD_SETSIZE
,
_rfd
,
_wfd
,
NULL
,
&
timeout
)
==
-
1
)
{
rv
=
poll
(
&
pfd
,
1
,
msec
);
if
(
rv
==
-
1
)
{
return
REDIS_ESYS
;
return
REDIS_ESYS
;
}
}
/* No
t in set
means
select
(2) timed out */
/* No
events
means
poll
(2) timed out */
if
(
!
FD_ISSET
(
fd
,
_set
)
)
{
if
(
rv
==
0
)
{
errno
=
ETIMEDOUT
;
errno
=
ETIMEDOUT
;
return
REDIS_ESYS
;
return
REDIS_ESYS
;
}
}
/* Check for socket error */
/* POLLERR is always bad */
so_error
=
redis_fd_error
(
fd
);
if
(
pfd
.
revents
&
POLLERR
)
{
if
(
so_error
<
0
)
{
goto
error
;
return
so_error
;
}
}
if
(
so_error
)
{
/* POLLHUP is only bad when interested in POLLOUT.
/* Act as if the socket error occured with select(2)
. */
* When interested in POLLIN, reads can continue until EOF
. */
errno
=
so_error
;
if
((
pfd
.
revents
&
POLLHUP
)
&&
(
pfd
.
events
&
POLLOUT
))
{
return
REDIS_ESYS
;
goto
error
;
}
}
return
REDIS_OK
;
return
REDIS_OK
;
error:
rv
=
redis_fd_error
(
fd
);
if
(
rv
<
0
)
{
return
rv
;
}
/* We got POLLERR so the socket error MUST be non-zero */
assert
(
rv
&&
"Expected socket error"
);
/* Act as if the socket error occured */
errno
=
rv
;
return
REDIS_ESYS
;
}
}
static
int
redis__wait
(
redis_handle
*
h
,
int
mode
)
{
static
int
redis__wait
(
redis_handle
*
h
,
int
mode
)
{
...
@@ -179,7 +190,7 @@ static int redis__wait(redis_handle *h, int mode) {
...
@@ -179,7 +190,7 @@ static int redis__wait(redis_handle *h, int mode) {
return
REDIS_ESYS
;
return
REDIS_ESYS
;
}
}
rv
=
redis__
select
(
mode
,
h
->
fd
,
h
->
timeout
);
rv
=
redis__
poll
(
mode
,
h
->
fd
,
h
->
timeout
);
if
(
rv
<
0
)
{
if
(
rv
<
0
)
{
return
REDIS_ESYS
;
return
REDIS_ESYS
;
}
}
...
...
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