OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | |
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 #ifndef CRASHPAD_UTIL_TEST_THREAD_H_ | |
16 #define CRASHPAD_UTIL_TEST_THREAD_H_ | |
17 | |
18 #include "build/build_config.h" | |
Mark Mentovai
2015/03/20 15:03:56
build > base
scottmg
2015/03/20 21:07:35
Done.
| |
19 #include "base/basictypes.h" | |
20 | |
21 #if defined(OS_POSIX) | |
22 #include <pthread.h> | |
23 #elif defined(OS_WIN) | |
24 #include <windows.h> | |
25 #endif // OS_POSIX | |
26 | |
27 namespace crashpad { | |
28 namespace test { | |
29 | |
30 template <class T> | |
31 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.
| |
32 public: | |
33 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.
| |
34 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.
| |
35 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.
| |
36 | |
37 T* data() { return data_; } | |
38 | |
39 private: | |
40 static | |
41 #if defined(OS_POSIX) | |
42 void* | |
43 #elif defined(OS_WIN) | |
44 DWORD WINAPI | |
45 #endif // OS_POSIX | |
46 ThreadEntryThunk(void* argument); | |
47 | |
48 void (*entry_)(T*); | |
49 T* data_; | |
50 | |
51 #if defined(OS_POSIX) | |
52 pthread_t thread_; | |
53 #elif defined(OS_WIN) | |
54 HANDLE thread_; | |
55 #endif | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(Thread); | |
58 }; | |
59 | |
60 template <class T> | |
61 void Thread<T>::Start(void (*entry)(T*), T* data) { | |
62 entry_ = entry; | |
63 data_ = data; | |
64 #if defined(OS_POSIX) | |
65 int rv = pthread_create(&thread_, nullptr, ThreadEntryThunk, this); | |
66 ASSERT_EQ(0, rv) << ErrnoMessage(rv, "pthread_create"); | |
67 #elif defined(OS_WIN) | |
68 thread_ = CreateThread(nullptr, 0, ThreadEntryThunk, this, 0, nullptr); | |
69 ASSERT_NE(nullptr, thread_) << ErrorMessage("CreateThread"); | |
70 #endif // OS_POSIX | |
71 } | |
72 | |
73 template <class T> | |
74 void Thread<T>::Join() { | |
75 #if defined(OS_POSIX) | |
76 int rv = pthread_join(thread_, nullptr); | |
77 ASSERT_EQ(0, rv) << ErrnoMessage(rv, "pthread_join"); | |
78 #elif defined(OS_WIN) | |
79 DWORD result = WaitForSingleObject(thread_, INFINITE); | |
80 EXPECT_EQ(WAIT_OBJECT_0, result) << ErrorMessage("WaitForSingleObject"); | |
81 #endif // OS_POSIX | |
82 } | |
83 | |
84 // static | |
85 template <class T> | |
86 #if defined(OS_POSIX) | |
87 void* | |
88 #elif defined(OS_WIN) | |
89 DWORD WINAPI | |
90 #endif // OS_POSIX | |
91 Thread<T>::ThreadEntryThunk(void* argument) { | |
92 Thread* self = reinterpret_cast<Thread*>(argument); | |
93 self->entry_(self->data_); | |
94 | |
95 #if defined(OS_POSIX) | |
96 return nullptr; | |
97 #elif defined(OS_WIN) | |
98 return 0; | |
99 #endif // OS_POSIX | |
100 } | |
101 | |
102 } // namespace test | |
103 } // namespace crashpad | |
104 | |
105 #endif // CRASHPAD_UTIL_TEST_THREAD_H_ | |
OLD | NEW |