bthread_butex_unittest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // Copyright (c) 2014 Baidu, Inc.
  2. // Author: Ge,Jun (gejun@baidu.com)
  3. // Date: Sun Jul 13 15:04:18 CST 2014
  4. #include <gtest/gtest.h>
  5. #include "butil/atomicops.h"
  6. #include "butil/time.h"
  7. #include "butil/macros.h"
  8. #include "butil/logging.h"
  9. #include "bthread/butex.h"
  10. #include "bthread/task_control.h"
  11. #include "bthread/task_group.h"
  12. #include "bthread/bthread.h"
  13. #include "bthread/unstable.h"
  14. namespace bthread {
  15. extern butil::atomic<TaskControl*> g_task_control;
  16. inline TaskControl* get_task_control() {
  17. return g_task_control.load(butil::memory_order_consume);
  18. }
  19. } // namespace bthread
  20. namespace {
  21. TEST(ButexTest, wait_on_already_timedout_butex) {
  22. uint32_t* butex = bthread::butex_create_checked<uint32_t>();
  23. ASSERT_TRUE(butex);
  24. timespec now;
  25. ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &now));
  26. *butex = 1;
  27. ASSERT_EQ(-1, bthread::butex_wait(butex, 1, &now));
  28. ASSERT_EQ(ETIMEDOUT, errno);
  29. }
  30. void* sleeper(void* arg) {
  31. bthread_usleep((uint64_t)arg);
  32. return NULL;
  33. }
  34. void* joiner(void* arg) {
  35. const long t1 = butil::gettimeofday_us();
  36. for (bthread_t* th = (bthread_t*)arg; *th; ++th) {
  37. if (0 != bthread_join(*th, NULL)) {
  38. LOG(FATAL) << "fail to join thread_" << th - (bthread_t*)arg;
  39. }
  40. long elp = butil::gettimeofday_us() - t1;
  41. EXPECT_LE(labs(elp - (th - (bthread_t*)arg + 1) * 100000L), 10000L)
  42. << "timeout when joining thread_" << th - (bthread_t*)arg;
  43. LOG(INFO) << "Joined thread " << *th << " at " << elp << "us ["
  44. << bthread_self() << "]";
  45. }
  46. for (bthread_t* th = (bthread_t*)arg; *th; ++th) {
  47. EXPECT_EQ(0, bthread_join(*th, NULL));
  48. }
  49. return NULL;
  50. }
  51. struct A {
  52. uint64_t a;
  53. char dummy[0];
  54. };
  55. struct B {
  56. uint64_t a;
  57. };
  58. TEST(ButexTest, with_or_without_array_zero) {
  59. ASSERT_EQ(sizeof(B), sizeof(A));
  60. }
  61. TEST(ButexTest, join) {
  62. const size_t N = 6;
  63. const size_t M = 6;
  64. bthread_t th[N+1];
  65. bthread_t jth[M];
  66. pthread_t pth[M];
  67. for (size_t i = 0; i < N; ++i) {
  68. bthread_attr_t attr = (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
  69. ASSERT_EQ(0, bthread_start_urgent(
  70. &th[i], &attr, sleeper,
  71. (void*)(100000L/*100ms*/ * (i + 1))));
  72. }
  73. th[N] = 0; // joiner will join tids in `th' until seeing 0.
  74. for (size_t i = 0; i < M; ++i) {
  75. ASSERT_EQ(0, bthread_start_urgent(&jth[i], NULL, joiner, th));
  76. }
  77. for (size_t i = 0; i < M; ++i) {
  78. ASSERT_EQ(0, pthread_create(&pth[i], NULL, joiner, th));
  79. }
  80. for (size_t i = 0; i < M; ++i) {
  81. ASSERT_EQ(0, bthread_join(jth[i], NULL))
  82. << "i=" << i << " error=" << berror();
  83. }
  84. for (size_t i = 0; i < M; ++i) {
  85. ASSERT_EQ(0, pthread_join(pth[i], NULL));
  86. }
  87. }
  88. struct WaiterArg {
  89. int expected_result;
  90. int expected_value;
  91. butil::atomic<int> *butex;
  92. const timespec *ptimeout;
  93. };
  94. void* waiter(void* arg) {
  95. WaiterArg * wa = (WaiterArg*)arg;
  96. const long t1 = butil::gettimeofday_us();
  97. const int rc = bthread::butex_wait(
  98. wa->butex, wa->expected_value, wa->ptimeout);
  99. const long t2 = butil::gettimeofday_us();
  100. if (rc == 0) {
  101. EXPECT_EQ(wa->expected_result, 0) << bthread_self();
  102. } else {
  103. EXPECT_EQ(wa->expected_result, errno) << bthread_self();
  104. }
  105. LOG(INFO) << "after wait, time=" << (t2-t1) << "us";
  106. return NULL;
  107. }
  108. TEST(ButexTest, sanity) {
  109. const size_t N = 5;
  110. WaiterArg args[N * 4];
  111. pthread_t t1, t2;
  112. butil::atomic<int>* b1 =
  113. bthread::butex_create_checked<butil::atomic<int> >();
  114. ASSERT_TRUE(b1);
  115. bthread::butex_destroy(b1);
  116. b1 = bthread::butex_create_checked<butil::atomic<int> >();
  117. *b1 = 1;
  118. ASSERT_EQ(0, bthread::butex_wake(b1));
  119. WaiterArg *unmatched_arg = new WaiterArg;
  120. unmatched_arg->expected_value = *b1 + 1;
  121. unmatched_arg->expected_result = EWOULDBLOCK;
  122. unmatched_arg->butex = b1;
  123. unmatched_arg->ptimeout = NULL;
  124. pthread_create(&t2, NULL, waiter, unmatched_arg);
  125. bthread_t th;
  126. ASSERT_EQ(0, bthread_start_urgent(&th, NULL, waiter, unmatched_arg));
  127. const timespec abstime = butil::seconds_from_now(1);
  128. for (size_t i = 0; i < 4*N; ++i) {
  129. args[i].expected_value = *b1;
  130. args[i].butex = b1;
  131. if ((i % 2) == 0) {
  132. args[i].expected_result = 0;
  133. args[i].ptimeout = NULL;
  134. } else {
  135. args[i].expected_result = ETIMEDOUT;
  136. args[i].ptimeout = &abstime;
  137. }
  138. if (i < 2*N) {
  139. pthread_create(&t1, NULL, waiter, &args[i]);
  140. } else {
  141. ASSERT_EQ(0, bthread_start_urgent(&th, NULL, waiter, &args[i]));
  142. }
  143. }
  144. sleep(2);
  145. for (size_t i = 0; i < 2*N; ++i) {
  146. ASSERT_EQ(1, bthread::butex_wake(b1));
  147. }
  148. ASSERT_EQ(0, bthread::butex_wake(b1));
  149. sleep(1);
  150. bthread::butex_destroy(b1);
  151. }
  152. struct ButexWaitArg {
  153. int* butex;
  154. int expected_val;
  155. long wait_msec;
  156. int error_code;
  157. };
  158. void* wait_butex(void* void_arg) {
  159. ButexWaitArg* arg = static_cast<ButexWaitArg*>(void_arg);
  160. const timespec ts = butil::milliseconds_from_now(arg->wait_msec);
  161. int rc = bthread::butex_wait(arg->butex, arg->expected_val, &ts);
  162. int saved_errno = errno;
  163. if (arg->error_code) {
  164. EXPECT_EQ(-1, rc);
  165. EXPECT_EQ(arg->error_code, saved_errno);
  166. } else {
  167. EXPECT_EQ(0, rc);
  168. }
  169. return NULL;
  170. }
  171. TEST(ButexTest, wait_without_stop) {
  172. int* butex = bthread::butex_create_checked<int>();
  173. *butex = 7;
  174. butil::Timer tm;
  175. const long WAIT_MSEC = 500;
  176. for (int i = 0; i < 2; ++i) {
  177. const bthread_attr_t attr =
  178. (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
  179. ButexWaitArg arg = { butex, *butex, WAIT_MSEC, ETIMEDOUT };
  180. bthread_t th;
  181. tm.start();
  182. ASSERT_EQ(0, bthread_start_urgent(&th, &attr, wait_butex, &arg));
  183. ASSERT_EQ(0, bthread_join(th, NULL));
  184. tm.stop();
  185. ASSERT_LT(abs(tm.m_elapsed() - WAIT_MSEC), 20);
  186. }
  187. bthread::butex_destroy(butex);
  188. }
  189. TEST(ButexTest, stop_after_running) {
  190. int* butex = bthread::butex_create_checked<int>();
  191. *butex = 7;
  192. butil::Timer tm;
  193. const long WAIT_MSEC = 500;
  194. const long SLEEP_MSEC = 10;
  195. for (int i = 0; i < 2; ++i) {
  196. const bthread_attr_t attr =
  197. (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
  198. bthread_t th;
  199. ButexWaitArg arg = { butex, *butex, WAIT_MSEC, ESTOP };
  200. tm.start();
  201. ASSERT_EQ(0, bthread_start_urgent(&th, &attr, wait_butex, &arg));
  202. ASSERT_EQ(0, bthread_usleep(SLEEP_MSEC * 1000L));
  203. ASSERT_EQ(0, bthread_stop(th));
  204. ASSERT_EQ(0, bthread_join(th, NULL));
  205. tm.stop();
  206. ASSERT_LT(abs(tm.m_elapsed() - SLEEP_MSEC), 10);
  207. // ASSERT_TRUE(bthread::get_task_control()->
  208. // timer_thread()._idset.empty());
  209. ASSERT_EQ(EINVAL, bthread_stop(th));
  210. }
  211. bthread::butex_destroy(butex);
  212. }
  213. TEST(ButexTest, stop_before_running) {
  214. int* butex = bthread::butex_create_checked<int>();
  215. *butex = 7;
  216. butil::Timer tm;
  217. const long WAIT_MSEC = 500;
  218. for (int i = 0; i < 2; ++i) {
  219. const bthread_attr_t attr =
  220. (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL) | BTHREAD_NOSIGNAL;
  221. bthread_t th;
  222. ButexWaitArg arg = { butex, *butex, WAIT_MSEC, ESTOP };
  223. tm.start();
  224. ASSERT_EQ(0, bthread_start_background(&th, &attr, wait_butex, &arg));
  225. ASSERT_EQ(0, bthread_stop(th));
  226. bthread_flush();
  227. ASSERT_EQ(0, bthread_join(th, NULL));
  228. tm.stop();
  229. ASSERT_LT(tm.m_elapsed(), 5);
  230. // ASSERT_TRUE(bthread::get_task_control()->
  231. // timer_thread()._idset.empty());
  232. ASSERT_EQ(EINVAL, bthread_stop(th));
  233. }
  234. bthread::butex_destroy(butex);
  235. }
  236. void* join_the_waiter(void* arg) {
  237. EXPECT_EQ(ESTOP, bthread_join((bthread_t)arg, NULL));
  238. return NULL;
  239. }
  240. TEST(ButexTest, join_cant_be_wakeup) {
  241. const long WAIT_MSEC = 100;
  242. int* butex = bthread::butex_create_checked<int>();
  243. *butex = 7;
  244. butil::Timer tm;
  245. ButexWaitArg arg = { butex, *butex, 1000, ESTOP };
  246. for (int i = 0; i < 2; ++i) {
  247. const bthread_attr_t attr =
  248. (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
  249. tm.start();
  250. bthread_t th, th2;
  251. ASSERT_EQ(0, bthread_start_urgent(&th, NULL, wait_butex, &arg));
  252. ASSERT_EQ(0, bthread_start_urgent(&th2, &attr, join_the_waiter, (void*)th));
  253. ASSERT_EQ(0, bthread_stop(th2));
  254. ASSERT_EQ(0, bthread_usleep(WAIT_MSEC / 2 * 1000L));
  255. ASSERT_TRUE(bthread::TaskGroup::exists(th));
  256. ASSERT_TRUE(bthread::TaskGroup::exists(th2));
  257. ASSERT_EQ(0, bthread_usleep(WAIT_MSEC / 2 * 1000L));
  258. ASSERT_EQ(0, bthread_stop(th));
  259. ASSERT_EQ(0, bthread_join(th2, NULL));
  260. ASSERT_EQ(0, bthread_join(th, NULL));
  261. tm.stop();
  262. ASSERT_LT(tm.m_elapsed(), WAIT_MSEC + 10);
  263. ASSERT_EQ(EINVAL, bthread_stop(th));
  264. ASSERT_EQ(EINVAL, bthread_stop(th2));
  265. }
  266. bthread::butex_destroy(butex);
  267. }
  268. TEST(ButexTest, stop_after_slept) {
  269. butil::Timer tm;
  270. const long SLEEP_MSEC = 100;
  271. const long WAIT_MSEC = 10;
  272. for (int i = 0; i < 2; ++i) {
  273. const bthread_attr_t attr =
  274. (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
  275. tm.start();
  276. bthread_t th;
  277. ASSERT_EQ(0, bthread_start_urgent(
  278. &th, &attr, sleeper, (void*)(SLEEP_MSEC*1000L)));
  279. ASSERT_EQ(0, bthread_usleep(WAIT_MSEC * 1000L));
  280. ASSERT_EQ(0, bthread_stop(th));
  281. ASSERT_EQ(0, bthread_join(th, NULL));
  282. tm.stop();
  283. if (attr.stack_type == BTHREAD_STACKTYPE_PTHREAD) {
  284. ASSERT_LT(abs(tm.m_elapsed() - SLEEP_MSEC), 15);
  285. } else {
  286. ASSERT_LT(abs(tm.m_elapsed() - WAIT_MSEC), 15);
  287. }
  288. // ASSERT_TRUE(bthread::get_task_control()->
  289. // timer_thread()._idset.empty());
  290. ASSERT_EQ(EINVAL, bthread_stop(th));
  291. }
  292. }
  293. TEST(ButexTest, stop_just_when_sleeping) {
  294. butil::Timer tm;
  295. const long SLEEP_MSEC = 100;
  296. for (int i = 0; i < 2; ++i) {
  297. const bthread_attr_t attr =
  298. (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
  299. tm.start();
  300. bthread_t th;
  301. ASSERT_EQ(0, bthread_start_urgent(
  302. &th, &attr, sleeper, (void*)(SLEEP_MSEC*1000L)));
  303. ASSERT_EQ(0, bthread_stop(th));
  304. ASSERT_EQ(0, bthread_join(th, NULL));
  305. tm.stop();
  306. if (attr.stack_type == BTHREAD_STACKTYPE_PTHREAD) {
  307. ASSERT_LT(abs(tm.m_elapsed() - SLEEP_MSEC), 15);
  308. } else {
  309. ASSERT_LT(tm.m_elapsed(), 15);
  310. }
  311. // ASSERT_TRUE(bthread::get_task_control()->
  312. // timer_thread()._idset.empty());
  313. ASSERT_EQ(EINVAL, bthread_stop(th));
  314. }
  315. }
  316. TEST(ButexTest, stop_before_sleeping) {
  317. butil::Timer tm;
  318. const long SLEEP_MSEC = 100;
  319. for (int i = 0; i < 2; ++i) {
  320. bthread_t th;
  321. const bthread_attr_t attr =
  322. (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL) | BTHREAD_NOSIGNAL;
  323. tm.start();
  324. ASSERT_EQ(0, bthread_start_background(&th, &attr, sleeper,
  325. (void*)(SLEEP_MSEC*1000L)));
  326. ASSERT_EQ(0, bthread_stop(th));
  327. bthread_flush();
  328. ASSERT_EQ(0, bthread_join(th, NULL));
  329. tm.stop();
  330. if (attr.stack_type == BTHREAD_STACKTYPE_PTHREAD) {
  331. ASSERT_LT(abs(tm.m_elapsed() - SLEEP_MSEC), 10);
  332. } else {
  333. ASSERT_LT(tm.m_elapsed(), 10);
  334. }
  335. // ASSERT_TRUE(bthread::get_task_control()->
  336. // timer_thread()._idset.empty());
  337. ASSERT_EQ(EINVAL, bthread_stop(th));
  338. }
  339. }
  340. } // namespace