/usr/include/dlib/timer/timer.h is in libdlib-dev 18.18-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | // Copyright (C) 2007 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_TIMEr_Hh_
#define DLIB_TIMEr_Hh_
#include "../threads.h"
#include "../algs.h"
#include "../misc_api.h"
#include "timer_abstract.h"
#include "../uintn.h"
#include "../binary_search_tree.h"
#include "../smart_pointers_thread_safe.h"
#include "timer_heavy.h"
namespace dlib
{
struct timer_base : public threaded_object
{
/*!
WHAT THIS OBJECT REPRESENTS
This object contains the base members of the timer object.
It exists so that we can access them from outside any templated functions.
!*/
unsigned long delay;
// these are only modified by the global_clock
uint64 next_time_to_run;
timestamper ts;
bool running;
bool in_global_clock;
};
// ----------------------------------------------------------------------------------------
class timer_global_clock : private threaded_object
{
/*!
This object sets up a timer that triggers the action function
for timer objects that are tracked inside this object.
INITIAL VALUE
- shutdown == false
- running == false
CONVENTION
- if (shutdown) then
- thread() should terminate
- else (running) then
- thread() is running
- tm[time] == pointer to a timer_base object
!*/
typedef binary_search_tree<uint64,timer_base*,memory_manager<char>::kernel_2b>::kernel_2a_c time_map;
public:
~timer_global_clock();
void add (
timer_base* r
);
/*!
requires
- m is locked
ensures
- starts the thread if it isn't already started
- adds r to tm
- #r->in_global_clock == true
- updates r->next_time_to_run appropriately according to
r->delay
!*/
void remove (
timer_base* r
);
/*!
requires
- m is locked
ensures
- if (r is in tm) then
- removes r from tm
- #r->in_global_clock == false
!*/
void adjust_delay (
timer_base* r,
unsigned long new_delay
);
/*!
requires
- m is locked
ensures
- #r->delay == new_delay
- if (r->in_global_clock) then
- the time to the next event will have been appropriately adjusted
!*/
mutex m;
friend shared_ptr_thread_safe<timer_global_clock> get_global_clock();
private:
timer_global_clock();
time_map tm;
signaler s;
bool shutdown;
bool running;
timestamper ts;
void thread();
/*!
ensures
- spawns timer tasks as is appropriate
!*/
};
shared_ptr_thread_safe<timer_global_clock> get_global_clock();
/*!
ensures
- returns the global instance of the timer_global_clock object
!*/
// ----------------------------------------------------------------------------------------
template <
typename T
>
class timer : private timer_base
{
/*!
INITIAL VALUE
- running == false
- delay == 1000
- ao == a pointer to the action_object()
- af == a pointer to the action_function()
- in_global_clock == false
- next_time_to_run == 0
- gc == get_global_clock()
CONVENTION
- the mutex used to lock everything is gc->m
- running == is_running()
- delay == delay_time()
- *ao == action_object()
- af == action_function()
- if (!running) then
- in_global_clock == false
- else
- next_time_to_run == the next time this timer should run according
to the timestamper in the global_clock
!*/
public:
// These typedefs are here for backwards compatibility with previous versions of
// dlib.
typedef timer_heavy<T> kernel_1a;
typedef timer kernel_2a;
typedef void (T::*af_type)();
timer(
T& ao_,
af_type af_
);
virtual ~timer(
);
void clear(
);
af_type action_function (
) const;
const T& action_object (
) const;
T& action_object (
);
bool is_running (
) const;
unsigned long delay_time (
) const;
void set_delay_time (
unsigned long milliseconds
);
void start (
);
void stop (
);
void stop_and_wait (
);
private:
void thread (
);
/*!
ensures
- calls the action function
!*/
// data members
T& ao;
const af_type af;
shared_ptr_thread_safe<timer_global_clock> gc;
// restricted functions
timer(const timer&); // copy constructor
timer& operator=(const timer&); // assignment operator
};
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// member function definitions
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <
typename T
>
timer<T>::
timer(
T& ao_,
af_type af_
) :
ao(ao_),
af(af_),
gc(get_global_clock())
{
delay = 1000;
next_time_to_run = 0;
running = false;
in_global_clock = false;
}
// ----------------------------------------------------------------------------------------
template <
typename T
>
timer<T>::
~timer(
)
{
clear();
wait();
}
// ----------------------------------------------------------------------------------------
template <
typename T
>
void timer<T>::
clear(
)
{
auto_mutex M(gc->m);
running = false;
gc->remove(this);
delay = 1000;
next_time_to_run = 0;
}
// ----------------------------------------------------------------------------------------
template <
typename T
>
typename timer<T>::af_type timer<T>::
action_function (
) const
{
return af;
}
// ----------------------------------------------------------------------------------------
template <
typename T
>
const T& timer<T>::
action_object (
) const
{
return ao;
}
// ----------------------------------------------------------------------------------------
template <
typename T
>
T& timer<T>::
action_object (
)
{
return ao;
}
// ----------------------------------------------------------------------------------------
template <
typename T
>
bool timer<T>::
is_running (
) const
{
auto_mutex M(gc->m);
return running;
}
// ----------------------------------------------------------------------------------------
template <
typename T
>
unsigned long timer<T>::
delay_time (
) const
{
auto_mutex M(gc->m);
return delay;
}
// ----------------------------------------------------------------------------------------
template <
typename T
>
void timer<T>::
set_delay_time (
unsigned long milliseconds
)
{
auto_mutex M(gc->m);
gc->adjust_delay(this,milliseconds);
}
// ----------------------------------------------------------------------------------------
template <
typename T
>
void timer<T>::
start (
)
{
auto_mutex M(gc->m);
if (!running)
{
gc->add(this);
running = true;
}
}
// ----------------------------------------------------------------------------------------
template <
typename T
>
void timer<T>::
stop (
)
{
gc->m.lock();
running = false;
gc->remove(this);
gc->m.unlock();
}
// ----------------------------------------------------------------------------------------
template <
typename T
>
void timer<T>::
thread (
)
{
// call the action function
(ao.*af)();
auto_mutex M(gc->m);
if (running)
{
gc->remove(this);
gc->add(this);
}
}
// ----------------------------------------------------------------------------------------
template <
typename T
>
void timer<T>::
stop_and_wait (
)
{
gc->m.lock();
running = false;
gc->remove(this);
gc->m.unlock();
wait();
}
// ----------------------------------------------------------------------------------------
}
#ifdef NO_MAKEFILE
#include "timer.cpp"
#endif
#endif // DLIB_TIMEr_Hh_
|