(This is a merge of http://go/wvgerrit/13761 from the Widevine repository.) This cleans up our includes to be in Google Style Guide order and in alphabetic order, for the parts of the code that are expected to follow Google Style. This also converts places in our code that were including C headers in the C++ style (i.e. <cstring> instead of <string.h>) to use C style instead. This is because, although it was not causing problems for us yet, on Android these actually include different headers. (<cstring> is provided by libcxx, while <string.h> is provided by Bionic) Lastly, this change puts all headers that do not come from within our project in <brackets> instead of "quotes," which was not being done consistently. This change is explicitly NOT trying to standardize the spacing of our header includes. I have tried to respect, in each file, the spacing style already present. Change-Id: If3dc06532ab9b68010285d64518ef21dce3d6354
103 lines
2.1 KiB
C++
103 lines
2.1 KiB
C++
// Copyright 2013 Google Inc. All Rights Reserved.
|
|
//
|
|
// Timer class - provides a simple Android specific timer implementation
|
|
|
|
#include "timer.h"
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <utils/Mutex.h>
|
|
#include <utils/RefBase.h>
|
|
#include <utils/StrongPointer.h>
|
|
#include <utils/Thread.h>
|
|
|
|
namespace wvcdm {
|
|
|
|
class Timer::Impl : virtual public android::RefBase {
|
|
private:
|
|
class ImplThread : public android::Thread {
|
|
public:
|
|
ImplThread() : Thread(false), handler_(NULL), period_ns_(0) {}
|
|
virtual ~ImplThread() {};
|
|
|
|
bool Start(TimerHandler *handler, uint32_t time_in_secs) {
|
|
handler_ = handler;
|
|
period_ns_ = time_in_secs * 1000000000ll;
|
|
return run() == android::NO_ERROR;
|
|
}
|
|
|
|
void Stop() {
|
|
{
|
|
android::Mutex::Autolock autoLock(lock_);
|
|
stop_condition_.signal();
|
|
}
|
|
requestExitAndWait();
|
|
}
|
|
|
|
private:
|
|
virtual bool threadLoop() {
|
|
android::Mutex::Autolock autoLock(lock_);
|
|
stop_condition_.waitRelative(lock_, period_ns_);
|
|
handler_->OnTimerEvent();
|
|
return true;
|
|
}
|
|
|
|
TimerHandler *handler_;
|
|
uint64_t period_ns_;
|
|
android::Mutex lock_;
|
|
android::Condition stop_condition_;
|
|
|
|
CORE_DISALLOW_COPY_AND_ASSIGN(ImplThread);
|
|
};
|
|
|
|
android::sp<ImplThread> impl_thread_;
|
|
|
|
public:
|
|
Impl() {}
|
|
virtual ~Impl() {};
|
|
|
|
bool Start(TimerHandler *handler, uint32_t time_in_secs) {
|
|
impl_thread_ = new ImplThread();
|
|
return impl_thread_->Start(handler, time_in_secs);
|
|
}
|
|
|
|
void Stop() {
|
|
impl_thread_->Stop();
|
|
impl_thread_.clear();
|
|
}
|
|
|
|
bool IsRunning() {
|
|
return (impl_thread_ != NULL) && (impl_thread_->isRunning());
|
|
}
|
|
|
|
CORE_DISALLOW_COPY_AND_ASSIGN(Impl);
|
|
};
|
|
|
|
Timer::Timer() : impl_(new Timer::Impl()) {
|
|
}
|
|
|
|
Timer::~Timer() {
|
|
if (IsRunning())
|
|
Stop();
|
|
|
|
delete impl_;
|
|
impl_ = NULL;
|
|
}
|
|
|
|
bool Timer::Start(TimerHandler *handler, uint32_t time_in_secs) {
|
|
if (!handler || time_in_secs == 0)
|
|
return false;
|
|
|
|
return impl_->Start(handler, time_in_secs);
|
|
}
|
|
|
|
void Timer::Stop() {
|
|
impl_->Stop();
|
|
}
|
|
|
|
bool Timer::IsRunning() {
|
|
return impl_->IsRunning();
|
|
}
|
|
|
|
} // namespace wvcdm
|