• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nasa / trick / 27378623444

11 Jun 2026 09:29PM UTC coverage: 56.138% (+0.01%) from 56.126%
27378623444

Pull #2146

github

web-flow
Merge a004cc2c2 into 0fbae67ec
Pull Request #2146: Make itimer wake up earlier when necessary.

0 of 1 new or added line in 1 file covered. (0.0%)

1 existing line in 1 file now uncovered.

14684 of 26157 relevant lines covered (56.14%)

452542.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

43.08
/trick_source/sim_services/Timer/ITimer.cpp
1
/*
2
PURPOSE:
3
    ( gettimeofday clock )
4
PROGRAMMERS:
5
    (((Robert W. Bailey) (LinCom) (April 1992) (--) (Realtime.))
6
     ((Robert W. Bailey) (LinCom) (7/22/92) (--) (--))
7
     ((Eddie J. Paddock) (MDSSC) (Oct 1992) (--) (Realtime.)))
8
     ((Alex Lin) (NASA) (April 2009) (--) (c++ port)))
9
*/
10

11
#include <iostream>
12
#include <sstream>
13

14
#include <fcntl.h>
15
#include <stdio.h>
16
#include <string.h>
17
#include <errno.h>
18
#include <math.h>
19
#include <signal.h>
20
#include <sys/types.h>
21
#include <unistd.h>
22

23
#include "trick/ITimer.hh"
24
#include "trick/exec_proto.h"
25
#include "trick/message_proto.h"
26

27
Trick::ITimer * the_itimer ;
28

29
Trick::ITimer::ITimer() : Timer() {
187✔
30

31
    the_itimer = this ;
187✔
32

33
#if ! ( __APPLE__ | __INTERIX )
34
    clock_getres(CLOCK_REALTIME, &res);
187✔
35
#else
36
    res.tv_nsec = 10000000;
37
    res.tv_sec = 0;
38
#endif
39

40
    return ;
187✔
41
}
42

43
Trick::ITimer::~ITimer() {
187✔
44
    return ;
187✔
45
}
187✔
46

47
void Trick::ITimer::semaphore_post() {
×
48
    sem_post(semaphore) ;
×
49
}
×
50

51
/**
52
@details
53
-# Set the SIGALRM signal handler to the ITimer signal handler routine.
54
-# Open a unique named semaphore
55
*/
56
int Trick::ITimer::init() {
2✔
57

58
    static struct sigaction sigact;
59
    std::stringstream sem_name_stream ;
2✔
60

61
    sigact.sa_handler = (void (*)(int)) it_handler;
2✔
62
    if (sigaction(SIGALRM, &sigact, NULL) < 0) {
2✔
63
        perror("sigaction() failed for SIGALRM");
×
64
    }
65

66
    // Initialize the itimer semaphore used by SIGALRM handler to
67
    // wake up the sim_exec_loop from its underrun sleepy time.
68

69
    sem_name_stream << "itimersepmaphore_" << getpid() ;
2✔
70
    sem_name = sem_name_stream.str() ;
2✔
71

72
    semaphore = sem_open(sem_name.c_str(), O_CREAT, S_IRWXU , 0);
2✔
73

74
    return (0) ;
2✔
75
}
2✔
76

77
/**
78
@details
79
-# If the timer is enabled
80
   -# If the frame time is valid
81
      -# Set the system itimer to expire one time at the desired frame period.
82
   -# Else termiate the simulation with the error message stating that the
83
      itimer frame period is invalid.
84
*/
85
int Trick::ITimer::start(double in_frame_time) {
51✔
86

87
    struct itimerval ivalue;
88
    int ret ;
89
    unsigned long long frame_sec ;
90
    unsigned int frame_usec ;
91

92
    if ( enabled ) {
51✔
93

94
        // clear out any built up signals.
95
        while ( (ret = sem_trywait(semaphore)) == 0 ) ;
×
96

97
        frame_sec = ( unsigned long long )in_frame_time ;
×
98
        frame_usec = (unsigned int)((in_frame_time - frame_sec) * 1000000) ;
×
99

100
        // check minimum time > 10ms
101
        if ( frame_sec == 0 && frame_usec < 10000 ) {
×
102
            active = false ;
×
103
            return(0) ;
×
104
        }
105
        // Arm the itimer slightly early so clock_spin() can busy-wait to the
106
        // frame boundary.  On macOS, timer coalescing can delay SIGALRM delivery
107
        // by 3-15ms, so scale the value to 5% of the frame (with a range of 2-8ms).
108
        // On Linux a fixed 2ms is sufficient.
109
#if __APPLE__
110
        {
111
            // Frames < 10ms are already rejected above, so frame_usec >= 10000 thus >= 8000
112
            // When frame_sec == 0, meaning the borrow path only triggers for frames >= 1s.
113
            unsigned int scaled_usec = (unsigned int)(in_frame_time * 1000000) / 20; // 5% of frame
114
            if (scaled_usec < 2000)
115
                scaled_usec = 2000;
116
            if (scaled_usec > 8000)
117
                scaled_usec = 8000;
118
            if (frame_usec < scaled_usec)
119
            {
120
                frame_sec  -= 1;
121
                frame_usec += 1000000;
122
            }
123
            frame_usec -= scaled_usec;
124
        }
125
#else
NEW
126
        if (frame_usec < 2000 && frame_sec > 0)
×
127
        {
128
            frame_sec  -= 1 ;
×
129
            frame_usec += 1000000 ;
×
130
        }
131
        frame_usec -= 2000 ;
×
132
#endif
133

134
        if ( in_frame_time > 0 ) {
×
135
            /* Set timer interval in micro-seconds */
136
            ivalue.it_interval.tv_sec = 0;
×
137
            ivalue.it_interval.tv_usec = 0;
×
138
            ivalue.it_value.tv_sec = frame_sec;
×
139
            ivalue.it_value.tv_usec = (long) frame_usec;
×
140

141
            setitimer(ITIMER_REAL, &ivalue, NULL);
×
142
            active = true ;
×
143
        } else {
144
            char error_message[256];
145
            snprintf(error_message, sizeof(error_message), "itimer frame_time is not set\n" ) ;
×
146
            exec_terminate_with_return(-1, __FILE__, __LINE__ , error_message);
×
147
        }
148
    }
149
    return (0) ;
51✔
150

151
}
152

153
/*
154
@details
155
-# Call the start function
156
*/
157
int Trick::ITimer::reset(double in_frame_time) {
50✔
158
    start(in_frame_time) ;
50✔
159
    return (0) ;
50✔
160
}
161

162
/**
163
@details
164
-# Set the system itimer with the expiriation time of 0.
165
*/
166
int Trick::ITimer::stop() {
×
167

168
    static struct itimerval ivalue, ovalue;
169

170
    /* Turn off itimer */
171
    ivalue.it_interval.tv_usec = 0;
×
172
    ivalue.it_interval.tv_sec = 0;
×
173

174
    ivalue.it_value.tv_usec = 0;
×
175
    ivalue.it_value.tv_sec = 0;
×
176

177
    (void) setitimer(ITIMER_REAL, &ivalue, &ovalue);
×
178

179
    active = false ;
×
180

181
    return (0) ;
×
182

183
}
184

185
/**
186
@details
187
-# If the timer is enabled
188
   -# Call sem_wait to decrement the semaphore.  sem_wait will sleep and wait
189
      for the semaphore to be posted from the signal handler.
190
   -# If sem_wait returns an error, write an error message
191
*/
192
int Trick::ITimer::pause() {
50✔
193

194
    int sem_ret;
195

196
    if ( enabled and active ) {
50✔
197

198
        do {
199
            sem_ret = sem_wait(semaphore);
×
200
            //TODO: I'd like to use sem_timedwait
201
            //sem_ret = sem_timedwait(semaphore, &timeout);
202
        } while ((sem_ret < 0 && errno == EINTR) && (errno != EDEADLK));
×
203

204
        if (sem_ret != 0) {
×
205
            // Prepend info to the error message string (given by errno)
206
            perror( "ITimer call to sem_wait()" );
×
207
        }
208

209
    }
210

211
    return (0) ;
50✔
212

213
}
214

215
/**
216
@details
217
-# Unlink the semaphore
218
*/
219
int Trick::ITimer::shutdown() {
1✔
220
    sem_unlink(sem_name.c_str()) ;
1✔
221
    return(0) ;
1✔
222
}
223

224
Trick::ITimer * get_itimer() {
×
225
    return (the_itimer) ;
×
226
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc