Skip to content

Commit 2540ab1

Browse files
committed
Timer: minor revisions
* C++11-ify a little. * Make it copy/move constructible.
1 parent 029109a commit 2540ab1

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

drivers/Timer.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#include "platform/NonCopyable.h"
2323

2424
namespace mbed {
25+
26+
class CriticalSectionLock;
27+
2528
/**
2629
* \defgroup drivers_Timer Timer class
2730
* \ingroup drivers-public-api-ticker
@@ -51,7 +54,7 @@ namespace mbed {
5154
* }
5255
* @endcode
5356
*/
54-
class TimerBase : private NonCopyable<TimerBase> {
57+
class TimerBase {
5558

5659
public:
5760
/** Start the timer
@@ -109,13 +112,24 @@ class TimerBase : private NonCopyable<TimerBase> {
109112
protected:
110113
TimerBase(const ticker_data_t *data);
111114
TimerBase(const ticker_data_t *data, bool lock_deepsleep);
115+
TimerBase(const TimerBase &t);
116+
TimerBase(TimerBase &&t);
112117
~TimerBase();
118+
119+
const TimerBase &operator=(const TimerBase &) = delete;
120+
113121
std::chrono::microseconds slicetime() const;
114122
TickerDataClock::time_point _start{}; // the start time of the latest slice
115123
std::chrono::microseconds _time{}; // any accumulated time from previous slices
116124
TickerDataClock _ticker_data;
117125
bool _lock_deepsleep; // flag that indicates if deep sleep should be disabled
118126
bool _running = false; // whether the timer is running
127+
128+
private:
129+
// Copy storage while a lock is held
130+
TimerBase(const TimerBase &t, const CriticalSectionLock &) : TimerBase(t, false) {}
131+
// Copy storage only - used by delegating constructors
132+
TimerBase(const TimerBase &t, bool) : _start(t._start), _time(t._time), _ticker_data(t._ticker_data), _lock_deepsleep(t._lock_deepsleep), _running(t._running) {}
119133
};
120134
#endif
121135

drivers/source/Timer.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ TimerBase::TimerBase(const ticker_data_t *data, bool lock_deepsleep) : _ticker_d
3838
reset();
3939
}
4040

41+
// This creates a temporary CriticalSectionLock while we delegate to the
42+
// constructor that does the copy, thus holding critical section during the copy,
43+
// ensuring locking on the source. Then continue our own initialization
44+
// outside the critical section
45+
TimerBase::TimerBase(const TimerBase &t) : TimerBase(t, CriticalSectionLock{})
46+
{
47+
// If running, new copy needs an extra lock
48+
if (_running && _lock_deepsleep) {
49+
sleep_manager_lock_deep_sleep();
50+
}
51+
}
52+
53+
// Unlike copy constructor, no need for lock on move - we must be only person
54+
// accessing source.
55+
TimerBase::TimerBase(TimerBase &&t) : TimerBase(t, false)
56+
{
57+
// Original is marked as no longer running - we adopt any lock it had
58+
t._running = false;
59+
}
60+
4161
TimerBase::~TimerBase()
4262
{
4363
if (_running) {

0 commit comments

Comments
 (0)