5 changed files with 272 additions and 18 deletions
-
4README.md
-
65src/components/thread/signal.cpp
-
11src/components/thread/signal.hpp
-
208src/components/thread/signal_test.cpp
-
2src/configs/version.hpp
@ -0,0 +1,208 @@ |
|||||
|
|
||||
|
|
||||
|
#ifdef SIGNAL_TEST
|
||||
|
#include "signal.hpp"
|
||||
|
|
||||
|
#include <unistd.h>
|
||||
|
|
||||
|
#include <chrono>
|
||||
|
#include <thread>
|
||||
|
|
||||
|
using namespace iflytop; |
||||
|
using namespace core; |
||||
|
using namespace std; |
||||
|
using namespace std::chrono; |
||||
|
|
||||
|
int64_t getnow() { return duration_cast<microseconds>(steady_clock::now().time_since_epoch()).count(); } |
||||
|
|
||||
|
#define TITLE(title) \
|
||||
|
printf("========================================\n"); \ |
||||
|
printf("= %s\n", title); |
||||
|
|
||||
|
#define LOG(str, ...) printf("= " str "\n", ##__VA_ARGS__);
|
||||
|
#define CTR() printf("=\n");
|
||||
|
#define END() printf("=\n");
|
||||
|
|
||||
|
#define CHECK(passed) \
|
||||
|
if (!(passed)) { \ |
||||
|
printf("= check %s failed\n", #passed); \ |
||||
|
exit(1); \ |
||||
|
} else { \ |
||||
|
printf("= check %s passed\n", #passed); \ |
||||
|
} |
||||
|
|
||||
|
#define CHECK_EQ(a, b, maxdiff) \
|
||||
|
if (abs((a) - (b)) > (maxdiff)) { \ |
||||
|
printf("= check %s == %s failed\n", #a, #b); \ |
||||
|
exit(1); \ |
||||
|
} else { \ |
||||
|
printf("= check %s == %s passed\n", #a, #b); \ |
||||
|
} |
||||
|
|
||||
|
int main(int argc, char const *argv[]) { |
||||
|
/* code */ |
||||
|
|
||||
|
{ |
||||
|
TITLE("=test sleep_for_us cost 1500000 us"); |
||||
|
|
||||
|
Singal signal; |
||||
|
int64_t now = getnow(); |
||||
|
signal.sleep_for_us(1500 * 1000); |
||||
|
int64_t after = getnow(); |
||||
|
int64_t real = after - now; |
||||
|
|
||||
|
LOG("real:%ld, diff:%ld ms ", real, (real - 1500000) / 1000); |
||||
|
|
||||
|
CTR(); |
||||
|
CHECK(((real - 1500000) / 1000) < 5); |
||||
|
|
||||
|
END(); |
||||
|
} |
||||
|
printf("\n"); |
||||
|
|
||||
|
{ |
||||
|
TITLE("test wake up \n"); |
||||
|
Singal signal; |
||||
|
|
||||
|
int64_t start, callwakeup, wakeup, jointime; |
||||
|
|
||||
|
thread t1([&]() { |
||||
|
LOG("thread 1 start sleep"); |
||||
|
signal.sleep(); |
||||
|
wakeup = getnow(); |
||||
|
LOG("thread 1 wake up"); |
||||
|
}); |
||||
|
|
||||
|
start = getnow(); |
||||
|
usleep(1000 * 1000); // 等待线程1开始睡眠
|
||||
|
LOG("thread 2 start notify"); |
||||
|
callwakeup = getnow(); |
||||
|
signal.notify(); |
||||
|
t1.join(); |
||||
|
jointime = getnow(); |
||||
|
|
||||
|
int64_t notify_delay = wakeup - callwakeup; |
||||
|
int64_t sleep_time = wakeup - start; |
||||
|
|
||||
|
LOG("thread 2 notify cost %ld us, thread 1 sleep %ld ms", notify_delay, sleep_time / 1000); |
||||
|
LOG("thread 2 join cost %ld ms", (jointime - callwakeup) / 1000); |
||||
|
|
||||
|
CTR(); |
||||
|
CHECK(notify_delay < 1000); // 通知延迟应该小于30us
|
||||
|
CHECK(abs(sleep_time - 1000 * 1000) < 3000); // 睡眠时间应该大于1秒
|
||||
|
CTR(); |
||||
|
} |
||||
|
printf("\n"); |
||||
|
|
||||
|
{ |
||||
|
TITLE("test wake up 2"); |
||||
|
Singal signal; |
||||
|
|
||||
|
int64_t start, callwakeup, wakeup, jointime; |
||||
|
|
||||
|
thread t1([&]() { |
||||
|
LOG("thread 1 start sleep"); |
||||
|
signal.sleep_for_us(10 * 1000 * 1000); |
||||
|
wakeup = getnow(); |
||||
|
LOG("thread 1 wake up"); |
||||
|
}); |
||||
|
|
||||
|
start = getnow(); |
||||
|
usleep(1000 * 1000); // 等待线程1开始睡眠
|
||||
|
LOG("thread 2 start notify"); |
||||
|
callwakeup = getnow(); |
||||
|
signal.notify(); |
||||
|
t1.join(); |
||||
|
jointime = getnow(); |
||||
|
|
||||
|
int64_t notify_delay = wakeup - callwakeup; |
||||
|
int64_t sleep_time = wakeup - start; |
||||
|
|
||||
|
LOG("thread 2 notify cost %ld us, thread 1 sleep %ld ms", notify_delay, sleep_time / 1000); |
||||
|
LOG("thread 2 join cost %ld ms", (jointime - callwakeup) / 1000); |
||||
|
|
||||
|
CTR(); |
||||
|
CHECK_EQ(notify_delay, 0, 1000); |
||||
|
CHECK_EQ(sleep_time / 1000, 1000, 2); |
||||
|
CTR(); |
||||
|
} |
||||
|
printf("\n"); |
||||
|
|
||||
|
{ |
||||
|
TITLE("test wake up twice"); |
||||
|
Singal signal; |
||||
|
|
||||
|
int64_t start, callwakeup, wakeup, jointime; |
||||
|
|
||||
|
thread t1([&]() { |
||||
|
LOG("thread 1 start sleep"); |
||||
|
signal.sleep_for_us(3 * 1000 * 1000); |
||||
|
signal.sleep_for_us(3 * 1000 * 1000); |
||||
|
wakeup = getnow(); |
||||
|
LOG("thread 1 wake up"); |
||||
|
}); |
||||
|
|
||||
|
start = getnow(); |
||||
|
usleep(1000 * 1000); // 等待线程1开始睡眠
|
||||
|
LOG("thread 2 start notify"); |
||||
|
callwakeup = getnow(); |
||||
|
signal.notify(); |
||||
|
signal.notify(); |
||||
|
t1.join(); |
||||
|
jointime = getnow(); |
||||
|
|
||||
|
int64_t notify_delay = wakeup - callwakeup; |
||||
|
int64_t sleep_time = wakeup - start; |
||||
|
|
||||
|
LOG("thread 2 notify cost %ld us, thread 1 sleep %ld ms", notify_delay, sleep_time / 1000); |
||||
|
LOG("thread 2 join cost %ld ms", (jointime - callwakeup) / 1000); |
||||
|
|
||||
|
CTR(); |
||||
|
CHECK_EQ(notify_delay, 0, 1000); |
||||
|
CHECK_EQ(sleep_time / 1000, 1000, 2); |
||||
|
CTR(); |
||||
|
} |
||||
|
printf("\n"); |
||||
|
|
||||
|
{ |
||||
|
TITLE("test wake up twice 2"); |
||||
|
Singal signal; |
||||
|
|
||||
|
int64_t start, callwakeup, wakeup, jointime; |
||||
|
|
||||
|
thread t1([&]() { |
||||
|
LOG("thread 1 start sleep"); |
||||
|
signal.sleep_for_us(3 * 1000 * 1000); |
||||
|
signal.sleep_for_us(3 * 1000 * 1000); |
||||
|
wakeup = getnow(); |
||||
|
LOG("thread 1 wake up"); |
||||
|
}); |
||||
|
|
||||
|
start = getnow(); |
||||
|
usleep(1000 * 1000); // 等待线程1开始睡眠
|
||||
|
LOG("thread 2 start notify"); |
||||
|
callwakeup = getnow(); |
||||
|
signal.notify(); |
||||
|
t1.join(); |
||||
|
jointime = getnow(); |
||||
|
|
||||
|
int64_t notify_delay = wakeup - callwakeup; |
||||
|
int64_t sleep_time = wakeup - start; |
||||
|
|
||||
|
LOG("thread 2 notify cost %ld us, thread 1 sleep %ld ms", notify_delay, sleep_time / 1000); |
||||
|
LOG("thread 2 join cost %ld ms", (jointime - callwakeup) / 1000); |
||||
|
|
||||
|
CTR(); |
||||
|
CHECK_EQ(notify_delay, 3000000, 1000); // 通知延迟应该等于3秒
|
||||
|
CHECK_EQ(sleep_time / 1000, 4000, 2); // 睡眠时间应该等于4秒
|
||||
|
CTR(); |
||||
|
} |
||||
|
printf("\n"); |
||||
|
|
||||
|
printf("========================================\n"); |
||||
|
printf("= All tests passed\n"); |
||||
|
printf("========================================\n"); |
||||
|
|
||||
|
return 0; |
||||
|
} |
||||
|
#endif
|
@ -1,2 +1,2 @@ |
|||||
#pragma once
|
#pragma once
|
||||
#define VERSION "1.0.0"
|
|
||||
|
#define VERSION "1.0.1"
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue