blob: e38e103509fc5bd4513018e11d373145b3b3ad11 [file] [log] [blame]
fei.dengf7a0cd32023-08-29 09:36:37 +00001/*
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>
fei.dengb9a1a572023-09-13 01:33:57 +000023#include "Mutex.h"
24#include "Condition.h"
fei.dengf7a0cd32023-08-29 09:36:37 +000025
26typedef void* (*pthread_entry_func)(void*);
fei.dengdd910ef2024-06-07 10:25:30 +080027#define MAX_THREAD_NAME_LEN 16
fei.dengf7a0cd32023-08-29 09:36:37 +000028namespace Tls {
29class 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;
72private:
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
fei.dengb9a1a572023-09-13 01:33:57 +000077 pthread_t mThread;
fei.dengf7a0cd32023-08-29 09:36:37 +000078 char mThreadName[MAX_THREAD_NAME_LEN];
fei.dengb9a1a572023-09-13 01:33:57 +000079 mutable Tls::Mutex mLock;
80 Tls::Condition mCondition;
fei.dengf7a0cd32023-08-29 09:36:37 +000081 int mStatus;
82 // note that all accesses of mExitPending and mRunning need to hold mLock
fei.dengb9a1a572023-09-13 01:33:57 +000083 volatile bool mExitPending;
84 volatile bool mRunning;
fei.dengf7a0cd32023-08-29 09:36:37 +000085 int mPriority;
86};
87}
88
89
90
91
92#endif /*_TOOS_THREAD_H_*/