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

Commit 790dde7

Browse files
author
iivlev
committed
Signal watcher functionality with test
1 parent f35328f commit 790dde7

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

bindings/cpp/Makefile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ else
4141
$(error Expected value of THREADING to be single/pthread/std)
4242
endif
4343

44+
ifneq "$(THREADING)" "std"
45+
override POSIX_SUPPORT = "0"
46+
endif
47+
4448
# Required flag customizations.
4549
override CPPFLAGS += -Iinclude
4650
override CPPFLAGS += -I$(GTEST_DIR)/include
@@ -67,6 +71,10 @@ PLATFORM_HEADERS := \
6771
include/wtf/platform/platform_myriad2sparc_impl.h \
6872
include/wtf/platform/platform_myriad2sparc_inl.h
6973

74+
ifeq "$(POSIX_SUPPORT)" "1"
75+
LIBRARY_HEADERS += include/wtf/posix_utils.h
76+
endif
77+
7078
ALL_HEADERS := $(LIBRARY_HEADERS) $(PLATFORM_HEADERS)
7179

7280
LIBRARY_SOURCES := \
@@ -81,6 +89,11 @@ TEST_SOURCES := \
8189
runtime_test.cc \
8290
threaded_torture_test.cc
8391

92+
ifeq "$(POSIX_SUPPORT)" "1"
93+
LIBRARY_SOURCES += posix_utils.cc
94+
TEST_SOURCES += posix_utils_test.cc
95+
endif
96+
8497
LIBRARY_OBJECTS := $(LIBRARY_SOURCES:%.cc=%.o)
8598

8699
.PHONY: clean all test
@@ -113,7 +126,13 @@ clean:
113126
$(wildcard tmp*.wtf-trace)
114127

115128
### TESTING.
129+
ifneq "$(POSIX_SUPPORT)" "1"
116130
test: buffer_test macros_test runtime_test threaded_torture_test
131+
else
132+
test: buffer_test macros_test runtime_test threaded_torture_test posix_utils_test
133+
@echo "Running posix_utils_test"
134+
./posix_utils_test
135+
endif
117136
@echo "Running buffer_test"
118137
./buffer_test
119138
@echo "Running macros_test"
@@ -138,6 +157,9 @@ macros_test: macros_test.o gtest.o libwtf.a
138157
runtime_test: runtime_test.o gtest.o libwtf.a
139158
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $@ $+ $(LDLIBS)
140159

160+
posix_utils_test: posix_utils_test.o gtest.o libwtf.a
161+
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $@ $+ $(LDLIBS)
162+
141163
### THREADED TORTURE TEST
142164
ifneq "$(THREADING)" "single"
143165
threaded_torture_test: threaded_torture_test.o libwtf.a
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef POSIX_UTILS_H
2+
#define POSIX_UTILS_H
3+
4+
namespace wtf {
5+
namespace posix_utils {
6+
7+
void InstallSignalHandler(int number, char const* filename);
8+
9+
} // namespace posix_utils
10+
} // namespace wtf
11+
12+
#endif // POSIX_UTILS_H

bindings/cpp/posix_utils.cc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <wtf/runtime.h>
2+
#include <signal.h>
3+
4+
#include <thread>
5+
#include <condition_variable>
6+
7+
namespace {
8+
9+
std::condition_variable gCv;
10+
std::mutex gMutex;
11+
std::string gFilename;
12+
13+
void WtfOnSignal(int) {
14+
gCv.notify_all();
15+
};
16+
17+
void WtfSignalWatcherThread () {
18+
std::unique_lock<std::mutex> lock { gMutex };
19+
while (true) {
20+
gCv.wait(lock);
21+
wtf::Runtime::GetInstance()->SaveToFile(gFilename.c_str());
22+
}
23+
}
24+
25+
} // namespace
26+
27+
namespace wtf {
28+
namespace posix_utils {
29+
30+
void InstallSignalHandler(int number, char const* filename) {
31+
if (!filename) {
32+
fprintf(stderr, "%s: Error: filename can't be null\n", __PRETTY_FUNCTION__);
33+
return;
34+
}
35+
36+
static std::atomic<bool> only_once {false};
37+
bool expected = false;
38+
if (only_once.compare_exchange_strong(expected, true)) {
39+
40+
gFilename = filename;
41+
std::thread thread { WtfSignalWatcherThread };
42+
43+
signal(number, WtfOnSignal);
44+
45+
thread.detach();
46+
47+
}
48+
49+
}
50+
51+
} // namespace posix_utils
52+
} // namespace wtf

bindings/cpp/posix_utils_test.cc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <unistd.h>
2+
#include <sys/stat.h>
3+
4+
#include "wtf/posix_utils.h"
5+
#include "wtf/macros.h"
6+
7+
#include "gtest/gtest.h"
8+
9+
const char kSignalWatcherFilename[] { "./tmptstsignal_watcher.wtf-trace" };
10+
11+
namespace wtf {
12+
13+
class PosixUtilsTest : public ::testing::Test {
14+
protected:
15+
void TearDown() override {
16+
wtf::Runtime::GetInstance()->DisableCurrentThread();
17+
wtf::Runtime::GetInstance()->ResetForTesting();
18+
}
19+
20+
};
21+
22+
23+
TEST_F(PosixUtilsTest, SignalWatcher) {
24+
unlink(kSignalWatcherFilename);
25+
// watch SIGALRM
26+
wtf::posix_utils::InstallSignalHandler(14, kSignalWatcherFilename);
27+
WTF_EVENT0("MacrosTest#SignalWatcher");
28+
29+
alarm(1);
30+
31+
const int second = 1000;
32+
usleep(5000 * second);
33+
34+
alarm(0);
35+
// allow the thread to finish it's work
36+
usleep(1000 * second);
37+
struct stat buffer;
38+
39+
EXPECT_TRUE(stat(kSignalWatcherFilename, &buffer) == 0);
40+
EXPECT_TRUE(buffer.st_size > 0);
41+
}
42+
} // namespace wtf
43+
44+
int main(int argc, char** argv) {
45+
::testing::InitGoogleTest(&argc, argv);
46+
return RUN_ALL_TESTS();
47+
}

0 commit comments

Comments
 (0)