[ create a new paste ] login | about

Link: http://codepad.org/kO3MJK60    [ raw code | fork ]

D, pasted on Sep 28:
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
/*
Object-oriented D version of the systems programming language benchmark
Author:  M. J. Jordan  Cambridge Computer Laboratory.

Modified by:  M. Richards, Nov 1996
  to be ANSI C and runnable on 64 bit machines + other minor changes
Modified by:  M. Richards, 20 Oct 1998
  made minor corrections to improve ANSI compliance (suggested
  by David Levine)

Ported to Object-oriented D from the C# version.
This version has getters/setters and final classes.
*/

version (Tango) {
    import tango.stdc.stdio: putchar, printf;
    import Integer = tango.text.convert.Integer;
    alias Integer.parse toInt;
} else {
    import std.c.stdio: putchar, printf;
    import std.conv: toInt;
}


// set this to true to see the debug output
const bool TraceOn = false;


final class Scheduler {
    enum TaskId {
        Idle,
        Worker,
        HandlerA,
        HandlerB,
        DeviceA,
        DeviceB
    }

    const TaskId Idle = TaskId.Idle;
    const TaskId Worker = TaskId.Worker;
    const TaskId HandlerA = TaskId.HandlerA;
    const TaskId HandlerB = TaskId.HandlerB;
    const TaskId DeviceA = TaskId.DeviceA;
    const TaskId DeviceB = TaskId.DeviceB;
    private const int MaxTasks = 10;

    private Tcb[MaxTasks] table;
    private Tcb list = null;
    private Tcb currentTcb;
    private TaskId currentId;

    int queueCount = 0;
    int holdCount = 0;

    void AddIdleTask(TaskId anId, int aPriority, Packet aWorkQueue, int aCount) {
        AddRunningTask(anId, aPriority, aWorkQueue, new IdleTask(this, 1, aCount));
    }

    void AddWorkerTask(TaskId anId, int aPriority, Packet aWorkQueue) {
        AddTask(anId, aPriority, aWorkQueue,
           new WorkerTask(this, HandlerA, 0));
    }

    void AddHandlerTask(TaskId anId, int aPriority, Packet aWorkQueue) {
        AddTask(anId, aPriority, aWorkQueue, new HandlerTask(this));
    }

    void AddDeviceTask(TaskId anId, int aPriority, Packet aWorkQueue) {
        AddTask(anId, aPriority, aWorkQueue, new DeviceTask(this));
    }

    protected void AddTask(TaskId anId, int aPriority, Packet aWorkQueue, ISchedulerTask aTask) {
        currentTcb = new Tcb(list, anId, aPriority, aWorkQueue, aTask);
        list = currentTcb;
        table[cast(int)anId] = currentTcb;
    }

    protected void AddRunningTask(TaskId anId, int aPriority, Packet aWorkQueue, ISchedulerTask aTask) {
        AddTask(anId, aPriority, aWorkQueue, aTask);
        currentTcb.SetRunning();
    }

    void Schedule() {
        currentTcb = list;
        while (currentTcb !is null) {
            if (currentTcb.IsHeldOrSuspended())
                currentTcb = currentTcb.Link;
            else {
                currentId = currentTcb.Id;
                static if (TraceOn) Bench.Trace(cast(byte)(cast(int)'0' + currentId + 1));
                currentTcb = currentTcb.Run();
            }
        }
    }

    Tcb Queue(Packet aPacket) {
        Tcb t = table[cast(int)aPacket.Id];
        if (t is null)
            return t;
        queueCount++;
        aPacket.Link = null;
        aPacket.Id = currentId;
        return t.CheckPriorityAdd(currentTcb, aPacket);
    }

    Tcb Release(TaskId anId) {
        Tcb t = table[cast(int)anId];
        if (t is null)
            return t;
        t.NotHeld();
        if (t.Priority > currentTcb.Priority)
            return t;
        else
            return currentTcb;
    }

    Tcb HoldCurrent() {
        ++holdCount;
        currentTcb.Held();
        return currentTcb.Link;
    }

    Tcb SuspendCurrent() {
        currentTcb.Suspended();
        return currentTcb;
    }
}


interface ISchedulerTask {
    Tcb run(Packet aPacket);
}

/* try this instead
abstract class SchedulerTask {
    abstract Tcb run(Packet aPacket);
}
*/


final class DeviceTask : ISchedulerTask {
    Packet v1;
    Scheduler s;

    this(Scheduler aScheduler) {
        s = aScheduler;
    }

    public Tcb run(Packet aPacket) {
        if (aPacket is null) {
            if (v1 is null)
                return s.SuspendCurrent();
            Packet v = v1;
            v1 = null;
            return s.Queue(v);
        } else {
            v1 = aPacket;
            static if (TraceOn) Bench.Trace(aPacket.A1);
            return s.HoldCurrent();
        }
    }
}



final class HandlerTask : ISchedulerTask {
    Packet v1, v2;
    Scheduler s;

    this(Scheduler aScheduler) {
        s = aScheduler;
    }

    public Tcb run(Packet aPacket) {
        if (aPacket !is null) {
            if (aPacket.Kind == Packet.Work)
                v1 = aPacket.AddTo(v1);
            else
                v2 = aPacket.AddTo(v2);
        }
        if (v1 !is null) {
            byte count = v1.A1;
            Packet v;
            if (count < Packet.dataSize) {
                if (v2 !is null) {
                    v = v2;
                    v2 = v2.Link;
                    v.A1 = v1.A2[count];
                    v1.A1 = cast(byte)(count + 1);
                    return s.Queue(v);
                }
            } else {
                v = v1;
                v1 = v1.Link;
                return s.Queue(v);
            }
        }
        return s.SuspendCurrent();
    }
}


final class IdleTask : ISchedulerTask {
    int v1, v2;
    Scheduler s;

    this(Scheduler aScheduler, int aValue1, int aValue2) {
        v1 = aValue1;
        v2 = aValue2;
        s = aScheduler;
    }

    public Tcb run(Packet aPacket) {
        v2--;
        if (v2 == 0)
            return s.HoldCurrent();

        if ((v1 & 1) == 0) {
            v1 = v1 >> 1;
            return s.Release(Scheduler.DeviceA);
        } else {
            v1 = (v1 >> 1) ^ 0xD008;
            return s.Release(Scheduler.DeviceB);
        }
    }
}


final class WorkerTask : ISchedulerTask {
    Scheduler.TaskId v1;
    int v2;
    Scheduler s;

    this(Scheduler aScheduler, Scheduler.TaskId aValue1, int aValue2) {
        v1 = aValue1;
        v2 = aValue2;
        s = aScheduler;
    }

    public Tcb run(Packet aPacket) {
        if (aPacket is null) {
            return s.SuspendCurrent();
        } else {
            if (v1 == Scheduler.HandlerA)
                v1 = Scheduler.HandlerB;
            else
                v1 = Scheduler.HandlerA;
            aPacket.Id = v1;
            aPacket.A1 = 0;
            for (int i = 0; i < Packet.dataSize; i++) {
                v2++;
                if (v2 > 26)
                    v2 = 1;
                aPacket.A2[i] = cast(byte)('A' + v2 - 1);
            }
            return s.Queue(aPacket);
        }
    }
}


final class Tcb {
    // Variables from spec
    private Tcb link;       // pointer to another tcb or nil
    private Scheduler.TaskId id;  // identifier (a small integer)
    private int pri;        // priority (a positive integer)
    private Packet wkq;      // list of Packets in the tasks work queue
    private int state;      // a 3 bit value giving the state
    private ISchedulerTask task;

    this(Tcb aTcb, Scheduler.TaskId anId, int aPriority, Packet aWorkQueue, ISchedulerTask aTask) {
        link = aTcb;
        id = anId;
        pri = aPriority;
        wkq = aWorkQueue;
        task = aTask;
        if (wkq is null)
            state = SUSPENDED;
        else
            state = SUSPENDED_RUNNABLE;
    }

    ///<value>The next Tcb or null.</value>
    Tcb Link() {
        return this.link;
    }

    Scheduler.TaskId Id() {
        return this.id;
    }

    int Priority() {
        return this.pri;
    }

    Tcb CheckPriorityAdd(Tcb aTask, Packet aPacket) {
        if (wkq is null) {
            wkq = aPacket;
            state = state | RUNNABLE;
            if (pri > aTask.Priority) { return this; }
        } else {
            wkq = aPacket.AddTo(wkq);
        }
        return aTask;
    }

    Tcb Run() {
        Packet packet;
        if (state == SUSPENDED_RUNNABLE) {
            packet = wkq;
            wkq = packet.Link;
            if (wkq is null)
                state = RUNNING;
            else
                state = RUNNABLE;
        } else {
            packet = null;
        }
        return task.run(packet);
    }

    // Named bit masks to access 3 bit state value.
    static const int RUNNING = 0;
    static const int RUNNABLE = 1;
    static const int SUSPENDED = 2;
    static const int HELD = 4;
    static const int SUSPENDED_RUNNABLE = SUSPENDED | RUNNABLE;
    static const int NOT_HELD = ~HELD;

    void SetRunning() {
        state = RUNNING;
    }

    void Suspended() {
        state = state | SUSPENDED;
    }

    void Held() {
        state = state | HELD;
    }

    void NotHeld() {
        state = state & NOT_HELD;
    }

    bool IsHeldOrSuspended() {
        return (state & HELD) != 0 || (state == SUSPENDED);
    }
}


final class Packet {
    enum Kinds { Device, Work };

    const int dataSize = 4;
    // Variables from spec
    private Packet link;     // pointer to the next Packet or nil
    private Scheduler.TaskId id;  // task or device that sent the packet
    private Kinds kind;      // part of the message
    private byte a1 = 0;     // part of the message
    private byte[dataSize] a2;


    // Device & Work alias the enum Kinds values.
    const Kinds Device = Kinds.Device;
    const Kinds Work = Kinds.Work;

    this(Packet aLink, Scheduler.TaskId anId, Kinds aKind) {
        link = aLink;
        id = anId;
        kind = aKind;
    }

    Packet AddTo(Packet aQueue) {
        Packet next, peek;
        link = null;
        if (aQueue is null)
            return this;
        next = aQueue;
        while ((peek = next.Link) !is null)
            next = peek;
        next.Link = this;
        return aQueue;
    }

    Packet Link() {
        return this.link;
    }

    void Link(Packet value) {
        return this.link = value;
    }

    Scheduler.TaskId Id() {
        return this.id;
    }

    void Id(Scheduler.TaskId value) {
        this.id = value;
    }

    Kinds Kind() {
        return this.kind;
    }

    byte A1() {
        return a1;
    }

    void A1(byte value) {
        this.a1 = value;
    }

    byte[] A2() {
        return this.a2;
    }
}


final class Bench {
    static int Layout = 0;

    static void Trace(byte aByte) {
        --Layout;
        if (Layout <= 0) {
            putchar('\n');
            Layout = 50;
        }
        putchar(aByte);
    }
}


void main(char[][] args) {
    int count = 10000;
    if (args.length > 1)
        count = toInt(args[1]);

    Scheduler s = new Scheduler();
    Packet wkq;

    s.AddIdleTask(Scheduler.Idle, 0, null, count);

    wkq = new Packet(null, Scheduler.Worker, Packet.Work);
    wkq = new Packet(wkq, Scheduler.Worker, Packet.Work);
    s.AddWorkerTask(Scheduler.Worker, 1000, wkq);

    wkq = new Packet(null, Scheduler.DeviceA, Packet.Device);
    wkq = new Packet(wkq, Scheduler.DeviceA, Packet.Device);
    wkq = new Packet(wkq, Scheduler.DeviceA, Packet.Device);
    s.AddHandlerTask(Scheduler.HandlerA, 2000, wkq);

    wkq = new Packet(null, Scheduler.DeviceB, Packet.Device);
    wkq = new Packet(wkq, Scheduler.DeviceB, Packet.Device);
    wkq = new Packet(wkq, Scheduler.DeviceB, Packet.Device);
    s.AddHandlerTask(Scheduler.HandlerB, 3000, wkq);

    s.AddDeviceTask(Scheduler.DeviceA, 4000, null);
    s.AddDeviceTask(Scheduler.DeviceB, 5000, null);

    s.Schedule();

    printf("QueueCount = %d\n", s.queueCount);
    printf("HoldCount = %d\n", s.holdCount);
}


Create a new paste based on this one


Comments: