Module: SystemTimer

Defined in:
lib/system_timer.rb,
lib/system_timer_stub.rb,
lib/system_timer/thread_timer.rb,
lib/system_timer/concurrent_timer_pool.rb,
ext/system_timer/system_timer_native.c,
ext/system_timer/system_timer_native.c

Overview

Copyright 2008 David Vollbracht & Philippe Hanrigou

Defined Under Namespace

Classes: ConcurrentTimerPool, ThreadTimer

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.timer_poolObject (readonly)

Returns the value of attribute timer_pool.



43
44
45
# File 'lib/system_timer.rb', line 43

def timer_pool
  @timer_pool
end

Class Method Details

.debug_enabled?Boolean

Returns:

  • (Boolean)


224
225
226
# File 'ext/system_timer/system_timer_native.c', line 224

static VALUE debug_enabled_p(VALUE self) {
    return debug_enabled ? Qtrue : Qfalse;
}

.disable_debugObject



233
234
235
236
# File 'ext/system_timer/system_timer_native.c', line 233

static VALUE disable_debug(VALUE self) {
    debug_enabled = 0;
    return Qnil;	
}

.enable_debugObject



228
229
230
231
# File 'ext/system_timer/system_timer_native.c', line 228

static VALUE enable_debug(VALUE self) {
    debug_enabled = 1;
    return Qnil;
}

.install_first_timer_and_save_original_configuration(seconds) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'ext/system_timer/system_timer_native.c', line 42

static VALUE install_first_timer_and_save_original_configuration(VALUE self, VALUE seconds)
{
    struct itimerval timer_interval;

    if (debug_enabled) {
        log_debug("[install_first_timer] %.2lfs\n", NUM2DBL(seconds));
    }

    /*
     * Block SIG_ALRM for safe processing of SIG_ALRM configuration and save mask.
     */
    if (0 != sigprocmask(SIG_BLOCK, &sigalarm_mask, &original_mask)) {
        log_error("[install_first_timer] Could not block SIG_ALRM\n", DISPLAY_ERRNO);
        return Qnil;
    }
    clear_pending_sigalrm_for_ruby_threads();
    log_debug("[install_first_timer] Successfully blocked SIG_ALRM at O.S. level\n");
	
   /*
    * Save previous signal handler.
    */
    log_debug("[install_first_timer] Saving original system handler\n");
    original_signal_handler.sa_handler = NULL;
    if (0 != sigaction(SIGALRM, NULL, &original_signal_handler)) {
        log_error("[install_first_timer] Could not save existing handler for SIG_ALRM\n", DISPLAY_ERRNO);
        restore_original_sigalrm_mask_when_blocked();
        return Qnil;
    }
    log_debug("[install_first_timer] Successfully saved existing SIG_ALRM handler\n");
    
	 /*
	  * Install Ruby Level SIG_ALRM handler
	  */
    install_ruby_sigalrm_handler(self);

    /*
     * Save original real time interval timer and aet new real time interval timer.     
     */	
    set_itimerval(&original_timer_interval, 0.0);
    set_itimerval_with_minimum_1s_interval(&timer_interval, seconds);
    if (0 != setitimer(ITIMER_REAL, &timer_interval, &original_timer_interval)) {
        log_error("[install_first_timer] Could not install our own timer, timeout will not work", DISPLAY_ERRNO);
        restore_original_ruby_sigalrm_handler(self);
        restore_original_sigalrm_mask_when_blocked();
        return Qnil;
    }
    if (debug_enabled) {
      log_debug("[install_first_timer] Successfully installed timer (%ds)\n", 
                timer_interval.it_value.tv_sec);
    }

    /*
     * Unblock SIG_ALRM
     */
    if (0 != sigprocmask(SIG_UNBLOCK, &sigalarm_mask, NULL)) {
        log_error("[install_first_timer] Could not unblock SIG_ALRM, timeout will not work", DISPLAY_ERRNO);
        restore_original_timer_interval();
        restore_original_ruby_sigalrm_handler(self);
        restore_original_sigalrm_mask_when_blocked();		
    }
    log_debug("[install_first_timer] Successfully unblocked SIG_ALRM.\n");

    return Qnil;
}

.install_next_timer(seconds) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'ext/system_timer/system_timer_native.c', line 107

static VALUE install_next_timer(VALUE self, VALUE seconds)
{
    struct itimerval timer_interval;
    sigset_t previous_sigalarm_mask;

    if (debug_enabled) {
        log_debug("[install_next_timer] %.2lfs\n", NUM2DBL(seconds));
    }

    /*
     * Block SIG_ALRM for safe processing of SIG_ALRM configuration and save mask.
     */
    if (0 != sigprocmask(SIG_BLOCK, &sigalarm_mask, &previous_sigalarm_mask)) {
        log_error("[install_next_timer] Could not block SIG_ALRM\n", DISPLAY_ERRNO);
        return Qnil;
    }
    clear_pending_sigalrm_for_ruby_threads();
    log_debug("[install_next_timer] Successfully blocked SIG_ALRM at O.S. level\n");
	
    /*
     * Set new real time interval timer.
     */	
    set_itimerval_with_minimum_1s_interval(&timer_interval, seconds);
    if (0 != setitimer(ITIMER_REAL, &timer_interval, NULL)) {
        log_error("[install_next_timer] Could not install our own timer, timeout will not work", DISPLAY_ERRNO);
        restore_sigalrm_mask(&previous_sigalarm_mask);
        return Qnil;
    }
    if (debug_enabled) {
      log_debug("[install_next_timer] Successfully installed timer (%ds + %dus)\n", 
                timer_interval.it_value.tv_sec, timer_interval.it_value.tv_usec);
    }

    /*
     * Unblock SIG_ALRM
     */
    if (0 != sigprocmask(SIG_UNBLOCK, &sigalarm_mask, NULL)) {
        log_error("[install_next_timer] Could not unblock SIG_ALRM, timeout will not work", DISPLAY_ERRNO);
        restore_sigalrm_mask(&previous_sigalarm_mask);
    }
    log_debug("[install_next_timer] Successfully unblocked SIG_ALRM.\n");

    return Qnil;
}

.restore_original_configurationObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'ext/system_timer/system_timer_native.c', line 152

static VALUE restore_original_configuration(VALUE self)
{
   /*
    * Block SIG_ALRM for safe processing of SIG_ALRM configuration.
    */
    if (0 != sigprocmask(SIG_BLOCK, &sigalarm_mask, NULL)) {
        log_error("restore_original_configuration: Could not block SIG_ALRM", errno);
    }
    clear_pending_sigalrm_for_ruby_threads();
    log_debug("[restore_original_configuration] Blocked SIG_ALRM\n");

   /*
    * Install Ruby Level SIG_ALRM handler
    */
    restore_original_ruby_sigalrm_handler(self);
	
    if (original_signal_handler.sa_handler == NULL) {
        log_error("[restore_original_configuration] Previous SIG_ALRM handler not initialized!", DO_NOT_DISPLAY_ERRNO);
    } else if (0 == sigaction(SIGALRM, &original_signal_handler, NULL)) {
        log_debug("[restore_original_configuration] Successfully restored previous handler for SIG_ALRM\n");
    } else {
        log_error("[restore_original_configuration] Could not restore previous handler for SIG_ALRM", DISPLAY_ERRNO);
    }
    original_signal_handler.sa_handler = NULL;
	
    restore_original_timer_interval();
    restore_original_sigalrm_mask_when_blocked();	
}

.timeout_after(seconds) ⇒ Object Also known as: timeout

Executes the method’s block. If the block execution terminates before seconds seconds has passed, it returns true. If not, it terminates the execution and raises a Timeout::Error.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/system_timer.rb', line 48

def timeout_after(seconds, exception_class = nil)
  new_timer = nil                                      # just for scope
  @monitor.synchronize do
    new_timer = timer_pool.add_timer seconds, exception_class
    timer_interval = timer_pool.next_trigger_interval_in_seconds
    debug "==== Install Timer ==== at #{Time.now.to_f}, next interval: #{timer_interval}"
    if timer_pool.first_timer?
      install_first_timer_and_save_original_configuration timer_interval
    else
      install_next_timer timer_interval
    end
  end
  return yield
ensure
  @monitor.synchronize do
    debug "==== Cleanup Timer ==== at #{Time.now.to_f}, #{new_timer} "
    timer_pool.cancel new_timer
    timer_pool.log_registered_timers if debug_enabled?
    next_interval = timer_pool.next_trigger_interval_in_seconds
    debug "Cleanup Timer : next interval #{next_interval.inspect} "
    if next_interval
      install_next_timer next_interval
    else
      restore_original_configuration
    end
  end
end