• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015-2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "Worker.h"
18 
19 #include <sys/prctl.h>
20 #include <sys/resource.h>
21 
22 namespace android {
23 
Worker(const char * name,int priority)24 Worker::Worker(const char *name, int priority)
25     : name_(name), priority_(priority), exit_(false), initialized_(false) {
26 }
27 
~Worker()28 Worker::~Worker() {
29   Exit();
30 }
31 
InitWorker()32 int Worker::InitWorker() {
33   std::lock_guard<std::mutex> lk(mutex_);
34   if (initialized())
35     return -EALREADY;
36 
37   thread_ = std::make_unique<std::thread>(&Worker::InternalRoutine, this);
38   initialized_ = true;
39   exit_ = false;
40 
41   return 0;
42 }
43 
Exit()44 void Worker::Exit() {
45   std::unique_lock<std::mutex> lk(mutex_);
46   exit_ = true;
47   if (initialized()) {
48     lk.unlock();
49     cond_.notify_all();
50     thread_->join();
51     initialized_ = false;
52   }
53 }
54 
WaitForSignalOrExitLocked(int64_t max_nanoseconds)55 int Worker::WaitForSignalOrExitLocked(int64_t max_nanoseconds) {
56   int ret = 0;
57   if (should_exit())
58     return -EINTR;
59 
60   std::unique_lock<std::mutex> lk(mutex_, std::adopt_lock);
61   if (max_nanoseconds < 0) {
62     cond_.wait(lk);
63   } else if (std::cv_status::timeout ==
64              cond_.wait_for(lk, std::chrono::nanoseconds(max_nanoseconds))) {
65     ret = -ETIMEDOUT;
66   }
67 
68   // exit takes precedence on timeout
69   if (should_exit())
70     ret = -EINTR;
71 
72   // release leaves mutex locked when going out of scope
73   lk.release();
74 
75   return ret;
76 }
77 
InternalRoutine()78 void Worker::InternalRoutine() {
79   setpriority(PRIO_PROCESS, 0, priority_);
80   prctl(PR_SET_NAME, name_.c_str());
81 
82   std::unique_lock<std::mutex> lk(mutex_, std::defer_lock);
83 
84   while (true) {
85     lk.lock();
86     if (should_exit())
87       return;
88     lk.unlock();
89 
90     Routine();
91   }
92 }
93 }  // namespace android
94