forked from bronevet/old_sight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsight_structure_init.h
More file actions
103 lines (86 loc) · 4.02 KB
/
sight_structure_init.h
File metadata and controls
103 lines (86 loc) · 4.02 KB
1
2
3
4
5
6
7
8
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#pragma once
#include <list>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <iostream>
#include <sstream>
#include <ostream>
#include <fstream>
#include <stdarg.h>
#include <assert.h>
#include "sight_common.h"
#include "utils.h"
#include "tools/callpath/include/Callpath.h"
#include "tools/callpath/include/CallpathRuntime.h"
#include "thread_local_storage.h"
#include <signal.h>
#include <deque>
#include <boost/graph/topological_sort.hpp>
#include <boost/graph/adjacency_list.hpp>
namespace sight {
namespace structure{
// This mechanism allows different widgets to register their own code to be called when each
// thread is started and stopped. This includes both the main thread and threads created
// using pthread_create. Each widget can provide initialization and finalization methods,
// and order them relative to other widgets' methods to maintain relative order among different
// initialization/finalization logic.
// Initializers/finalizers are specified by
// - deriving a widget-specific class from ThreadInitFinInstantiator that uses method
// addFuncs() in its constructor to register the widget-specific functions, and
// - declaring a single global variable of this type.
typedef void (*ThreadInitializer)();
typedef void (*ThreadFinalizer)();
class ThreadInitFinInstantiator : public sight::common::LoadTimeRegistry {
// The maximum unique ID assigned to any threadFuncs so far
static int* maxUID;
class threadFuncs {
public:
int UID;
ThreadInitializer init;
ThreadFinalizer fin;
set<std::string> mustFollow;
set<std::string> mustPrecede;
threadFuncs(ThreadInitializer init, ThreadFinalizer fin,
const set<std::string>& mustFollow, const set<std::string>& mustPrecede) :
UID((*maxUID)++), init(init), fin(fin), mustFollow(mustFollow), mustPrecede(mustPrecede) {}
std::string str() const {
return txt()<<"[threadFuncs: UID="<<UID<<", mustFollow="<<set2str(mustFollow)<<", mustPrecede="<<set2str(mustPrecede)<<"]";
}
};
public:
// Keep track of the funcs that are currently registered to initialize and finalize threads
// Maps the string labels of funcs to their threadFuncs data structures
// Each threadFuncs has a unique ID that is used as the vertex number in the dependence
// graph, which is implemented using the Boost Graph Library
static std::map<std::string, threadFuncs*>* funcsM;
// Vector of pointers to the threadFuncs in funcsM, where the index of each record is
// equal to its UID.
static std::vector<threadFuncs*>* funcsV;
// Records the dependencies among the entries in funcs
static boost::adjacency_list<boost::listS, boost::vecS, boost::directedS>* depGraph;
// Records the topological order among the funcs
static std::deque<int>* topoOrder;
// Records whether the dependence graph is upto-date relative to the current
// entries in funcs
static bool* depGraphUptoDate;
ThreadInitFinInstantiator();
// Called exactly once for each class that derives from LoadTimeRegistry to initialize its static data structures.
static void init();
// Adds the given initialization/finalization funcs under the given widet label.
// mustFollow - set of widget labels that the given functors must follow during initialization
// mustPrecede - set of widget labels that the given functors must precede during initialization
// finalization is performed in reverse
static void addFuncs(const std::string& label, ThreadInitializer init, ThreadFinalizer fin,
const common::easyset<std::string>& mustFollow, const common::easyset<std::string>& mustPrecede);
// Update the dependence graph based on funcs, if it not already upto-date
static void computeDepGraph();
// Calls all the initializers in their topological order
static void initialize();
// Calls all the finalizers in their topological order (reverse of initializers)
static void finalize();
static std::string str();
};
} // namespace structure
} // namespace sight