Skip to content

Commit e7c9667

Browse files
ianlancetaylorgopherbot
authored andcommitted
os: simplify process status
Since it no longer holds a reference count, just use values. For #70907 Change-Id: I19a42583988d4f8a9133b1c837356ca0179d688c Reviewed-on: https://go-review.googlesource.com/c/go/+/638578 LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent caf29da commit e7c9667

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

src/os/exec.go

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

20-
type processStatus uint64
20+
type processStatus uint32
2121

2222
const (
23-
// PID/handle OK to use.
24-
statusOK processStatus = 0
23+
// statusOK means that the Process is ready to use.
24+
statusOK processStatus = iota
2525

2626
// statusDone indicates that the PID/handle should not be used because
2727
// the process is done (has been successfully Wait'd on).
28-
statusDone processStatus = 1 << 62
28+
statusDone
2929

3030
// statusReleased indicates that the PID/handle should not be used
3131
// because the process is released.
32-
statusReleased processStatus = 1 << 63
33-
34-
processStatusMask = 0x3 << 62
32+
statusReleased
3533
)
3634

3735
// Process stores the information about a process created by [StartProcess].
@@ -42,7 +40,7 @@ type Process struct {
4240
//
4341
// This consists of the processStatus fields,
4442
// which indicate if the process is done/released.
45-
state atomic.Uint64
43+
state atomic.Uint32
4644

4745
// Used only when handle is nil
4846
sigMu sync.RWMutex // avoid race between wait and signal
@@ -138,7 +136,7 @@ func newDoneProcess(pid int) *Process {
138136
p := &Process{
139137
Pid: pid,
140138
}
141-
p.state.Store(uint64(statusDone)) // No persistent reference, as there is no handle.
139+
p.state.Store(uint32(statusDone)) // No persistent reference, as there is no handle.
142140
runtime.SetFinalizer(p, (*Process).Release)
143141
return p
144142
}
@@ -148,9 +146,9 @@ func (p *Process) handleTransientAcquire() (uintptr, processStatus) {
148146
panic("handleTransientAcquire called in invalid mode")
149147
}
150148

151-
state := p.state.Load()
152-
if state&processStatusMask != 0 {
153-
return 0, processStatus(state & processStatusMask)
149+
status := processStatus(p.state.Load())
150+
if status != statusOK {
151+
return 0, status
154152
}
155153
h, ok := p.handle.acquire()
156154
if ok {
@@ -161,11 +159,11 @@ func (p *Process) handleTransientAcquire() (uintptr, processStatus) {
161159
// We always set the status to non-zero before closing the handle.
162160
// If we get here the status must have been set non-zero after
163161
// we just checked it above.
164-
state = p.state.Load()
165-
if state&processStatusMask == 0 {
162+
status = processStatus(p.state.Load())
163+
if status == statusOK {
166164
panic("inconsistent process status")
167165
}
168-
return 0, processStatus(state & processStatusMask)
166+
return 0, status
169167
}
170168

171169
func (p *Process) handleTransientRelease() {
@@ -187,7 +185,7 @@ func (p *Process) handlePersistentRelease(reason processStatus) processStatus {
187185

188186
for {
189187
state := p.state.Load()
190-
status := processStatus(state & processStatusMask)
188+
status := processStatus(state)
191189
if status != statusOK {
192190
// Both Release and successful Wait will drop the
193191
// Process' persistent reference on the handle. We
@@ -196,7 +194,7 @@ func (p *Process) handlePersistentRelease(reason processStatus) processStatus {
196194
// reference is dropped exactly once.
197195
return status
198196
}
199-
if !p.state.CompareAndSwap(state, uint64(reason)) {
197+
if !p.state.CompareAndSwap(state, uint32(reason)) {
200198
continue
201199
}
202200
p.handle.release()
@@ -225,7 +223,7 @@ func (p *Process) pidDeactivate(reason processStatus) {
225223
// racing Release and Wait, Wait may successfully wait on the process,
226224
// returning the wait status, while future calls error with "process
227225
// released" rather than "process done".
228-
p.state.CompareAndSwap(0, uint64(reason))
226+
p.state.CompareAndSwap(0, uint32(reason))
229227
}
230228

231229
// ProcAttr holds the attributes that will be applied to a new process

src/os/export_linux_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ var (
1414
const StatusDone = statusDone
1515

1616
func (p *Process) Status() processStatus {
17-
return processStatus(p.state.Load() & processStatusMask)
17+
return processStatus(p.state.Load())
1818
}

0 commit comments

Comments
 (0)