rippled
Loading...
Searching...
No Matches
beast_CurrentThreadName_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2017 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/beast/core/CurrentThreadName.h>
21#include <xrpl/beast/unit_test.h>
22
23#include <thread>
24
25namespace ripple {
26namespace test {
27
29{
30private:
31 static void
33 std::string myName,
35 std::atomic<int>* state)
36 {
37 // Verify that upon creation a thread has no name.
38 auto const initialThreadName = beast::getCurrentThreadName();
39
40 // Set the new name.
42
43 // Indicate to caller that the name is set.
44 *state = 1;
45
46 // If there is an initial thread name then we failed.
47 if (!initialThreadName.empty())
48 return;
49
50 // Wait until all threads have their names.
51 while (!*stop)
52 ;
53
54 // Make sure the thread name that we set before is still there
55 // (not overwritten by, for instance, another thread).
56 if (beast::getCurrentThreadName() == myName)
57 *state = 2;
58 }
59
60public:
61 void
62 run() override
63 {
64 // Make two different threads with two different names. Make sure
65 // that the expected thread names are still there when the thread
66 // exits.
67 std::atomic<bool> stop{false};
68
69 std::atomic<int> stateA{0};
70 std::thread tA(exerciseName, "tA", &stop, &stateA);
71
72 std::atomic<int> stateB{0};
73 std::thread tB(exerciseName, "tB", &stop, &stateB);
74
75 // Wait until both threads have set their names.
76 while (stateA == 0 || stateB == 0)
77 ;
78
79 stop = true;
80 tA.join();
81 tB.join();
82
83 // Both threads should still have the expected name when they exit.
84 BEAST_EXPECT(stateA == 2);
85 BEAST_EXPECT(stateB == 2);
86 }
87};
88
89BEAST_DEFINE_TESTSUITE(CurrentThreadName, core, beast);
90
91} // namespace test
92} // namespace ripple
A testsuite class.
Definition: suite.h:55
static void exerciseName(std::string myName, std::atomic< bool > *stop, std::atomic< int > *state)
T join(T... args)
void setCurrentThreadName(std::string_view newThreadName)
Changes the name of the caller thread.
std::string getCurrentThreadName()
Returns the name of the caller thread.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25