blob: ab01d5ffde4e0757a2b570e9d5b8ac436b666352 [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
47 protected:
48 Worker(const char *name, int priority);
49 virtual ~Worker();
50
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
Sean Paulcb8676c2015-05-13 06:22:10 -070080#endif