This file is indexed.

/usr/share/gnu-smalltalk/kernel/Process.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
"======================================================================
|
|   Process Method Definitions
|
|
 ======================================================================"

"======================================================================
|
| Copyright 1988,92,94,95,99,2000,2001,2002,2003,2007,2008,2009
| Free Software Foundation, Inc.
| Written by Steve Byrne.
|
| This file is part of the GNU Smalltalk class library.
|
| The GNU Smalltalk class library is free software; you can redistribute it
| and/or modify it under the terms of the GNU Lesser General Public License
| as published by the Free Software Foundation; either version 2.1, or (at
| your option) any later version.
| 
| The GNU Smalltalk class library 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 Lesser
| General Public License for more details.
| 
| You should have received a copy of the GNU Lesser General Public License
| along with the GNU Smalltalk class library; see the file COPYING.LIB.
| If not, write to the Free Software Foundation, 59 Temple Place - Suite
| 330, Boston, MA 02110-1301, USA.  
|
 ======================================================================"



Link subclass: Process [
    | suspendedContext priority myList name environment interruptLock interrupts |
    
    <category: 'Language-Processes'>
    <comment: 'I represent a unit of computation.  My instances are independantly
executable blocks that have a priority associated with them, and they
can suspend themselves and resume themselves however they wish.'>

    Process class >> on: aBlockClosure at: aPriority suspend: aBoolean [
	"Private - Create a process running aBlockClosure at the given
	 priority.  The process is suspended immediately after
	 initialization if aBoolean is true"

	<category: 'private'>
	^self new 
	    onBlock: aBlockClosure
	    at: aPriority
	    suspend: aBoolean
    ]

    debugger [
	"Return the object in charge of debugging the receiver.  This always returns
	 nil unless the DebugTools package is loaded."

	<category: 'basic'>
	^self context debugger
    ]

    context [
	"Return the execution context of the receiver."

	<category: 'basic'>
	^self == Processor activeProcess 
	    ifTrue: [thisContext parentContext]
	    ifFalse: [suspendedContext]
    ]

    makeUntrusted: aBoolean [
	"Set whether the receiver is trusted or not."

	<category: 'basic'>
	| ctx |
	ctx := self context.
	[ctx isNil] whileFalse: 
		[ctx makeUntrusted: aBoolean.
		ctx := ctx parentContext]
    ]

    lowerPriority [
	"Lower a bit the priority of the receiver. A #lowerPriority will
	 cancel a previous #raisePriority, and vice versa."

	<category: 'basic'>
	self priority: self priority - 1
    ]

    raisePriority [
	"Raise a bit the priority of the receiver. A #lowerPriority will
	 cancel a previous #raisePriority, and vice versa."

	<category: 'basic'>
	self priority: self priority + 1
    ]

    singleStep [
	"Execute a limited amount of code (usually a bytecode, or up to the
	 next backward jump, or up to the next message send) of the receiver,
	 which must in a ready-to-run state (neither executing nor terminating
	 nor suspended), then restart running the current process.  The current
	 process should have higher priority than the receiver.  For better
	 performance, use the underlying primitive, Process>>#singleStepWaitingOn:."

	<category: 'basic'>
	^self singleStepWaitingOn: Semaphore new
    ]

    suspend [
	"Do nothing if we're already suspended. Note that the blue book made
	 suspend a primitive - but the real primitive is yielding control to
	 another process. Suspending is nothing more than taking ourselves out
	 of every scheduling list and THEN yielding control to another process"

	<category: 'builtins'>
	<primitive: VMpr_Process_suspend>
    ]

    finalize [
	"Terminate processes that are GCed while waiting on a dead semaphore."

	<category: 'basic'>
	^self terminate
    ]

    terminate [
	"Terminate the receiver after having evaluated all the #ensure: and
	 #ifCurtailed: blocks that are active in it.  This is done by signalling
	 a ProcessBeingTerminated notification."

	<category: 'basic'>
	| semaphore |

	[self isTerminated ifTrue: [^self].
	Processor activeProcess == self 
	    ifFalse: 
		[semaphore := self isWaiting ifTrue: [myList].
		self queueInterrupt:
		    [SystemExceptions.ProcessBeingTerminated new
			semaphore: semaphore;
			signal].
		^self]] 
		valueWithoutPreemption.
	SystemExceptions.ProcessBeingTerminated signal
    ]

    update: aSymbol [
	"Private - Terminate the process when ObjectMemory class>>#quit:
	 is sent.  This is invoked only after #terminateOnQuit."

	<category: 'private'>
        aSymbol == #aboutToQuit ifTrue: [ self terminate ]
    ]

    terminateOnQuit [
	"Mark the receiver so that it is terminated when
	 ObjectMemory class>>#quit: is sent."

	<category: 'basic'>
        ObjectMemory addDependent: self
    ]

    primTerminate [
	"Terminate the receiver - This is nothing more than prohibiting to
	 resume the process, then suspending it."

	<category: 'basic'>
	self removeToBeFinalized.
	suspendedContext := nil.
	self suspend
    ]

    printOn: aStream [
	"Print a representation of the receiver on aStream"

	<category: 'printing'>
	aStream
	    print: self class;
	    nextPut: $(;
	    print: name;
	    nextPutAll: ' at ';
	    nextPutAll: (Processor priorityName: self priority);
	    nextPut: $,.

	"The order here is important!"
	self isActive 
	    ifTrue: 
		[aStream nextPutAll: ' active)'.
		^self].
	self isTerminated 
	    ifTrue: 
		[aStream nextPutAll: ' terminated)'.
		^self].
	self isWaiting 
	    ifTrue: 
		[aStream nextPutAll: ' waiting on a semaphore)'.
		^self].
	self isSuspended 
	    ifTrue: 
		[aStream nextPutAll: ' suspended)'.
		^self].
	self isReady 
	    ifTrue: 
		[aStream nextPutAll: ' ready to run)'.
		^self].
	aStream nextPutAll: ' undefined state)'
    ]

    externalInterruptsEnabled [
	"Answer whether the receiver is executed with interrupts enabled"

	<category: 'accessing'>
	^interrupts isNil or: [interrupts <= 0]
    ]

    suspendedContext [
	"Answer the context that the process was executing at the time it was
	 suspended."

	<category: 'accessing'>
	^suspendedContext
    ]

    suspendedContext: aContext [
	"Modify the context that the process was executing at the time it was
	 suspended."

	<category: 'accessing'>
	suspendedContext := aContext
    ]

    name [
	"Answer the user-friendly name of the process."

	<category: 'accessing'>
	^name
    ]

    name: aString [
	"Give the name aString to the process"

	<category: 'accessing'>
	name := aString
    ]

    priority [
	"Answer the receiver's priority"

	<category: 'accessing'>
	^priority
    ]

    priority: anInteger [
	"Change the receiver's priority to anInteger"

	<category: 'accessing'>
	(anInteger between: Processor lowestPriority 
	    and: Processor highestPriority) 
		ifFalse: 
		    [SystemExceptions.ArgumentOutOfRange 
			signalOn: anInteger
			mustBeBetween: Processor lowestPriority
			and: Processor highestPriority].

	[
	    | activePriority waiting |
            activePriority := Processor activePriority.
	    waiting := self isActive not and: [ self isReady not ].
	    priority := anInteger.
	    waiting ifFalse: [
		"Atomically move the process to the right list."
                self resume.
                anInteger > activePriority ifTrue: [ Processor yield ] ].
	] valueWithoutPreemption
    ]

    valueWithoutInterrupts: aBlock [
	"Evaluate aBlock and delay all interrupts that are requested during its
	 execution to after aBlock returns."

	<category: 'accessing'>
	^self interruptLock critical: aBlock
    ]

    queueInterrupt: aBlock [
	"Force the receiver to be interrupted and to evaluate aBlock as soon as it
	 becomes the active process (this could mean NOW if the receiver is active).
	 If the process is temporarily suspended or waiting on a semaphore, it is
	 temporarily woken up so that the interrupt is processed as soon as the
	 process priority allows to do.  Answer the receiver."

	<category: 'accessing'>
	self interruptLock critical: 
		[| block suspended semaphore |
		self isActive 
		    ifTrue: 
			[aBlock value.
			^self].
		self isTerminated 
		    ifTrue: [^SystemExceptions.ProcessTerminated signalOn: self].
		semaphore := myList.
		suspended := self isReady not.
		block := suspended
			    ifFalse: 
				[self suspend.
				aBlock]
			    ifTrue: 
				[semaphore isNil 
				    ifTrue: [[self evaluate: aBlock ifNotTerminated: [self suspend]]]
				    ifFalse: [[self evaluate: aBlock ifNotTerminated: [semaphore wait]]]].
		suspendedContext := block asContext: suspendedContext.
		self resume]
    ]

    evaluate: aBlock ifNotTerminated: unwindBlock [
	<category: 'private'>
	| terminated |
	terminated := false.
	
	[aBlock on: ProcessBeingTerminated
	    do: 
		[:sig | 
		terminated := true.
		sig pass]] 
		ensure: [terminated ifFalse: [unwindBlock value]]
    ]

    environment [
	"This is private because it is not thread-safe.  Access via
	 ProcessorScheduler>>#environment only touches the environment
	 of the current process, so expensive semaphores are unnecessary.
	 We may want to revisit this in the future, but it won't be
	 backwards-incompatible."

	<category: 'private'>
	environment isNil ifTrue: [environment := IdentityDictionary new].
	^environment
    ]

    interruptLock [
	"Answer the RecursionLock object used to prevent nested interrupts."

	"Fast path for interruptLock ~~ nil."

	<category: 'private'>
	interruptLock isNil ifFalse: [^interruptLock].

	"Slow path for when initialization is needed."
	^
	["Look out for race conditions!"

	interruptLock isNil ifTrue: [interruptLock := RecursionLock new].
	interruptLock] 
		valueWithoutPreemption
    ]

    startExecution: aDirectedMessage [
	"It is important to retrieve this before we start the
	 process, because we want to choose whether to continue
	 running the new process based on the *old* activePriority,
	 not the one of the new process which is the maximum one."

	<category: 'private'>
	[aDirectedMessage send] on: SystemExceptions.ProcessBeingTerminated
	    do: [:sig | sig return]
    ]

    onBlock: aBlockClosure at: aPriority suspend: aBoolean [
	<category: 'private'>
	"It is important to retrieve this before we start the
	 process, because we want to choose whether to continue
	 running the new process based on the *old* activePriority,
	 not the one of the new process which is the maximum one."

	| closure activePriority |
	activePriority := Processor activePriority.
	closure :=
	    [[[
		"#priority: is inlined for two reasons.  First, to be able to
		 suspend the process, and second because we need to invert
		 the test on activePriority!  This because here we may want to
		 yield to the creator, while in #priority: we may want to yield
		 to the process whose priority was changed."
		priority := aPriority.
		aBoolean
		    ifTrue: [self suspend]
		    ifFalse: [
			self resume.
			aPriority < activePriority ifTrue: [ Processor yield ] ].
		aBlockClosure value]
			on: SystemExceptions.ProcessBeingTerminated
			do: 
			    [:sig | 
			    "If we terminate in the handler, the 'ensure' blocks are not
			     evaluated.  Instead, if the handler returns, the unwinding
			     is done properly."

			    sig return]] 
			ensure: [self primTerminate]].

	"Start the Process immediately so that we get into the
	 #on:do: handler.  Otherwise, we will not be able to
	 terminate the process with #terminate."
	suspendedContext := closure asContext: nil.
	priority := Processor unpreemptedPriority.
	self
	    addToBeFinalized;
	    resume.
	aPriority > Processor activePriority ifTrue: [ Processor yield ].
    ]

    isActive [
	"Answer whether the receiver is running"

	<category: 'private'>
	^self == Processor activeProcess
    ]

    isReady [
	"Answer whether the receiver is not suspended nor waiting on a
	 semaphore (maybe it is active, maybe it is not, though)"

	<category: 'private'>
	^myList == (Processor processesAt: priority)
    ]

    isSuspended [
	"Answer whether the receiver is suspended through #suspend"

	<category: 'private'>
	^myList isNil
    ]

    isTerminated [
	"Answer whether the receiver has already terminated"

	<category: 'private'>
	^suspendedContext isNil
    ]

    isWaiting [
	"Answer whether the receiver is wating on a semaphore"

	<category: 'private'>
	^self isReady not & self isSuspended not
    ]

    singleStepWaitingOn: aSemaphore [
	"Execute a limited amount of code (usually a bytecode, or up to the
	 next backward jump, or up to the next message send) of the receiver,
	 which must in a ready-to-run state (neither executing nor terminating
	 nor suspended), then restart running the current process.  aSemaphore
	 is used as a means to synchronize the execution of the current process
	 and the receiver and should have no signals on it.  The current process
	 should have higher priority than the receiver."

	<category: 'builtins'>
	<primitive: VMpr_Process_singleStepWaitingOn>
	SystemExceptions.InvalidProcessState signalOn: self
    ]

    resume [
	"Resume the receiver's execution"

	<category: 'builtins'>
	<primitive: VMpr_Process_resume>
	SystemExceptions.ProcessTerminated signalOn: self
    ]

    yield [
	"Yield control from the receiver to other processes"

	<category: 'builtins'>
	<primitive: VMpr_Process_yield>
	
    ]

    detach [
        "Do nothing, instances of Process are already detached."

        <category: 'debugging'>
    ]
]