~ubuntu-branches/ubuntu/quantal/mysql-workbench/quantal

« back to all changes in this revision

Viewing changes to library/base/base/threaded_timer.h

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2012-03-01 21:57:30 UTC
  • Revision ID: package-import@ubuntu.com-20120301215730-o7y8av8y38n162ro
Tags: upstream-5.2.38+dfsg
ImportĀ upstreamĀ versionĀ 5.2.38+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
 
3
 *
 
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
 
7
 * License.
 
8
 * 
 
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.
 
13
 * 
 
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
 
17
 * 02110-1301  USA
 
18
 */
 
19
 
 
20
#ifndef _THREADED_TIMER_H_
 
21
#define _THREADED_TIMER_H_
 
22
 
 
23
#include "common.h"
 
24
#include "glib.h"
 
25
 
 
26
#include <list>
 
27
 
 
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*);
 
32
 
 
33
#ifdef _WIN32
 
34
  #pragma warning(disable:4251) // We don't want to DLL export TimerTask, and we don't need a warning for that.
 
35
#endif
 
36
 
 
37
struct TimerTask {
 
38
  int task_id;
 
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).
 
46
};
 
47
 
 
48
typedef std::list<TimerTask> TaskList;
 
49
 
 
50
// The unit type of the timer value given to ThreadedTimer::add_task.
 
51
enum TimerUnit {
 
52
  TimerFrequency,
 
53
  TimerTimeSpan
 
54
};
 
55
 
 
56
/**
 
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.
 
60
 */
 
61
class BASELIBRARY_PUBLIC_FUNC ThreadedTimer
 
62
{
 
63
public:
 
64
  static ThreadedTimer* get();
 
65
  static void stop();
 
66
  
 
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);
 
69
private:
 
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.
 
75
  
 
76
  GThread *_thread;     // This thread loops endlessly executing tasks as they come in. 
 
77
  TaskList _tasks;
 
78
 
 
79
  ThreadedTimer(int base_frequency);
 
80
  ~ThreadedTimer();
 
81
 
 
82
  static gpointer start(gpointer data);
 
83
  static gpointer pool_function(gpointer data, gpointer user_data);
 
84
  void main_loop();
 
85
  void remove(int task_id);
 
86
};
 
87
 
 
88
#endif // _THREADED_TIMER_H_
 
89