blob: 47aeb8608f0d86634b85c10af2e6bd4f8e503976 [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 Salidofa37f672017-02-16 10:29:46 -080033 if (initialized())
34 return -EALREADY;
Sean Paulcb8676c2015-05-13 06:22:10 -070035
Adrian Salidofa37f672017-02-16 10:29:46 -080036 thread_ = std::unique_ptr<std::thread>(
37 new std::thread(&Worker::InternalRoutine, this));
Sean Paulcb8676c2015-05-13 06:22:10 -070038 initialized_ = true;
Adrian Salidofa37f672017-02-16 10:29:46 -080039
Sean Paulcb8676c2015-05-13 06:22:10 -070040 return 0;
41}
42
Adrian Salidofa37f672017-02-16 10:29:46 -080043void Worker::Exit() {
44 if (initialized()) {
45 Lock();
46 exit_ = true;
47 Unlock();
48 cond_.notify_all();
49 thread_->join();
50 initialized_ = false;
Sean Paulcb8676c2015-05-13 06:22:10 -070051 }
Sean Paulcb8676c2015-05-13 06:22:10 -070052}
53
Zach Reizner8467b122015-11-10 15:18:08 -080054int Worker::WaitForSignalOrExitLocked(int64_t max_nanoseconds) {
Adrian Salidofa37f672017-02-16 10:29:46 -080055 int ret = 0;
56 if (should_exit())
Sean Paulcb8676c2015-05-13 06:22:10 -070057 return -EINTR;
58
Adrian Salidofa37f672017-02-16 10:29:46 -080059 std::unique_lock<std::mutex> lk(mutex_, std::adopt_lock);
Zach Reizner8467b122015-11-10 15:18:08 -080060 if (max_nanoseconds < 0) {
Adrian Salidofa37f672017-02-16 10:29:46 -080061 cond_.wait(lk);
62 } else if (std::cv_status::timeout ==
63 cond_.wait_for(lk, std::chrono::nanoseconds(max_nanoseconds))) {
64 ret = -ETIMEDOUT;
Zach Reizner8467b122015-11-10 15:18:08 -080065 }
Sean Paulcb8676c2015-05-13 06:22:10 -070066
Adrian Salidofa37f672017-02-16 10:29:46 -080067 // exit takes precedence on timeout
68 if (should_exit())
69 ret = -EINTR;
70
71 // release leaves lock unlocked when returning
72 lk.release();
Sean Paulcb8676c2015-05-13 06:22:10 -070073
74 return ret;
75}
76
Adrian Salidofa37f672017-02-16 10:29:46 -080077void Worker::InternalRoutine() {
78 setpriority(PRIO_PROCESS, 0, priority_);
79 prctl(PR_SET_NAME, name_.c_str());
Sean Paulcb8676c2015-05-13 06:22:10 -070080
Adrian Salidofa37f672017-02-16 10:29:46 -080081 std::unique_lock<std::mutex> lk(mutex_, std::defer_lock);
Sean Paulcb8676c2015-05-13 06:22:10 -070082
83 while (true) {
Adrian Salidofa37f672017-02-16 10:29:46 -080084 lk.lock();
85 if (should_exit())
86 return;
87 lk.unlock();
Sean Paulcb8676c2015-05-13 06:22:10 -070088
Adrian Salidofa37f672017-02-16 10:29:46 -080089 Routine();
Sean Paulcb8676c2015-05-13 06:22:10 -070090 }
Sean Paulcb8676c2015-05-13 06:22:10 -070091}
92}