Skip to content

Commit

Permalink
Improves MythTimer contstructor.
Browse files Browse the repository at this point in the history
Many times a MythTimer is constructed and start() is called immediately afterward.
This adds a parameter to the constructor StartState, so instead of:
  MythTimer t;
  t.start();
  do_something();
  int e = t.elapsed();

you can write:
  MythTimer t(MythTimer::StartRunning);
  do_something();
  int e = t.elapsed();

This also reverts [9402858], which made starting the timer running the default.
The bug requiring that hack was fixed in [926464a].

This also allows you to define DEBUG_TIMER_API_USAGE and have MythTimer assert
that the timer is running when elapsed is called to catch timer API misuse.
  • Loading branch information
daniel-kristjansson committed Jan 27, 2013
1 parent 309c97e commit 58584fa
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
24 changes: 24 additions & 0 deletions mythtv/libs/libmythbase/mythtimer.cpp
Expand Up @@ -22,6 +22,25 @@
// MythTV includes
#include "mythtimer.h"

//#define DEBUG_TIMER_API_USAGE
#ifdef DEBUG_TIMER_API_USAGE
#undef NDEBUG
#include <cassert>
#endif

/** Creates a timer.
*
* If a start state of kStartRunning is passed in the timer is
* started immediately as if start() had been called.
*/
MythTimer::MythTimer(StartState state) : m_offset(0)
{
if (kStartRunning == state)
start();
else
stop();
}

/// starts measuring elapsed time.
void MythTimer::start(void)
{
Expand Down Expand Up @@ -69,7 +88,12 @@ void MythTimer::stop(void)
int MythTimer::elapsed(void) const
{
if (!m_timer.isValid())
{
#ifdef DEBUG_TIMER_API_USAGE
assert(0 == "elapsed called without timer being started");
#endif
return 0;
}

int64_t e = m_timer.elapsed();
if (!m_timer.isMonotonic() && (e > 86300000))
Expand Down
7 changes: 6 additions & 1 deletion mythtv/libs/libmythbase/mythtimer.h
Expand Up @@ -12,7 +12,12 @@
class MBASE_PUBLIC MythTimer
{
public:
MythTimer() : m_offset(0) { m_timer.start(); }
typedef enum {
kStartRunning,
kStartInactive,
} StartState;

MythTimer(StartState state = kStartInactive);

void start(void);
int restart(void);
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythbase/mythversion.h
Expand Up @@ -12,7 +12,7 @@
/// Update this whenever the plug-in API changes.
/// Including changes in the libmythbase, libmyth, libmythtv, libmythav* and
/// libmythui class methods used by plug-ins.
#define MYTH_BINARY_VERSION "0.27.20130124-1"
#define MYTH_BINARY_VERSION "0.27.20130127-1"

/** \brief Increment this whenever the MythTV network protocol changes.
*
Expand Down
9 changes: 8 additions & 1 deletion mythtv/libs/libmythbase/test/test_mythtimer.h
Expand Up @@ -56,13 +56,20 @@ class TestMythTimer: public QObject
QVERIFY(t.elapsed() > 50);
}

void TimeDoesNotElapseImmediatelyAfterConstruction(void)
void TimeDoesNotElapseImmediatelyAfterConstructionByDefault(void)
{
MythTimer t;
usleep(52 * 1000);
QVERIFY(t.elapsed() == 0);
}

void TimeDoesNotElapsesImmediatelyAfterContructionIfIntended(void)
{
MythTimer t(MythTimer::kStartRunning);
usleep(52 * 1000);
QVERIFY(t.elapsed() > 50);
}

void TimeElapsesContinually(void)
{
MythTimer t;
Expand Down

0 comments on commit 58584fa

Please sign in to comment.