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 +80
-0
lines changed Expand file tree Collapse file tree 4 files changed +80
-0
lines changed Original file line number Diff line number Diff line change 154154// Same as WTF_TASK_IF conditioned on the current namespace.
155155#define WTF_TASK (name ) WTF_TASK_IF(kWtfEnabledForNamespace , name)
156156
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_INIT_SIGNAL_WATCHER (filename ) \
170+ __INTERNAL_WTF_NAMESPACE::platform::condition_variable __WTF_INTERNAL_UNIQUE (cv); \
171+ __INTERNAL_WTF_NAMESPACE::platform::mutex __WTF_INTERNAL_UNIQUE (mutex); \
172+ void __wtfOnSignal (int ) { \
173+ __WTF_INTERNAL_UNIQUE (cv).notify_all (); \
174+ }; \
175+ void __wtfSignalWatcherThread () { \
176+ __INTERNAL_WTF_NAMESPACE::platform::unique_lock<__INTERNAL_WTF_NAMESPACE::platform::mutex> \
177+ lock { __WTF_INTERNAL_UNIQUE (mutex) }; \
178+ while (true ) { \
179+ __WTF_INTERNAL_UNIQUE (cv).wait (lock); \
180+ __INTERNAL_WTF_NAMESPACE::Runtime::GetInstance ()->SaveToFile (filename); \
181+ } \
182+ }
183+
184+ #define WTF_WATCH_SIGNAL (number ) \
185+ std::thread __WTF_INTERNAL_UNIQUE (thread) {__wtfSignalWatcherThread}; \
186+ do { \
187+ signal (number, __wtfOnSignal); \
188+ __WTF_INTERNAL_UNIQUE (thread).detach (); \
189+ } while (0 )
190+
191+ #endif
192+
193+
157194#endif // TRACING_FRAMEWORK_BINDINGS_CPP_INCLUDE_WTF_MACROS_H_
Original file line number Diff line number Diff line change 77
88#include < atomic>
99#include < mutex>
10+ #include < condition_variable>
1011
1112namespace wtf {
1213
@@ -17,9 +18,14 @@ using mutex = std::mutex;
1718template <typename T>
1819using lock_guard = std::lock_guard<T>;
1920
21+ template <typename T>
22+ using unique_lock = std::unique_lock<T>;
23+
2024template <typename T>
2125using atomic = std::atomic<T>;
2226
27+ using condition_variable = std::condition_variable;
28+
2329// Since memory_order is an old-school enum, it needs to be imported
2430// individually.
2531using std::memory_order;
Original file line number Diff line number Diff line change 77#include < atomic>
88#include < mutex>
99#include < thread>
10+ #include < condition_variable>
1011
1112namespace wtf {
1213
@@ -17,9 +18,14 @@ using mutex = std::mutex;
1718template <typename T>
1819using lock_guard = std::lock_guard<T>;
1920
21+ template <typename T>
22+ using unique_lock = std::unique_lock<T>;
23+
2024template <typename T>
2125using atomic = std::atomic<T>;
2226
27+ using condition_variable = std::condition_variable;
28+
2329using once_flag = std::once_flag;
2430template <class Callable >
2531inline void call_once (once_flag& flag, Callable&& f) {
Original file line number Diff line number Diff line change 11#include " wtf/macros.h"
22
33#include < fstream>
4+ #include < unistd.h>
5+ #include < sys/stat.h>
46
57#include " gtest/gtest.h"
68
79#ifndef TMP_PREFIX
810#define TMP_PREFIX " "
911#endif
1012
13+ char const * kSignalWatcherFilename = " ./tmptstsignal_watcher.wtf-trace" ;
14+
15+ WTF_INIT_SIGNAL_WATCHER (kSignalWatcherFilename );
16+
1117namespace wtf {
1218namespace {
1319
@@ -231,6 +237,31 @@ TEST_F(MacrosTest, BasicEndToEnd) {
231237 Runtime::GetInstance ()->SaveToFile (TMP_PREFIX " tmpmacrobuf.wtf-trace" ));
232238}
233239
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+
234265} // namespace
235266} // namespace wtf
236267
You can’t perform that action at this time.
0 commit comments