/
KThread.java
521 lines (405 loc) · 13.1 KB
/
KThread.java
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package nachos.threads;
import nachos.machine.*;
import java.util.*;
/**
* A KThread is a thread that can be used to execute Nachos kernel code. Nachos
* allows multiple threads to run concurrently.
*
* To create a new thread of execution, first declare a class that implements
* the <tt>Runnable</tt> interface. That class then implements the <tt>run</tt>
* method. An instance of the class can then be allocated, passed as an
* argument when creating <tt>KThread</tt>, and forked. For example, a thread
* that computes pi could be written as follows:
*
* <p><blockquote><pre>
* class PiRun implements Runnable {
* public void run() {
* // compute pi
* ...
* }
* }
* </pre></blockquote>
* <p>The following code would then create a thread and start it running:
*
* <p><blockquote><pre>
* PiRun p = new PiRun();
* new KThread(p).fork();
* </pre></blockquote>
*/
public class KThread {
public static int called = 0;
private static LinkedList<KThread> joinList = new LinkedList<KThread>();
public int cont = 0;
/**
* Get the current thread.
*
* @return the current thread.
*/
public static KThread currentThread() {
Lib.assertTrue(currentThread != null);
return currentThread;
}
/**
* Allocate a new <tt>KThread</tt>. If this is the first <tt>KThread</tt>,
* create an idle thread as well.
*/
public KThread() {
if (currentThread != null) {
tcb = new TCB();
}
else {
readyQueue = ThreadedKernel.scheduler.newThreadQueue(false);
readyQueue.acquire(this);
currentThread = this;
tcb = TCB.currentTCB();
name = "main";
restoreState();
createIdleThread();
}
}
/**
* Allocate a new KThread.
*
* @param target the object whose <tt>run</tt> method is called.
*/
public KThread(Runnable target) {
this();
this.target = target;
}
/**
* Set the target of this thread.
*
* @param target the object whose <tt>run</tt> method is called.
* @return this thread.
*/
public KThread setTarget(Runnable target) {
Lib.assertTrue(status == statusNew);
this.target = target;
return this;
}
/**
* Set the name of this thread. This name is used for debugging purposes
* only.
*
* @param name the name to give to this thread.
* @return this thread.
*/
public KThread setName(String name) {
this.name = name;
return this;
}
/**
* Get the name of this thread. This name is used for debugging purposes
* only.
*
* @return the name given to this thread.
*/
public String getName() {
return name;
}
/**
* Get the full name of this thread. This includes its name along with its
* numerical ID. This name is used for debugging purposes only.
*
* @return the full name given to this thread.
*/
public String toString() {
return (name + " (#" + id + ")");
}
/**
* Deterministically and consistently compare this thread to another
* thread.
*/
public int compareTo(Object o) {
KThread thread = (KThread) o;
if (id < thread.id)
return -1;
else if (id > thread.id)
return 1;
else
return 0;
}
/**
* Causes this thread to begin execution. The result is that two threads
* are running concurrently: the current thread (which returns from the
* call to the <tt>fork</tt> method) and the other thread (which executes
* its target's <tt>run</tt> method).
*/
public void fork() {
Lib.assertTrue(status == statusNew);
Lib.assertTrue(target != null);
Lib.debug(dbgThread,
"Forking thread: " + toString() + " Runnable: " + target);
boolean intStatus = Machine.interrupt().disable();
tcb.start(new Runnable() {
public void run() {
runThread();
}
});
ready();
Machine.interrupt().restore(intStatus);
}
private void runThread() {
begin();
target.run();
finish();
}
private void begin() {
Lib.debug(dbgThread, "Beginning thread: " + toString());
Lib.assertTrue(this == currentThread);
restoreState();
Machine.interrupt().enable();
}
/**
* Finish the current thread and schedule it to be destroyed when it is
* safe to do so. This method is automatically called when a thread's
* <tt>run</tt> method returns, but it may also be called directly.
*
* The current thread cannot be immediately destroyed because its stack and
* other execution state are still in use. Instead, this thread will be
* destroyed automatically by the next thread to run, when it is safe to
* delete this thread.
*/
public static void finish(){
Lib.debug(dbgThread, "Finishing thread: " + currentThread.toString());
Machine.interrupt().disable();
Machine.autoGrader().finishingCurrentThread();
Lib.assertTrue(toBeDestroyed == null);
toBeDestroyed = currentThread;
currentThread.status = statusFinished;
KThread aux = null;
if(joinList.size()>0){
aux = joinList.getFirst();
}
if(aux != null){
aux.ready();
joinList.removeFirst();
}
sleep();
}
/**
* Relinquish the CPU if any other thread is ready to run. If so, put the
* current thread on the ready queue, so that it will eventually be
* rescheuled.
*
* <p>
* Returns immediately if no other thread is ready to run. Otherwise
* returns when the current thread is chosen to run again by
* <tt>readyQueue.nextThread()</tt>.
*
* <p>
* Interrupts are disabled, so that the current thread can atomically add
* itself to the ready queue and switch to the next thread. On return,
* restores interrupts to the previous state, in case <tt>yield()</tt> was
* called with interrupts disabled.
*/
public static void yield() {
Lib.debug(dbgThread, "Yielding thread: " + currentThread.toString());
Lib.assertTrue(currentThread.status == statusRunning);
boolean intStatus = Machine.interrupt().disable();
currentThread.ready();
runNextThread();
Machine.interrupt().restore(intStatus);
}
/**
* Relinquish the CPU, because the current thread has either finished or it
* is blocked. This thread must be the current thread.
*
* <p>
* If the current thread is blocked (on a synchronization primitive, i.e.
* a <tt>Semaphore</tt>, <tt>Lock</tt>, or <tt>Condition</tt>), eventually
* some thread will wake this thread up, putting it back on the ready queue
* so that it can be rescheduled. Otherwise, <tt>finish()</tt> should have
* scheduled this thread to be destroyed by the next thread to run.
*/
public static void sleep() {
Lib.debug(dbgThread, "Sleeping thread: " + currentThread.toString());
Lib.assertTrue(Machine.interrupt().disabled());
if (currentThread.status != statusFinished)
currentThread.status = statusBlocked;
runNextThread();
}
/**
* Moves this thread to the ready state and adds this to the scheduler's
* ready queue.
*/
public void ready() {
Lib.debug(dbgThread, "Ready thread: " + toString());
Lib.assertTrue(Machine.interrupt().disabled());
Lib.assertTrue(status != statusReady);
status = statusReady;
if (this != idleThread)
readyQueue.waitForAccess(this);
Machine.autoGrader().readyThread(this);
}
/**
* Waits for this thread to finish. If this thread is already finished,
* return immediately. This method must only be called once; the second
* call is not guaranteed to return. This thread must not be the current
* thread.
*/
public void join() {
boolean intStatus = Machine.interrupt().disable();
Lib.debug(dbgThread, "Joining to thread: " + toString());
Lib.assertTrue(this != currentThread);
if(cont == 0){
cont = 1;
if(this.status == statusFinished){
return;
} else {
this.joinList.add(currentThread);
currentThread.sleep();
}
}
Machine.interrupt().restore(intStatus);
}
/**
* Create the idle thread. Whenever there are no threads ready to be run,
* and <tt>runNextThread()</tt> is called, it will run the idle thread. The
* idle thread must never block, and it will only be allowed to run when
* all other threads are blocked.
*
* <p>
* Note that <tt>ready()</tt> never adds the idle thread to the ready set.
*/
private static void createIdleThread() {
Lib.assertTrue(idleThread == null);
idleThread = new KThread(new Runnable() {
public void run() { while (true) yield(); }
});
idleThread.setName("idle");
Machine.autoGrader().setIdleThread(idleThread);
idleThread.fork();
}
/**
* Determine the next thread to run, then dispatch the CPU to the thread
* using <tt>run()</tt>.
*/
private static void runNextThread() {
KThread nextThread = readyQueue.nextThread();
if (nextThread == null)
nextThread = idleThread;
nextThread.run();
}
/**
* Dispatch the CPU to this thread. Save the state of the current thread,
* switch to the new thread by calling <tt>TCB.contextSwitch()</tt>, and
* load the state of the new thread. The new thread becomes the current
* thread.
*
* <p>
* If the new thread and the old thread are the same, this method must
* still call <tt>saveState()</tt>, <tt>contextSwitch()</tt>, and
* <tt>restoreState()</tt>.
*
* <p>
* The state of the previously running thread must already have been
* changed from running to blocked or ready (depending on whether the
* thread is sleeping or yielding).
*
* @param finishing <tt>true</tt> if the current thread is
* finished, and should be destroyed by the new
* thread.
*/
private void run() {
Lib.assertTrue(Machine.interrupt().disabled());
Machine.yield();
currentThread.saveState();
Lib.debug(dbgThread, "Switching from: " + currentThread.toString()
+ " to: " + toString());
currentThread = this;
tcb.contextSwitch();
currentThread.restoreState();
}
/**
* Prepare this thread to be run. Set <tt>status</tt> to
* <tt>statusRunning</tt> and check <tt>toBeDestroyed</tt>.
*/
protected void restoreState() {
Lib.debug(dbgThread, "Running thread: " + currentThread.toString());
Lib.assertTrue(Machine.interrupt().disabled());
Lib.assertTrue(this == currentThread);
Lib.assertTrue(tcb == TCB.currentTCB());
Machine.autoGrader().runningThread(this);
status = statusRunning;
if (toBeDestroyed != null) {
toBeDestroyed.tcb.destroy();
toBeDestroyed.tcb = null;
toBeDestroyed = null;
}
}
/**
* Prepare this thread to give up the processor. Kernel threads do not
* need to do anything here.
*/
protected void saveState() {
Lib.assertTrue(Machine.interrupt().disabled());
Lib.assertTrue(this == currentThread);
}
private static class PingTest implements Runnable {
PingTest(int which) {
this.which = which;
}
public void run() {
for (int i=0; i<5; i++) {
System.out.println("*** thread " + which + " looped " + i + " times, Tick:" + Machine.timer().getTime());
if ((which == 1) && (i==0))
ThreadedKernel.alarm.waitUntil(1000);
if ((which == 1) && (i==1))
dos.join();
if ((which == 0) && (i==2))
dos.join();
if ((which == 2) && (i==3))
tres.join();
if ((which == 1) && (i==3))
dos.join();
currentThread.yield();
}
}
private int which;
}
/**
* Tests whether this module is working.
*/
public static void selfTest() {
Boat test = new Boat();
test.selfTest();
}
public static KThread tres = null;
public static KThread uno = null;
public static KThread dos = null;
public static KThread cero = null;// seftTest();
// Tienen que remplazarlo por el selfTest
private static final char dbgThread = 't';
/**
* Additional state used by schedulers.
*
* @see nachos.threads.PriorityScheduler.ThreadState
*/
public Object schedulingState = null;
private static final int statusNew = 0;
private static final int statusReady = 1;
private static final int statusRunning = 2;
private static final int statusBlocked = 3;
private static final int statusFinished = 4;
/**
* The status of this thread. A thread can either be new (not yet forked),
* ready (on the ready queue but not running), running, or blocked (not
* on the ready queue and not running).
*/
private int status = statusNew;
private String name = "(unnamed thread)";
private Runnable target;
private TCB tcb;
/**
* Unique identifer for this thread. Used to deterministically compare
* threads.
*/
private int id = numCreated++;
/** Number of times the KThread constructor was called. */
private static int numCreated = 0;
private static ThreadQueue readyQueue = null;
private static KThread currentThread = null;
private static KThread toBeDestroyed = null;
private static KThread idleThread = null;
}