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,bool is_rt)24 Worker::Worker(const char *name, int priority, bool is_rt)
25     : name_(name), priority_(priority), is_rt_(is_rt), exit_(false), initialized_(false) {
26 }
27 
~Worker()28 Worker::~Worker() {
29 }
30 
InitWorker()31 int Worker::InitWorker() {
32   std::lock_guard<std::mutex> lk(mutex_);
33   if (initialized())
34     return -EALREADY;
35 
36   thread_ = std::unique_ptr<std::thread>(
37       new 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   if (is_rt_) {
80     struct sched_param param = { 0 };
81     param.sched_priority = priority_;
82     sched_setscheduler(0, SCHED_FIFO, &param);
83   } else {
84     setpriority(PRIO_PROCESS, 0, priority_);
85   }
86 
87   prctl(PR_SET_NAME, name_.c_str());
88 
89   std::unique_lock<std::mutex> lk(mutex_, std::defer_lock);
90 
91   while (true) {
92     lk.lock();
93     if (should_exit())
94       return;
95     lk.unlock();
96 
97     Routine();
98   }
99 }
100 }  // namespace android
101