Chromium Code Reviews| Index: util/test/thread.h |
| diff --git a/util/test/thread.h b/util/test/thread.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b63e7606e591fc80d77b74db9ca46ac0b5ed3516 |
| --- /dev/null |
| +++ b/util/test/thread.h |
| @@ -0,0 +1,105 @@ |
| +// Copyright 2015 The Crashpad Authors. All rights reserved. |
| +// |
| +// Licensed under the Apache License, Version 2.0 (the "License"); |
| +// you may not use this file except in compliance with the License. |
| +// You may obtain a copy of the License at |
| +// |
| +// http://www.apache.org/licenses/LICENSE-2.0 |
| +// |
| +// Unless required by applicable law or agreed to in writing, software |
| +// distributed under the License is distributed on an "AS IS" BASIS, |
| +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| +// See the License for the specific language governing permissions and |
| +// limitations under the License. |
| + |
| +#ifndef CRASHPAD_UTIL_TEST_THREAD_H_ |
| +#define CRASHPAD_UTIL_TEST_THREAD_H_ |
| + |
| +#include "build/build_config.h" |
|
Mark Mentovai
2015/03/20 15:03:56
build > base
scottmg
2015/03/20 21:07:35
Done.
|
| +#include "base/basictypes.h" |
| + |
| +#if defined(OS_POSIX) |
| +#include <pthread.h> |
| +#elif defined(OS_WIN) |
| +#include <windows.h> |
| +#endif // OS_POSIX |
| + |
| +namespace crashpad { |
| +namespace test { |
| + |
| +template <class T> |
| +class Thread { |
|
Mark Mentovai
2015/03/20 15:42:12
Provide at least a //! \brief for the class and al
scottmg
2015/03/20 21:07:35
Done.
|
| + public: |
| + Thread() : entry_(nullptr), data_(nullptr), thread_(nullptr) {} |
|
Mark Mentovai
2015/03/20 15:03:56
nullptr isn’t a valid pthread_t on all systems. 0
Mark Mentovai
2015/03/20 15:42:12
~Thread() should either EXPECT that if Start() was
scottmg
2015/03/20 21:07:35
Done.
scottmg
2015/03/20 21:07:35
Done.
|
| + void Start(void (*entry)(T*), T* data); |
|
Mark Mentovai
2015/03/20 15:03:56
The function pointer is a very C-like interface, w
Mark Mentovai
2015/03/20 15:42:12
I wrote:
scottmg
2015/03/20 21:07:35
Main()?
scottmg
2015/03/20 21:07:35
Done.
|
| + void Join(); |
|
Mark Mentovai
2015/03/20 15:42:12
Similarly, Start() should EXPECT that it hasn’t al
scottmg
2015/03/20 21:07:35
Done.
|
| + |
| + T* data() { return data_; } |
| + |
| + private: |
| + static |
| +#if defined(OS_POSIX) |
| + void* |
| +#elif defined(OS_WIN) |
| + DWORD WINAPI |
| +#endif // OS_POSIX |
| + ThreadEntryThunk(void* argument); |
| + |
| + void (*entry_)(T*); |
| + T* data_; |
| + |
| +#if defined(OS_POSIX) |
| + pthread_t thread_; |
| +#elif defined(OS_WIN) |
| + HANDLE thread_; |
| +#endif |
| + |
| + DISALLOW_COPY_AND_ASSIGN(Thread); |
| +}; |
| + |
| +template <class T> |
| +void Thread<T>::Start(void (*entry)(T*), T* data) { |
| + entry_ = entry; |
| + data_ = data; |
| +#if defined(OS_POSIX) |
| + int rv = pthread_create(&thread_, nullptr, ThreadEntryThunk, this); |
| + ASSERT_EQ(0, rv) << ErrnoMessage(rv, "pthread_create"); |
| +#elif defined(OS_WIN) |
| + thread_ = CreateThread(nullptr, 0, ThreadEntryThunk, this, 0, nullptr); |
| + ASSERT_NE(nullptr, thread_) << ErrorMessage("CreateThread"); |
| +#endif // OS_POSIX |
| +} |
| + |
| +template <class T> |
| +void Thread<T>::Join() { |
| +#if defined(OS_POSIX) |
| + int rv = pthread_join(thread_, nullptr); |
| + ASSERT_EQ(0, rv) << ErrnoMessage(rv, "pthread_join"); |
| +#elif defined(OS_WIN) |
| + DWORD result = WaitForSingleObject(thread_, INFINITE); |
| + EXPECT_EQ(WAIT_OBJECT_0, result) << ErrorMessage("WaitForSingleObject"); |
| +#endif // OS_POSIX |
| +} |
| + |
| +// static |
| +template <class T> |
| +#if defined(OS_POSIX) |
| +void* |
| +#elif defined(OS_WIN) |
| +DWORD WINAPI |
| +#endif // OS_POSIX |
| +Thread<T>::ThreadEntryThunk(void* argument) { |
| + Thread* self = reinterpret_cast<Thread*>(argument); |
| + self->entry_(self->data_); |
| + |
| +#if defined(OS_POSIX) |
| + return nullptr; |
| +#elif defined(OS_WIN) |
| + return 0; |
| +#endif // OS_POSIX |
| +} |
| + |
| +} // namespace test |
| +} // namespace crashpad |
| + |
| +#endif // CRASHPAD_UTIL_TEST_THREAD_H_ |