Apache Log4cxx  Version 2.0.0
Loading...
Searching...
No Matches
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>
28
29namespace LOG4CXX_NS
30{
31namespace helpers
32{
33
39using ThreadStartPre = std::function<void()>;
40
50using ThreadStarted = std::function
51 < void
52 ( LogString threadName
53 , std::thread::id threadId
54 , std::thread::native_handle_type nativeHandle
55 )
56 >;
57
62using ThreadStartPost = std::function<void()>;
63
71
72class LOG4CXX_EXPORT ThreadUtility
73{
74 private:
75 friend class SingletonHolder<ThreadUtility>;
76 ThreadUtility();
77
78 LOG4CXX_DECLARE_PRIVATE_MEMBER_PTR(priv_data, m_priv)
79 public:
81
82 static ThreadUtility* instance();
83
90
98 ( ThreadStartPre pre_start
99 , ThreadStarted set_name
100 , ThreadStartPost post_start
101 );
102
106 template<class Function, class... Args>
107 std::thread createThread
108 ( LogString name
109 , Function&& f
110 , Args&& ... args
111 )
112 {
113 ThreadStartContext ctx(this);
114 std::thread t( f, args... );
115 ctx.setThreadName(name, t.get_id(), t.native_handle());
116 return t;
117 }
118
119 using Period = std::chrono::milliseconds;
120
124 void addPeriodicTask(const LogString& taskName, std::function<void()> f, const Period& delay);
125
129 bool hasPeriodicTask(const LogString& taskName);
130
135
140
144 void removePeriodicTask(const LogString& taskName);
145
149 void removePeriodicTasksMatching(const LogString& namePrefix);
150
153
154 static ManagerPtr instancePtr();
155
156private:
157 struct LOG4CXX_EXPORT ThreadStartContext
158 {
159 LOG4CXX_DECLARE_PRIVATE_MEMBER_PTR(Impl, m_priv)
160 ThreadStartContext(ThreadUtility* parent);
161 ~ThreadStartContext();
162 void setThreadName
163 ( LogString threadName
164 , std::thread::id threadId
165 , std::thread::native_handle_type nativeHandle
166 ) const;
167 };
168};
169
170} /* namespace helpers */
171} /* namespace log4cxx */
172
173#endif
Wraps any singleton object so it can be added to APRInitializer.
Definition singletonholder.h:32
static ManagerPtr instancePtr()
void removePeriodicTasksMatching(const LogString &namePrefix)
Remove any periodic task matching namePrefix.
void removePeriodicTask(const LogString &taskName)
Remove the taskName periodic task.
SingletonHolder< ThreadUtility > Manager
Definition threadutility.h:151
bool isProcessingThreadActive() const
Is the processing thread running?
void addPeriodicTask(const LogString &taskName, std::function< void()> f, const Period &delay)
Add the taskName periodic task.
std::chrono::milliseconds Period
Definition threadutility.h:119
static ThreadUtility * instance()
ThreadConfigurationType configureFuncs(ThreadStartPre pre_start, ThreadStarted set_name, ThreadStartPost post_start)
Set call-back functions to use when starting a thread.
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:108
bool hasPeriodicTask(const LogString &taskName)
Has a taskName periodic task already been added?
static ThreadConfigurationType configure(ThreadConfigurationType new_value)
Use new_value as the behviour when starting a thread.
ThreadConfigurationType
Definition threadutility.h:65
@ NoConfiguration
Definition threadutility.h:66
@ BlockSignalsAndNameThread
Definition threadutility.h:69
@ NameThreadOnly
Definition threadutility.h:68
@ BlockSignalsOnly
Definition threadutility.h:67
std::function< void()> ThreadStartPre
A function that will be called before a thread is started.
Definition threadutility.h:39
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:50
std::function< void()> ThreadStartPost
Called after a thread has started.
Definition threadutility.h:62
std::basic_string< logchar > LogString
Definition logstring.h:51