Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(283)

Side by Side Diff: src/gtest-port.cc

Issue 129067: 148 - ThreadLocal and Mutex for pthread Base URL: http://googletest.googlecode.com/svn/trunk/
Patch Set: Created 15 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2008, Google Inc. 1 // Copyright 2008, Google Inc.
2 // All rights reserved. 2 // All rights reserved.
3 // 3 //
4 // Redistribution and use in source and binary forms, with or without 4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are 5 // modification, are permitted provided that the following conditions are
6 // met: 6 // met:
7 // 7 //
8 // * Redistributions of source code must retain the above copyright 8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer. 9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above 10 // * Redistributions in binary form must reproduce the above
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 namespace testing { 66 namespace testing {
67 namespace internal { 67 namespace internal {
68 68
69 #if defined(_MSC_VER) || defined(__BORLANDC__) 69 #if defined(_MSC_VER) || defined(__BORLANDC__)
70 // MSVC and C++Builder do not provide a definition of STDERR_FILENO. 70 // MSVC and C++Builder do not provide a definition of STDERR_FILENO.
71 const int kStdErrFileno = 2; 71 const int kStdErrFileno = 2;
72 #else 72 #else
73 const int kStdErrFileno = STDERR_FILENO; 73 const int kStdErrFileno = STDERR_FILENO;
74 #endif // _MSC_VER 74 #endif // _MSC_VER
75 75
76 #if GTEST_HAS_PTHREAD
77 pthread_exception::pthread_exception(int error,const char* msg) : buffer_() {
78 buffer_[0] = 0;
79 snprintf(buffer_,sizeof(buffer_),"%s - failed: %d:%s",msg,error, strerror(erro r));
80 }
81
82 const char* pthread_exception::what() const throw() {
83 return buffer_;·
84 }
85
86 PthreadMutex::PthreadMutex() : mutex_(), owner_(0) {
87 init();
88 }
89
90 PthreadMutex::PthreadMutex(NoConstructorNeededSelector) {}
91
92 PthreadMutex::~PthreadMutex() {
93 int err = pthread_mutex_destroy(&mutex_);
94 if (err != 0)
95 throw pthread_exception(err,"pthread_mutex_destroy");
96 }
97 ··
98 void PthreadMutex::AssertHeld() const {
99 GTEST_CHECK_ (owner_ == pthread_self()) << "AssertHeld failed.";
Vlad 2009/10/20 22:40:20 Please provide a more informative message, such as
100 }
101
102 void PthreadMutex::lock() {
103 int err = pthread_mutex_lock(&mutex_);
104 if (err != 0)
105 throw pthread_exception(err,"pthread_mutex_lock");
106 owner_ = pthread_self();
107 }
108 ··
109 void PthreadMutex::unlock() {
110 int err = pthread_mutex_unlock(&mutex_);
111 if (err != 0)
112 throw pthread_exception(err,"pthread_mutex_unlock");
113 owner_ = 0;
florianlink 2009/10/15 07:38:20 Setting the owner_ to 0 here is too late, it is a
mfazekas 2009/10/15 14:33:07 Yes, the owner_ = 0 should be moved before pthread
114 }
115 ··
116 void PthreadMutex::init() {
117 owner_ = 0;
118 int err = pthread_mutex_init(&mutex_,0);
119 if (err != 0)
120 throw pthread_exception(err,"pthread_mutex_init");
121 }
122
123 Mutex::ImplMutex& Mutex::lazyInit() {
124 // This double checked locking handles the case where two threads trying use
125 // a mutex before it was constructed. It can only happen when if we launch·
126 // threads before main is entered, or we use Mutex as static locals on a·
127 // compiler where it's not threadsafe.
128 // Maybe we don't need to bother with all this, and we could remove this·
129 // whacky stuff here. Not to meantion, that we'd also need a fence to make thi s·
130 // work.
131 if (inited_ == 0) {
132 ImplMutex::LockGuard lock(&staticMutex());
133 if (inited_ == 0) {
134 new (&uninitializedImpl_) ImplMutex();············
135 // fenc();
136 inited_++;
137 }
138 }
139 return uninitializedImpl_;
140 }
141
142 Mutex::ImplMutex& Mutex::_staticMutex() {
143 // Unfortunatelyy static locals are not thread safe on all compilers
144 // so we provide staticMutex() which uses pthread_once to avoid the data race
145 static ImplMutex mutex;
146 return mutex;
147 }
148
149 void Mutex::initStaticMutexOnce() {
150 _staticMutex();
151 }
152
153 pthread_once_t Mutex::once_control = PTHREAD_ONCE_INIT;
154
155 Mutex::ImplMutex& Mutex::staticMutex() {
156 int err = pthread_once(&once_control, &Mutex::initStaticMutexOnce);
157 if (err != 0)
158 throw pthread_exception(err,"pthread_mutex_init");
159 return _staticMutex();
160 }
161
162 #endif
Vlad 2009/10/20 22:40:20 #endif // GTEST_HAS_PTHREAD
163
76 #if GTEST_OS_MAC 164 #if GTEST_OS_MAC
77 165
78 // Returns the number of threads running in the process, or 0 to indicate that 166 // Returns the number of threads running in the process, or 0 to indicate that
79 // we cannot detect it. 167 // we cannot detect it.
80 size_t GetThreadCount() { 168 size_t GetThreadCount() {
81 const task_t task = mach_task_self(); 169 const task_t task = mach_task_self();
82 mach_msg_type_number_t thread_count; 170 mach_msg_type_number_t thread_count;
83 thread_act_array_t thread_list; 171 thread_act_array_t thread_list;
84 const kern_return_t status = task_threads(task, &thread_list, &thread_count); 172 const kern_return_t status = task_threads(task, &thread_list, &thread_count);
85 if (status == KERN_SUCCESS) { 173 if (status == KERN_SUCCESS) {
(...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 // Reads and returns the string environment variable corresponding to 759 // Reads and returns the string environment variable corresponding to
672 // the given flag; if it's not set, returns default_value. 760 // the given flag; if it's not set, returns default_value.
673 const char* StringFromGTestEnv(const char* flag, const char* default_value) { 761 const char* StringFromGTestEnv(const char* flag, const char* default_value) {
674 const String env_var = FlagToEnvVar(flag); 762 const String env_var = FlagToEnvVar(flag);
675 const char* const value = posix::GetEnv(env_var.c_str()); 763 const char* const value = posix::GetEnv(env_var.c_str());
676 return value == NULL ? default_value : value; 764 return value == NULL ? default_value : value;
677 } 765 }
678 766
679 } // namespace internal 767 } // namespace internal
680 } // namespace testing 768 } // namespace testing
OLDNEW

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b