summaryrefslogtreecommitdiff
path: root/thread.c
diff options
context:
space:
mode:
authornormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-12-17 08:26:12 +0000
committernormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-12-17 08:26:12 +0000
commit78cc1491d5dd106af94edd91b3ca4698423daaf7 (patch)
treeb47fbe75d07c70ae566f7f3987de9a1637f2bb20 /thread.c
parent1389cdaba1d2bcb8ce9f30f9d3128767b8032090 (diff)
thread.c: hoist out timeval arithmetic functions
timeval arithmetic may be reused in other places and this makes sleep_timeval easier-to-read. * thread.c (timeval_add): hoist out of sleep_timeval (timeval_update_expire): ditto (sleep_timeval): use new functions git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61301 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'thread.c')
-rw-r--r--thread.c66
1 files changed, 41 insertions, 25 deletions
diff --git a/thread.c b/thread.c
index cc62ea3905..d896a1b04b 100644
--- a/thread.c
+++ b/thread.c
@@ -1135,41 +1135,57 @@ getclockofday(struct timeval *tp)
}
static void
+timeval_add(struct timeval *dst, const struct timeval *tv)
+{
+ if (TIMEVAL_SEC_MAX - tv->tv_sec < dst->tv_sec)
+ dst->tv_sec = TIMEVAL_SEC_MAX;
+ else
+ dst->tv_sec += tv->tv_sec;
+ if ((dst->tv_usec += tv->tv_usec) >= 1000000) {
+ if (dst->tv_sec == TIMEVAL_SEC_MAX)
+ dst->tv_usec = 999999;
+ else {
+ dst->tv_sec++;
+ dst->tv_usec -= 1000000;
+ }
+ }
+}
+
+static int
+timeval_update_expire(struct timeval *tv, const struct timeval *to)
+{
+ struct timeval tvn;
+
+ getclockofday(&tvn);
+ if (to->tv_sec < tvn.tv_sec) return 1;
+ if (to->tv_sec == tvn.tv_sec && to->tv_usec <= tvn.tv_usec) return 1;
+ thread_debug("timeval_update_expire: "
+ "%"PRI_TIMET_PREFIX"d.%.6ld > %"PRI_TIMET_PREFIX"d.%.6ld\n",
+ (time_t)to->tv_sec, (long)to->tv_usec,
+ (time_t)tvn.tv_sec, (long)tvn.tv_usec);
+ tv->tv_sec = to->tv_sec - tvn.tv_sec;
+ if ((tv->tv_usec = to->tv_usec - tvn.tv_usec) < 0) {
+ --tv->tv_sec;
+ tv->tv_usec += 1000000;
+ }
+ return 0;
+}
+
+static void
sleep_timeval(rb_thread_t *th, struct timeval tv, int spurious_check)
{
- struct timeval to, tvn;
+ struct timeval to;
enum rb_thread_status prev_status = th->status;
getclockofday(&to);
- if (TIMEVAL_SEC_MAX - tv.tv_sec < to.tv_sec)
- to.tv_sec = TIMEVAL_SEC_MAX;
- else
- to.tv_sec += tv.tv_sec;
- if ((to.tv_usec += tv.tv_usec) >= 1000000) {
- if (to.tv_sec == TIMEVAL_SEC_MAX)
- to.tv_usec = 999999;
- else {
- to.tv_sec++;
- to.tv_usec -= 1000000;
- }
- }
-
+ timeval_add(&to, &tv);
th->status = THREAD_STOPPED;
RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
while (th->status == THREAD_STOPPED) {
native_sleep(th, &tv);
RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
- getclockofday(&tvn);
- if (to.tv_sec < tvn.tv_sec) break;
- if (to.tv_sec == tvn.tv_sec && to.tv_usec <= tvn.tv_usec) break;
- thread_debug("sleep_timeval: %"PRI_TIMET_PREFIX"d.%.6ld > %"PRI_TIMET_PREFIX"d.%.6ld\n",
- (time_t)to.tv_sec, (long)to.tv_usec,
- (time_t)tvn.tv_sec, (long)tvn.tv_usec);
- tv.tv_sec = to.tv_sec - tvn.tv_sec;
- if ((tv.tv_usec = to.tv_usec - tvn.tv_usec) < 0) {
- --tv.tv_sec;
- tv.tv_usec += 1000000;
- }
+ if (timeval_update_expire(&tv, &to))
+ break;
if (!spurious_check)
break;
}