bool replay_checkpoint(ReplayCheckpoint checkpoint) { bool res = false; assert(EVENT_CHECKPOINT + checkpoint <= EVENT_CHECKPOINT_LAST); replay_save_instructions(); if (!replay_file) { return true; } replay_mutex_lock(); if (replay_mode == REPLAY_MODE_PLAY) { if (replay_next_event_is(EVENT_CHECKPOINT + checkpoint)) { replay_finish_event(); } else if (replay_state.data_kind != EVENT_ASYNC) { res = false; goto out; } replay_read_events(checkpoint); /* replay_read_events may leave some unread events. Return false if not all of the events associated with checkpoint were processed */ res = replay_state.data_kind != EVENT_ASYNC; } else if (replay_mode == REPLAY_MODE_RECORD) { replay_put_event(EVENT_CHECKPOINT + checkpoint); replay_save_events(checkpoint); res = true; } out: replay_mutex_unlock(); return res; }
bool replay_checkpoint(ReplayCheckpoint checkpoint) { bool res = false; static bool in_checkpoint; assert(EVENT_CHECKPOINT + checkpoint <= EVENT_CHECKPOINT_LAST); if (!replay_file) { return true; } if (in_checkpoint) { /* If we are already in checkpoint, then there is no need for additional synchronization. Recursion occurs when HW event modifies timers. Timer modification may invoke the checkpoint and proceed to recursion. */ return true; } in_checkpoint = true; replay_save_instructions(); if (replay_mode == REPLAY_MODE_PLAY) { g_assert(replay_mutex_locked()); if (replay_next_event_is(EVENT_CHECKPOINT + checkpoint)) { replay_finish_event(); } else if (replay_state.data_kind != EVENT_ASYNC) { res = false; goto out; } replay_read_events(checkpoint); /* replay_read_events may leave some unread events. Return false if not all of the events associated with checkpoint were processed */ res = replay_state.data_kind != EVENT_ASYNC; } else if (replay_mode == REPLAY_MODE_RECORD) { g_assert(replay_mutex_locked()); replay_put_event(EVENT_CHECKPOINT + checkpoint); /* This checkpoint belongs to several threads. Processing events from different threads is non-deterministic */ if (checkpoint != CHECKPOINT_CLOCK_WARP_START /* FIXME: this is temporary fix, other checkpoints may also be invoked from the different threads someday. Asynchronous event processing should be refactored to create additional replay event kind which is nailed to the one of the threads and which processes the event queue. */ && checkpoint != CHECKPOINT_CLOCK_VIRTUAL) { replay_save_events(checkpoint); } res = true; } out: in_checkpoint = false; return res; }
bool replay_read(Replay *rpy, SDL_RWops *file, ReplayReadMode mode, const char *source) { int64_t filesize; // must be signed SDL_RWops *vfile = file; if(!source) { source = "<unknown>"; } if(!(mode & REPLAY_READ_ALL) ) { log_fatal("%s: Called with invalid read mode %x", source, mode); } mode &= REPLAY_READ_ALL; filesize = SDL_RWsize(file); if(filesize < 0) { log_warn("%s: SDL_RWsize() failed: %s", source, SDL_GetError()); } if(mode & REPLAY_READ_META) { memset(rpy, 0, sizeof(Replay)); if(filesize > 0 && filesize <= sizeof(replay_magic_header) + 2) { log_warn("%s: Replay file is too short (%"PRIi64")", source, filesize); return false; } size_t ofs = 0; if(!replay_read_header(rpy, file, filesize, &ofs, source)) { return false; } bool compression = false; if(rpy->version & REPLAY_VERSION_COMPRESSION_BIT) { if(rpy->fileoffset < SDL_RWtell(file)) { log_warn("%s: Invalid offset %"PRIi32"", source, rpy->fileoffset); return false; } vfile = SDL_RWWrapZReader(SDL_RWWrapSegment(file, ofs, rpy->fileoffset, false), REPLAY_COMPRESSION_CHUNK_SIZE, true); filesize = -1; compression = true; } if(!replay_read_meta(rpy, vfile, filesize, source)) { if(compression) { SDL_RWclose(vfile); } return false; } if(compression) { SDL_RWclose(vfile); vfile = file; } else { rpy->fileoffset = SDL_RWtell(file); } } if(mode & REPLAY_READ_EVENTS) { if(!(mode & REPLAY_READ_META)) { if(!rpy->fileoffset) { log_fatal("%s: Tried to read events before reading metadata", source); } for(int i = 0; i < rpy->numstages; ++i) { if(rpy->stages[i].events) { log_warn("%s: BUG: Reading events into a replay that already had events, call replay_destroy_events() if this is intended", source); replay_destroy_events(rpy); break; } } if(SDL_RWseek(file, rpy->fileoffset, RW_SEEK_SET) < 0) { log_warn("%s: SDL_RWseek() failed: %s", source, SDL_GetError()); return false; } } bool compression = false; if(rpy->version & REPLAY_VERSION_COMPRESSION_BIT) { vfile = SDL_RWWrapZReader(file, REPLAY_COMPRESSION_CHUNK_SIZE, false); filesize = -1; compression = true; } if(!replay_read_events(rpy, vfile, filesize, source)) { if(compression) { SDL_RWclose(vfile); } replay_destroy_events(rpy); return false; } if(compression) { SDL_RWclose(vfile); } // useless byte to simplify the premature EOF check, can be anything SDL_ReadU8(file); } return true; }