-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththreads.cpp
62 lines (51 loc) · 1.27 KB
/
threads.cpp
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
#include "threads.h"
#include <assert.h>
#include <string>
#include <sstream>
// TLS globals
TLS_KEY tls_key;
#ifdef THREADSAFE
PIN_LOCK lock;
#endif
THREADID numThreads;
void initVSThreading()
{
numThreads = 0;
// Initialize the lock
#ifdef THREADSAFE
PIN_InitLock(&lock);
#endif
}
void initThread(THREADID threadid, bool tracing)
{
#ifdef THREADSAFE
PIN_GetLock(&lock, threadid+1);
#endif
numThreads++;
#ifdef THREADSAFE
PIN_ReleaseLock(&lock);
#endif
thread_data_t* tdata = new thread_data_t;
tdata->lastBB = 0;
tdata->tracing = tracing;
// String local debuglog
stringstream logbuilder;
string logfile;
logbuilder << "thread_" << threadid << ".log";
logbuilder >> logfile;
if(KnobDebugTrace)
tdata->debuglog = fopen(logfile.c_str(), "w");
assert(tdata != nullptr);
PIN_SetThreadData(tls_key, tdata, threadid);
#ifndef THREADSAFE
if(numThreads>1)
cerr << "Traced program has multiple threads and Vector Seeker is not built with thread support" << endl;
#endif
}
// function to access thread-specific data
thread_data_t* get_tls(THREADID threadid)
{
thread_data_t* tdata =
static_cast<thread_data_t*>(PIN_GetThreadData(tls_key, threadid));
return tdata;
}