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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
use ui;
use graphics;
use core:geometry;
use core:lang;
use progvis:data;
/**
* Representation of a thread.
*/
class ThreadView extends DataView {
// Create.
init(progvis:data:Thread thread) {
init {
thread = thread;
enableStepFn = &alwaysTrue;
source(thread.src.owner);
step(stepDecoration, SolidBrush(yellow*0.5 + red), buttonSize);
bigStep(bigStepDecoration, SolidBrush(yellow*0.5 + red), buttonSize);
pause(pauseDecoration, SolidBrush(black), buttonSize);
runSlow(runDecoration, SolidBrush(green), buttonSize);
runFast(runFastDecoration, SolidBrush(green), buttonSize);
runVeryFast(runVeryFastDecoration, SolidBrush(green), buttonSize);
removeDead(removeDecoration, SolidBrush(red), buttonSize);
srcSize = sourceSize;
}
step.onClick = &this.onStep;
bigStep.onClick = &this.onBigStep;
pause.onClick = &this.onPause;
runSlow.onClick = &this.onRunSlow;
runFast.onClick = &this.onRunFast;
runVeryFast.onClick = &this.onRunVeryFast;
removeDead.onClick = &this.onRemove;
buttons = [step, bigStep, pause, runSlow, runFast, runVeryFast];
}
// Thread we are controlling.
private progvis:data:Thread thread;
// Source code view.
SourceView source;
// Shall we be removed (when the user clicks "remove").
Bool removeMe;
// Enable stepping.
Fn<Bool> enableStepFn;
// Buttons.
private SmallButton step;
private SmallButton bigStep;
private SmallButton pause;
private SmallButton runSlow;
private SmallButton runFast;
private SmallButton runVeryFast;
private SmallButton removeDead;
// Buttons. Duplicates of the variables for easier layout.
private SmallButton[] buttons;
// When resizing the ThreadData, we are essentially resizing only the source listing. So this is
// the size of the source listing.
private Size srcSize;
// Stack frames.
private WithCaption[] frames;
// Layout offset for frames.
private Float[] layoutOffset;
// Pointer to the allocation for this thread, so that we can remove the thread.
private StickyAllocation? srcAlloc;
// Set the allocation.
assign alloc(Allocation a) {
if (a as StickyAllocation)
srcAlloc = a;
}
// Don't remove the thread until it has terminated and the user explicitly removes it.
Bool remove() : override {
removeMe;
}
// Mark this thread as being the next step in some way.
void markNext(Bool barrier) {
clearNext();
if (barrier)
bigStep.marked = true;
else
step.marked = true;
}
// Clear markings for the next step.
void clearNext() {
bigStep.marked = false;
step.marked = false;
}
// Get the source data.
Data? srcData() : override { thread; }
// Get the thread ID.
Nat threadId() { thread.id; }
void update(Workspace:Update ws) : override {
for (id, f in thread.frames) {
if (id >= frames.count) {
frames << create(ws.world, f);
layoutOffset << 0;
} else if (frames[id].srcData !is f) {
frames[id] = create(ws.world, f);
}
frames[id].update(ws);
}
while (frames.count > thread.frames.count)
frames.pop();
if (thread.alive) {
if (pos = thread.pos)
source.display(pos);
else
source.displayEmpty();
if (thread.sleeping) {
source.highlight = sourceSleepingHighlight;
} else if (thread.afterReturn) {
source.highlight = sourceReturnedHighlight;
} else {
source.highlight = sourceHighlight;
}
} else {
if (!thread.crashed) {
source.highlight = sourceReturnedHighlight;
source.displayEmpty("<thread exited normally>");
} else {
source.highlight = sourceTerminatedHighlight;
}
if (buttons.last !is removeDead)
buttons << removeDead;
}
invalidateSize();
}
private WithCaption create(World w, progvis:data:Thread:Frame f) {
WithCaption(CompositeView(f), f.title(w));
}
void resize(Size s) : override {
Size computed = computeSize();
srcSize = (s - (computed - srcSize)).max(Size(10));
invalidateSize();
}
protected Size computeSize() : override {
Float w = 0;
Float h = 0;
if (frames.any) {
for (Nat i = frames.count; i > 0; i--) {
var frame = frames[i - 1];
h += frame.captionSize.h + compositeBorder.h;
Size sz = frame.size;
h = max(h, sz.h);
w = max(w, sz.w + i.int.float*stackOffset);
}
}
w = max(srcSize.w, w);
w = max(w, buttons.count.int.float * (buttonSize.w + compositeBorder.w));
h += compositeBorder.h + srcSize.h;
h += compositeBorder.h + buttonSize.h;
Size(w, h) + compositeBorder*2;
}
void draw(Graphics g, Bool active) : override {
super:draw(g, active);
Rect rect = this.rect;
Point offset = rect.p0 + compositeBorder;
Float xOffset = 0;
for (Nat i = frames.count; i > 0; i--) {
var frame = frames[i - 1];
Size sz = frame.size;
if (i < frames.count) {
if (c = frame.wrap as CompositeView)
xOffset -= frame.size.w - c.maxDataWidth - dataBorder.w*3;
// xOffset -= c.maxNameWidth;
}
layoutOffset[i - 1] = xOffset;
xOffset += sz.w;
}
Float xLimit = rect.size.w - compositeBorder.w*2;
// Right-align everything.
if (xOffset < xLimit)
offset.x += xLimit - xOffset;
for (id, frame in frames) {
Size sz = frame.size;
Float dx = min(layoutOffset[id], xLimit - sz.w);
frame.pos = offset + Point(dx, 0);
Float delta = frame.captionSize.h + compositeBorder.h;
offset.y += delta;
xLimit -= stackOffset;
}
Rect sourcePos(rect.p0, rect.p1 - compositeBorder);
sourcePos.p1.y -= compositeBorder.h + buttonSize.h;
sourcePos.p0.x = rect.p0.x + compositeBorder.w;
sourcePos.p0.y = sourcePos.p1.y - srcSize.h;
source.draw(g, sourcePos);
Point p(sourcePos.p0.x, sourcePos.p1.y + compositeBorder.h);
for (b in buttons) {
if (!showingButton(b))
continue;
b.pos = p;
b.draw(g, false);
p.x += buttonSize.w + compositeBorder.w;
}
// Draw almost everything now. We only omit pointers from the last stack frame. Otherwise
// they will appear below other threads.
for (id, frame in frames) {
Bool a = id == frames.count - 1;
frame.draw(g, a);
if (!a)
frame.drawLinks(g, false);
}
}
void drawLinks(Graphics g, Bool active) : override {
if (frames.any)
frames.last.drawLinks(g, active);
}
Drawable findAt(Point pt) : override {
for (b in buttons) {
if (!showingButton(b))
continue;
if (b.rect.contains(pt))
return b;
}
this;
}
private void onStep() {
if (enableStepFn.call())
thread.src.resume();
}
private void onBigStep() {
if (enableStepFn.call())
thread.src.resumeBarrier();
}
private void onPause() {
if (enableStepFn.call())
thread.src.pause();
}
private void onRunSlow() {
if (enableStepFn.call())
thread.src.resume(1 s);
}
private void onRunFast() {
if (enableStepFn.call())
thread.src.resume(500 ms);
}
private void onRunVeryFast() {
if (enableStepFn.call())
thread.src.resume(100 ms);
}
private void onRemove() {
removeMe = true;
if (srcAlloc)
srcAlloc.unstick();
// Make it happen now, and not later.
thread.src.owner.notifyChange(true);
}
private Bool showingButton(SmallButton b) {
if (b is bigStep)
thread.barriersAvailable;
else
true;
}
}
|