Commit f6ad04c8 authored by Davis King's avatar Davis King

Converted the timer component into two single implementation components.

These are the timer and timer_heavy objects now.
parent 52dae74c
......@@ -5,33 +5,6 @@
#include "timer/timer_kernel_1.h"
#include "timer/timer_kernel_2.h"
#include "uintn.h"
namespace dlib
{
template <
typename T
>
class timer
{
timer() {}
public:
//----------- kernels ---------------
// kernel_1a
typedef timer_kernel_1<T>
kernel_1a;
// kernel_2a
typedef timer_kernel_2<T>
kernel_2a;
};
}
#endif // DLIB_TIMEr_
......@@ -14,7 +14,7 @@ namespace dlib
template <
typename T
>
class timer_kernel_1
class timer_heavy
{
/*!
INITIAL VALUE
......@@ -46,12 +46,12 @@ namespace dlib
typedef void (T::*af_type)();
timer_kernel_1(
timer_heavy(
T& ao_,
af_type af_
);
virtual ~timer_kernel_1(
virtual ~timer_heavy(
);
void clear(
......@@ -110,8 +110,8 @@ namespace dlib
uint64 next_time_to_run;
// restricted functions
timer_kernel_1(const timer_kernel_1<T>&); // copy constructor
timer_kernel_1<T>& operator=(const timer_kernel_1<T>&); // assignment operator
timer_heavy(const timer_heavy<T>&); // copy constructor
timer_heavy<T>& operator=(const timer_heavy<T>&); // assignment operator
};
......@@ -124,8 +124,8 @@ namespace dlib
template <
typename T
>
timer_kernel_1<T>::
timer_kernel_1(
timer_heavy<T>::
timer_heavy(
T& ao_,
af_type af_
) :
......@@ -143,8 +143,8 @@ namespace dlib
template <
typename T
>
timer_kernel_1<T>::
~timer_kernel_1(
timer_heavy<T>::
~timer_heavy(
)
{
stop_and_wait();
......@@ -155,7 +155,7 @@ namespace dlib
template <
typename T
>
void timer_kernel_1<T>::
void timer_heavy<T>::
clear(
)
{
......@@ -171,7 +171,7 @@ namespace dlib
template <
typename T
>
typename timer_kernel_1<T>::af_type timer_kernel_1<T>::
typename timer_heavy<T>::af_type timer_heavy<T>::
action_function (
) const
{
......@@ -183,7 +183,7 @@ namespace dlib
template <
typename T
>
const T& timer_kernel_1<T>::
const T& timer_heavy<T>::
action_object (
) const
{
......@@ -195,7 +195,7 @@ namespace dlib
template <
typename T
>
T& timer_kernel_1<T>::
T& timer_heavy<T>::
action_object (
)
{
......@@ -207,7 +207,7 @@ namespace dlib
template <
typename T
>
bool timer_kernel_1<T>::
bool timer_heavy<T>::
is_running (
) const
{
......@@ -220,7 +220,7 @@ namespace dlib
template <
typename T
>
unsigned long timer_kernel_1<T>::
unsigned long timer_heavy<T>::
delay_time (
) const
{
......@@ -233,7 +233,7 @@ namespace dlib
template <
typename T
>
void timer_kernel_1<T>::
void timer_heavy<T>::
set_delay_time (
unsigned long milliseconds
)
......@@ -257,7 +257,7 @@ namespace dlib
template <
typename T
>
void timer_kernel_1<T>::
void timer_heavy<T>::
start (
)
{
......@@ -274,10 +274,10 @@ namespace dlib
running = true;
// start the thread
if (create_new_thread<timer_kernel_1,&timer_kernel_1::thread>(*this) == false)
if (create_new_thread<timer_heavy,&timer_heavy::thread>(*this) == false)
{
running = false;
throw dlib::thread_error("error creating new thread in timer_kernel_1::start");
throw dlib::thread_error("error creating new thread in timer_heavy::start");
}
}
}
......@@ -287,7 +287,7 @@ namespace dlib
template <
typename T
>
void timer_kernel_1<T>::
void timer_heavy<T>::
stop (
)
{
......@@ -302,7 +302,7 @@ namespace dlib
template <
typename T
>
void timer_kernel_1<T>::
void timer_heavy<T>::
thread (
)
{
......@@ -354,7 +354,7 @@ namespace dlib
template <
typename T
>
void timer_kernel_1<T>::
void timer_heavy<T>::
stop_and_wait (
)
{
......
......@@ -10,8 +10,8 @@ namespace dlib
// ----------------------------------------------------------------------------------------
timer_kernel_2_global_clock::
timer_kernel_2_global_clock(
timer_global_clock::
timer_global_clock(
):
s(m),
shutdown(false),
......@@ -21,8 +21,8 @@ namespace dlib
// ----------------------------------------------------------------------------------------
timer_kernel_2_global_clock::
~timer_kernel_2_global_clock()
timer_global_clock::
~timer_global_clock()
{
m.lock();
shutdown = true;
......@@ -33,9 +33,9 @@ namespace dlib
// ----------------------------------------------------------------------------------------
void timer_kernel_2_global_clock::
void timer_global_clock::
add (
timer_kernel_2_base* r
timer_base* r
)
{
if (r->in_global_clock == false)
......@@ -56,7 +56,7 @@ namespace dlib
// next event in tm
s.signal();
}
timer_kernel_2_base* rtemp = r;
timer_base* rtemp = r;
uint64 ttemp = t;
tm.add(ttemp,rtemp);
r->next_time_to_run = t;
......@@ -66,9 +66,9 @@ namespace dlib
// ----------------------------------------------------------------------------------------
void timer_kernel_2_global_clock::
void timer_global_clock::
remove (
timer_kernel_2_base* r
timer_base* r
)
{
if (r->in_global_clock)
......@@ -79,7 +79,7 @@ namespace dlib
if (tm.element().value() == r)
{
uint64 t;
timer_kernel_2_base* rtemp;
timer_base* rtemp;
tm.remove_current_element(t,rtemp);
r->in_global_clock = false;
break;
......@@ -90,9 +90,9 @@ namespace dlib
// ----------------------------------------------------------------------------------------
void timer_kernel_2_global_clock::
void timer_global_clock::
adjust_delay (
timer_kernel_2_base* r,
timer_base* r,
unsigned long new_delay
)
{
......@@ -117,7 +117,7 @@ namespace dlib
r->running = false;
r->delay = new_delay;
timer_kernel_2_base* rtemp = r;
timer_base* rtemp = r;
uint64 ttemp = t;
tm.add(ttemp,rtemp);
r->next_time_to_run = t;
......@@ -135,7 +135,7 @@ namespace dlib
// ----------------------------------------------------------------------------------------
void timer_kernel_2_global_clock::
void timer_global_clock::
thread()
{
auto_mutex M(m);
......@@ -155,8 +155,8 @@ namespace dlib
if (t <= cur_time + 999)
{
// remove this event from the tm map
timer_kernel_2_base* r = tm.element().value();
timer_kernel_2_base* rtemp;
timer_base* r = tm.element().value();
timer_base* rtemp;
tm.remove_current_element(t,rtemp);
r->in_global_clock = false;
......@@ -181,24 +181,24 @@ namespace dlib
// ----------------------------------------------------------------------------------------
shared_ptr_thread_safe<timer_kernel_2_global_clock> get_global_clock()
shared_ptr_thread_safe<timer_global_clock> get_global_clock()
{
static shared_ptr_thread_safe<timer_kernel_2_global_clock> d(new timer_kernel_2_global_clock);
static shared_ptr_thread_safe<timer_global_clock> d(new timer_global_clock);
return d;
}
// ----------------------------------------------------------------------------------------
// do this just to make sure get_global_clock() gets called at program startup
class timer_kernel_2_global_clock_helper
class timer_global_clock_helper
{
public:
timer_kernel_2_global_clock_helper()
timer_global_clock_helper()
{
get_global_clock();
}
};
static timer_kernel_2_global_clock_helper call_get_global_clock;
static timer_global_clock_helper call_get_global_clock;
// ----------------------------------------------------------------------------------------
......
......@@ -10,15 +10,16 @@
#include "../uintn.h"
#include "../binary_search_tree.h"
#include "../smart_pointers_thread_safe.h"
#include "timer_kernel_1.h"
namespace dlib
{
struct timer_kernel_2_base : public threaded_object
struct timer_base : public threaded_object
{
/*!
WHAT THIS OBJECT REPRESENTS
This object contains the base members of the timer_kernel_2 object.
This object contains the base members of the timer object.
It exists so that we can access them from outside any templated functions.
!*/
......@@ -32,11 +33,11 @@ namespace dlib
// ----------------------------------------------------------------------------------------
class timer_kernel_2_global_clock : private threaded_object
class timer_global_clock : private threaded_object
{
/*!
This object sets up a timer that triggers the action function
for timer_kernel_2 objects that are tracked inside this object.
for timer objects that are tracked inside this object.
INITIAL VALUE
- shutdown == false
- running == false
......@@ -47,15 +48,15 @@ namespace dlib
- else (running) then
- thread() is running
- tm[time] == pointer to a timer_kernel_2_base object
- tm[time] == pointer to a timer_base object
!*/
typedef binary_search_tree<uint64,timer_kernel_2_base*,memory_manager<char>::kernel_2b>::kernel_2a_c time_map;
typedef binary_search_tree<uint64,timer_base*,memory_manager<char>::kernel_2b>::kernel_2a_c time_map;
public:
~timer_kernel_2_global_clock();
~timer_global_clock();
void add (
timer_kernel_2_base* r
timer_base* r
);
/*!
requires
......@@ -69,7 +70,7 @@ namespace dlib
!*/
void remove (
timer_kernel_2_base* r
timer_base* r
);
/*!
requires
......@@ -81,7 +82,7 @@ namespace dlib
!*/
void adjust_delay (
timer_kernel_2_base* r,
timer_base* r,
unsigned long new_delay
);
/*!
......@@ -95,10 +96,10 @@ namespace dlib
mutex m;
friend shared_ptr_thread_safe<timer_kernel_2_global_clock> get_global_clock();
friend shared_ptr_thread_safe<timer_global_clock> get_global_clock();
private:
timer_kernel_2_global_clock();
timer_global_clock();
time_map tm;
signaler s;
......@@ -112,10 +113,10 @@ namespace dlib
- spawns timer tasks as is appropriate
!*/
};
shared_ptr_thread_safe<timer_kernel_2_global_clock> get_global_clock();
shared_ptr_thread_safe<timer_global_clock> get_global_clock();
/*!
ensures
- returns the global instance of the timer_kernel_2_global_clock object
- returns the global instance of the timer_global_clock object
!*/
// ----------------------------------------------------------------------------------------
......@@ -123,7 +124,7 @@ namespace dlib
template <
typename T
>
class timer_kernel_2 : private timer_kernel_2_base
class timer : private timer_base
{
/*!
INITIAL VALUE
......@@ -150,14 +151,19 @@ namespace dlib
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_kernel_2(
timer(
T& ao_,
af_type af_
);
virtual ~timer_kernel_2(
virtual ~timer(
);
void clear(
......@@ -203,11 +209,11 @@ namespace dlib
// data members
T& ao;
const af_type af;
shared_ptr_thread_safe<timer_kernel_2_global_clock> gc;
shared_ptr_thread_safe<timer_global_clock> gc;
// restricted functions
timer_kernel_2(const timer_kernel_2&); // copy constructor
timer_kernel_2& operator=(const timer_kernel_2&); // assignment operator
timer(const timer&); // copy constructor
timer& operator=(const timer&); // assignment operator
};
......@@ -220,8 +226,8 @@ namespace dlib
template <
typename T
>
timer_kernel_2<T>::
timer_kernel_2(
timer<T>::
timer(
T& ao_,
af_type af_
) :
......@@ -240,8 +246,8 @@ namespace dlib
template <
typename T
>
timer_kernel_2<T>::
~timer_kernel_2(
timer<T>::
~timer(
)
{
clear();
......@@ -253,7 +259,7 @@ namespace dlib
template <
typename T
>
void timer_kernel_2<T>::
void timer<T>::
clear(
)
{
......@@ -269,7 +275,7 @@ namespace dlib
template <
typename T
>
typename timer_kernel_2<T>::af_type timer_kernel_2<T>::
typename timer<T>::af_type timer<T>::
action_function (
) const
{
......@@ -281,7 +287,7 @@ namespace dlib
template <
typename T
>
const T& timer_kernel_2<T>::
const T& timer<T>::
action_object (
) const
{
......@@ -293,7 +299,7 @@ namespace dlib
template <
typename T
>
T& timer_kernel_2<T>::
T& timer<T>::
action_object (
)
{
......@@ -305,7 +311,7 @@ namespace dlib
template <
typename T
>
bool timer_kernel_2<T>::
bool timer<T>::
is_running (
) const
{
......@@ -318,7 +324,7 @@ namespace dlib
template <
typename T
>
unsigned long timer_kernel_2<T>::
unsigned long timer<T>::
delay_time (
) const
{
......@@ -331,7 +337,7 @@ namespace dlib
template <
typename T
>
void timer_kernel_2<T>::
void timer<T>::
set_delay_time (
unsigned long milliseconds
)
......@@ -345,7 +351,7 @@ namespace dlib
template <
typename T
>
void timer_kernel_2<T>::
void timer<T>::
start (
)
{
......@@ -362,7 +368,7 @@ namespace dlib
template <
typename T
>
void timer_kernel_2<T>::
void timer<T>::
stop (
)
{
......@@ -377,7 +383,7 @@ namespace dlib
template <
typename T
>
void timer_kernel_2<T>::
void timer<T>::
thread (
)
{
......@@ -396,7 +402,7 @@ namespace dlib
template <
typename T
>
void timer_kernel_2<T>::
void timer<T>::
stop_and_wait (
)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment