1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* thread_pthread.c (rb_thread_wakeup_timer_thread_fd): add fd

argument and remove hardcoded dependency of timer_thread_pipe[1].
* thread_pthread.c (consume_communication_pipe): add fd argument.
* thread_pthread.c (close_communication_pipe): ditto.

* thread_pthread.c (timer_thread_sleep): adjust the above changes.

* thread_pthread.c (setup_communication_pipe_internal): factor
  out pipe initialize logic.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39685 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
kosaki 2013-03-10 04:00:10 +00:00
parent 0eeb116923
commit 9c021064a2
2 changed files with 58 additions and 32 deletions

View file

@ -1,3 +1,15 @@
Wed Mar 6 22:36:19 2013 KOSAKI Motohiro <kosaki.motohiro@gmail.com>
* thread_pthread.c (rb_thread_wakeup_timer_thread_fd): add fd
argument and remove hardcoded dependency of timer_thread_pipe[1].
* thread_pthread.c (consume_communication_pipe): add fd argument.
* thread_pthread.c (close_communication_pipe): ditto.
* thread_pthread.c (timer_thread_sleep): adjust the above changes.
* thread_pthread.c (setup_communication_pipe_internal): factor
out pipe initialize logic.
Wed Mar 6 22:56:14 2013 KOSAKI Motohiro <kosaki.motohiro@gmail.com> Wed Mar 6 22:56:14 2013 KOSAKI Motohiro <kosaki.motohiro@gmail.com>
* thread_pthread.c (ubf_select): add to small comments why we * thread_pthread.c (ubf_select): add to small comments why we

View file

@ -1155,10 +1155,9 @@ static int check_signal_thread_list(void) { return 0; }
static int timer_thread_pipe[2] = {-1, -1}; static int timer_thread_pipe[2] = {-1, -1};
static int timer_thread_pipe_owner_process; static int timer_thread_pipe_owner_process;
/* only use signal-safe system calls here */ /* only use signal-safe system calls here */
void static void
rb_thread_wakeup_timer_thread(void) rb_thread_wakeup_timer_thread_fd(int fd)
{ {
ssize_t result; ssize_t result;
@ -1166,7 +1165,7 @@ rb_thread_wakeup_timer_thread(void)
if (timer_thread_pipe_owner_process == getpid()) { if (timer_thread_pipe_owner_process == getpid()) {
const char *buff = "!"; const char *buff = "!";
retry: retry:
if ((result = write(timer_thread_pipe[1], buff, 1)) <= 0) { if ((result = write(fd, buff, 1)) <= 0) {
switch (errno) { switch (errno) {
case EINTR: goto retry; case EINTR: goto retry;
case EAGAIN: case EAGAIN:
@ -1185,9 +1184,15 @@ rb_thread_wakeup_timer_thread(void)
} }
} }
void
rb_thread_wakeup_timer_thread(void)
{
rb_thread_wakeup_timer_thread_fd(timer_thread_pipe[1]);
}
/* VM-dependent API is not available for this function */ /* VM-dependent API is not available for this function */
static void static void
consume_communication_pipe(void) consume_communication_pipe(int fd)
{ {
#define CCP_READ_BUFF_SIZE 1024 #define CCP_READ_BUFF_SIZE 1024
/* buffer can be shared because no one refers to them. */ /* buffer can be shared because no one refers to them. */
@ -1195,7 +1200,7 @@ consume_communication_pipe(void)
ssize_t result; ssize_t result;
while (1) { while (1) {
result = read(timer_thread_pipe[0], buff, sizeof(buff)); result = read(fd, buff, sizeof(buff));
if (result == 0) { if (result == 0) {
return; return;
} }
@ -1213,15 +1218,15 @@ consume_communication_pipe(void)
} }
static void static void
close_communication_pipe(void) close_communication_pipe(int pipes[2])
{ {
if (close(timer_thread_pipe[0]) < 0) { if (close(pipes[0]) < 0) {
rb_bug_errno("native_stop_timer_thread - close(ttp[0])", errno); rb_bug_errno("native_stop_timer_thread - close(ttp[0])", errno);
} }
if (close(timer_thread_pipe[1]) < 0) { if (close(pipes[1]) < 0) {
rb_bug_errno("native_stop_timer_thread - close(ttp[1])", errno); rb_bug_errno("native_stop_timer_thread - close(ttp[1])", errno);
} }
timer_thread_pipe[0] = timer_thread_pipe[1] = -1; pipes[0] = pipes[1] = -1;
} }
#if USE_SLEEPY_TIMER_THREAD #if USE_SLEEPY_TIMER_THREAD
@ -1241,35 +1246,44 @@ set_nonblock(int fd)
} }
#endif #endif
#if USE_SLEEPY_TIMER_THREAD
static void
setup_communication_pipe_internal(int pipes[2])
{
int err;
if (pipes[0] != -1) {
/* close pipe of parent process */
close_communication_pipe(pipes);
}
err = rb_cloexec_pipe(pipes);
if (err != 0) {
rb_bug_errno("setup_communication_pipe: Failed to create communication pipe for timer thread", errno);
}
rb_update_max_fd(pipes[0]);
rb_update_max_fd(pipes[1]);
set_nonblock(pipes[0]);
set_nonblock(pipes[1]);
}
#endif /* USE_SLEEPY_TIMER_THREAD */
/* communication pipe with timer thread and signal handler */
static void static void
setup_communication_pipe(void) setup_communication_pipe(void)
{ {
#if USE_SLEEPY_TIMER_THREAD #if USE_SLEEPY_TIMER_THREAD
int err; if (timer_thread_pipe_owner_process == getpid()) {
/* already set up. */
/* communication pipe with timer thread and signal handler */ return;
if (timer_thread_pipe_owner_process != getpid()) {
if (timer_thread_pipe[0] != -1) {
/* close pipe of parent process */
close_communication_pipe();
}
err = rb_cloexec_pipe(timer_thread_pipe);
if (err != 0) {
rb_bug_errno("setup_communication_pipe: Failed to create communication pipe for timer thread", errno);
}
rb_update_max_fd(timer_thread_pipe[0]);
rb_update_max_fd(timer_thread_pipe[1]);
set_nonblock(timer_thread_pipe[0]);
set_nonblock(timer_thread_pipe[1]);
/* validate pipe on this process */
timer_thread_pipe_owner_process = getpid();
} }
setup_communication_pipe_internal(timer_thread_pipe);
/* validate pipe on this process */
timer_thread_pipe_owner_process = getpid();
#endif /* USE_SLEEPY_TIMER_THREAD */ #endif /* USE_SLEEPY_TIMER_THREAD */
} }
/** /**
* Let the timer thread sleep a while. * Let the timer thread sleep a while.
* *
@ -1301,7 +1315,7 @@ timer_thread_sleep(rb_global_vm_lock_t* gvl)
/* maybe timeout */ /* maybe timeout */
} }
else if (result > 0) { else if (result > 0) {
consume_communication_pipe(); consume_communication_pipe(timer_thread_pipe[0]);
} }
else { /* result < 0 */ else { /* result < 0 */
switch (errno) { switch (errno) {