File: timers.cpp

package info (click to toggle)
openhpi 3.6.1-2.2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 25,544 kB
  • ctags: 25,275
  • sloc: ansic: 202,885; cpp: 63,639; java: 16,472; cs: 15,161; python: 11,884; makefile: 4,872; perl: 1,529; sh: 371; xml: 36; asm: 13
file content (185 lines) | stat: -rw-r--r-- 4,456 bytes parent folder | download
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
/* 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 "timers.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( g_cond_new() ),
      m_mutex( g_mutex_new() ),
      m_stop( false )
{
}

cTimers::~cTimers()
{
    if ( m_thread ) {
        m_stop = true;
        g_mutex_lock( m_mutex );
        g_cond_signal( m_cond );
        g_mutex_unlock( m_mutex );
        g_thread_join( m_thread );
    }
    g_mutex_free( m_mutex );
    g_cond_free( m_cond );
}

bool cTimers::Start()
{
    m_thread = g_thread_create( 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;
    }

    glong sec  = timeout / 1000000000LL; // TODO possible overflow
    glong usec = ( timeout % 1000000000LL ) / 1000LL;

    Timer timer;
    timer.callback = callback;
    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;
    }

    g_mutex_lock( m_mutex );
    m_timers.push_back( timer );
    g_cond_signal( m_cond );
    g_mutex_unlock( m_mutex );
}

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

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

    g_cond_signal( m_cond );
    g_mutex_unlock( m_mutex );
}

bool cTimers::HasTimerSet( const cTimerCallback * callback )
{
    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();

    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;
    }

    g_mutex_lock( m_mutex );

    GTimeVal now, next;

    while ( !m_stop ) {
        g_get_current_time( &next );
        g_time_val_add( &next, 1800000000L /* + 30 min from now */ );
        Timers rest;
        while ( ( !m_stop ) && ( !m_timers.empty() ) ) {
            Timer t = m_timers.front();
            m_timers.pop_front();
            g_get_current_time( &now );
            if ( now < t.expire ) {
                rest.push_back( t );
                if ( t.expire < next ) {
                    next = t.expire;
                }
            } else {
                g_mutex_unlock( m_mutex );
                // TODO Callback can be deleted there
                t.callback->TimerEvent();
                g_mutex_lock( m_mutex );
            }
        }
        if ( m_stop ) {
            break;
        }
        m_timers.swap( rest );
        g_cond_timed_wait( m_cond, m_mutex, &next );
    }

    g_mutex_unlock( m_mutex );
}


}; // namespace TA