Commit 917ceef5 authored by DEPRECATED (Jonathan Mace) (Use @JonathanMace instead)'s avatar DEPRECATED (Jonathan Mace) (Use @JonathanMace instead)
Browse files

Add some auto baggage macros

parent dec5547e
......@@ -91,5 +91,32 @@ namespace ThreadLocalBaggage {
}
#define BAGGAGE(b) BaggageHelpers::AutoBaggageScopeImpl __baggage__auto_baggage_(&b, __FILE__, __LINE__)
namespace BaggageHelpers {
// Used by AutoBaggageScope macro to scope the current block to the provided baggage
class AutoBaggageScopeImpl {
private:
Baggage* baggageSource;
Baggage suspendedBaggage;
const char* file;
int line;
public:
AutoBaggageScopeImpl(Baggage* b, const char* file, int line) : file(file), line(line) {
baggageSource = b;
suspendedBaggage = ThreadLocalBaggage::Swap(*b, file, line);
}
~AutoBaggageScopeImpl() {
*baggageSource = ThreadLocalBaggage::Swap(suspendedBaggage, file, line);
}
};
}
#endif
\ No newline at end of file
......@@ -33,7 +33,6 @@ int main(int argc, char *argv[]) {
XTRACE("c", {{"key1", "value1"}, {"key2", "value2"}});
Baggage branched_baggage = BRANCH_CURRENT_BAGGAGE();
std::thread branched_thread([&branched_baggage]() {
SET_CURRENT_BAGGAGE(branched_baggage);
XTRACE("f");
......@@ -41,11 +40,35 @@ int main(int argc, char *argv[]) {
branched_baggage = TAKE_CURRENT_BAGGAGE();
});
// Use the auto baggage macro
Baggage branched_baggage2 = BRANCH_CURRENT_BAGGAGE();
std::thread branched_thread2([&branched_baggage2]() {
BAGGAGE(branched_baggage2);
XTRACE("f");
XTRACE("g");
});
Baggage inline_branched_baggage = BRANCH_CURRENT_BAGGAGE();
{
BAGGAGE(inline_branched_baggage);
XTRACE("hello world");
XTRACE("goodbyte world");
}
XTRACE("d");
XTRACE("e");
branched_thread.join();
branched_thread2.join();
JOIN_CURRENT_BAGGAGE(branched_baggage);
JOIN_CURRENT_BAGGAGE(branched_baggage2);
JOIN_CURRENT_BAGGAGE(inline_branched_baggage);
XTRACE("h");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment