| 22 | |
| 23 | DEFINE_FLAG(int, size, 8, "log2(number of DFA nodes)"); |
| 24 | DEFINE_FLAG(int, repeat, 2, "Repetition count."); |
| 25 | DEFINE_FLAG(int, threads, 4, "number of threads"); |
| 26 | |
| 27 | namespace re2 { |
| 28 | |
| 29 | static int state_cache_resets = 0; |
| 30 | static int search_failures = 0; |
| 31 | |
| 32 | struct SetHooks { |
| 33 | SetHooks() { |
| 34 | hooks::SetDFAStateCacheResetHook([](const hooks::DFAStateCacheReset&) { |
| 35 | ++state_cache_resets; |
| 36 | }); |
| 37 | hooks::SetDFASearchFailureHook([](const hooks::DFASearchFailure&) { |
| 38 | ++search_failures; |
| 39 | }); |
| 40 | } |
| 41 | } set_hooks; |
| 42 | |
| 43 | // Check that multithreaded access to DFA class works. |
| 44 | |
| 45 | // Helper function: builds entire DFA for prog. |
| 46 | static void DoBuild(Prog* prog) { |
| 47 | ASSERT_TRUE(prog->BuildEntireDFA(Prog::kFirstMatch, nullptr)); |
| 48 | } |
| 49 | |
| 50 | TEST(Multithreaded, BuildEntireDFA) { |
| 51 | // Create regexp with 2^FLAGS_size states in DFA. |
| 52 | std::string s = "a"; |
| 53 | for (int i = 0; i < GetFlag(FLAGS_size); i++) |
| 54 | s += "[ab]"; |
| 55 | s += "b"; |
| 56 | Regexp* re = Regexp::Parse(s, Regexp::LikePerl, NULL); |
| 57 | ASSERT_TRUE(re != NULL); |
| 58 | |
| 59 | // Check that single-threaded code works. |
| 60 | { |
| 61 | Prog* prog = re->CompileToProg(0); |
| 62 | ASSERT_TRUE(prog != NULL); |
| 63 | |
| 64 | std::thread t(DoBuild, prog); |
| 65 | t.join(); |
| 66 | |
| 67 | delete prog; |
| 68 | } |
| 69 | |
| 70 | // Build the DFA simultaneously in a bunch of threads. |
| 71 | for (int i = 0; i < GetFlag(FLAGS_repeat); i++) { |
| 72 | Prog* prog = re->CompileToProg(0); |
| 73 | ASSERT_TRUE(prog != NULL); |
| 74 | |
| 75 | std::vector<std::thread> threads; |
| 76 | for (int j = 0; j < GetFlag(FLAGS_threads); j++) |
| 77 | threads.emplace_back(DoBuild, prog); |
| 78 | for (int j = 0; j < GetFlag(FLAGS_threads); j++) |
| 79 | threads[j].join(); |
| 80 | |
| 81 | // One more compile, to make sure everything is okay. |
no test coverage detected