-
Notifications
You must be signed in to change notification settings - Fork 1
/
thread.cpp
174 lines (147 loc) · 5.13 KB
/
thread.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* thread.cpp
*
* Copyright (C) 2017 Alexandre Luiz Brisighello Filho
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#include <stdio.h>
#include <iostream>
#include "thread.h"
#include "trace_bank.h"
#include "log.h"
#include "exec_tracker.h"
// Current thread status
THREAD_INFO *all_threads;
THREADID max_tid;
static int pram;
void thread_init(int _pram)
{
// Initialize thread information, including mutex and initial states
all_threads = (THREAD_INFO *) malloc(MAX_THREADS * sizeof(THREAD_INFO));
max_tid = 0;
pram = _pram;
for(int i = 0; i < MAX_THREADS; i++) {
all_threads[i].ins_count = 0;
all_threads[i].sync_holder = 0;
all_threads[i].pin_tid = i;
all_threads[i].status = UNREGISTERED;
all_threads[i].create_value = 0;
// If sleeping, threads should be stopped by the semaphores.
PIN_SemaphoreInit(&all_threads[i].active);
PIN_SemaphoreClear(&all_threads[i].active);
}
trace_bank_init(pram);
exec_tracker_init();
DEBUG(cerr << "[Thread] Threads structure initialized" << std::endl);
}
// Returns: 1 if all threads have finished,
// 0 otherwise.
int thread_all_finished()
{
// Not a pram can't rely on exec_track
if(pram == 0) {
for(int i = 0; i < MAX_THREADS; i++) {
if(all_threads[i].status != UNREGISTERED &&
all_threads[i].status != FINISHED) {
return 0;
}
}
return 1;
}
if(exec_track_is_empty() == 0) {
return 0;
}
// It's finished, check if it's deadlocked.
for(int i = 0; i < MAX_THREADS; i++) {
if(all_threads[i].status == UNLOCKED) {
cerr << "[PINocchio] Internal Error: exec_track says it's empty but a thread is running: ";
cerr << print_id(i) << std::endl;
fail();
} else if(all_threads[i].status == LOCKED) {
cerr << "[PINocchio] Error: Deadlock on thread ";
cerr << print_id(i) << std::endl;
fail();
}
}
return 1;
}
// Check what are the threads that can be released.
void thread_try_release_all()
{
// In other words: keep trying to start threads until it's not possible.
for(THREAD_INFO *t = exec_tracker_awake(); t != NULL; t = exec_tracker_awake()) {
// Release thread semaphore, that's all required to let thread continue.
PIN_SemaphoreSet(&t->active);
}
}
void thread_start(THREAD_INFO *target, THREAD_INFO *creator)
{
// Update holder max_tid if a bigger arrived
if(max_tid < target->pin_tid) {
max_tid = target->pin_tid;
}
// Thread 0 is special, will pass NULL and starts with 0.
// Other threads should start running and should be awake at first round.
target->ins_count = creator != NULL ? creator->ins_count : 0;
target->status = UNLOCKED;
trace_bank_register(target->pin_tid, target->ins_count);
// Thread start running or a deadlock might happen.
// If, for some reason, there is someone really advanced, next sync
// will stop anything important.
exec_tracker_plus();
PIN_SemaphoreSet(&target->active);
}
void thread_finish(THREAD_INFO *target)
{
target->status = FINISHED;
trace_bank_finish(target->pin_tid, target->ins_count);
exec_tracker_minus();
}
// Should:
// - Lock the semaphore. It will be awake, so it requires to be locked.
// - Update trace bank, it's a change on thread state.
// - Update exec_tracker running counter.
void thread_lock(THREAD_INFO *target)
{
PIN_SemaphoreClear(&target->active);
target->status = LOCKED;
trace_bank_update(target->pin_tid, target->ins_count, LOCKED);
exec_tracker_minus();
}
void thread_unlock(THREAD_INFO *target, THREAD_INFO *unlocker)
{
target->status = UNLOCKED;
// Why check it? Because with the period option, a thread could be awaken
// by a thread in the past. Avoid time travel, please.
if(unlocker->ins_count > target->ins_count) {
target->ins_count = unlocker->ins_count;
}
trace_bank_update(target->pin_tid, target->ins_count, UNLOCKED);
exec_tracker_insert(target);
}
void thread_sleep(THREAD_INFO *target)
{
// Should only wait on semaphore if it was actually added to exec tracker
if(exec_tracker_sleep(target) > 0) {
PIN_SemaphoreClear(&target->active);
}
}
// It has advanced if exec_tracker ins_max has changed
int thread_has_advanced()
{
return exec_tracker_changed();
}
void print_threads()
{
const char *status[] = {"UNLOCKED", "LOCKED", "UNREGISTERED", "FINISHED"};
cerr << "--------- thread status ---------" << std::endl;
for(UINT32 i = 0; i <= max_tid; i++) {
cerr << "Thread id: " << i << std::endl;
cerr << " - status: " << status[all_threads[i].status] << std::endl;
cerr << " - create_value: " << all_threads[i].create_value << std::endl;
cerr << " - ins_count: " << all_threads[i].ins_count << std::endl;
cerr << " - active: " << PIN_SemaphoreIsSet(&all_threads[i].active) << std::endl;
}
cerr << "------------------------ " << std::endl;
}