This file is indexed.

/usr/share/gnu-smalltalk/examples/Richards.st is in gnu-smalltalk-common 3.2.4-2.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
"======================================================================
|
|   The Richards Benchmark in Smalltalk
|
|
 ======================================================================"


"======================================================================
|
| Copyright 2000 Free Software Foundation, Inc.
|
| This file is part of GNU Smalltalk.
|
| GNU Smalltalk is free software; you can redistribute it and/or modify it
| under the terms of the GNU General Public License as published by the Free
| Software Foundation; either version 2, or (at your option) any later version.
| 
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
| FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
| details.
| 
| You should have received a copy of the GNU General Public License along with
| GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
| Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  
|
 ======================================================================"

Object subclass: #RBObject
    instanceVariableNames: ''
    classVariableNames: 'DeviceA DeviceB DevicePacketKind HandlerA HandlerB Idler Worker WorkPacketKind '
    poolDictionaries: ''
    category: 'Examples-Richards'!

RBObject subclass: #Packet
    instanceVariableNames: 'link identity kind datum data '
    classVariableNames: ''
    poolDictionaries: ''
    category: 'Examples-Richards'!

RBObject subclass: #RichardsBenchmark
    instanceVariableNames: 'taskList currentTask currentTaskIdentity taskTable tracing layout queuePacketCount holdCount '
    classVariableNames: ''
    poolDictionaries: ''
    category: 'Examples-Richards'!

RBObject subclass: #TaskState
    instanceVariableNames: 'packetPendingIV taskWaiting taskHolding '
    classVariableNames: ''
    poolDictionaries: ''
    category: 'Examples-Richards'!

RBObject subclass: #DeviceTaskDataRecord
    instanceVariableNames: 'pending '
    classVariableNames: ''
    poolDictionaries: ''
    category: 'Examples-Richards'!

RBObject subclass: #HandlerTaskDataRecord
    instanceVariableNames: 'workIn deviceIn '
    classVariableNames: ''
    poolDictionaries: ''
    category: 'Examples-Richards'!

RBObject subclass: #WorkerTaskDataRecord
    instanceVariableNames: 'destination count '
    classVariableNames: ''
    poolDictionaries: ''
    category: 'Examples-Richards'!

TaskState subclass: #TaskControlBlock
    instanceVariableNames: 'link identity priority input state function handle '
    classVariableNames: ''
    poolDictionaries: ''
    category: 'Examples-Richards'!

RBObject subclass: #IdleTaskDataRecord
    instanceVariableNames: 'control count '
    classVariableNames: ''
    poolDictionaries: ''
    category: 'Examples-Richards'!


!RBObject methodsFor: 'utilities'!

append: packet head: queueHead
    | mouse link |
    packet link: nil.
    queueHead isNil ifTrue: [^packet].
    mouse := queueHead.
    [(link := mouse link) isNil]
	whileFalse: [mouse := link].
    mouse link: packet.
    ^queueHead! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

RBObject class
    instanceVariableNames: ''!


!RBObject class methodsFor: 'initialize'!

initialize
    "RBObject initialize"
    DeviceA := 5.
    DeviceB := 6.
    DevicePacketKind := 1.
    HandlerA := 3.
    HandlerB := 4.
    Idler := 1.
    Worker := 2.
    WorkPacketKind := 2! !

RBObject initialize!




!Packet methodsFor: 'initialize'!

link: aLink identity: anIdentity kind: aKind 
    link := aLink.
    identity := anIdentity. 
    kind := aKind.
    datum := 1.
    data := ByteArray new: 4! !

!Packet methodsFor: 'accessing'!

data
    ^data!

datum
    ^datum!

datum:    someData
    datum := someData!

identity
    ^identity!

identity: anIdentity
    identity := anIdentity!

kind
    ^kind!

link
    ^link!

link: aWorkQueue 
    link := aWorkQueue! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

Packet class
    instanceVariableNames: ''!


!Packet class methodsFor: 'instance creation'!

create: link identity: identity kind: kind 
    ^super new
	link: link
	identity: identity
	kind: kind! !



!RichardsBenchmark methodsFor: 'creation'!

createDevice: identity priority: priority work: work state: state 
    | data |
    data := DeviceTaskDataRecord create.
    self
	createTask: identity
	priority: priority
	work: work
	state: state
	function: 
	    [:work :word | | data functionWork |
	    data := word.
	    functionWork := work.
	    functionWork isNil
		ifTrue:
		    [(functionWork := data pending) isNil
			ifTrue: [self wait]
			ifFalse: 
			    [data pending: nil.
			    self queuePacket: functionWork]]
		ifFalse: 
		    [data pending: functionWork.
		    tracing
			ifTrue: 
			    [self trace: functionWork datum].
		    self holdSelf]]
	data: data!

createHandler: identity priority: priority work: work state: state 
    | data |
    data := HandlerTaskDataRecord create.
    self
	createTask: identity
	priority: priority
	work: work
	state: state
	function: 
	    [:work :word | | data workPacket count devicePacket |
	    data := word.
	    work isNil
		ifFalse: [WorkPacketKind == work kind 
		    ifTrue: [data workInAdd: work]
		    ifFalse: [data deviceInAdd: work]].
	    (workPacket := data workIn) isNil
		ifTrue: [self wait]
		ifFalse: 
		    [count := workPacket datum.
		    count > 4
			ifTrue: 
			    [data workIn: workPacket link.
			    self queuePacket: workPacket]
			ifFalse:
			    [(devicePacket := data deviceIn) isNil
				ifTrue: [self wait]
				ifFalse: 
				    [data deviceIn: devicePacket link.
				    devicePacket datum: (workPacket data at: count).
				    workPacket datum: count + 1.
				    self queuePacket: devicePacket]]]]
	data: data!

createIdler: identity priority: priority work: work state: state 
    | data |
    data := IdleTaskDataRecord create.
    self
	createTask: identity
	priority: priority
	work: work
	state: state
	function: 
	    [:work :word | | data |
	    data := word.
	    data count: data count - 1.
	    0 = data count
		ifTrue: [self holdSelf]
		ifFalse:
		    [0 = (data control bitAnd: 1)
			ifTrue: 
			    [data control: data control // 2.
			    self release: DeviceA]
			ifFalse: 
			    [data control: (data control // 2 bitXor: 53256).
			    self release: DeviceB]]]
	data: data!

createPacket: link identity: identity kind: kind 
    ^Packet
	create: link
	identity: identity
	kind: kind!

createTask: identity priority: priority work: work state: state function: aBlock data: data 
    | t |
    t := TaskControlBlock
		link: taskList
		create: identity
		priority: priority
		initialWorkQueue: work
		initialState: state
		function: aBlock
		privateData: data.
    taskList := t.
    taskTable at: identity put: t!

createWorker: identity priority: priority work: work state: state 
    | data |
    data := WorkerTaskDataRecord create.
    self
	createTask: identity
	priority: priority
	work: work
	state: state
	function: 
	    [:work :word | | data |
	    data := word.
	    work isNil
		ifTrue: [self wait]
		ifFalse: 
		    [data destination: (HandlerA = data destination
			    ifTrue: [HandlerB]
			    ifFalse: [HandlerA]).
		    work identity: data destination.
		    work datum: 1.
		    1 to: 4 do: 
			[:i | 
			data count: data count + 1.
			data count > 26 ifTrue: [data count: 1].
			work data at: i put: $A asInteger + data count - 1].
		    self queuePacket: work]]
	data: data! !

!RichardsBenchmark methodsFor: 'private'!

findTask: identity 
    | t |
    t := taskTable at: identity.
    t isNil ifTrue: [self error: 'findTask failed'].
    ^t!

holdSelf
    holdCount := holdCount + 1.
    currentTask taskHolding: true.
    ^currentTask link!

initScheduler
    queuePacketCount := holdCount := 0.
    taskTable := Array new: 6.
    taskList := nil!

initTrace: trace
    tracing := trace.
    layout := 0!

queuePacket: packet 
    | t |
    t := self findTask: packet identity.
    t isNil ifTrue: [^nil].
    queuePacketCount := queuePacketCount + 1.
    packet link: nil.
    packet identity: currentTaskIdentity.
    ^t addInput: packet checkPriority: currentTask!

release: identity 
    | t |
    t := self findTask: identity.
    t isNil ifTrue: [^nil].
    t taskHolding: false.
    t priority > currentTask priority
	ifTrue: [^t]
	ifFalse: [^currentTask]!

trace: id 
    layout := layout - 1.
    0 >= layout
	ifTrue: 
	    [Transcript cr.
	    layout := 50].
    Transcript show: id printString!

wait 
    currentTask taskWaiting: true.
    ^currentTask! !

!RichardsBenchmark methodsFor: 'scheduling'!

schedule
    currentTask := taskList. 
    [currentTask isNil]
	whileFalse: 
	    [currentTask isTaskHoldingOrWaiting
		ifTrue: [currentTask := currentTask link]
		ifFalse: 
		    [currentTaskIdentity := currentTask identity.
		    tracing ifTrue: [self trace: currentTaskIdentity].
		    currentTask := currentTask runTask]]! !

!RichardsBenchmark methodsFor: 'initialize'!

start
    ^self start: false
!

start: trace
    | workQ mark1 mark2 mark3 mark4 |
    self initTrace: trace; initScheduler.
    mark1 := Time millisecondClockValue.
    tracing ifTrue: [Transcript show: 'Bench mark starting'; cr].
    self
	createIdler: Idler
	priority: 0
	work: nil
	state: TaskState running.
    workQ := self
		createPacket: nil
		identity: Worker
		kind: WorkPacketKind.
    workQ := self
		createPacket: workQ
		identity: Worker
		kind: WorkPacketKind.
    self
	createWorker: Worker
	priority: 1000
	work: workQ
	state: TaskState waitingWithPacket.
    workQ := self
		createPacket: nil
		identity: DeviceA
		kind: DevicePacketKind.
    workQ := self
		createPacket: workQ
		identity: DeviceA
		kind: DevicePacketKind.
    workQ := self
		createPacket: workQ
		identity: DeviceA
		kind: DevicePacketKind.
    self
	createHandler: HandlerA
	priority: 2000
	work: workQ
	state: TaskState waitingWithPacket.
    workQ := self
		createPacket: nil
		identity: DeviceB
		kind: DevicePacketKind.
    workQ := self
		createPacket: workQ
		identity: DeviceB
		kind: DevicePacketKind.
    workQ := self
		createPacket: workQ
		identity: DeviceB
		kind: DevicePacketKind.
    self
	createHandler: HandlerB
	priority: 3000
	work: workQ
	state: TaskState waitingWithPacket.
    self
	createDevice: DeviceA
	priority: 4000
	work: nil
	state: TaskState waiting.
    self
	createDevice: DeviceB
	priority: 5000
	work: nil
	state: TaskState waiting.
    tracing ifTrue: [Transcript show: 'Starting'; cr].
    mark2 := Time millisecondClockValue.
    self schedule.
    mark3 := Time millisecondClockValue.
    tracing
	ifTrue: 
	    [Transcript show: 'Finished'; cr.
	    Transcript show: 'QueuePacket count = ' ;
		show: queuePacketCount printString;
		show: ' HoldCount = ';
		show: holdCount printString;
		cr.
	    Transcript cr; show: 'End of run'; cr].
    queuePacketCount = 23246 & (holdCount = 9297) ifFalse: [self error: 'wrong result'].
    mark4 := Time millisecondClockValue.
    tracing
	ifTrue:
	    [Transcript show: '***Scheduler time = ';
	        show: (mark3 - mark2) printString;
	        show: ' Total time = ';
	        show: (mark4 - mark1) printString;
	        cr].

  ^mark3 - mark2! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

RichardsBenchmark class
    instanceVariableNames: ''!

!RichardsBenchmark class methodsFor: 'instance creation'!

report: string times: count run: aBlock
        "Report the time required to execute the given block."

        | time |
        time := 0.
	count timesRepeat: [ time := time + aBlock value ].
        Transcript show: string, ' ', (time // count) printString, ' milliseconds'; cr.!

standardBenchmark
        "This the combined benchmark."
        "Planner standardBenchmark"

        self report: 'Richards simulations' times: 100 run: [ self start ]!

start: trace
    "RichardsBenchmark start: true"

    ^self new start: trace!

start
    "RichardsBenchmark start"

    ^self new start! !



!TaskState methodsFor: 'initialize'!

packetPending
    packetPendingIV := true.
    taskWaiting := false.
    taskHolding := false!

running
    packetPendingIV := taskWaiting := taskHolding := false!

waiting
    packetPendingIV := taskHolding := false.
    taskWaiting := true!

waitingWithPacket
    taskHolding := false.
    taskWaiting := packetPendingIV := true! !

!TaskState methodsFor: 'accessing'!

isPacketPending
    ^packetPendingIV!

isTaskHolding
    ^taskHolding!

isTaskWaiting
    ^taskWaiting!

taskHolding: aBoolean
    taskHolding := aBoolean!

taskWaiting: aBoolean 
    taskWaiting := aBoolean! !

!TaskState methodsFor: 'testing'!

isRunning
    ^packetPendingIV not and: [taskWaiting not and: [taskHolding not]]!

isTaskHoldingOrWaiting
    ^taskHolding or: [packetPendingIV not and: [taskWaiting]]!

isWaiting
    ^packetPendingIV not and: [taskWaiting and: [taskHolding not]]!

isWaitingWithPacket
    ^packetPendingIV and: [taskWaiting and: [taskHolding not]]! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

TaskState class
    instanceVariableNames: ''!


!TaskState class methodsFor: 'instance creation'!

packetPending
    ^super new packetPending!

running
    ^super new running!

waiting
    ^super new waiting!

waitingWithPacket
    ^super new waitingWithPacket! !



!DeviceTaskDataRecord methodsFor: 'initialize'!

create
    pending := nil! !

!DeviceTaskDataRecord methodsFor: 'accessing'!

pending
    ^pending!

pending: packet 
    pending := packet! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

DeviceTaskDataRecord class
    instanceVariableNames: ''!


!DeviceTaskDataRecord class methodsFor: 'instance creation'!

create
    ^super new create! !



!HandlerTaskDataRecord methodsFor: 'initialize'!

create
    workIn := deviceIn := nil! !

!HandlerTaskDataRecord methodsFor: 'accessing'!

deviceIn
    ^deviceIn!

deviceIn: aPacket
    deviceIn := aPacket!

deviceInAdd: packet
    deviceIn := self append: packet head: deviceIn!

workIn
    ^workIn!

workIn: aWorkQueue 
    workIn := aWorkQueue!

workInAdd: packet
    workIn := self append: packet head: workIn! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

HandlerTaskDataRecord class
    instanceVariableNames: ''!


!HandlerTaskDataRecord class methodsFor: 'instance creation'!

create
    ^super new create! !



!WorkerTaskDataRecord methodsFor: 'initialize'!

create
    destination := HandlerA.
    count := 0! !

!WorkerTaskDataRecord methodsFor: 'accessing'!

count
    ^count!

count: aCount
    count := aCount!

destination
    ^destination!

destination: aHandler
    destination := aHandler! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

WorkerTaskDataRecord class
    instanceVariableNames: ''!


!WorkerTaskDataRecord class methodsFor: 'instance creation'!

create
    ^super new create! !



!TaskControlBlock methodsFor: 'initialize'!

link: aLink identity: anIdentity priority: aPriority initialWorkQueue: anInitialWorkQueue initialState: anInitialState function: aBlock privateData: aPrivateData 
    link := aLink.
    identity := anIdentity.
    priority := aPriority.
    input := anInitialWorkQueue.
    packetPendingIV := anInitialState isPacketPending.
    taskWaiting := anInitialState isTaskWaiting.
    taskHolding := anInitialState isTaskHolding.
    function := aBlock.
    handle := aPrivateData! !

!TaskControlBlock methodsFor: 'accessing'!

identity
    ^identity!

link
    ^link!

priority
    ^priority! !

!TaskControlBlock methodsFor: 'scheduling'!

addInput: packet checkPriority: oldTask
    input isNil
	ifTrue: 
	    [input := packet.
	    packetPendingIV := true.
	    priority > oldTask priority ifTrue: [^self]]
	ifFalse: 
	    [input := self append: packet head: input].
    ^oldTask!

runTask
    | message |
    self isWaitingWithPacket
	ifTrue: 
	    [message := input.
	    input := message link.
	    input isNil
		ifTrue: [self running]
		ifFalse: [self packetPending]]
	ifFalse: [message := nil].
    ^function value: message value: handle! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

TaskControlBlock class
    instanceVariableNames: ''!


!TaskControlBlock class methodsFor: 'instance creation'!

link: link create: identity priority: priority initialWorkQueue: initialWorkQueue initialState: initialState function: aBlock privateData: privateData 
    ^super new
	link: link
	identity: identity
	priority: priority
	initialWorkQueue: initialWorkQueue
	initialState: initialState
	function: aBlock
	privateData: privateData! !


!IdleTaskDataRecord methodsFor: 'initialize'!

create
    control := 1.
    count := 10000! !

!IdleTaskDataRecord methodsFor: 'accessing'!

control
    ^control!

control: aNumber
    control := aNumber!

count
    ^count!

count: aCount
    count := aCount! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

IdleTaskDataRecord class
    instanceVariableNames: ''!


!IdleTaskDataRecord class methodsFor: 'instance creation'!

create
    ^super new create! !