Skip to content

Commit

Permalink
cyber: fix exit hang issue (ApolloAuto#9384)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangchaoltt authored Aug 14, 2019
1 parent cd16a93 commit d1651f4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
11 changes: 6 additions & 5 deletions cyber/component/timer_component.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,20 @@ bool TimerComponent::Initialize(const TimerComponentConfig& config) {
return false;
}

std::weak_ptr<TimerComponent> self =
std::shared_ptr<TimerComponent> self =
std::dynamic_pointer_cast<TimerComponent>(shared_from_this());
auto func = [self]() {
auto ptr = self.lock();
if (ptr) {
ptr->Proc();
}
self->Proc();
};
timer_.reset(new Timer(config.interval(), func, false));
timer_->Start();
return true;
}

void TimerComponent::Clear() {
timer_.reset();
}

uint64_t TimerComponent::GetInterval() const { return interval_; }

} // namespace cyber
Expand Down
1 change: 1 addition & 0 deletions cyber/component/timer_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class TimerComponent : public ComponentBase {
* @return returns true if successful, otherwise returns false
*/
bool Initialize(const TimerComponentConfig& config) override;
void Clear() override;
bool Process();
uint64_t GetInterval() const;

Expand Down
3 changes: 3 additions & 0 deletions cyber/task/task_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ void TaskManager::Shutdown() {
if (stop_.exchange(true)) {
return;
}
for (uint32_t i = 0; i < num_threads_; i++) {
scheduler::Instance()->RemoveTask(task_prefix + std::to_string(i));
}
}

} // namespace cyber
Expand Down
14 changes: 12 additions & 2 deletions cyber/timer/timer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,23 @@ bool Timer::InitTimerTask() {
task_->interval_ms = timer_opt_.period;
task_->next_fire_duration_ms = task_->interval_ms;
if (timer_opt_.oneshot) {
task_->callback = timer_opt_.callback;
std::weak_ptr<TimerTask> task_weak_ptr = task_;
task_->callback = [callback = this->timer_opt_.callback, task_weak_ptr]() {
auto task = task_weak_ptr.lock();
if (!task) {
return;
}
std::lock_guard<std::mutex> lg(task->mtx_);
callback();
};
} else {
std::weak_ptr<TimerTask> task_weak_ptr = task_;
task_->callback = [callback = this->timer_opt_.callback, task_weak_ptr]() {
auto task = task_weak_ptr.lock();
if (!task) {
return;
}
std::lock_guard<std::mutex> lg(task->mtx_);
auto start = Time::MonoTime().ToNanosecond();
callback();
auto end = Time::MonoTime().ToNanosecond();
Expand Down Expand Up @@ -130,7 +139,8 @@ void Timer::Start() {

void Timer::Stop() {
if (started_.exchange(false)) {
ADEBUG << "stop timer ";
AINFO << "stop timer, the timer_id: " << timer_id_;
std::lock_guard<std::mutex> lg(task_->mtx_);
task_.reset();
}
}
Expand Down
2 changes: 2 additions & 0 deletions cyber/timer/timer_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define CYBER_TIMER_TIMER_TASK_H_

#include <functional>
#include <mutex>

namespace apollo {
namespace cyber {
Expand All @@ -33,6 +34,7 @@ struct TimerTask {
uint64_t next_fire_duration_ms = 0;
int64_t accumulated_error_ns_ = 0;
uint64_t last_execute_time_ns_ = 0;
std::mutex mtx_;
};

} // namespace cyber
Expand Down
6 changes: 6 additions & 0 deletions modules/control/control_component_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class ControlComponentTest : public ::testing::Test {
SetupCyber();
}

virtual void TearDown() {
if (control_component_) {
control_component_->Shutdown();
}
}

protected:
bool FeedTestData();
void SetupCyber();
Expand Down

0 comments on commit d1651f4

Please sign in to comment.