Menu

[r24]: / trunk / uploader / libctb / src / linux / timer.cpp  Maximize  Restore  History

Download this file

98 lines (86 with data), 2.7 kB

 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
/////////////////////////////////////////////////////////////////////////////
// Name: linux/timer.cpp
// Purpose:
// Author: Joachim Buermann
// Id: $Id: timer.cpp,v 1.1.1.1 2004/11/24 10:30:11 jb Exp $
// Copyright: (c) 2001 Joachim Buermann
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#include "ctb-0.16/timer.h"
#include <unistd.h>
namespace ctb {
// a dummy function, see below
static void timer_exit(void* arg)
{
};
static void* timer_fnc(void* arg)
{
// the timer thread should be canceled every time
// (asyncronously)
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
// this is tricky, but absolutly necessarily to avoid segfaults
// if the destructor finished a running thread
pthread_cleanup_push(timer_exit,NULL);
timer_control *tc = (timer_control*)arg;
// linux allows a real sleep, means the timer thread will
// be sleeping (no reduce of the system performance)
usleep(tc->usecs);
// time is over, system reawake the thread.
// if there is an exit function, calling it
if(tc->exitfnc) tc->exitfnc(NULL);
// set the exit flag
if(tc->exitflag) *tc->exitflag = 1;
// deallocate the system resources (thread)
pthread_cleanup_pop(1);
return NULL;
};
// the constructor initiate the internal control struct
Timer::Timer( unsigned int msecs, int* exitflag, void*( *exitfnc )(void*) )
{
control.usecs = msecs * 1000;
control.exitflag = exitflag;
control.exitfnc = exitfnc;
stopped = 1;
};
// if a timer instance leave it's valid range, it automaticaly will
// be finished
Timer::~Timer()
{
if(!stopped) {
// only a running thread may be canceled
stop();
}
};
// starts the timer thread
int Timer::start()
{
stopped = 0;
if(pthread_create(&tid, // result parameter, covers the id
// of the new threads
NULL, // thread attribute object, NULL means
// the defaults
timer_fnc, // start function of the thread
&control // start function parameter, must refer
// as void*
) == -1) {
return -1; // there was something going wrong
}
pthread_detach(tid); // thread status must be "detach". In the other
// case, the destructor call of a running
// thread throws a segfault
return 0;
};
// stop the timer thread
int Timer::stop()
{
if(control.exitflag && (*control.exitflag == 0)) {
pthread_cancel(tid);
}
stopped = 1;
return 0;
};
void sleepms(unsigned int ms)
{
usleep(ms * 1000);
};
} // namespace ctb
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.