Skip to content

Commit ba7271f

Browse files
committed
use std::atomic instead of std::mutex
1 parent 54e3973 commit ba7271f

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

NativeApp/include/Android/AndroidAppBase.hpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#pragma once
2727

2828
#include <memory>
29-
#include <mutex>
29+
#include <atomic>
3030

3131
#include <android_native_app_glue.h>
3232
#include "FlagEnum.h"
@@ -73,7 +73,7 @@ class AndroidAppBase : public AppBase
7373
APP_STATUS_FLAG_HAS_REAL_SURFACE = 0x00000008
7474
};
7575

76-
APP_STATUS_FLAGS app_status_ = APP_STATUS_FLAG_NONE;
76+
std::atomic<APP_STATUS_FLAGS> app_status_ {APP_STATUS_FLAG_NONE};
7777

7878
protected:
7979
virtual void Initialize()
@@ -104,8 +104,6 @@ class AndroidAppBase : public AppBase
104104
android_app* app_ = nullptr;
105105
std::string native_activity_class_name_;
106106

107-
std::mutex mutex;
108-
109107
private:
110108
void UpdatePosition(AInputEvent* event, int32_t iIndex, float& fX, float& fY);
111109
void SuspendSensors();
@@ -121,6 +119,23 @@ class AndroidAppBase : public AppBase
121119
ASensorManager* sensor_manager_ = nullptr;
122120
const ASensor* accelerometer_sensor_ = nullptr;
123121
ASensorEventQueue* sensor_event_queue_ = nullptr;
122+
123+
class atomic_mutex {
124+
std::atomic<bool> flag{false};
125+
126+
public:
127+
void lock()
128+
{
129+
while (flag.exchange(true, std::memory_order_relaxed));
130+
std::atomic_thread_fence(std::memory_order_acquire);
131+
}
132+
133+
void unlock()
134+
{
135+
std::atomic_thread_fence(std::memory_order_release);
136+
flag.store(false, std::memory_order_relaxed);
137+
}
138+
};
124139
};
125140

126141
DEFINE_FLAG_ENUM_OPERATORS(AndroidAppBase::APP_STATUS_FLAGS)

NativeApp/src/Android/AndroidAppBase.cpp

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,19 @@ void AndroidAppBase::HandleCmd(struct android_app* app, int32_t cmd)
129129
LOG_INFO_MESSAGE("INIT DISPLAY - HAS SURFACE");
130130
eng->InitDisplay();
131131
eng->DrawFrame();
132-
std::lock_guard<std::mutex> lock(eng->mutex);
133-
eng->app_status_ |= APP_STATUS_FLAG_HAS_REAL_SURFACE;
132+
eng->app_status_.store(eng->app_status_.load() | APP_STATUS_FLAG_HAS_REAL_SURFACE);
134133
}
135134
else
136135
{
137136
LOG_INFO_MESSAGE("NO SURFACE");
138-
std::lock_guard<std::mutex> lock(eng->mutex);
139-
eng->app_status_ &= ~APP_STATUS_FLAG_HAS_REAL_SURFACE;
137+
eng->app_status_.store(eng->app_status_.load() & ~APP_STATUS_FLAG_HAS_REAL_SURFACE);
140138
}
141139
break;
142140

143141
case APP_CMD_TERM_WINDOW:
144142
LOG_INFO_MESSAGE("APP_CMD_TERM_WINDOW - LOST SURFACE - TERM DISPLAY");
145143
{
146-
std::lock_guard<std::mutex> lock(eng->mutex);
147-
eng->app_status_ &= ~APP_STATUS_FLAG_HAS_REAL_SURFACE;
144+
eng->app_status_.store(eng->app_status_.load() & ~APP_STATUS_FLAG_HAS_REAL_SURFACE);
148145
}
149146
eng->TermDisplay();
150147
break;
@@ -186,26 +183,23 @@ void AndroidAppBase::HandleCmd(struct android_app* app, int32_t cmd)
186183
case APP_CMD_GAINED_FOCUS:
187184
LOG_INFO_MESSAGE("APP_CMD_GAINED_FOCUS - HAS FOCUS");
188185
{
189-
std::lock_guard<std::mutex> lock(eng->mutex);
190-
eng->app_status_ |= APP_STATUS_FLAG_FOCUSED;
186+
eng->app_status_.store(eng->app_status_.load() | APP_STATUS_FLAG_FOCUSED);
191187
}
192188
eng->ResumeSensors();
193189
break;
194190

195191
case APP_CMD_LOST_FOCUS:
196192
LOG_INFO_MESSAGE("APP_CMD_LOST_FOCUS - LOST FOCUS");
197193
{
198-
std::lock_guard<std::mutex> lock(eng->mutex);
199-
eng->app_status_ &= ~APP_STATUS_FLAG_FOCUSED;
194+
eng->app_status_.store(eng->app_status_.load() & ~APP_STATUS_FLAG_FOCUSED);
200195
}
201196
eng->SuspendSensors();
202197
break;
203198

204199
case APP_CMD_RESUME:
205200
LOG_INFO_MESSAGE("APP_CMD_RESUME - IS ACTIVE");
206201
{
207-
std::lock_guard<std::mutex> lock(eng->mutex);
208-
eng->app_status_ |= APP_STATUS_FLAG_ACTIVE;
202+
eng->app_status_.store(eng->app_status_.load() | APP_STATUS_FLAG_ACTIVE);
209203
}
210204
break;
211205

@@ -216,8 +210,7 @@ void AndroidAppBase::HandleCmd(struct android_app* app, int32_t cmd)
216210
case APP_CMD_PAUSE:
217211
LOG_INFO_MESSAGE("APP_CMD_PAUSE - IS NOT ACTIVE");
218212
{
219-
std::lock_guard<std::mutex> lock(eng->mutex);
220-
eng->app_status_ &= ~APP_STATUS_FLAG_ACTIVE;
213+
eng->app_status_.store(eng->app_status_.load() & ~APP_STATUS_FLAG_ACTIVE);
221214
}
222215
break;
223216

@@ -235,8 +228,7 @@ void AndroidAppBase::HandleCmd(struct android_app* app, int32_t cmd)
235228
case APP_CMD_DESTROY:
236229
LOG_INFO_MESSAGE("APP_CMD_DESTROY - IS NOT RUNNING");
237230
{
238-
std::lock_guard<std::mutex> lock(eng->mutex);
239-
eng->app_status_ &= ~APP_STATUS_FLAG_RUNNING;
231+
eng->app_status_.store(eng->app_status_.load() & ~APP_STATUS_FLAG_RUNNING);
240232
}
241233
break;
242234

@@ -312,11 +304,11 @@ void AndroidAppBase::SetState(android_app* state, const char* native_activity_cl
312304

313305
bool AndroidAppBase::IsReady()
314306
{
315-
std::lock_guard<std::mutex> lock(mutex);
316-
return (app_status_ & APP_STATUS_FLAG_RUNNING) != APP_STATUS_FLAG_NONE &&
317-
(app_status_ & APP_STATUS_FLAG_ACTIVE) != APP_STATUS_FLAG_NONE &&
318-
(app_status_ & APP_STATUS_FLAG_FOCUSED) != APP_STATUS_FLAG_NONE &&
319-
(app_status_ & APP_STATUS_FLAG_HAS_REAL_SURFACE) != APP_STATUS_FLAG_NONE;
307+
APP_STATUS_FLAGS app_status = app_status_.load();
308+
return (app_status & APP_STATUS_FLAG_RUNNING) != APP_STATUS_FLAG_NONE &&
309+
(app_status & APP_STATUS_FLAG_ACTIVE) != APP_STATUS_FLAG_NONE &&
310+
(app_status & APP_STATUS_FLAG_FOCUSED) != APP_STATUS_FLAG_NONE &&
311+
(app_status & APP_STATUS_FLAG_HAS_REAL_SURFACE) != APP_STATUS_FLAG_NONE;
320312
}
321313

322314
//void Engine::TransformPosition( ndk_helper::Vec2& vec )

0 commit comments

Comments
 (0)