Skip to content

Commit b485e5b

Browse files
ianlancetaylorgopherbot
authored andcommitted
os: remove Process.mode field
It's now redundant with checking whether the handle field is nil. For #70907 Change-Id: I877f2a7c63d15ab5f8e3d2c9aa24776c2e3e2056 Reviewed-on: https://go-review.googlesource.com/c/go/+/638576 Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Commit-Queue: Ian Lance Taylor <[email protected]>
1 parent 5c2b5e0 commit b485e5b

File tree

3 files changed

+19
-51
lines changed

3 files changed

+19
-51
lines changed

src/os/exec.go

+12-37
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,6 @@ import (
1717
// ErrProcessDone indicates a [Process] has finished.
1818
var ErrProcessDone = errors.New("os: process already finished")
1919

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-
4020
type processStatus uint64
4121

4222
const (
@@ -58,15 +38,13 @@ const (
5838
type Process struct {
5939
Pid int
6040

61-
mode processMode
62-
6341
// State contains the atomic process state.
6442
//
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.
6745
//
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.
7048
//
7149
// The Process itself initially holds 1 persistent reference. Any
7250
// operation that uses the handle with a system call temporarily holds
@@ -87,7 +65,7 @@ type Process struct {
8765
// errors returned by concurrent calls.
8866
state atomic.Uint64
8967

90-
// Used only in modePID.
68+
// Used only when handle is nil
9169
sigMu sync.RWMutex // avoid race between wait and signal
9270

9371
// handle, if not nil, is a pointer to a struct
@@ -154,8 +132,7 @@ func (ph *processHandle) release() {
154132

155133
func newPIDProcess(pid int) *Process {
156134
p := &Process{
157-
Pid: pid,
158-
mode: modePID,
135+
Pid: pid,
159136
}
160137
runtime.SetFinalizer(p, (*Process).Release)
161138
return p
@@ -172,7 +149,6 @@ func newHandleProcess(pid int, handle uintptr) *Process {
172149

173150
p := &Process{
174151
Pid: pid,
175-
mode: modeHandle,
176152
handle: ph,
177153
}
178154
p.state.Store(1) // 1 persistent reference
@@ -182,16 +158,15 @@ func newHandleProcess(pid int, handle uintptr) *Process {
182158

183159
func newDoneProcess(pid int) *Process {
184160
p := &Process{
185-
Pid: pid,
186-
mode: modePID,
161+
Pid: pid,
187162
}
188163
p.state.Store(uint64(statusDone)) // No persistent reference, as there is no handle.
189164
runtime.SetFinalizer(p, (*Process).Release)
190165
return p
191166
}
192167

193168
func (p *Process) handleTransientAcquire() (uintptr, processStatus) {
194-
if p.mode != modeHandle {
169+
if p.handle == nil {
195170
panic("handleTransientAcquire called in invalid mode")
196171
}
197172

@@ -213,7 +188,7 @@ func (p *Process) handleTransientAcquire() (uintptr, processStatus) {
213188
}
214189

215190
func (p *Process) handleTransientRelease() {
216-
if p.mode != modeHandle {
191+
if p.handle == nil {
217192
panic("handleTransientRelease called in invalid mode")
218193
}
219194

@@ -250,7 +225,7 @@ func (p *Process) handleTransientRelease() {
250225
// Returns the status prior to this call. If this is not statusOK, then the
251226
// reference was not dropped or status changed.
252227
func (p *Process) handlePersistentRelease(reason processStatus) processStatus {
253-
if p.mode != modeHandle {
228+
if p.handle == nil {
254229
panic("handlePersistentRelease called in invalid mode")
255230
}
256231

@@ -280,15 +255,15 @@ func (p *Process) handlePersistentRelease(reason processStatus) processStatus {
280255
}
281256

282257
func (p *Process) pidStatus() processStatus {
283-
if p.mode != modePID {
258+
if p.handle != nil {
284259
panic("pidStatus called in invalid mode")
285260
}
286261

287262
return processStatus(p.state.Load())
288263
}
289264

290265
func (p *Process) pidDeactivate(reason processStatus) {
291-
if p.mode != modePID {
266+
if p.handle != nil {
292267
panic("pidDeactivate called in invalid mode")
293268
}
294269

src/os/exec_unix.go

+6-13
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ const (
2121

2222
func (p *Process) wait() (ps *ProcessState, err error) {
2323
// Which type of Process do we have?
24-
switch p.mode {
25-
case modeHandle:
24+
if p.handle != nil {
2625
// pidfd
2726
return p.pidfdWait()
28-
case modePID:
27+
} else {
2928
// Regular PID
3029
return p.pidWait()
31-
default:
32-
panic("unreachable")
3330
}
3431
}
3532

@@ -85,15 +82,12 @@ func (p *Process) signal(sig Signal) error {
8582
}
8683

8784
// Which type of Process do we have?
88-
switch p.mode {
89-
case modeHandle:
85+
if p.handle != nil {
9086
// pidfd
9187
return p.pidfdSendSignal(s)
92-
case modePID:
88+
} else {
9389
// Regular PID
9490
return p.pidSignal(s)
95-
default:
96-
panic("unreachable")
9791
}
9892
}
9993

@@ -131,15 +125,14 @@ func (p *Process) release() error {
131125
// solely on statusReleased to determine that the Process is released.
132126
p.Pid = pidReleased
133127

134-
switch p.mode {
135-
case modeHandle:
128+
if p.handle != nil {
136129
// Drop the Process' reference and mark handle unusable for
137130
// future calls.
138131
//
139132
// Ignore the return value: we don't care if this was a no-op
140133
// racing with Wait, or a double Release.
141134
p.handlePersistentRelease(statusReleased)
142-
case modePID:
135+
} else {
143136
// Just mark the PID unusable.
144137
p.pidDeactivate(statusReleased)
145138
}

src/os/exec_windows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"time"
1313
)
1414

15-
// Note that Process.mode is always modeHandle because Windows always requires
15+
// Note that Process.handle is never nil because Windows always requires
1616
// a handle. A manually-created Process literal is not valid.
1717

1818
func (p *Process) wait() (ps *ProcessState, err error) {

0 commit comments

Comments
 (0)