2
* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License as
6
* published by the Free Software Foundation; version 2 of the
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
#ifndef _THREADED_TIMER_H_
21
#define _THREADED_TIMER_H_
28
// The callback type used for timer events. It gets the id of the task returned from add_task
29
// and must return a boolean value which tells us if the task should continue to run or
30
// immediately be stopped. For one-shot tasks the return value has no meaning.
31
typedef bool (*timer_function) (int, void*);
34
#pragma warning(disable:4251) // We don't want to DLL export TimerTask, and we don't need a warning for that.
39
gdouble next_time; // Precomputed target time when this task must be triggered again.
40
gdouble wait_time; // The time in seconds to wait until this task is executed again.
41
timer_function callback; // The callback to trigger when the timer fires.
42
bool stop; // Tells the scheduler to remove this task.
43
bool single_shot; // If true then this task will only run once.
44
void* user_data; // Data to be passed to the callback.
45
bool scheduled; // True if the task has been scheduled currently (it is waiting in the pool to get executed).
48
typedef std::list<TimerTask> TaskList;
50
// The unit type of the timer value given to ThreadedTimer::add_task.
57
* The threaded timer is supposed to run as a singleton and provide scheduled timer events. It takes orders when to trigger
58
* a timer event, depending on the given frequency (if it is a repeating timer) or delay (for one-shot timers).
59
* It forms the base for timed services like animations, server pings in the background etc.
61
class BASELIBRARY_PUBLIC_FUNC ThreadedTimer
64
static ThreadedTimer* get();
67
static int add_task(TimerUnit unit, double value, bool single_shot, timer_function callback_, void* user_data);
68
static void remove_task(int task_id);
70
GMutex* _timer_lock; // Synchronize access to the timer class.
71
GThreadPool* _pool; // A number of threads which trigger the callbacks (to make them independant of each other).
72
int _wait_time; // The time the timer thread has to wait until looking for new tasks to execute.
73
bool _terminate; // Set to true when shutting down the timer.
74
int _next_id; // A counter for task ids.
76
GThread *_thread; // This thread loops endlessly executing tasks as they come in.
79
ThreadedTimer(int base_frequency);
82
static gpointer start(gpointer data);
83
static gpointer pool_function(gpointer data, gpointer user_data);
85
void remove(int task_id);
88
#endif // _THREADED_TIMER_H_