From 99094a9cf8474915e65b27525276861c9c99114a Mon Sep 17 00:00:00 2001 From: Suresh Sundriyal Date: Fri, 24 Apr 2015 00:37:57 -0700 Subject: [PATCH] [timer] Some refactoring --- src/timer.cc | 25 +++++++++---------------- src/timer.hh | 2 +- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/timer.cc b/src/timer.cc index f05614dc..7eb45731 100644 --- a/src/timer.cc +++ b/src/timer.cc @@ -10,22 +10,13 @@ timer::error::error(int err):e_err(err) { } timer::interrupt_timer::interrupt_timer(struct timeval t, sighandler_t_ sighandler=SIG_IGN) : new_handler(sighandler), - old_handler(NULL), armed(true) { - this->new_val = (struct itimerval){ - { 0, 0 }, - t - }; - this->old_val = (struct itimerval){ - { 0, 0 }, - { 0, 0 } - }; -} + old_handler(NULL), new_val((struct itimerval){{0,0},t}), + old_val(disable), armed(true) { } int timer::interrupt_timer::arm_timer() { - // Disable the interval timer before resetting the handler and rearming - // the previous interval timer or else we will have a race-condition - // where the timer might fire and the appropriate handler might not be - // set. + // Disable the interval timer before setting the handler and arming the + // interval timer or else we will have a race-condition where the timer + // might fire and the appropriate handler might not be set. if (setitimer(ITIMER_REAL, &disable, &this->old_val) != 0) { log_error("Unable to disable the timer: %s", strerror(errno)); @@ -36,7 +27,8 @@ int timer::interrupt_timer::arm_timer() { log_error("Unable to set the signal handler: %s", strerror(errno)); if (setitimer(ITIMER_REAL, &this->old_val, NULL) != 0) { - log_error("Unable to reset the interrupt timer: %s", strerror(errno)); + log_error("Unable to reset the interrupt timer: %s", + strerror(errno)); throw timer::error(errno); } return -1; @@ -44,7 +36,8 @@ int timer::interrupt_timer::arm_timer() { if (setitimer(ITIMER_REAL, &this->new_val, NULL) != 0) { if(signal(SIGALRM, this->old_handler) == SIG_ERR) { - log_error("Unable to reset the signal handler: %s", strerror(errno)); + log_error("Unable to reset the signal handler: %s", + strerror(errno)); throw timer::error(errno); } log_error("Unable to set the timer: %s", strerror(errno)); diff --git a/src/timer.hh b/src/timer.hh index 53e1ee41..d056c5a4 100644 --- a/src/timer.hh +++ b/src/timer.hh @@ -25,8 +25,8 @@ class interrupt_timer { int arm_timer(); ~interrupt_timer(); private: - struct itimerval new_val, old_val; sighandler_t_ new_handler, old_handler; + struct itimerval new_val, old_val; bool armed; }; }