Skip to content

Commit

Permalink
Much thread, very safety
Browse files Browse the repository at this point in the history
  • Loading branch information
LIJI32 committed Nov 23, 2024
1 parent 1ab690b commit a39b191
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Core/apu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value)

void GB_set_sample_rate(GB_gameboy_t *gb, unsigned sample_rate)
{
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
gb->apu_output.sample_rate = sample_rate;
if (sample_rate) {
gb->apu_output.highpass_rate = pow(0.999958, GB_get_clock_rate(gb) / (double)sample_rate);
Expand All @@ -1764,7 +1765,7 @@ void GB_set_sample_rate(GB_gameboy_t *gb, unsigned sample_rate)

void GB_set_sample_rate_by_clocks(GB_gameboy_t *gb, double cycles_per_sample)
{

GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
if (cycles_per_sample == 0) {
GB_set_sample_rate(gb, 0);
return;
Expand Down
6 changes: 6 additions & 0 deletions Core/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,17 @@ uint8_t GB_camera_read_image(GB_gameboy_t *gb, uint16_t addr)

void GB_set_camera_get_pixel_callback(GB_gameboy_t *gb, GB_camera_get_pixel_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->camera_get_pixel_callback = callback;
}

void GB_set_camera_update_request_callback(GB_gameboy_t *gb, GB_camera_update_request_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
if (gb->camera_countdown > 0 && callback) {
GB_log(gb, "Camera update request callback set while camera was proccessing, clearing camera countdown.\n");
gb->camera_countdown = 0;
Expand Down
6 changes: 6 additions & 0 deletions Core/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ const GB_palette_t *GB_get_palette(GB_gameboy_t *gb)

void GB_set_vblank_callback(GB_gameboy_t *gb, GB_vblank_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->vblank_callback = callback;
}

void GB_set_rgb_encode_callback(GB_gameboy_t *gb, GB_rgb_encode_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}

gb->rgb_encode_callback = callback;
GB_update_dmg_palette(gb);
Expand Down
45 changes: 45 additions & 0 deletions Core/gb.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,8 @@ int GB_save_battery_size(GB_gameboy_t *gb)

int GB_save_battery_to_buffer(GB_gameboy_t *gb, uint8_t *buffer, size_t size)
{
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)

if (!gb->cartridge_type->has_battery) return 0; // Nothing to save.
if (gb->cartridge_type->mbc_type == GB_TPP1 && !(gb->rom[0x153] & 8)) return 0; // Nothing to save.
if (gb->mbc_ram_size == 0 && !gb->cartridge_type->has_rtc) return 0; /* Claims to have battery, but has no RAM or RTC */
Expand Down Expand Up @@ -1241,12 +1243,18 @@ uint32_t *GB_get_pixels_output(GB_gameboy_t *gb)

void GB_set_log_callback(GB_gameboy_t *gb, GB_log_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->log_callback = callback;
}

void GB_set_input_callback(GB_gameboy_t *gb, GB_input_callback_t callback)
{
#ifndef GB_DISABLE_DEBUGGER
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
if (gb->input_callback == default_input_callback) {
gb->async_input_callback = NULL;
}
Expand All @@ -1257,27 +1265,42 @@ void GB_set_input_callback(GB_gameboy_t *gb, GB_input_callback_t callback)
void GB_set_async_input_callback(GB_gameboy_t *gb, GB_input_callback_t callback)
{
#ifndef GB_DISABLE_DEBUGGER
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->async_input_callback = callback;
#endif
}

void GB_set_execution_callback(GB_gameboy_t *gb, GB_execution_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->execution_callback = callback;
}

void GB_set_lcd_line_callback(GB_gameboy_t *gb, GB_lcd_line_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->lcd_line_callback = callback;
}

void GB_set_lcd_status_callback(GB_gameboy_t *gb, GB_lcd_status_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->lcd_status_callback = callback;
}

void GB_set_infrared_callback(GB_gameboy_t *gb, GB_infrared_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->infrared_callback = callback;
}

Expand All @@ -1288,16 +1311,25 @@ void GB_set_infrared_input(GB_gameboy_t *gb, bool state)

void GB_set_rumble_callback(GB_gameboy_t *gb, GB_rumble_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->rumble_callback = callback;
}

void GB_set_serial_transfer_bit_start_callback(GB_gameboy_t *gb, GB_serial_transfer_bit_start_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->serial_transfer_bit_start_callback = callback;
}

void GB_set_serial_transfer_bit_end_callback(GB_gameboy_t *gb, GB_serial_transfer_bit_end_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->serial_transfer_bit_end_callback = callback;
}

Expand Down Expand Up @@ -1341,6 +1373,7 @@ void GB_serial_set_data_bit(GB_gameboy_t *gb, bool data)

void GB_disconnect_serial(GB_gameboy_t *gb)
{
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
gb->serial_transfer_bit_start_callback = NULL;
gb->serial_transfer_bit_end_callback = NULL;

Expand Down Expand Up @@ -1941,22 +1974,34 @@ double GB_get_usual_frame_rate(GB_gameboy_t *gb)

void GB_set_joyp_write_callback(GB_gameboy_t *gb, GB_joyp_write_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->joyp_write_callback = callback;
}

void GB_set_icd_pixel_callback(GB_gameboy_t *gb, GB_icd_pixel_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->icd_pixel_callback = callback;
}

void GB_set_icd_hreset_callback(GB_gameboy_t *gb, GB_icd_hreset_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->icd_hreset_callback = callback;
}


void GB_set_icd_vreset_callback(GB_gameboy_t *gb, GB_icd_vreset_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->icd_vreset_callback = callback;
}

Expand Down
3 changes: 3 additions & 0 deletions Core/joypad.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,8 @@ void GB_set_emulate_joypad_bouncing(GB_gameboy_t *gb, bool emulate)

void GB_set_update_input_hint_callback(GB_gameboy_t *gb, GB_update_input_hint_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->update_input_hint_callback = callback;
}
6 changes: 6 additions & 0 deletions Core/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,9 @@ static read_function_t *const read_map[] =

void GB_set_read_memory_callback(GB_gameboy_t *gb, GB_read_memory_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->read_memory_callback = callback;
}

Expand Down Expand Up @@ -1781,6 +1784,9 @@ static write_function_t *const write_map[] =

void GB_set_write_memory_callback(GB_gameboy_t *gb, GB_write_memory_callback_t callback)
{
if (!callback) {
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
}
gb->write_memory_callback = callback;
}

Expand Down
1 change: 1 addition & 0 deletions Core/printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ static bool serial_end(GB_gameboy_t *gb)

void GB_connect_printer(GB_gameboy_t *gb, GB_print_image_callback_t callback, GB_printer_done_callback_t done_callback)
{
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
memset(&gb->printer, 0, sizeof(gb->printer));
GB_set_serial_transfer_bit_start_callback(gb, serial_start);
GB_set_serial_transfer_bit_end_callback(gb, serial_end);
Expand Down
1 change: 1 addition & 0 deletions Core/workboy.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ void GB_connect_workboy(GB_gameboy_t *gb,
GB_workboy_set_time_callback_t set_time_callback,
GB_workboy_get_time_callback_t get_time_callback)
{
GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
memset(&gb->workboy, 0, sizeof(gb->workboy));
GB_set_serial_transfer_bit_start_callback(gb, serial_start);
GB_set_serial_transfer_bit_end_callback(gb, serial_end);
Expand Down

0 comments on commit a39b191

Please sign in to comment.