Skip to content

Commit

Permalink
Fixed potential deadlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
LIJI32 committed Aug 12, 2023
1 parent 7e6f1f8 commit 3168e0e
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Cocoa/Document.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ @implementation Document
void (^ volatile _pendingAtomicBlock)();

NSDate *_fileModificationTime;
__weak NSThread *_emulationThread;
}

static void boot_rom_load(GB_gameboy_t *gb, GB_boot_rom_t type)
Expand Down Expand Up @@ -606,7 +607,7 @@ - (void) start
}
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
Expand Down Expand Up @@ -1584,7 +1585,7 @@ - (void) writeMemory:(uint16_t)addr value:(uint8_t)value
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);
Expand All @@ -1601,6 +1602,11 @@ - (void) performAtomicBlock: (void (^)())block
return;
}

if ([NSThread currentThread] == _emulationThread) {
block();
return;
}

_pendingAtomicBlock = block;
while (_pendingAtomicBlock);
}
Expand Down

0 comments on commit 3168e0e

Please sign in to comment.