diff options
Diffstat (limited to 'ml/dlib/dlib/timer')
-rw-r--r-- | ml/dlib/dlib/timer/timer.cpp | 235 | ||||
-rw-r--r-- | ml/dlib/dlib/timer/timer.h | 427 | ||||
-rw-r--r-- | ml/dlib/dlib/timer/timer_abstract.h | 190 | ||||
-rw-r--r-- | ml/dlib/dlib/timer/timer_heavy.h | 392 |
4 files changed, 0 insertions, 1244 deletions
diff --git a/ml/dlib/dlib/timer/timer.cpp b/ml/dlib/dlib/timer/timer.cpp deleted file mode 100644 index d12aa6f29..000000000 --- a/ml/dlib/dlib/timer/timer.cpp +++ /dev/null @@ -1,235 +0,0 @@ -// Copyright (C) 2007 Davis E. King (davis@dlib.net) -// License: Boost Software License See LICENSE.txt for the full license. -#ifndef DLIB_TIMER_cPPh_ -#define DLIB_TIMER_cPPh_ - -#include "timer.h" - -namespace dlib -{ - -// ---------------------------------------------------------------------------------------- - - timer_global_clock:: - timer_global_clock( - ): - s(m), - shutdown(false), - running(false) - { - } - -// ---------------------------------------------------------------------------------------- - - timer_global_clock:: - ~timer_global_clock() - { - // The only time this destructor is called is when - // - // a) the process terminates - // b) the dynamic library(.so/.dll) is unloaded (could be a part of a)) - // - // in case of a) - // windows: the process termination is especially painful, since threads are killed - // before destructors of the process image .dll's are called. - // Thus, for the windows platform, there is no threads running, so the only thing - // to do here is just let the standard memberwise destructors run - // linux: it's ok to just signal shutdown and wait for the running thread, to exit - // - // in case of b) - // windows: - // if it's part of the termination process, a) applies - // if its part of user doing manual load_library/unload_library - // there is no (safe/robust)solution, but best practices are described here - // https://msdn.microsoft.com/en-us/library/windows/desktop/dn633971.aspx - // to support such a clean shutdown, you are required to make a call prior to - // unload dll, that shutdown all the threads in the contained dll. - // This could be done in this module by providing a global_delete_clock() - // - // linux: the destructor for linux will do it's usual job regardless. - // - - #ifndef _WIN32 - m.lock(); - shutdown = true; - s.signal(); - m.unlock(); - wait(); - #endif - } - -// ---------------------------------------------------------------------------------------- - - void timer_global_clock:: - add ( - timer_base* r - ) - { - if (r->in_global_clock == false) - { - // if the thread isn't running then start it up - if (!running) - { - start(); - running = true; - } - - uint64 t = ts.get_timestamp() + r->delay*1000; - tm.reset(); - if (!tm.move_next() || t < tm.element().key()) - { - // we need to make the thread adjust its next time to - // trigger if this new event occurrs sooner than the - // next event in tm - s.signal(); - } - timer_base* rtemp = r; - uint64 ttemp = t; - tm.add(ttemp,rtemp); - r->next_time_to_run = t; - r->in_global_clock = true; - } - } - -// ---------------------------------------------------------------------------------------- - - void timer_global_clock:: - remove ( - timer_base* r - ) - { - if (r->in_global_clock) - { - tm.position_enumerator(r->next_time_to_run-1); - do - { - if (tm.element().value() == r) - { - uint64 t; - timer_base* rtemp; - tm.remove_current_element(t,rtemp); - r->in_global_clock = false; - break; - } - } while (tm.move_next()); - } - } - -// ---------------------------------------------------------------------------------------- - - void timer_global_clock:: - adjust_delay ( - timer_base* r, - unsigned long new_delay - ) - { - if (r->in_global_clock) - { - remove(r); - // compute the new next_time_to_run and store it in t - uint64 t = r->next_time_to_run; - t -= r->delay*1000; - t += new_delay*1000; - - tm.reset(); - if (!tm.move_next() || t < tm.element().key()) - { - // we need to make the thread adjust its next time to - // trigger if this new event occurrs sooner than the - // next event in tm - s.signal(); - } - - // set this incase add throws - r->running = false; - r->delay = new_delay; - - timer_base* rtemp = r; - uint64 ttemp = t; - tm.add(ttemp,rtemp); - r->next_time_to_run = t; - r->in_global_clock = true; - - // put this back now that we know add didn't throw - r->running = true; - - } - else - { - r->delay = new_delay; - } - } - -// ---------------------------------------------------------------------------------------- - - void timer_global_clock:: - thread() - { - auto_mutex M(m); - while (!shutdown) - { - unsigned long delay = 100000; - - tm.reset(); - tm.move_next(); - // loop and start all the action functions for timers that should have - // triggered. - while(tm.current_element_valid()) - { - const uint64 cur_time = ts.get_timestamp(); - uint64 t = tm.element().key(); - // if the next event in tm is ready to trigger - if (t <= cur_time + 999) - { - // remove this event from the tm map - timer_base* r = tm.element().value(); - timer_base* rtemp; - tm.remove_current_element(t,rtemp); - r->in_global_clock = false; - - // if this timer is still "running" then start its action function - if (r->running) - { - r->restart(); - } - } - else - { - // there aren't any more timers that should trigger so we compute - // the delay to the next timer event. - delay = static_cast<unsigned long>((t - cur_time)/1000); - break; - } - } - - s.wait_or_timeout(delay); - } - } - -// ---------------------------------------------------------------------------------------- - - std::shared_ptr<timer_global_clock> get_global_clock() - { - static std::shared_ptr<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_global_clock_helper - { - public: - timer_global_clock_helper() - { - get_global_clock(); - } - }; - static timer_global_clock_helper call_get_global_clock; - -// ---------------------------------------------------------------------------------------- - -} - -#endif // DLIB_TIMER_cPPh_ - diff --git a/ml/dlib/dlib/timer/timer.h b/ml/dlib/dlib/timer/timer.h deleted file mode 100644 index c8b6c0af6..000000000 --- a/ml/dlib/dlib/timer/timer.h +++ /dev/null @@ -1,427 +0,0 @@ -// 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 <memory> - -#include "../threads.h" -#include "../algs.h" -#include "../misc_api.h" -#include "timer_abstract.h" -#include "../uintn.h" -#include "../binary_search_tree.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 std::shared_ptr<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 - !*/ - }; - std::shared_ptr<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; - std::shared_ptr<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_ - - diff --git a/ml/dlib/dlib/timer/timer_abstract.h b/ml/dlib/dlib/timer/timer_abstract.h deleted file mode 100644 index 180cd490d..000000000 --- a/ml/dlib/dlib/timer/timer_abstract.h +++ /dev/null @@ -1,190 +0,0 @@ -// Copyright (C) 2005 Davis E. King (davis@dlib.net) -// License: Boost Software License See LICENSE.txt for the full license. -#undef DLIB_TIMER_KERNEl_ABSTRACT_ -#ifdef DLIB_TIMER_KERNEl_ABSTRACT_ - -#include "../threads.h" - -namespace dlib -{ - - template < - typename T - > - class timer - { - /*! - INITIAL VALUE - is_running() == false - delay_time() == 1000 - action_object() == The object that is passed into the constructor - action_function() == The member function pointer that is passed to - the constructor. - - WHAT THIS OBJECT REPRESENTS - This object represents a timer that will call a given member function - (the action function) repeatedly at regular intervals and in its own - thread. - - Note that the delay_time() is measured in milliseconds but you are not - guaranteed to have that level of resolution. The actual resolution - is implementation dependent. - - THREAD SAFETY - All methods of this class are thread safe. - !*/ - - public: - - typedef void (T::*af_type)(); - - timer ( - T& ao, - af_type af - ); - /*! - requires - - af does not throw - ensures - - does not block. - - #*this is properly initialized - - #action_object() == ao - - #action_function() == af - (af is a member function pointer to a member in the class T) - throws - - std::bad_alloc - - dlib::thread_error - !*/ - - virtual ~timer ( - ); - /*! - requires - - is not called from inside the action_function() - ensures - - any resources associated with *this have been released - - will not call the action_function() anymore. - - if (the action function is currently executing) then - - blocks until it finishes - !*/ - - void clear( - ); - /*! - ensures - - #*this has its initial value - - does not block - throws - - std::bad_alloc or dlib::thread_error - If either of these exceptions are thrown then #*this is unusable - until clear() is called and succeeds. - !*/ - - af_type action_function ( - ) const; - /*! - ensures - - does not block. - - returns a pointer to the member function of action_object() that is - called by *this. - !*/ - - const T& action_object ( - ) const; - /*! - ensures - - does not block. - - returns a const reference to the object used to call the member - function pointer action_function() - !*/ - - T& action_object ( - ); - /*! - ensures - - does not block. - - returns a non-const reference to the object used to call the member - function pointer action_function() - !*/ - - bool is_running ( - ) const; - /*! - ensures - - does not block. - - if (*this is currently scheduled to call the action_function()) then - - returns true - - else - - returns false - !*/ - - unsigned long delay_time ( - ) const; - /*! - ensures - - does not block. - - returns the amount of time, in milliseconds, that *this will wait between - the return of one call to the action_function() and the beginning of the - next call to the action_function(). - !*/ - - void set_delay_time ( - unsigned long milliseconds - ); - /*! - ensures - - does not block. - - #delay_time() == milliseconds - throws - - std::bad_alloc or dlib::thread_error - If either of these exceptions are thrown then #is_running() == false - but otherwise this function succeeds - !*/ - - void start ( - ); - /*! - ensures - - does not block. - - if (is_running() == false) then - - #is_running() == true - - The action_function() will run in another thread. - - The first call to the action_function() will occur in roughly - delay_time() milliseconds. - - else - - this call to start() has no effect - throws - - dlib::thread_error or std::bad_alloc - If this exception is thrown then #is_running() == false but - otherwise this call to start() has no effect. - !*/ - - void stop ( - ); - /*! - ensures - - #is_running() == false - - does not block. - !*/ - - void stop_and_wait ( - ); - /*! - ensures - - #is_running() == false - - if (the action function is currently executing) then - - blocks until it finishes - !*/ - - private: - - // restricted functions - timer(const timer<T>&); // copy constructor - timer<T>& operator=(const timer<T>&); // assignment operator - - }; - -} - -#endif // DLIB_TIMER_KERNEl_ABSTRACT_ - diff --git a/ml/dlib/dlib/timer/timer_heavy.h b/ml/dlib/dlib/timer/timer_heavy.h deleted file mode 100644 index 693b91ad9..000000000 --- a/ml/dlib/dlib/timer/timer_heavy.h +++ /dev/null @@ -1,392 +0,0 @@ -// Copyright (C) 2005 Davis E. King (davis@dlib.net) -// License: Boost Software License See LICENSE.txt for the full license. -#ifndef DLIB_TIMER_KERNEl_1_ -#define DLIB_TIMER_KERNEl_1_ - -#include "../threads.h" -#include "../algs.h" -#include "../misc_api.h" -#include "timer_abstract.h" - -namespace dlib -{ - - template < - typename T - > - class timer_heavy - { - /*! - WHAT THIS OBJECT REPRESENTS - This is an implementation of the timer_abstract.h interface. It is very - simple and uses only one thread which is always alive in a timer_heavy. - The reason this object exists is for historical reasons. Originally, the - dlib::timer was a multi-implementation component and the timer_heavy was - its first implementation. It was superseded later by the more efficient - dlib::timer. However, timer_heavy is still around so that - dlib::timer::kernel_1a has something to refer to. This way, old client - code which somehow depends on the same thread always calling a timer action - function isn't going to be disrupted. - - - INITIAL VALUE - - running == false - - delay == 1000 - - ao == a pointer to the action_object() - - af == a pointer to the action_function() - - m == a mutex that locks everything in this class - - s == a signaler for mutex m - - stop_running == false - - CONVENTION - - running && !stop_running == is_running() - - delay == delay_time() - - *ao == action_object() - - af == action_function() - - - if (running) then - - there is a thread running - - if (is_running()) then - - next_time_to_run == the time when the next execution of the action - function should occur. (the time is given by ts.get_timestamp()) - - - stop_running is used to tell the thread to quit. If it is - set to true then the thread should end. - !*/ - - public: - - typedef void (T::*af_type)(); - - timer_heavy( - T& ao_, - af_type af_ - ); - - virtual ~timer_heavy( - ); - - 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 ( - ); - /*! - requires - - is run in its own thread - ensures - - calls the action function for the given timer object in the manner - specified by timer_kernel_abstract.h - !*/ - - // data members - T& ao; - const af_type af; - unsigned long delay; - mutex m; - signaler s; - - bool running; - bool stop_running; - timestamper ts; - uint64 next_time_to_run; - - // restricted functions - timer_heavy(const timer_heavy<T>&); // copy constructor - timer_heavy<T>& operator=(const timer_heavy<T>&); // assignment operator - - }; - -// ---------------------------------------------------------------------------------------- -// ---------------------------------------------------------------------------------------- - // member function definitions -// ---------------------------------------------------------------------------------------- -// ---------------------------------------------------------------------------------------- - - template < - typename T - > - timer_heavy<T>:: - timer_heavy( - T& ao_, - af_type af_ - ) : - ao(ao_), - af(af_), - delay(1000), - s(m), - running(false), - stop_running(false) - { - } - -// ---------------------------------------------------------------------------------------- - - template < - typename T - > - timer_heavy<T>:: - ~timer_heavy( - ) - { - stop_and_wait(); - } - -// ---------------------------------------------------------------------------------------- - - template < - typename T - > - void timer_heavy<T>:: - clear( - ) - { - m.lock(); - stop_running = true; - delay = 1000; - s.broadcast(); - m.unlock(); - } - -// ---------------------------------------------------------------------------------------- - - template < - typename T - > - typename timer_heavy<T>::af_type timer_heavy<T>:: - action_function ( - ) const - { - return af; - } - -// ---------------------------------------------------------------------------------------- - - template < - typename T - > - const T& timer_heavy<T>:: - action_object ( - ) const - { - return ao; - } - -// ---------------------------------------------------------------------------------------- - - template < - typename T - > - T& timer_heavy<T>:: - action_object ( - ) - { - return ao; - } - -// ---------------------------------------------------------------------------------------- - - template < - typename T - > - bool timer_heavy<T>:: - is_running ( - ) const - { - auto_mutex M(m); - return running && !stop_running; - } - -// ---------------------------------------------------------------------------------------- - - template < - typename T - > - unsigned long timer_heavy<T>:: - delay_time ( - ) const - { - auto_mutex M(m); - return delay; - } - -// ---------------------------------------------------------------------------------------- - - template < - typename T - > - void timer_heavy<T>:: - set_delay_time ( - unsigned long milliseconds - ) - { - m.lock(); - - // if (is_running()) then we should adjust next_time_to_run - if (running && !stop_running) - { - next_time_to_run -= delay*1000; - next_time_to_run += milliseconds*1000; - } - - delay = milliseconds; - s.broadcast(); - m.unlock(); - } - -// ---------------------------------------------------------------------------------------- - - template < - typename T - > - void timer_heavy<T>:: - start ( - ) - { - auto_mutex M(m); - - // if (is_running() == false) then reset the countdown to the next call - // to the action_function() - if ( (running && !stop_running) == false) - next_time_to_run = ts.get_timestamp() + delay*1000; - - stop_running = false; - if (running == false) - { - running = true; - - // start the thread - if (create_new_thread<timer_heavy,&timer_heavy::thread>(*this) == false) - { - running = false; - throw dlib::thread_error("error creating new thread in timer_heavy::start"); - } - } - } - -// ---------------------------------------------------------------------------------------- - - template < - typename T - > - void timer_heavy<T>:: - stop ( - ) - { - m.lock(); - stop_running = true; - s.broadcast(); - m.unlock(); - } - -// ---------------------------------------------------------------------------------------- - - template < - typename T - > - void timer_heavy<T>:: - thread ( - ) - { - auto_mutex M(m); - unsigned long delay_remaining; - uint64 current_time = ts.get_timestamp(); - - if (current_time < next_time_to_run) - delay_remaining = static_cast<unsigned long>((next_time_to_run-current_time)/1000); - else - delay_remaining = 0; - - while (stop_running == false) - { - if (delay_remaining > 0) - s.wait_or_timeout(delay_remaining); - - if (stop_running) - break; - - current_time = ts.get_timestamp(); - if (current_time < next_time_to_run) - { - // then we woke up too early so we should keep waiting - delay_remaining = static_cast<unsigned long>((next_time_to_run-current_time)/1000); - - // rounding might make this be zero anyway. So if it is - // then we will say we have hit the next time to run. - if (delay_remaining > 0) - continue; - } - - // call the action function - m.unlock(); - (ao.*af)(); - m.lock(); - - current_time = ts.get_timestamp(); - next_time_to_run = current_time + delay*1000; - delay_remaining = delay; - } - running = false; - stop_running = false; - s.broadcast(); - } - -// ---------------------------------------------------------------------------------------- - - template < - typename T - > - void timer_heavy<T>:: - stop_and_wait ( - ) - { - m.lock(); - if (running) - { - // make the running thread terminate - stop_running = true; - - s.broadcast(); - // wait for the thread to quit - while (running) - s.wait(); - } - m.unlock(); - } - -// ---------------------------------------------------------------------------------------- - -} - -#endif // DLIB_TIMER_KERNEl_1_ - |