diff --git a/libwvdrmengine/cdm/src/timer.cpp b/libwvdrmengine/cdm/src/timer.cpp index 97a6200e..fd5f146e 100644 --- a/libwvdrmengine/cdm/src/timer.cpp +++ b/libwvdrmengine/cdm/src/timer.cpp @@ -8,6 +8,7 @@ #include "utils/RefBase.h" #include "utils/StrongPointer.h" #include "utils/Thread.h" +#include "utils/Mutex.h" namespace wvcdm { @@ -15,27 +16,35 @@ class Timer::Impl : virtual public android::RefBase { private: class ImplThread : public android::Thread { public: - ImplThread() : Thread(false), handler_(NULL), period_(0) {} + ImplThread() : Thread(false), handler_(NULL), period_ns_(0) {} virtual ~ImplThread() {}; bool Start(TimerHandler *handler, uint32_t time_in_secs) { handler_ = handler; - period_ = time_in_secs; + 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() { - struct timeval timeout; - timeout.tv_sec = period_; - timeout.tv_usec = 0; - TEMP_FAILURE_RETRY(select(0, NULL, NULL, NULL, &timeout)); + android::Mutex::Autolock autoLock(lock_); + stop_condition_.waitRelative(lock_, period_ns_); handler_->OnTimerEvent(); return true; } TimerHandler *handler_; - uint32_t period_; + uint64_t period_ns_; + android::Mutex lock_; + android::Condition stop_condition_; CORE_DISALLOW_COPY_AND_ASSIGN(ImplThread); }; @@ -52,7 +61,7 @@ class Timer::Impl : virtual public android::RefBase { } void Stop() { - impl_thread_->requestExitAndWait(); + impl_thread_->Stop(); impl_thread_.clear(); }