Lesson 35 - Get Compute Auth Token Working

This commit is contained in:
Norman Lansing
2026-02-28 12:32:28 -05:00
parent 1d477ee42a
commit 4fde462bce
7743 changed files with 1397833 additions and 18 deletions

View File

@@ -0,0 +1,427 @@
// 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_

View File

@@ -0,0 +1,190 @@
// 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_

View File

@@ -0,0 +1,392 @@
// 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_