blob: 74cfdc4b3a9271b26f4a1ccbc64af17d4478360f [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
17#ifndef ANDROID_WORKER_H_
18#define ANDROID_WORKER_H_
19
Adrian Salidofa37f672017-02-16 10:29:46 -080020#include <condition_variable>
Roman Stratiienkoe78235c2021-12-23 17:36:12 +020021#include <cstdint>
22#include <cstdlib>
Adrian Salidofa37f672017-02-16 10:29:46 -080023#include <mutex>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030024#include <string>
Adrian Salidofa37f672017-02-16 10:29:46 -080025#include <thread>
26
Sean Paulcb8676c2015-05-13 06:22:10 -070027namespace android {
28
29class Worker {
30 public:
Adrian Salidofa37f672017-02-16 10:29:46 -080031 void Lock() {
32 mutex_.lock();
33 }
34 void Unlock() {
35 mutex_.unlock();
36 }
Sean Paulcb8676c2015-05-13 06:22:10 -070037
Adrian Salidofa37f672017-02-16 10:29:46 -080038 void Signal() {
39 cond_.notify_all();
40 }
41 void Exit();
Sean Paulcb8676c2015-05-13 06:22:10 -070042
Adrian Salidofa37f672017-02-16 10:29:46 -080043 bool initialized() const {
44 return initialized_;
45 }
Sean Paulcb8676c2015-05-13 06:22:10 -070046
Roman Stratiienkocf80b9b2022-04-28 15:42:36 +030047 virtual ~Worker();
48
Sean Paulcb8676c2015-05-13 06:22:10 -070049 protected:
50 Worker(const char *name, int priority);
Sean Paulcb8676c2015-05-13 06:22:10 -070051
52 int InitWorker();
Sean Paulcb8676c2015-05-13 06:22:10 -070053 virtual void Routine() = 0;
54
55 /*
Zach Reizner8467b122015-11-10 15:18:08 -080056 * Must be called with the lock acquired. max_nanoseconds may be negative to
57 * indicate infinite timeout, otherwise it indicates the maximum time span to
58 * wait for a signal before returning.
59 * Returns -EINTR if interrupted by exit request, or -ETIMEDOUT if timed out
Sean Paulcb8676c2015-05-13 06:22:10 -070060 */
Zach Reizner8467b122015-11-10 15:18:08 -080061 int WaitForSignalOrExitLocked(int64_t max_nanoseconds = -1);
Sean Paulcb8676c2015-05-13 06:22:10 -070062
Adrian Salidofa37f672017-02-16 10:29:46 -080063 bool should_exit() const {
64 return exit_;
65 }
Sean Paulcb8676c2015-05-13 06:22:10 -070066
Adrian Salidofa37f672017-02-16 10:29:46 -080067 std::mutex mutex_;
68 std::condition_variable cond_;
69
70 private:
71 void InternalRoutine();
Sean Paulcb8676c2015-05-13 06:22:10 -070072
73 std::string name_;
74 int priority_;
75
Adrian Salidofa37f672017-02-16 10:29:46 -080076 std::unique_ptr<std::thread> thread_;
Sean Paulcb8676c2015-05-13 06:22:10 -070077 bool exit_;
78 bool initialized_;
79};
Sean Paulf72cccd2018-08-27 13:59:08 -040080} // namespace android
Sean Paulcb8676c2015-05-13 06:22:10 -070081#endif