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__) && \ 26 !defined(I18N_PHONENUMBERS_NO_THREAD_SAFETY) 27#error Building without Boost, please provide \ 28 -DI18N_PHONENUMBERS_NO_THREAD_SAFETY 29#endif 30 31#endif 32 33#if !defined(NDEBUG) && !defined(I18N_PHONENUMBERS_USE_BOOST) && \ 34 (defined(__linux__) || defined(__apple__)) 35 36#include <pthread.h> 37 38namespace i18n { 39namespace phonenumbers { 40 41class ThreadChecker { 42 public: 43 ThreadChecker() : thread_id_(pthread_self()) {} 44 45 bool CalledOnValidThread() const { 46 return thread_id_ == pthread_self(); 47 } 48 49 private: 50 const pthread_t thread_id_; 51}; 52 53#else 54 55namespace i18n { 56namespace phonenumbers { 57 58class ThreadChecker { 59 public: 60 bool CalledOnValidThread() const { 61 return true; 62 } 63}; 64 65#endif 66 67} // namespace phonenumbers 68} // namespace i18n 69 70#endif // I18N_PHONENUMBERS_BASE_THREAD_CHECKER_H_ 71