From 3168e0e1fabe4a00e96f2b853996a23f9ba9b2af Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 12 Aug 2023 13:34:57 +0300 Subject: [PATCH] Fixed potential deadlocks --- Cocoa/Document.m | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index b477bbcc4..789ed326a 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -125,6 +125,7 @@ enum model { void (^ volatile _pendingAtomicBlock)(); NSDate *_fileModificationTime; + __weak NSThread *_emulationThread; } static void boot_rom_load(GB_gameboy_t *gb, GB_boot_rom_t type) @@ -606,7 +607,7 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency) } if (_running) return; _running = true; - [[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] start]; + [_emulationThread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] start]; } - (void) stop @@ -1584,7 +1585,7 @@ static bool is_path_writeable(const char *path) GB_write_memory(&_gb, addr, value); } -- (void) performAtomicBlock: (void (^)())block +- (void)performAtomicBlock: (void (^)())block { while (!GB_is_inited(&_gb)); bool isRunning = _running && !GB_debugger_is_stopped(&_gb); @@ -1601,6 +1602,11 @@ static bool is_path_writeable(const char *path) return; } + if ([NSThread currentThread] == _emulationThread) { + block(); + return; + } + _pendingAtomicBlock = block; while (_pendingAtomicBlock); }