• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2011 The Libphonenumber Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Author: Philippe Liard
16 
17 #ifndef I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_H_
18 #define I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_H_
19 
20 #if defined(I18N_PHONENUMBERS_USE_BOOST)
21 #include <boost/thread/mutex.hpp>
22 
23 namespace i18n {
24 namespace phonenumbers {
25 
26 typedef boost::mutex Lock;
27 typedef boost::mutex::scoped_lock AutoLock;
28 
29 }  // namespace phonenumbers
30 }  // namespace i18n
31 
32 #else  // I18N_PHONENUMBERS_USE_BOOST
33 
34 #include "phonenumbers/base/logging.h"
35 #include "phonenumbers/base/thread_checker.h"
36 
37 // C++11 Lock implementation based on std::mutex.
38 #if  __cplusplus>=201103L
39 #include <mutex>
40 
41 namespace i18n {
42 namespace phonenumbers {
43 
44 class Lock {
45 public:
46   Lock() = default;
47 
Acquire()48   void Acquire() const {
49     mutex_.lock();
50   }
51 
Release()52   void Release() const {
53     mutex_.unlock();
54   }
55 
56 private:
57   mutable std::mutex mutex_;
58 };
59 
60 }  // namespace phonenumbers
61 }  // namespace i18n
62 
63 // Dummy lock implementation on non-POSIX platforms. If you are running on a
64 // different platform and care about thread-safety, please compile with
65 // -DI18N_PHONENUMBERS_USE_BOOST.
66 #elif !defined(__linux__) && !defined(__APPLE__)
67 
68 namespace i18n {
69 namespace phonenumbers {
70 
71 class Lock {
72  public:
Lock()73   Lock() {}
74 
Acquire()75   void Acquire() const {
76     DCHECK(thread_checker_.CalledOnValidThread());
77     IGNORE_UNUSED(thread_checker_);
78   }
79 
Release()80   void Release() const {
81     DCHECK(thread_checker_.CalledOnValidThread());
82     IGNORE_UNUSED(thread_checker_);
83   }
84 
85  private:
86   const ThreadChecker thread_checker_;
87 };
88 
89 }  // namespace phonenumbers
90 }  // namespace i18n
91 
92 #else
93 #include "phonenumbers/base/synchronization/lock_posix.h"
94 #endif
95 
96 namespace i18n {
97 namespace phonenumbers {
98 
99 class AutoLock {
100  public:
AutoLock(Lock & lock)101   AutoLock(Lock& lock) : lock_(lock) {
102     lock_.Acquire();
103   }
104 
~AutoLock()105   ~AutoLock() {
106     lock_.Release();
107   }
108 
109  private:
110   Lock& lock_;
111 };
112 
113 }  // namespace phonenumbers
114 }  // namespace i18n
115 
116 #endif  // I18N_PHONENUMBERS_USE_BOOST
117 #endif  // I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_H_
118