blob: d2b60c82bf96697d51a8b74fac3234f69cb09b93 [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
Roman Stratiienko13cc3662020-08-29 21:35:39 +030017#include "Worker.h"
Sean Paulcb8676c2015-05-13 06:22:10 -070018
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
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020037 thread_ = std::make_unique<std::thread>(&Worker::InternalRoutine, this);
Sean Paulcb8676c2015-05-13 06:22:10 -070038 initialized_ = true;
Adrian Salidoba494c82017-04-25 13:23:45 -070039 exit_ = false;
Adrian Salidofa37f672017-02-16 10:29:46 -080040
Sean Paulcb8676c2015-05-13 06:22:10 -070041 return 0;
42}
43
Adrian Salidofa37f672017-02-16 10:29:46 -080044void Worker::Exit() {
Adrian Salidoba494c82017-04-25 13:23:45 -070045 std::unique_lock<std::mutex> lk(mutex_);
46 exit_ = true;
Adrian Salidofa37f672017-02-16 10:29:46 -080047 if (initialized()) {
Adrian Salidoba494c82017-04-25 13:23:45 -070048 lk.unlock();
Adrian Salidofa37f672017-02-16 10:29:46 -080049 cond_.notify_all();
50 thread_->join();
51 initialized_ = false;
Sean Paulcb8676c2015-05-13 06:22:10 -070052 }
Sean Paulcb8676c2015-05-13 06:22:10 -070053}
54
Zach Reizner8467b122015-11-10 15:18:08 -080055int Worker::WaitForSignalOrExitLocked(int64_t max_nanoseconds) {
Adrian Salidofa37f672017-02-16 10:29:46 -080056 int ret = 0;
57 if (should_exit())
Sean Paulcb8676c2015-05-13 06:22:10 -070058 return -EINTR;
59
Adrian Salidofa37f672017-02-16 10:29:46 -080060 std::unique_lock<std::mutex> lk(mutex_, std::adopt_lock);
Zach Reizner8467b122015-11-10 15:18:08 -080061 if (max_nanoseconds < 0) {
Adrian Salidofa37f672017-02-16 10:29:46 -080062 cond_.wait(lk);
63 } else if (std::cv_status::timeout ==
64 cond_.wait_for(lk, std::chrono::nanoseconds(max_nanoseconds))) {
65 ret = -ETIMEDOUT;
Zach Reizner8467b122015-11-10 15:18:08 -080066 }
Sean Paulcb8676c2015-05-13 06:22:10 -070067
Adrian Salidofa37f672017-02-16 10:29:46 -080068 // exit takes precedence on timeout
69 if (should_exit())
70 ret = -EINTR;
71
Adrian Salidoba494c82017-04-25 13:23:45 -070072 // release leaves mutex locked when going out of scope
Adrian Salidofa37f672017-02-16 10:29:46 -080073 lk.release();
Sean Paulcb8676c2015-05-13 06:22:10 -070074
75 return ret;
76}
77
Adrian Salidofa37f672017-02-16 10:29:46 -080078void Worker::InternalRoutine() {
79 setpriority(PRIO_PROCESS, 0, priority_);
80 prctl(PR_SET_NAME, name_.c_str());
Sean Paulcb8676c2015-05-13 06:22:10 -070081
Adrian Salidofa37f672017-02-16 10:29:46 -080082 std::unique_lock<std::mutex> lk(mutex_, std::defer_lock);
Sean Paulcb8676c2015-05-13 06:22:10 -070083
84 while (true) {
Adrian Salidofa37f672017-02-16 10:29:46 -080085 lk.lock();
86 if (should_exit())
87 return;
88 lk.unlock();
Sean Paulcb8676c2015-05-13 06:22:10 -070089
Adrian Salidofa37f672017-02-16 10:29:46 -080090 Routine();
Sean Paulcb8676c2015-05-13 06:22:10 -070091 }
Sean Paulcb8676c2015-05-13 06:22:10 -070092}
Sean Paulf72cccd2018-08-27 13:59:08 -040093} // namespace android