Apache log4cxx  Version 0.13.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 
25 #include "log4cxx/logstring.h"
26 
27 namespace log4cxx {
28 namespace helpers {
29 
35 typedef std::function<void()> ThreadStartPre;
36 
46 typedef std::function<void( LogString threadName,
47  std::thread::id threadId,
48  std::thread::native_handle_type nativeHandle )> ThreadStarted;
49 
54 typedef std::function<void()> ThreadStartPost;
55 
61 };
62 
63 class ThreadUtility;
65 
66 class LOG4CXX_EXPORT ThreadUtility {
67 private:
68  ThreadUtility();
69 
70  log4cxx::helpers::ThreadStartPre preStartFunction();
71  log4cxx::helpers::ThreadStarted threadStartedFunction();
72  log4cxx::helpers::ThreadStartPost postStartFunction();
73 
74  struct priv_data;
75  std::unique_ptr<priv_data> m_priv;
76 
77 public:
78  ~ThreadUtility();
79 
80  static std::shared_ptr<ThreadUtility> instance();
81 
86  static void configure( ThreadConfigurationType type );
87 
93  void configureFuncs( ThreadStartPre pre_start,
94  ThreadStarted started,
95  ThreadStartPost post_start );
96 
102  void preThreadBlockSignals();
103 
108  void threadStartedNameThread(LogString threadName,
109  std::thread::id thread_id,
110  std::thread::native_handle_type native_handle);
111 
117  void postThreadUnblockSignals();
118 
122  template<class Function, class... Args>
123  std::thread createThread(LogString name,
124  Function&& f,
125  Args&&... args){
126  log4cxx::helpers::ThreadStartPre pre_start = preStartFunction();
127  log4cxx::helpers::ThreadStarted thread_start = threadStartedFunction();
128  log4cxx::helpers::ThreadStartPost post_start = postStartFunction();
129 
130  if( pre_start ){
131  pre_start();
132  }
133  std::thread t( f, args... );
134  if( thread_start ){
135  thread_start( name,
136  t.get_id(),
137  t.native_handle() );
138  }
139  if( post_start ){
140  post_start();
141  }
142  return t;
143  }
144 };
145 
146 } /* namespace helpers */
147 } /* namespace log4cxx */
148 
149 #endif
ThreadConfigurationType
Definition: threadutility.h:56
LOG4CXX_PTR_DEF(AppenderAttachableImpl)
std::thread createThread(LogString name, Function &&f, Args &&... args)
Start a thread.
Definition: threadutility.h:123
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:48
std::function< void()> ThreadStartPre
A function that will be called before a thread is started.
Definition: threadutility.h:35
Definition: threadutility.h:66
std::function< void()> ThreadStartPost
Called after a thread has started.
Definition: threadutility.h:54
Definition: appender.h:32
std::basic_string< logchar > LogString
Definition: logstring.h:66