@@ -17,26 +17,6 @@ import (
17
17
// ErrProcessDone indicates a [Process] has finished.
18
18
var ErrProcessDone = errors .New ("os: process already finished" )
19
19
20
- type processMode uint8
21
-
22
- const (
23
- // modePID means that Process operations such use the raw PID from the
24
- // Pid field. handle is not used.
25
- //
26
- // This may be due to the host not supporting handles, or because
27
- // Process was created as a literal, leaving handle unset.
28
- //
29
- // This must be the zero value so Process literals get modePID.
30
- modePID processMode = iota
31
-
32
- // modeHandle means that Process operations use handle, which is
33
- // initialized with an OS process handle.
34
- //
35
- // Note that Release and Wait will deactivate and eventually close the
36
- // handle, so acquire may fail, indicating the reason.
37
- modeHandle
38
- )
39
-
40
20
type processStatus uint64
41
21
42
22
const (
@@ -58,15 +38,13 @@ const (
58
38
type Process struct {
59
39
Pid int
60
40
61
- mode processMode
62
-
63
41
// State contains the atomic process state.
64
42
//
65
- // In modePID , this consists only of the processStatus fields, which
66
- // indicate if the process is done/released.
43
+ // If handle is nil , this consists only of the processStatus fields,
44
+ // which indicate if the process is done/released.
67
45
//
68
- // In modeHandle , the lower bits also contain a reference count for the
69
- // handle field.
46
+ // In handle is not nil , the lower bits also contain a reference
47
+ // count for the handle field.
70
48
//
71
49
// The Process itself initially holds 1 persistent reference. Any
72
50
// operation that uses the handle with a system call temporarily holds
@@ -87,7 +65,7 @@ type Process struct {
87
65
// errors returned by concurrent calls.
88
66
state atomic.Uint64
89
67
90
- // Used only in modePID.
68
+ // Used only when handle is nil
91
69
sigMu sync.RWMutex // avoid race between wait and signal
92
70
93
71
// handle, if not nil, is a pointer to a struct
@@ -154,8 +132,7 @@ func (ph *processHandle) release() {
154
132
155
133
func newPIDProcess (pid int ) * Process {
156
134
p := & Process {
157
- Pid : pid ,
158
- mode : modePID ,
135
+ Pid : pid ,
159
136
}
160
137
runtime .SetFinalizer (p , (* Process ).Release )
161
138
return p
@@ -172,7 +149,6 @@ func newHandleProcess(pid int, handle uintptr) *Process {
172
149
173
150
p := & Process {
174
151
Pid : pid ,
175
- mode : modeHandle ,
176
152
handle : ph ,
177
153
}
178
154
p .state .Store (1 ) // 1 persistent reference
@@ -182,16 +158,15 @@ func newHandleProcess(pid int, handle uintptr) *Process {
182
158
183
159
func newDoneProcess (pid int ) * Process {
184
160
p := & Process {
185
- Pid : pid ,
186
- mode : modePID ,
161
+ Pid : pid ,
187
162
}
188
163
p .state .Store (uint64 (statusDone )) // No persistent reference, as there is no handle.
189
164
runtime .SetFinalizer (p , (* Process ).Release )
190
165
return p
191
166
}
192
167
193
168
func (p * Process ) handleTransientAcquire () (uintptr , processStatus ) {
194
- if p .mode != modeHandle {
169
+ if p .handle == nil {
195
170
panic ("handleTransientAcquire called in invalid mode" )
196
171
}
197
172
@@ -213,7 +188,7 @@ func (p *Process) handleTransientAcquire() (uintptr, processStatus) {
213
188
}
214
189
215
190
func (p * Process ) handleTransientRelease () {
216
- if p .mode != modeHandle {
191
+ if p .handle == nil {
217
192
panic ("handleTransientRelease called in invalid mode" )
218
193
}
219
194
@@ -250,7 +225,7 @@ func (p *Process) handleTransientRelease() {
250
225
// Returns the status prior to this call. If this is not statusOK, then the
251
226
// reference was not dropped or status changed.
252
227
func (p * Process ) handlePersistentRelease (reason processStatus ) processStatus {
253
- if p .mode != modeHandle {
228
+ if p .handle == nil {
254
229
panic ("handlePersistentRelease called in invalid mode" )
255
230
}
256
231
@@ -280,15 +255,15 @@ func (p *Process) handlePersistentRelease(reason processStatus) processStatus {
280
255
}
281
256
282
257
func (p * Process ) pidStatus () processStatus {
283
- if p .mode != modePID {
258
+ if p .handle != nil {
284
259
panic ("pidStatus called in invalid mode" )
285
260
}
286
261
287
262
return processStatus (p .state .Load ())
288
263
}
289
264
290
265
func (p * Process ) pidDeactivate (reason processStatus ) {
291
- if p .mode != modePID {
266
+ if p .handle != nil {
292
267
panic ("pidDeactivate called in invalid mode" )
293
268
}
294
269
0 commit comments