@@ -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.
160166func 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).
171179func 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 }
0 commit comments