CUBRID Engine  latest
thread_entry_task.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * thread_entry_task.hpp
21  */
22 
23 #ifndef _THREAD_ENTRY_TASK_HPP_
24 #define _THREAD_ENTRY_TASK_HPP_
25 
26 #if !defined (SERVER_MODE) && !defined (SA_MODE)
27 #error Wrong module
28 #endif // not SERVER_MODE and not SA_MODE
29 
30 #include "thread_task.hpp"
31 
32 #include <cassert>
33 
34 namespace cubthread
35 {
36 
37  // forward definition
38  class entry;
39 
40  // cubthread::entry_task
41  //
42  // description:
43  // entry_task class is a cubthread::task specialization using entry as Context; see thread_task.hpp
44  // class is friend of cubthread::manager and cannot be used directly with cubthread::daemon or
45  // cubthread::worker_pool
46  //
47  // how to use:
48  // 1. extend entry_task and override virtual functions
49  // override execute (entry &) function to define execution
50  // [optional] override create_context, retire_context and retire functions (see thread_task.hpp)
51  //
52  // 2. provide custom task to manager and either:
53  // 2.1. create a daemon to loop and execute task repeatedly
54  // 2.2. pass it to a worker pool to be executed once
55  // see thread_manager.hpp, thread_daemon.hpp and thread_worker_pool.hpp for more details
56  //
59 
60  // cubthread::entry_manager
61  //
62  // description:
63  // entry_manager abstract class is a cubthread::context_manager specialization using entry as Context.
64  // the entry pool is managed by thread_manager. entry_manager acts as an base for specialized context managers
65  // that may need to do additional operations on entry contexts during create, retire and recycle context
66  // operations.
67  //
68  // how to use:
69  // create a context manager derived from entry manager and override on_create, on_retire and on_recycle functions.
70  // create worker pools using derived context manager
71  //
72  class entry_manager : public context_manager<entry>
73  {
74  public:
75  entry_manager (void) = default;
76 
77  entry &create_context (void) final;
78  void retire_context (entry &context) final;
79  void recycle_context (entry &context) final;
80  void stop_execution (entry &context) override;
81 
82  protected:
83 
84  virtual void on_create (context_type &) // manipulate entry after claim and before any execution
85  {
86  // does nothing by default
87  }
88  virtual void on_retire (context_type &) // manipulate entry before retire; should mirror on_create
89  {
90  // does nothing by default
91  }
92  virtual void on_recycle (context_type &) // manipulate entry between execution cycles
93  {
94  // does nothing by default
95  }
96  };
97 
98  // cubthread::daemon_entry_manager
99  //
100  // description:
101  // daemon_entry_manager is derived from entry_manager and adds extra logic specific for daemon threads
102  // initialization and destruction. for more details see entry_manager description.
103  //
104  // how to use:
105  // create a daemon_entry_manager derived from entry_manager and override on_daemon_create and on on_daemon_retire
106  // functions if daemon require custom logic on initialization or destruction.
107  // create daemon threads using daemon_entry_manager class
108  //
110  {
111  public:
112  daemon_entry_manager () = default;
113  ~daemon_entry_manager () = default;
114 
115  protected:
116  virtual void on_daemon_create (entry &)
117  {
118  // does nothing by default
119  }
120 
121  void on_create (entry &) final;
122 
123  virtual void on_daemon_retire (entry &)
124  {
125  // does nothing by default
126  }
127 
128  void on_retire (entry &) final;
129 
130  void on_recycle (entry &) final
131  {
132  // daemon threads are not recycled
133  }
134  };
135 
136 } // namespace cubthread
137 
138 #endif // _THREAD_ENTRY_TASK_HPP_
void on_recycle(entry &) final
void stop_execution(entry &context) override
void recycle_context(entry &context) final
entry_manager(void)=default
virtual void on_daemon_retire(entry &)
void retire_context(entry &context) final
virtual void on_retire(context_type &)
virtual void on_recycle(context_type &)
entry & create_context(void) final
virtual void on_daemon_create(entry &)
virtual void on_create(context_type &)