• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_APP_DISPATCHER_THREADING_WORKER_THREAD_H
17 #define OHOS_APP_DISPATCHER_THREADING_WORKER_THREAD_H
18 
19 #include <atomic>
20 
21 #include "thread_factory.h"
22 #include "task.h"
23 #include "hilog_wrapper.h"
24 #include "appexecfwk_errors.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 class WorkerThread;
29 /**
30  *  Interface for a work delegate.
31  */
32 class Delegate {
33 public:
34     /**
35      *  Do works for WorkerThread.
36      *
37      *  @param worker is the thread-holder.
38      *
39      */
40     virtual ErrCode DoWorks(const std::shared_ptr<WorkerThread> &worker) = 0;
41 };
42 
43 /**
44  *  WorkerThread is a thread with a loop which can execute incoming tasks.
45  */
46 class WorkerThread : public std::enable_shared_from_this<WorkerThread> {
47 public:
48     /**
49      *  The construct function of the WorkerThread class.
50      *
51      *  @param delegate delegates run works for this thread.
52      *  @param firstTask if not null, take it as the first task in this thread. It can be null.
53      *  @param factory can create a new thread which had some attributes set.
54      */
55     WorkerThread(const std::shared_ptr<Delegate> &delegate, const std::shared_ptr<Task> &firstTask,
56         const std::shared_ptr<ThreadFactory> &factory);
~WorkerThread()57     virtual ~WorkerThread(){};
58 
59     void CreateThread();
60 
61     /**
62      *  Increase the task counter of self.
63      *
64      */
65     void IncTaskCount(void);
66 
67     /**
68      *  Gets the thread name.
69      *
70      *  @return The thread name.
71      *
72      */
73     std::string GetThreadName(void);
74 
75     /**
76      *  Get and remove |firstTask| of self.
77      *
78      *  @return the |firstTask|.
79      *
80      */
81     std::shared_ptr<Task> PollFirstTask(void);
82 
83     /**
84      *  Get count of tasks which were done.
85      *
86      *  @return the count of tasks which were done.
87      *
88      */
89     long GetTaskCounter(void);
90 
91     std::shared_ptr<Thread> GetThread(void);
92 
93     /**
94      *  join thread
95      *
96      */
97     void Join();
98 
99 protected:
100     std::shared_ptr<Thread> thread_;
101     std::atomic_long task_counter_;
102 
103 private:
104     std::shared_ptr<Task> first_task_;
105     std::shared_ptr<Delegate> delegate_;
106     std::shared_ptr<ThreadFactory> factory_;
107 };
108 }  // namespace AppExecFwk
109 }  // namespace OHOS
110 #endif
111