• 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 // Dummy lock implementation on non-POSIX platforms. If you are running on a
38 // different platform and care about thread-safety, please compile with
39 // -DI18N_PHONENUMBERS_USE_BOOST.
40 #if !defined(__linux__) && !defined(__APPLE__)
41 
42 namespace i18n {
43 namespace phonenumbers {
44 
45 class Lock {
46  public:
Lock()47   Lock() : thread_checker_() {}
48 
Acquire()49   void Acquire() const {
50     DCHECK(thread_checker_.CalledOnValidThread());
51   }
52 
Release()53   void Release() const {
54     DCHECK(thread_checker_.CalledOnValidThread());
55   }
56 
57  private:
58   const ThreadChecker thread_checker_;
59 };
60 
61 }  // namespace phonenumbers
62 }  // namespace i18n
63 
64 #else
65 #include "phonenumbers/base/synchronization/lock_posix.h"
66 #endif
67 
68 namespace i18n {
69 namespace phonenumbers {
70 
71 class AutoLock {
72  public:
AutoLock(Lock & lock)73   AutoLock(Lock& lock) : lock_(lock) {
74     lock_.Acquire();
75   }
76 
~AutoLock()77   ~AutoLock() {
78     lock_.Release();
79   }
80 
81  private:
82   Lock& lock_;
83 };
84 
85 }  // namespace phonenumbers
86 }  // namespace i18n
87 
88 #endif  // I18N_PHONENUMBERS_USE_BOOST
89 #endif  // I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_H_
90