The Battle for Wesnoth  1.17.0-dev
save_blocker.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2021
3  by Daniel Franke
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "save_blocker.hpp"
17 #include <exception>
18 #include <iostream>
19 
21 void (play_controller::*save_blocker::callback_)() = nullptr;
22 SDL_sem* save_blocker::sem_ = SDL_CreateSemaphore(1);
23 
25  block();
26 }
27 
29  try {
30  unblock();
31  if(controller_ && callback_) {
32  (controller_->*callback_)();
33  controller_ = nullptr;
34  callback_ = nullptr;
35  }
36  } catch (const std::exception & e) {
37  std::cerr << "Save blocker dtor swallowing an exception: " << e.what() << "\n";
38  }
39 }
40 
42  if(try_block()) {
43  unblock();
44  (controller->*callback)();
45  } else {
47  callback_ = callback;
48  }
49 }
50 
52  return SDL_SemValue(sem_) == 0;
53 }
54 
56  SDL_SemWait(sem_);
57 }
58 
60  return SDL_SemTryWait(sem_) == 0;
61 }
62 
64  assert(SDL_SemValue(sem_) == 0);
65  SDL_SemPost(sem_);
66 }
static void unblock()
static void on_unblock(play_controller *controller, void(play_controller::*callback)())
static void(play_controller::* callback_)()
static bool saves_are_blocked()
static void block()
static play_controller * controller_
static SDL_sem * sem_
static bool try_block()
#define e