Apache Log4cxx  Version 1.5.0
threadutility.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 #ifndef _LOG4CXX_THREADUTILITY_H
19 #define _LOG4CXX_THREADUTILITY_H
20 
21 #include <thread>
22 #include <functional>
23 #include <memory>
24 #include <chrono>
25 
26 #include "log4cxx/logstring.h"
27 #include "singletonholder.h"
28 
29 namespace LOG4CXX_NS
30 {
31 namespace helpers
32 {
33 
39 typedef std::function<void()> ThreadStartPre;
40 
50 typedef std::function<void( LogString threadName,
51  std::thread::id threadId,
52  std::thread::native_handle_type nativeHandle )> ThreadStarted;
53 
58 typedef std::function<void()> ThreadStartPost;
59 
61 {
66 };
67 
68 class LOG4CXX_EXPORT ThreadUtility
69 {
70  private:
71  friend class SingletonHolder<ThreadUtility>;
72  ThreadUtility();
73 
74  LOG4CXX_NS::helpers::ThreadStartPre preStartFunction();
75  LOG4CXX_NS::helpers::ThreadStarted threadStartedFunction();
76  LOG4CXX_NS::helpers::ThreadStartPost postStartFunction();
77 
78  LOG4CXX_DECLARE_PRIVATE_MEMBER_PTR(priv_data, m_priv)
79  public:
81 
83 
88  static void configure( ThreadConfigurationType type );
89 
95  void configureFuncs( ThreadStartPre pre_start,
96  ThreadStarted started,
97  ThreadStartPost post_start );
98 
105 
111  std::thread::id thread_id,
112  std::thread::native_handle_type native_handle);
113 
120 
124  template<class Function, class... Args>
125  std::thread createThread(LogString name,
126  Function&& f,
127  Args&& ... args)
128  {
129  LOG4CXX_NS::helpers::ThreadStartPre pre_start = preStartFunction();
130  LOG4CXX_NS::helpers::ThreadStarted thread_start = threadStartedFunction();
131  LOG4CXX_NS::helpers::ThreadStartPost post_start = postStartFunction();
132 
133  if ( pre_start )
134  {
135  pre_start();
136  }
137 
138  std::thread t( f, args... );
139 
140  if ( thread_start )
141  {
142  thread_start( name,
143  t.get_id(),
144  t.native_handle() );
145  }
146 
147  if ( post_start )
148  {
149  post_start();
150  }
151 
152  return t;
153  }
154 
155  using Period = std::chrono::milliseconds;
156 
160  void addPeriodicTask(const LogString& taskName, std::function<void()> f, const Period& delay);
161 
165  bool hasPeriodicTask(const LogString& taskName);
166 
171 
175  void removePeriodicTask(const LogString& taskName);
176 
180  void removePeriodicTasksMatching(const LogString& namePrefix);
181 
184 
185  static ManagerPtr instancePtr();
186 };
187 
188 } /* namespace helpers */
189 } /* namespace log4cxx */
190 
191 #endif
Wraps any singleton object so it can be added to APRInitializer.
Definition: singletonholder.h:32
Definition: threadutility.h:69
static ManagerPtr instancePtr()
void removePeriodicTasksMatching(const LogString &namePrefix)
Remove any periodic task matching namePrefix.
void configureFuncs(ThreadStartPre pre_start, ThreadStarted started, ThreadStartPost post_start)
Configure the thread functions that log4cxx will use.
void removePeriodicTask(const LogString &taskName)
Remove the taskName periodic task.
void preThreadBlockSignals()
A pre-start thread function that blocks signals to the new thread (if the system has pthreads).
void threadStartedNameThread(LogString threadName, std::thread::id thread_id, std::thread::native_handle_type native_handle)
A thread_started function that names the thread using the appropriate system call.
void addPeriodicTask(const LogString &taskName, std::function< void()> f, const Period &delay)
Add the taskName periodic task.
std::chrono::milliseconds Period
Definition: threadutility.h:155
static ThreadUtility * instance()
void removeAllPeriodicTasks()
Remove all periodic tasks and stop the processing thread.
std::thread createThread(LogString name, Function &&f, Args &&... args)
Start a thread.
Definition: threadutility.h:125
bool hasPeriodicTask(const LogString &taskName)
Has a taskName periodic task already been added?
void postThreadUnblockSignals()
A post-start thread function that unblocks signals that preThreadBlockSignals blocked before starting...
static void configure(ThreadConfigurationType type)
Utility method for configuring the ThreadUtility in a standard configuration.
std::function< void()> ThreadStartPost
Called after a thread has started.
Definition: threadutility.h:58
ThreadConfigurationType
Definition: threadutility.h:61
std::function< void(LogString threadName, std::thread::id threadId, std::thread::native_handle_type nativeHandle)> ThreadStarted
Called when a new thread has started.
Definition: threadutility.h:52
std::function< void()> ThreadStartPre
A function that will be called before a thread is started.
Definition: threadutility.h:39
std::basic_string< logchar > LogString
Definition: logstring.h:60