1cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul/* 2cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul * Copyright (C) 2015 The Android Open Source Project 3cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul * 4cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul * Licensed under the Apache License, Version 2.0 (the "License"); 5cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul * you may not use this file except in compliance with the License. 6cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul * You may obtain a copy of the License at 7cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul * 8cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul * http://www.apache.org/licenses/LICENSE-2.0 9cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul * 10cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul * Unless required by applicable law or agreed to in writing, software 11cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul * distributed under the License is distributed on an "AS IS" BASIS, 12cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul * See the License for the specific language governing permissions and 14cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul * limitations under the License. 15cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul */ 16cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul 17cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul#ifndef ANDROID_WORKER_H_ 18cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul#define ANDROID_WORKER_H_ 19cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul 20cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul#include <pthread.h> 218467b129419b0f78974bd478809ad32da4fd09faZach Reizner#include <stdint.h> 22cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul#include <string> 23cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul 24cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paulnamespace android { 25cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul 26cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paulclass Worker { 27cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul public: 28cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul int Lock(); 29cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul int Unlock(); 30cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul 31cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul // Must be called with the lock acquired 32cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul int SignalLocked(); 33cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul int ExitLocked(); 34cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul 35cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul // Convenience versions of above, acquires the lock 36cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul int Signal(); 37cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul int Exit(); 38cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul 39cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul protected: 40cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul Worker(const char *name, int priority); 41cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul virtual ~Worker(); 42cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul 43cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul int InitWorker(); 44cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul 45cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul bool initialized() const; 46cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul 47cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul virtual void Routine() = 0; 48cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul 49cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul /* 508467b129419b0f78974bd478809ad32da4fd09faZach Reizner * Must be called with the lock acquired. max_nanoseconds may be negative to 518467b129419b0f78974bd478809ad32da4fd09faZach Reizner * indicate infinite timeout, otherwise it indicates the maximum time span to 528467b129419b0f78974bd478809ad32da4fd09faZach Reizner * wait for a signal before returning. 538467b129419b0f78974bd478809ad32da4fd09faZach Reizner * Returns -EINTR if interrupted by exit request, or -ETIMEDOUT if timed out 54cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul */ 558467b129419b0f78974bd478809ad32da4fd09faZach Reizner int WaitForSignalOrExitLocked(int64_t max_nanoseconds = -1); 56cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul 57cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul private: 58cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul static void *InternalRoutine(void *worker); 59cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul 60cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul // Must be called with the lock acquired 61cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul int SignalThreadLocked(bool exit); 62cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul 63cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul std::string name_; 64cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul int priority_; 65cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul 66cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul pthread_t thread_; 67cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul pthread_mutex_t lock_; 68cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul pthread_cond_t cond_; 69cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul 70cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul bool exit_; 71cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul bool initialized_; 72cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul}; 73cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul} 74cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul 75cb8676c6dffe8ef0577ec4c367ad810a291ed390Sean Paul#endif 76