1// Copyright (C) 2013 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_THREAD_CHECKER_H_
18#define I18N_PHONENUMBERS_BASE_THREAD_CHECKER_H_
19
20#if !defined(I18N_PHONENUMBERS_USE_BOOST)
21
22// Note that I18N_PHONENUMBERS_NO_THREAD_SAFETY must be defined only to let the
23// user of the library know that it can't be used in a thread-safe manner when
24// it is not depending on Boost.
25#if !defined(__linux__) && !defined(__APPLE__) && !defined(I18N_PHONENUMBERS_HAVE_POSIX_THREAD) && \
26    !defined(I18N_PHONENUMBERS_NO_THREAD_SAFETY) && \
27	!((__cplusplus >= 201103L) && defined(I18N_PHONENUMBERS_USE_STDMUTEX)) && \
28	!defined(WIN32)
29#error Building without Boost, please provide \
30       -DI18N_PHONENUMBERS_NO_THREAD_SAFETY
31#endif
32
33#endif
34
35#if !defined(NDEBUG) && !defined(I18N_PHONENUMBERS_USE_BOOST) && \
36    (defined(__linux__) || defined(__APPLE__) || defined(I18N_PHONENUMBERS_HAVE_POSIX_THREAD))
37
38#include <pthread.h>
39
40namespace i18n {
41namespace phonenumbers {
42
43class ThreadChecker {
44 public:
45  ThreadChecker() : thread_id_(pthread_self()) {}
46
47  bool CalledOnValidThread() const {
48    return thread_id_ == pthread_self();
49  }
50
51 private:
52  const pthread_t thread_id_;
53};
54
55#else
56
57namespace i18n {
58namespace phonenumbers {
59
60class ThreadChecker {
61 public:
62  bool CalledOnValidThread() const {
63    return true;
64  }
65};
66
67#endif
68
69}  // namespace phonenumbers
70}  // namespace i18n
71
72#endif  // I18N_PHONENUMBERS_BASE_THREAD_CHECKER_H_
73