baggage.h 3.8 KB
Newer Older
1
2
3
4
5
6
7
#ifndef _BAGGAGE_H_
#define _BAGGAGE_H_

#include <iostream>
#include <cstdint>
#include <cstddef>
#include <vector>
8
#include <string>
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47


/*
This file defines the main interfaces (Atoms and Baggage) of the Brown Tracing Plane

Most interactions will be with Baggage and ThreadLocalBaggage
*/


// An atom is just an array of bytes
struct Atom {

	std::vector<uint8_t> bytes;

	Atom() : bytes(0) {}
	Atom(int size) : bytes(size) {}
	Atom(std::vector<uint8_t> bytes) : bytes(bytes) {}

	// Lexicographic comparison of two atoms
	const int compare(const Atom& other);

	friend std::ostream& operator<< ( std::ostream& os, const Atom& atom );
};


// Baggage is an array of atoms
struct Baggage {

	std::vector<Atom> atoms;

	Baggage() : atoms(0) {}

	// Duplicates this baggage
	Baggage branch();

	// Lexicographic merge of two baggage instances, as defined in the Brown Tracing Plane
	static Baggage merge(Baggage& a, Baggage& b);

	// Serialize this baggage, as defined in the Brown Tracing Plane (length-prefix each atom in order)
48
	std::string str();
49
50
51
	std::vector<uint8_t> serialize();

	// Deserialize a serialized baggage instance, as defined in the Brown Tracing Plane
52
	static Baggage deserialize(std::string bytes);
53
54
55
56
57
58
59
	static Baggage deserialize(std::vector<uint8_t> bytes);

	friend std::ostream& operator<< ( std::ostream& os, const Baggage& baggage );

};


60
61
62
63
64
65
66
67
68
#define GET_CURRENT_BAGGAGE() ThreadLocalBaggage::Get(__FILE__, __LINE__)
#define TAKE_CURRENT_BAGGAGE() ThreadLocalBaggage::Take(__FILE__, __LINE__)
#define BRANCH_CURRENT_BAGGAGE() ThreadLocalBaggage::Branch(__FILE__, __LINE__)
#define DELETE_CURRENT_BAGGAGE() ThreadLocalBaggage::Delete(__FILE__, __LINE__)
#define SET_CURRENT_BAGGAGE(b) ThreadLocalBaggage::Set(b, __FILE__, __LINE__)
#define JOIN_CURRENT_BAGGAGE(b) ThreadLocalBaggage::Join(b, __FILE__, __LINE__)
#define SWAP_CURRENT_BAGGAGE(b) ThreadLocalBaggage::Swap(b, __FILE__, __LINE__)


69
70
71
// Helper methods for storing a baggage instance in thread-local storage.  Usage is optional, but very useful
namespace ThreadLocalBaggage {

72
	Baggage& Get();                      // Get the current thread's baggage
73
74
75
76
77
78
	Baggage Take();                      // Get the current thread's baggage, and clear the thread-local storage
	Baggage Branch();                    // Get a copy of the current thread's baggage

	void Delete();                       // Delete the current thread's baggage
	void Set(Baggage new_baggage);       // Set the current thread's baggage to the provided baggage, destroying the previous baggage
	void Join(Baggage &otherBaggage);    // Merge the current thread's baggage with the provided baggage
79
	Baggage Swap (Baggage otherBaggage); // Set the current thread's baggage to the provided baggage, and return the previous baggage
80

81
82
83
84
85
86
87
88
89
90
91


	Baggage& Get(const char* file, int line);	                   
	Baggage Take(const char* file, int line);                      
	Baggage Branch(const char* file, int line);                    

	void Delete(const char* file, int line);                       
	void Set(Baggage new_baggage, const char* file, int line);      
	void Join(Baggage &otherBaggage, const char* file, int line);   
	Baggage Swap (Baggage otherBaggage, const char* file, int line);

92
93
}

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#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);
		}

	};


}

121
122

#endif