implementation of save_context and restore_context for context switching