memory_limit_checker.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2020 by
  4. * The Salk Institute for Biological Studies
  5. *
  6. * Use of this source code is governed by an MIT-style
  7. * license that can be found in the LICENSE file or at
  8. * https://opensource.org/licenses/MIT.
  9. *
  10. ******************************************************************************/
  11. #ifndef SRC4_MEMORY_LIMIT_CHECKER_H_
  12. #define SRC4_MEMORY_LIMIT_CHECKER_H_
  13. #include "defines.h"
  14. #include "libs/cpptime/cpptime.h"
  15. namespace MCell {
  16. class World;
  17. class MemoryLimitChecker {
  18. public:
  19. MemoryLimitChecker() :
  20. world(nullptr),
  21. limit_gb(-1),
  22. exit_when_over_limit(false),
  23. over_limit(false),
  24. timer(nullptr), created_timer_id(0) {
  25. }
  26. ~MemoryLimitChecker();
  27. // does nothing when limit_in_gb_ <= 0
  28. void start_timed_check(
  29. World* world_, const int limit_gb_, const bool exit_when_over_limit_ = true);
  30. void stop_timed_check();
  31. bool is_over_memory_limit() const {
  32. return over_limit;
  33. }
  34. private:
  35. // set in start_timed_check
  36. World* world;
  37. int limit_gb; // -1 to ignore
  38. bool exit_when_over_limit;
  39. bool over_limit; // safe to read asynchronously
  40. CppTime::Timer* timer;
  41. CppTime::timer_id created_timer_id;
  42. };
  43. } /* namespace MCell */
  44. #endif /* SRC4_MEMORY_LIMIT_CHECKER_H_ */