Skip to content

Commit

Permalink
Replace legacy ValueMutex with Furi mutex
Browse files Browse the repository at this point in the history
ValueMutex is obsolete since flipperdevices/flipperzero-firmware#2467
  • Loading branch information
Xorboo committed Jun 1, 2023
1 parent a4b62c5 commit f46ebd5
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions roots_of_life_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ typedef struct {

int rerolls;
int score;

FuriMutex* mutex;
} GameState;

typedef struct {
Expand Down Expand Up @@ -565,10 +567,9 @@ static void draw_end_ui(Canvas* canvas, GameState* state) {
}

static void roots_draw_callback(Canvas* const canvas, void* ctx) {
GameState* state = acquire_mutex((ValueMutex*)ctx, 25);
if(state == NULL) {
return;
}
furi_assert(ctx);
GameState* state = ctx;
furi_mutex_acquire(state->mutex, FuriWaitForever);

if(!state->initialDraw) {
state->initialDraw = true;
Expand Down Expand Up @@ -600,7 +601,7 @@ static void roots_draw_callback(Canvas* const canvas, void* ctx) {
break;
}

release_mutex((ValueMutex*)ctx, state);
furi_mutex_release(state->mutex);
}

static void roots_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
Expand Down Expand Up @@ -678,16 +679,16 @@ int32_t roots_of_life_game_app(void* p) {
GameState* state = malloc(sizeof(GameState));
game_state_init(state);

ValueMutex state_mutex;
if(!init_mutex(&state_mutex, state, sizeof(GameState))) {
state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
if(!state->mutex) {
FURI_LOG_E(TAG, "Cannot create mutex\r\n");
return_code = 255;
goto free_and_exit;
}

// Set system callbacks
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, roots_draw_callback, &state_mutex);
view_port_draw_callback_set(view_port, roots_draw_callback, state);
view_port_input_callback_set(view_port, roots_input_callback, event_queue);

FuriTimer* timer =
Expand All @@ -702,7 +703,7 @@ int32_t roots_of_life_game_app(void* p) {
GameEvent event;
for(bool processing = true; processing;) {
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
GameState* state = (GameState*)acquire_mutex_block(&state_mutex);
furi_mutex_acquire(state->mutex, FuriWaitForever);

if(event_status == FuriStatusOk) {
// Key events
Expand Down Expand Up @@ -733,7 +734,7 @@ int32_t roots_of_life_game_app(void* p) {
}

view_port_update(view_port);
release_mutex(&state_mutex, state);
furi_mutex_release(state->mutex);
}

furi_timer_free(timer);
Expand All @@ -742,7 +743,7 @@ int32_t roots_of_life_game_app(void* p) {
furi_record_close(RECORD_GUI);
furi_record_close(RECORD_NOTIFICATION);
view_port_free(view_port);
delete_mutex(&state_mutex);
furi_mutex_free(state->mutex);

free_and_exit:
FURI_LOG_D(TAG, "Quitting game...");
Expand Down

0 comments on commit f46ebd5

Please sign in to comment.