blob: b524d370abae46f8a50f799a729d0b15af2832a0 [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 Stratiienkobde95662022-12-10 20:27:58 +020017#pragma once
Sean Paulcb8676c2015-05-13 06:22:10 -070018
Adrian Salidofa37f672017-02-16 10:29:46 -080019#include <condition_variable>
Roman Stratiienkoe78235c2021-12-23 17:36:12 +020020#include <cstdint>
21#include <cstdlib>
Adrian Salidofa37f672017-02-16 10:29:46 -080022#include <mutex>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030023#include <string>
Adrian Salidofa37f672017-02-16 10:29:46 -080024#include <thread>
25
Sean Paulcb8676c2015-05-13 06:22:10 -070026namespace android {
27
28class Worker {
29 public:
Adrian Salidofa37f672017-02-16 10:29:46 -080030 void Lock() {
31 mutex_.lock();
32 }
33 void Unlock() {
34 mutex_.unlock();
35 }
Sean Paulcb8676c2015-05-13 06:22:10 -070036
Adrian Salidofa37f672017-02-16 10:29:46 -080037 void Signal() {
38 cond_.notify_all();
39 }
40 void Exit();
Sean Paulcb8676c2015-05-13 06:22:10 -070041
Adrian Salidofa37f672017-02-16 10:29:46 -080042 bool initialized() const {
43 return initialized_;
44 }
Sean Paulcb8676c2015-05-13 06:22:10 -070045
Roman Stratiienkocf80b9b2022-04-28 15:42:36 +030046 virtual ~Worker();
47
Sean Paulcb8676c2015-05-13 06:22:10 -070048 protected:
49 Worker(const char *name, int priority);
Sean Paulcb8676c2015-05-13 06:22:10 -070050
51 int InitWorker();
Sean Paulcb8676c2015-05-13 06:22:10 -070052 virtual void Routine() = 0;
53
54 /*
Zach Reizner8467b122015-11-10 15:18:08 -080055 * Must be called with the lock acquired. max_nanoseconds may be negative to
56 * indicate infinite timeout, otherwise it indicates the maximum time span to
57 * wait for a signal before returning.
58 * Returns -EINTR if interrupted by exit request, or -ETIMEDOUT if timed out
Sean Paulcb8676c2015-05-13 06:22:10 -070059 */
Zach Reizner8467b122015-11-10 15:18:08 -080060 int WaitForSignalOrExitLocked(int64_t max_nanoseconds = -1);
Sean Paulcb8676c2015-05-13 06:22:10 -070061
Adrian Salidofa37f672017-02-16 10:29:46 -080062 bool should_exit() const {
63 return exit_;
64 }
Sean Paulcb8676c2015-05-13 06:22:10 -070065
Adrian Salidofa37f672017-02-16 10:29:46 -080066 std::mutex mutex_;
67 std::condition_variable cond_;
68
69 private:
70 void InternalRoutine();
Sean Paulcb8676c2015-05-13 06:22:10 -070071
72 std::string name_;
73 int priority_;
74
Adrian Salidofa37f672017-02-16 10:29:46 -080075 std::unique_ptr<std::thread> thread_;
Sean Paulcb8676c2015-05-13 06:22:10 -070076 bool exit_;
77 bool initialized_;
78};
Sean Paulf72cccd2018-08-27 13:59:08 -040079} // namespace android