This repository was archived by the owner on Dec 29, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +84
-0
lines changed Expand file tree Collapse file tree 4 files changed +84
-0
lines changed Original file line number Diff line number Diff line change 154
154
// Same as WTF_TASK_IF conditioned on the current namespace.
155
155
#define WTF_TASK (name ) WTF_TASK_IF(kWtfEnabledForNamespace , name)
156
156
157
+ #if defined(WTF_SINGLE_THREADED)
158
+
159
+ #include < signal.h>
160
+ void __wtfOnSignal (int ) {}
161
+ #define WTF_INIT_SIGNAL_WATCHER (unused )
162
+ #define WTF_WATCH_SIGNAL (number ) do { signal (number, __wtfOnSignal); } while (false )
163
+
164
+ #else
165
+
166
+ #include < signal.h>
167
+ #include < thread>
168
+
169
+ #define __WTF_PLATFORM __INTERNAL_WTF_NAMESPACE::platform
170
+
171
+ #define WTF_INIT_SIGNAL_WATCHER (filename ) \
172
+ __WTF_PLATFORM::condition_variable __WTF_INTERNAL_UNIQUE (cv); \
173
+ __WTF_PLATFORM::mutex __WTF_INTERNAL_UNIQUE (mutex); \
174
+ void __wtfOnSignal (int ) { \
175
+ __WTF_INTERNAL_UNIQUE (cv).notify_all (); \
176
+ }; \
177
+ void __wtfSignalWatcherThread () { \
178
+ __WTF_PLATFORM::unique_lock<__WTF_PLATFORM::mutex> \
179
+ lock { __WTF_INTERNAL_UNIQUE (mutex) }; \
180
+ while (true ) { \
181
+ __WTF_INTERNAL_UNIQUE (cv).wait (lock); \
182
+ __INTERNAL_WTF_NAMESPACE::Runtime::GetInstance ()->SaveToFile (filename); \
183
+ } \
184
+ }
185
+
186
+ #define WTF_WATCH_SIGNAL (number ) \
187
+ std::thread __WTF_INTERNAL_UNIQUE (thread) {__wtfSignalWatcherThread}; \
188
+ do { \
189
+ signal (number, __wtfOnSignal); \
190
+ __WTF_INTERNAL_UNIQUE (thread).detach (); \
191
+ } while (0 )
192
+
193
+ #undef __WTF_PLATFORM
194
+
195
+ #endif
196
+
197
+
157
198
#endif // TRACING_FRAMEWORK_BINDINGS_CPP_INCLUDE_WTF_MACROS_H_
Original file line number Diff line number Diff line change 7
7
8
8
#include < atomic>
9
9
#include < mutex>
10
+ #include < condition_variable>
10
11
11
12
namespace wtf {
12
13
@@ -17,9 +18,14 @@ using mutex = std::mutex;
17
18
template <typename T>
18
19
using lock_guard = std::lock_guard<T>;
19
20
21
+ template <typename T>
22
+ using unique_lock = std::unique_lock<T>;
23
+
20
24
template <typename T>
21
25
using atomic = std::atomic<T>;
22
26
27
+ using condition_variable = std::condition_variable;
28
+
23
29
// Since memory_order is an old-school enum, it needs to be imported
24
30
// individually.
25
31
using std::memory_order;
Original file line number Diff line number Diff line change 7
7
#include < atomic>
8
8
#include < mutex>
9
9
#include < thread>
10
+ #include < condition_variable>
10
11
11
12
namespace wtf {
12
13
@@ -17,9 +18,14 @@ using mutex = std::mutex;
17
18
template <typename T>
18
19
using lock_guard = std::lock_guard<T>;
19
20
21
+ template <typename T>
22
+ using unique_lock = std::unique_lock<T>;
23
+
20
24
template <typename T>
21
25
using atomic = std::atomic<T>;
22
26
27
+ using condition_variable = std::condition_variable;
28
+
23
29
using once_flag = std::once_flag;
24
30
template <class Callable >
25
31
inline void call_once (once_flag& flag, Callable&& f) {
Original file line number Diff line number Diff line change 1
1
#include " wtf/macros.h"
2
2
3
3
#include < fstream>
4
+ #include < unistd.h>
5
+ #include < sys/stat.h>
4
6
5
7
#include " gtest/gtest.h"
6
8
7
9
#ifndef TMP_PREFIX
8
10
#define TMP_PREFIX " "
9
11
#endif
10
12
13
+ char const * kSignalWatcherFilename = " ./tmptstsignal_watcher.wtf-trace" ;
14
+
15
+ WTF_INIT_SIGNAL_WATCHER (kSignalWatcherFilename );
16
+
11
17
namespace wtf {
12
18
namespace {
13
19
@@ -231,6 +237,31 @@ TEST_F(MacrosTest, BasicEndToEnd) {
231
237
Runtime::GetInstance ()->SaveToFile (TMP_PREFIX " tmpmacrobuf.wtf-trace" ));
232
238
}
233
239
240
+ TEST_F (MacrosTest, SignalWatcher) {
241
+ ClearEventBuffer ();
242
+ unlink (kSignalWatcherFilename );
243
+ // watch SIGALRM
244
+ WTF_WATCH_SIGNAL (14 );
245
+ WTF_EVENT0 (" MacrosTest#SignalWatcher" );
246
+
247
+ alarm (1 );
248
+
249
+ const int second = 1000 ;
250
+ usleep (5000 * second);
251
+
252
+ alarm (0 );
253
+ // allow the thread to finish it's work
254
+ usleep (1000 * second);
255
+ struct stat buffer;
256
+
257
+ #if defined(WTF_SINGLE_THREADED)
258
+ EXPECT_FALSE (stat (kSignalWatcherFilename , &buffer) == 0 );
259
+ #else
260
+ EXPECT_TRUE (stat (kSignalWatcherFilename , &buffer) == 0 );
261
+ EXPECT_TRUE (buffer.st_size > 0 );
262
+ #endif
263
+ }
264
+
234
265
} // namespace
235
266
} // namespace wtf
236
267
You can’t perform that action at this time.
0 commit comments