fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2021 Amlogic Corporation. |
| 3 | * |
| 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 | #ifndef _TOOS_THREAD_H_ |
| 17 | #define _TOOS_THREAD_H_ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <unistd.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <pthread.h> |
| 23 | #include <mutex> |
| 24 | #include <condition_variable> |
| 25 | |
| 26 | typedef void* (*pthread_entry_func)(void*); |
| 27 | #define MAX_THREAD_NAME_LEN 128 |
| 28 | namespace Tls { |
| 29 | class Thread |
| 30 | { |
| 31 | public: |
| 32 | explicit Thread(); |
| 33 | virtual ~Thread(); |
| 34 | // Start the thread in threadLoop() which needs to be implemented. |
| 35 | virtual int run(const char *name); |
| 36 | |
| 37 | // Good place to do one-time initializations |
| 38 | virtual void readyToRun(); |
| 39 | // Good place to do one-time exit |
| 40 | virtual void readyToExit(); |
| 41 | |
| 42 | // Ask this object's thread to exit. This function is asynchronous, when the |
| 43 | // function returns the thread might still be running. Of course, this |
| 44 | // function can be called from a different thread. |
| 45 | virtual void requestExit(); |
| 46 | // Call requestExit() and wait until this object's thread exits. |
| 47 | // BE VERY CAREFUL of deadlocks. In particular, it would be silly to call |
| 48 | // this function from this object's thread. Will return WOULD_BLOCK in |
| 49 | // that case. |
| 50 | int requestExitAndWait(); |
| 51 | // Wait until this object's thread exits. Returns immediately if not yet running. |
| 52 | // Do not call from this object's thread; will return WOULD_BLOCK in that case. |
| 53 | int join(); |
| 54 | // Indicates whether this thread is running or not. |
| 55 | bool isRunning() const; |
| 56 | |
| 57 | //set thread priority from 1 ~ 99, this will set |
| 58 | //thread policy to SCHED_RR,this api must be called before |
| 59 | //calling run api |
| 60 | void setThreadPriority(int priority); |
| 61 | |
| 62 | protected: |
| 63 | // isExitPending() returns true if requestExit() has been called. |
| 64 | bool isExitPending() const; |
| 65 | private: |
| 66 | // Derived class must implement threadLoop(). The thread starts its life |
| 67 | // here. There are two ways of using the Thread object: |
| 68 | // 1) loop: if threadLoop() returns true, it will be called again if |
| 69 | // requestExit() wasn't called. |
| 70 | // 2) once: if threadLoop() returns false, the thread will exit upon return. |
| 71 | virtual bool threadLoop() = 0; |
| 72 | private: |
| 73 | Thread& operator=(const Thread&); |
| 74 | static void* _threadLoop(void* user); |
| 75 | int _createThread(pthread_entry_func entryFunction); |
| 76 | // always hold mLock when reading or writing |
| 77 | pthread_t mThread; |
| 78 | char mThreadName[MAX_THREAD_NAME_LEN]; |
| 79 | mutable std::mutex mMutex; |
| 80 | std::condition_variable mCond; |
| 81 | int mStatus; |
| 82 | // note that all accesses of mExitPending and mRunning need to hold mLock |
| 83 | volatile bool mExitPending; |
| 84 | volatile bool mRunning; |
| 85 | int mPriority; |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | #endif /*_TOOS_THREAD_H_*/ |