summaryrefslogtreecommitdiff
path: root/lib/common
diff options
context:
space:
mode:
authorChris Wilson <chris+github@qwirx.com>2014-09-04 01:36:13 +0000
committerChris Wilson <chris+github@qwirx.com>2014-09-04 01:36:13 +0000
commita134f6eca8102400f41ae0a1e2e9ab3236b1649b (patch)
tree614ed773d8a00bc8d6c1c4a7c296877adfd878a2 /lib/common
parent426a506afd1ffb3bd67e61b4693ee9bb968097a1 (diff)
Backport Timers::Cleanup that's safe to use in test cleanup.
Allows it not to throw an exception if timers weren't initialised when cleanup was requested. Normally we want an exception thrown, but not while we're cleaning up a test that might have failed with timers uninitialised. More timers fixes after cleanup no-exception option. Merged back changes from the test refactor branch to reduce diffs.
Diffstat (limited to 'lib/common')
-rw-r--r--lib/common/CommonException.txt1
-rw-r--r--lib/common/Timer.cpp50
-rw-r--r--lib/common/Timer.h3
3 files changed, 47 insertions, 7 deletions
diff --git a/lib/common/CommonException.txt b/lib/common/CommonException.txt
index 05da2709..deca57ba 100644
--- a/lib/common/CommonException.txt
+++ b/lib/common/CommonException.txt
@@ -55,3 +55,4 @@ DatabaseRecordAlreadyExists 47 The database already contains a record with this
DatabaseRecordBadSize 48 The database contains a record with an invalid size
DatabaseIterateFailed 49 Failed to iterate over the database keys
ReferenceNotFound 50 The database does not contain an expected reference
+TimersNotInitialised 51 The timer framework should have been ready at this point
diff --git a/lib/common/Timer.cpp b/lib/common/Timer.cpp
index 68042db2..e0a4b7f7 100644
--- a/lib/common/Timer.cpp
+++ b/lib/common/Timer.cpp
@@ -72,13 +72,23 @@ void Timers::Init()
// Created: 6/11/2006
//
// --------------------------------------------------------------------------
-void Timers::Cleanup()
+void Timers::Cleanup(bool throw_exception_if_not_initialised)
{
- ASSERT(spTimers);
- if (!spTimers)
+ if (throw_exception_if_not_initialised)
{
- BOX_ERROR("Tried to clean up timers when not initialised!");
- return;
+ ASSERT(spTimers);
+ if (!spTimers)
+ {
+ BOX_ERROR("Tried to clean up timers when not initialised!");
+ return;
+ }
+ }
+ else
+ {
+ if (!spTimers)
+ {
+ return;
+ }
}
#if defined WIN32 && ! defined PLATFORM_CYGWIN
@@ -110,6 +120,26 @@ void Timers::Cleanup()
// --------------------------------------------------------------------------
//
// Function
+// Name: static void Timers::AssertInitialised()
+// Purpose: Throw an assertion error if timers are not ready
+// NOW. It's a common mistake (for me) when writing
+// tests to forget to initialise timers first.
+// Created: 15/05/2014
+//
+// --------------------------------------------------------------------------
+
+void Timers::AssertInitialised()
+{
+ if (!spTimers)
+ {
+ THROW_EXCEPTION(CommonException, TimersNotInitialised);
+ }
+ ASSERT(spTimers);
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
// Name: static void Timers::Add(Timer&)
// Purpose: Add a new timer to the set, and reschedule next wakeup
// Created: 5/11/2006
@@ -135,8 +165,16 @@ void Timers::Add(Timer& rTimer)
// --------------------------------------------------------------------------
void Timers::Remove(Timer& rTimer)
{
- ASSERT(spTimers);
ASSERT(&rTimer);
+
+ if(!spTimers)
+ {
+ BOX_WARNING(TIMER_ID_OF(rTimer) " was still active after "
+ "timer subsystem was cleaned up, already removed.");
+ return;
+ }
+
+ ASSERT(spTimers);
BOX_TRACE(TIMER_ID_OF(rTimer) " removed from global queue, rescheduling");
bool restart = true;
diff --git a/lib/common/Timer.h b/lib/common/Timer.h
index 09be58fa..68592aaa 100644
--- a/lib/common/Timer.h
+++ b/lib/common/Timer.h
@@ -43,7 +43,8 @@ class Timers
public:
static void Init();
- static void Cleanup();
+ static void Cleanup(bool throw_exception_if_not_initialised = true);
+ static void AssertInitialised();
static void Add (Timer& rTimer);
static void Remove(Timer& rTimer);
static void RequestReschedule();