Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 5ed87d2

Browse files
author
iivlev
committed
Signal watcher functionality with test
1 parent f35328f commit 5ed87d2

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

bindings/cpp/include/wtf/macros.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,41 @@
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_

bindings/cpp/include/wtf/platform/platform_aux_pthreads_threaded_inl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <atomic>
99
#include <mutex>
10+
#include <condition_variable>
1011

1112
namespace wtf {
1213

@@ -17,9 +18,14 @@ using mutex = std::mutex;
1718
template <typename T>
1819
using lock_guard = std::lock_guard<T>;
1920

21+
template <typename T>
22+
using unique_lock = std::unique_lock<T>;
23+
2024
template <typename T>
2125
using 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.
2531
using std::memory_order;

bindings/cpp/include/wtf/platform/platform_aux_std_threaded_inl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <atomic>
88
#include <mutex>
99
#include <thread>
10+
#include <condition_variable>
1011

1112
namespace wtf {
1213

@@ -17,9 +18,14 @@ using mutex = std::mutex;
1718
template <typename T>
1819
using lock_guard = std::lock_guard<T>;
1920

21+
template <typename T>
22+
using unique_lock = std::unique_lock<T>;
23+
2024
template <typename T>
2125
using atomic = std::atomic<T>;
2226

27+
using condition_variable = std::condition_variable;
28+
2329
using once_flag = std::once_flag;
2430
template <class Callable>
2531
inline void call_once(once_flag& flag, Callable&& f) {

bindings/cpp/macros_test.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
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+
1117
namespace wtf {
1218
namespace {
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

0 commit comments

Comments
 (0)