File: timers.cpp

package info (click to toggle)
openhpi 3.8.0-2.3
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 31,888 kB
  • sloc: ansic: 225,326; cpp: 63,687; java: 16,472; cs: 15,161; python: 11,884; sh: 11,508; makefile: 4,945; perl: 1,529; xml: 36; asm: 13
file content (231 lines) | stat: -rw-r--r-- 6,215 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
106
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
151
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTUTILSLITY or FITNESS FOR A PARTICULAR PURPOSE.  This
 * file and program are licensed under a BSD style license.  See
 * the Copying file included with the OpenHPI distribution for
 * full licensing terms.
 *
 * Author(s):
 *        Anton Pak <anton.pak@pigeonpoint.com>
 */

#include <algorithm>

// #include <oh_error.h>
#include "timers.h"

#include "sahpi_wrappers.h"

namespace TA {


/**************************************************************
 * Helper functions
 *************************************************************/
struct CallbackPred
{
    bool operator ()( const Timer& item ) const
    {
        return item.callback == callback;
    }

    const cTimerCallback * callback;
};

/**********************************************************
 * NB: comparison relies on the fact
 * that x1.tv_usec and x2.tv_usec both < 1000000.
 * I.e. GTimeVal data is valid.
 *********************************************************/
bool operator <( const GTimeVal& x1, const GTimeVal& x2 )
{
    if ( x1.tv_sec != x2.tv_sec ) {
        return x1.tv_sec < x2.tv_sec;
    } else {
        return x1.tv_usec < x2.tv_usec;
    }
}


/**************************************************************
 * class cTimers
 *************************************************************/
cTimers::cTimers()
    : m_thread( 0 ),
      m_cond( wrap_g_cond_new_init() ),
      m_mutex( wrap_g_mutex_new_init() ),
      m_stop( false )
{
}

cTimers::~cTimers()
{
    if ( m_thread ) {
        m_stop = true;
        wrap_g_mutex_lock( m_mutex );
        g_cond_signal( m_cond );
        wrap_g_mutex_unlock( m_mutex );
        g_thread_join( m_thread );
    }
    wrap_g_mutex_free_clear( m_mutex );
    wrap_g_cond_free( m_cond );
}

bool cTimers::Start()
{
    m_thread = wrap_g_thread_create_new( "Start", ThreadFuncAdapter, this, TRUE, 0 );
//g_usleep( 1000 );

    return ( m_thread != 0 );
}

void cTimers::SetTimer( cTimerCallback * callback, SaHpiTimeoutT timeout )
{
    if ( timeout == SAHPI_TIMEOUT_IMMEDIATE ) {
        callback->TimerEvent();
        return;
    } else if ( timeout == SAHPI_TIMEOUT_BLOCK ) {
        return;
    }

    #if GLIB_CHECK_VERSION (2, 32, 0)
        gint64 usec = timeout / 1000LL; // Just convert to microseconds
	// CRIT("GLIB > 2.32 and timeout in usec is %li\n", usec);
    #else 
        glong sec  = timeout / 1000000000LL; // TODO possible overflow
        glong usec = ( timeout % 1000000000LL ) / 1000LL;
	// CRIT("GLIB < 2.32 and timeout is %ld secs and %ld usecs\n", sec, usec);
    #endif

    Timer timer;
    timer.callback = callback;
    #if GLIB_CHECK_VERSION (2, 32, 0)
    timer.expire = g_get_monotonic_time();
    timer.expire += usec;
    #else
    g_get_current_time( &timer.expire );
    timer.expire.tv_sec  += sec;
    timer.expire.tv_usec += usec;
    if ( timer.expire.tv_usec > 1000000L ) {
        ++timer.expire.tv_sec;
        timer.expire.tv_usec -= 1000000L;
    }
    #endif

    wrap_g_mutex_lock( m_mutex );
    m_timers.push_back( timer );
    g_cond_signal( m_cond );
    wrap_g_mutex_unlock( m_mutex );
}

void cTimers::CancelTimer( const cTimerCallback * callback )
{
    wrap_g_mutex_lock( m_mutex );

    CallbackPred pred;
    pred.callback = callback;
    m_timers.remove_if( pred );

    g_cond_signal( m_cond );
    wrap_g_mutex_unlock( m_mutex );
}

bool cTimers::HasTimerSet( const cTimerCallback * callback )
{
    wrap_g_mutex_lock( m_mutex );

    CallbackPred pred;
    pred.callback = callback;
    Timers::const_iterator iter = std::find_if( m_timers.begin(), m_timers.end(), pred );
    bool has = iter != m_timers.end();

    wrap_g_mutex_unlock( m_mutex );

    return has;
}


gpointer cTimers::ThreadFuncAdapter( gpointer data )
{
    cTimers * me = reinterpret_cast<cTimers *>(data);
    me->ThreadFunc();

    return 0;
}

void cTimers::ThreadFunc()
{
    if ( m_stop ) {
        return;
    }

    wrap_g_mutex_lock( m_mutex );

    #if GLIB_CHECK_VERSION (2, 32, 0)
    gint64 now, next;
    #else
    GTimeVal now, next;
    #endif

    while ( !m_stop ) {
        Timers rest;
        #if GLIB_CHECK_VERSION (2, 32, 0)
        next = g_get_monotonic_time();
        next = next + (1800000000L /* + 30 min from now */ );
        while ( ( !m_stop ) && ( !m_timers.empty() ) ) {
            Timer t = m_timers.front();
            m_timers.pop_front();
            now = g_get_monotonic_time();
            // CRIT("GLIB>2.32 now=%li front=%li\n",now, t.expire);
            if ( now < t.expire ) {
                rest.push_back( t );
                if ( t.expire < next ) {
                    next = t.expire;
                }
            } else {
                wrap_g_mutex_unlock( m_mutex );
                // TODO Callback can be deleted there
                t.callback->TimerEvent();
                wrap_g_mutex_lock( m_mutex );
            }
        }        
        #else
        g_get_current_time( &next );
        g_time_val_add( &next, 1800000000L /* + 30 min from now */ );

        while ( ( !m_stop ) && ( !m_timers.empty() ) ) {
            Timer t = m_timers.front();
            m_timers.pop_front();
            g_get_current_time( &now );
            // CRIT("GLIB<2.32 now %ld secs, %ld usecs t %ld secs %ld usecs\n",
	    //	now.tv_sec, no.tv_usec, t.expire.tv_sec, t.expire.tv_usec);
            if ( now < t.expire ) {
                rest.push_back( t );
                if ( t.expire < next ) {
                    next = t.expire;
                }
            } else {
                wrap_g_mutex_unlock( m_mutex );
                // TODO Callback can be deleted there
                t.callback->TimerEvent();
                wrap_g_mutex_lock( m_mutex );
            }
        }
        #endif
        if ( m_stop ) {
            break;
        }
        m_timers.swap( rest );
        #if GLIB_CHECK_VERSION (2, 32, 0)
        wrap_g_cond_timed_wait( m_cond, m_mutex, next );
        #else
        wrap_g_cond_timed_wait( m_cond, m_mutex, &next );
        #endif
    }

    wrap_g_mutex_unlock( m_mutex );
}


}; // namespace TA