Skip to content

Commit b2c3593

Browse files
xaionaro@dx.centerxaionaro@dx.center
authored andcommitted
fix: ensure all mutex Unlock calls use defer
1 parent 29f8437 commit b2c3593

4 files changed

Lines changed: 71 additions & 46 deletions

File tree

examples/alarm/main.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,24 @@ func run(vm *jni.VM, output *bytes.Buffer) error {
127127
go func() {
128128
time.Sleep(delay)
129129
if err := postNotification(vm, appCtxObj); err != nil {
130-
ui.OutputMu.Lock()
131-
fmt.Fprintf(output, "notification error: %v\n", err)
132-
ui.OutputMu.Unlock()
130+
func() {
131+
ui.OutputMu.Lock()
132+
defer ui.OutputMu.Unlock()
133+
fmt.Fprintf(output, "notification error: %v\n", err)
134+
}()
133135
}
134136
if err := playAlarmRingtone(vm, appCtxObj); err != nil {
135-
ui.OutputMu.Lock()
136-
fmt.Fprintf(output, "ringtone error: %v\n", err)
137-
ui.OutputMu.Unlock()
137+
func() {
138+
ui.OutputMu.Lock()
139+
defer ui.OutputMu.Unlock()
140+
fmt.Fprintf(output, "ringtone error: %v\n", err)
141+
}()
138142
}
139-
ui.OutputMu.Lock()
140-
fmt.Fprintf(output, "alarm fired!\n")
141-
ui.OutputMu.Unlock()
143+
func() {
144+
ui.OutputMu.Lock()
145+
defer ui.OutputMu.Unlock()
146+
fmt.Fprintf(output, "alarm fired!\n")
147+
}()
142148
ui.RenderOutput()
143149
}()
144150

examples/common/ui/ui.go

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,11 @@ func OnCreate(
9090
) {
9191
vm = cvm
9292
activityRef = activity
93-
OutputMu.Lock()
94-
outputBuf.Reset()
95-
OutputMu.Unlock()
93+
func() {
94+
OutputMu.Lock()
95+
defer OutputMu.Unlock()
96+
outputBuf.Reset()
97+
}()
9698
exampleStarted = false
9799
}
98100

@@ -115,9 +117,11 @@ func OnNativeWindowCreated(windowPtr unsafe.Pointer) {
115117
var err error
116118
needed, err = getUngrantedPermissions(env, activityRef)
117119
if err != nil {
118-
OutputMu.Lock()
119-
fmt.Fprintf(&outputBuf, "permissions check: %v\n", err)
120-
OutputMu.Unlock()
120+
func() {
121+
OutputMu.Lock()
122+
defer OutputMu.Unlock()
123+
fmt.Fprintf(&outputBuf, "permissions check: %v\n", err)
124+
}()
121125
}
122126
return nil
123127
})
@@ -147,9 +151,11 @@ func startExample() {
147151
if err := runFunc(vm, &localBuf); err != nil {
148152
fmt.Fprintf(&localBuf, "ERROR: %v\n", err)
149153
}
150-
OutputMu.Lock()
151-
outputBuf.Write(localBuf.Bytes())
152-
OutputMu.Unlock()
154+
func() {
155+
OutputMu.Lock()
156+
defer OutputMu.Unlock()
157+
outputBuf.Write(localBuf.Bytes())
158+
}()
153159
}
154160
RenderOutput()
155161
}()
@@ -158,9 +164,11 @@ func startExample() {
158164
// RenderOutput re-renders the current output buffer to the screen.
159165
// Call from background goroutines after appending to the shared buffer.
160166
func RenderOutput() {
161-
OutputMu.Lock()
162-
text := outputBuf.String()
163-
OutputMu.Unlock()
167+
text := func() string {
168+
OutputMu.Lock()
169+
defer OutputMu.Unlock()
170+
return outputBuf.String()
171+
}()
164172
if text == "" {
165173
text = "(no output)"
166174
}
@@ -169,13 +177,16 @@ func RenderOutput() {
169177

170178
// OnResume is called when the activity resumes (e.g. after permission dialog).
171179
func OnResume(activity *jni.Object) {
172-
OutputMu.Lock()
173-
hasOutput := outputBuf.Len() > 0
174-
var text string
175-
if hasOutput {
176-
text = outputBuf.String()
177-
}
178-
OutputMu.Unlock()
180+
hasOutput, text := func() (bool, string) {
181+
OutputMu.Lock()
182+
defer OutputMu.Unlock()
183+
has := outputBuf.Len() > 0
184+
var t string
185+
if has {
186+
t = outputBuf.String()
187+
}
188+
return has, t
189+
}()
179190
if nativeWindow != nil && hasOutput {
180191
renderText(text)
181192
}

examples/location/main.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,14 @@ func requestFreshLocation(vm *jni.VM, mgr *location.Manager) (*location.Extracte
230230

231231
loc, err := location.ExtractLocation(env, locObj)
232232
if err == nil && loc != nil {
233-
mu.Lock()
234-
if result == nil {
235-
result = loc
236-
close(done)
237-
}
238-
mu.Unlock()
233+
func() {
234+
mu.Lock()
235+
defer mu.Unlock()
236+
if result == nil {
237+
result = loc
238+
close(done)
239+
}
240+
}()
239241
}
240242
}
241243
return nil, nil

proxy.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,31 @@ var (
2626
// registerProxy stores a handler and returns a unique ID.
2727
func registerProxy(h ProxyHandler) int64 {
2828
id := proxyNextID.Add(1)
29-
proxyMu.Lock()
30-
proxyHandlers[id] = h
31-
proxyMu.Unlock()
29+
func() {
30+
proxyMu.Lock()
31+
defer proxyMu.Unlock()
32+
proxyHandlers[id] = h
33+
}()
3234
return id
3335
}
3436

3537
// registerProxyFull stores a full handler and returns a unique ID.
3638
func registerProxyFull(h ProxyHandlerFull) int64 {
3739
id := proxyNextID.Add(1)
38-
proxyMu.Lock()
39-
proxyFullHandlers[id] = h
40-
proxyMu.Unlock()
40+
func() {
41+
proxyMu.Lock()
42+
defer proxyMu.Unlock()
43+
proxyFullHandlers[id] = h
44+
}()
4145
return id
4246
}
4347

4448
// unregisterProxy removes a handler by ID from both registries.
4549
func unregisterProxy(id int64) {
4650
proxyMu.Lock()
51+
defer proxyMu.Unlock()
4752
delete(proxyHandlers, id)
4853
delete(proxyFullHandlers, id)
49-
proxyMu.Unlock()
5054
}
5155

5256
// RegisterProxyHandler stores a basic ProxyHandler in the global registry
@@ -66,16 +70,16 @@ func UnregisterProxyHandler(id int64) {
6670
// lookupProxy retrieves a handler by ID.
6771
func lookupProxy(id int64) (ProxyHandler, bool) {
6872
proxyMu.Lock()
73+
defer proxyMu.Unlock()
6974
h, ok := proxyHandlers[id]
70-
proxyMu.Unlock()
7175
return h, ok
7276
}
7377

7478
// lookupProxyFull retrieves a full handler by ID.
7579
func lookupProxyFull(id int64) (ProxyHandlerFull, bool) {
7680
proxyMu.Lock()
81+
defer proxyMu.Unlock()
7782
h, ok := proxyFullHandlers[id]
78-
proxyMu.Unlock()
7983
return h, ok
8084
}
8185

@@ -158,9 +162,11 @@ func findClassWithFallback(
158162
}
159163
capi.ExceptionClear(env)
160164

161-
proxyMu.Lock()
162-
cl := proxyClassLoader
163-
proxyMu.Unlock()
165+
cl := func() capi.Object {
166+
proxyMu.Lock()
167+
defer proxyMu.Unlock()
168+
return proxyClassLoader
169+
}()
164170
if cl == 0 {
165171
return 0
166172
}

0 commit comments

Comments
 (0)