blob: 0dceb16d0e454b6c0938a710deb051cade188300 [file] [log] [blame]
Sean Paulcb8676c2015-05-13 06:22:10 -07001/*
Adrian Salidofa37f672017-02-16 10:29:46 -08002 * Copyright (C) 2015-2016 The Android Open Source Project
Sean Paulcb8676c2015-05-13 06:22:10 -07003 *
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
Sean Paulcb8676c2015-05-13 06:22:10 -070017#include "worker.h"
18
Adrian Salidofa37f672017-02-16 10:29:46 -080019#include <sys/prctl.h>
Sean Paulcb8676c2015-05-13 06:22:10 -070020#include <sys/resource.h>
Sean Paulcb8676c2015-05-13 06:22:10 -070021
22namespace android {
23
24Worker::Worker(const char *name, int priority)
25 : name_(name), priority_(priority), exit_(false), initialized_(false) {
26}
27
28Worker::~Worker() {
Adrian Salidofa37f672017-02-16 10:29:46 -080029 Exit();
Sean Paulcb8676c2015-05-13 06:22:10 -070030}
31
32int Worker::InitWorker() {
Adrian Salidoba494c82017-04-25 13:23:45 -070033 std::lock_guard<std::mutex> lk(mutex_);
Adrian Salidofa37f672017-02-16 10:29:46 -080034 if (initialized())
35 return -EALREADY;
Sean Paulcb8676c2015-05-13 06:22:10 -070036
Adrian Salidofa37f672017-02-16 10:29:46 -080037 thread_ = std::unique_ptr<std::thread>(
38 new std::thread(&Worker::InternalRoutine, this));
Sean Paulcb8676c2015-05-13 06:22:10 -070039 initialized_ = true;
Adrian Salidoba494c82017-04-25 13:23:45 -070040 exit_ = false;
Adrian Salidofa37f672017-02-16 10:29:46 -080041
Sean Paulcb8676c2015-05-13 06:22:10 -070042 return 0;
43}
44
Adrian Salidofa37f672017-02-16 10:29:46 -080045void Worker::Exit() {
Adrian Salidoba494c82017-04-25 13:23:45 -070046 std::unique_lock<std::mutex> lk(mutex_);
47 exit_ = true;
Adrian Salidofa37f672017-02-16 10:29:46 -080048 if (initialized()) {
Adrian Salidoba494c82017-04-25 13:23:45 -070049 lk.unlock();
Adrian Salidofa37f672017-02-16 10:29:46 -080050 cond_.notify_all();
51 thread_->join();
52 initialized_ = false;
Sean Paulcb8676c2015-05-13 06:22:10 -070053 }
Sean Paulcb8676c2015-05-13 06:22:10 -070054}
55
Zach Reizner8467b122015-11-10 15:18:08 -080056int Worker::WaitForSignalOrExitLocked(int64_t max_nanoseconds) {
Adrian Salidofa37f672017-02-16 10:29:46 -080057 int ret = 0;
58 if (should_exit())
Sean Paulcb8676c2015-05-13 06:22:10 -070059 return -EINTR;
60
Adrian Salidofa37f672017-02-16 10:29:46 -080061 std::unique_lock<std::mutex> lk(mutex_, std::adopt_lock);
Zach Reizner8467b122015-11-10 15:18:08 -080062 if (max_nanoseconds < 0) {
Adrian Salidofa37f672017-02-16 10:29:46 -080063 cond_.wait(lk);
64 } else if (std::cv_status::timeout ==
65 cond_.wait_for(lk, std::chrono::nanoseconds(max_nanoseconds))) {
66 ret = -ETIMEDOUT;
Zach Reizner8467b122015-11-10 15:18:08 -080067 }
Sean Paulcb8676c2015-05-13 06:22:10 -070068
Adrian Salidofa37f672017-02-16 10:29:46 -080069 // exit takes precedence on timeout
70 if (should_exit())
71 ret = -EINTR;
72
Adrian Salidoba494c82017-04-25 13:23:45 -070073 // release leaves mutex locked when going out of scope
Adrian Salidofa37f672017-02-16 10:29:46 -080074 lk.release();
Sean Paulcb8676c2015-05-13 06:22:10 -070075
76 return ret;
77}
78
Adrian Salidofa37f672017-02-16 10:29:46 -080079void Worker::InternalRoutine() {
80 setpriority(PRIO_PROCESS, 0, priority_);
81 prctl(PR_SET_NAME, name_.c_str());
Sean Paulcb8676c2015-05-13 06:22:10 -070082
Adrian Salidofa37f672017-02-16 10:29:46 -080083 std::unique_lock<std::mutex> lk(mutex_, std::defer_lock);
Sean Paulcb8676c2015-05-13 06:22:10 -070084
85 while (true) {
Adrian Salidofa37f672017-02-16 10:29:46 -080086 lk.lock();
87 if (should_exit())
88 return;
89 lk.unlock();
Sean Paulcb8676c2015-05-13 06:22:10 -070090
Adrian Salidofa37f672017-02-16 10:29:46 -080091 Routine();
Sean Paulcb8676c2015-05-13 06:22:10 -070092 }
Sean Paulcb8676c2015-05-13 06:22:10 -070093}
Sean Paulf72cccd2018-08-27 13:59:08 -040094} // namespace android