Apache log4cxx  Version 0.11.0
mutex.h
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_HELPERS_MUTEX_H
19 #define _LOG4CXX_HELPERS_MUTEX_H
20 
21 #include <log4cxx/log4cxx.h>
22 
23 #if defined(RW_MUTEX)
24  #include <apr_portable.h>
25  #include <atomic>
26 #endif
27 
28 extern "C" {
29  struct apr_thread_mutex_t;
30  struct apr_pool_t;
31  struct apr_thread_rwlock_t;
32 }
33 
34 
35 namespace log4cxx
36 {
37 namespace helpers
38 {
39 class Pool;
40 
41 class LOG4CXX_EXPORT Mutex
42 {
43  public:
45  Mutex(apr_pool_t* p);
46  ~Mutex();
47  apr_thread_mutex_t* getAPRMutex() const;
48 
49  private:
50  Mutex(const Mutex&);
51  Mutex& operator=(const Mutex&);
52  apr_thread_mutex_t* mutex;
53 };
54 } // namespace helpers
55 } // namespace log4cxx
56 
57 
58 #if defined(RW_MUTEX)
59 
60 namespace log4cxx
61 {
62 namespace helpers
63 {
64 class Pool;
65 
66 class LOG4CXX_EXPORT RWMutex
67 {
68  public:
69  RWMutex(log4cxx::helpers::Pool& p);
70  RWMutex(apr_pool_t* p);
71  ~RWMutex();
72 
73  void rdLock() const;
74  void rdUnlock() const;
75 
76  void wrLock() const;
77  void wrUnlock() const;
78 
79  private:
80  mutable std::atomic<apr_os_thread_t> id;
81  mutable unsigned count;
82  RWMutex(const RWMutex&);
83  RWMutex& operator=(const RWMutex&);
84  apr_thread_rwlock_t* mutex;
85 };
86 } // namespace helpers
87 } // namespace log4cxx
88 
89 #define SHARED_MUTEX log4cxx::helpers::RWMutex
90 
91 #else // no RW_MUTEX
92 
93 #define SHARED_MUTEX log4cxx::helpers::Mutex
94 
95 #endif // RW_MUTEX
96 
97 #define SHARED_MUTEX_INIT(mutex, p) mutex(p)
98 
99 #if defined(NON_BLOCKING)
100 
101 namespace log4cxx
102 {
103 namespace helpers
104 {
105 struct SemaphoreImpl;
106 
107 class LOG4CXX_EXPORT Semaphore
108 {
109  public:
110  Semaphore(log4cxx::helpers::Pool& p);
111  ~Semaphore();
112 
113  void await() const;
114  void signalAll() const;
115 
116  private:
117  Semaphore(const Semaphore&);
118  Semaphore& operator=(const Semaphore&);
119 
120  SemaphoreImpl* impl;
121 };
122 } // namespace helpers
123 } // namespace log4cxx
124 
125 #endif // NON_BLOCKING
126 
127 #endif //_LOG4CXX_HELPERS_MUTEX_H
Definition: mutex.h:41
Definition: pool.h:32
Definition: appender.h:33