statemanager.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "statemanager.h"
  2. #include "util/file.h"
  3. #include "util/system.h"
  4. #include "util/time.h"
  5. #include "core/define.h"
  6. #include "core/config.h"
  7. #include "core/global.h"
  8. #define LOOP_STATE_USE 1
  9. bool state_manager::start()
  10. {
  11. close();
  12. ::ithread::start();
  13. m_module_manager.start();
  14. return true;
  15. }
  16. void state_manager::close()
  17. {
  18. ::ithread::stop();
  19. ::ithread::wait();
  20. luastate* state = nullptr;
  21. while (m_states.pop(state))
  22. delete state;
  23. m_module_manager.close();
  24. }
  25. luastate* state_manager::create()
  26. {
  27. luastate* lua = new luastate();
  28. lua->flag = m_flag;
  29. // 加载库或模块
  30. m_module_manager.load(lua->state);
  31. return lua;
  32. }
  33. luastate* state_manager::get()
  34. {
  35. luastate* result = nullptr;
  36. while (m_states.pop(result))
  37. {
  38. if (result->flag != m_flag)
  39. delete result;
  40. else
  41. return result;
  42. }
  43. return create();
  44. }
  45. void state_manager::push(luastate* state)
  46. {
  47. if (state == nullptr)
  48. return;
  49. if (state->flag != m_flag)
  50. {
  51. delete state;
  52. return;
  53. }
  54. m_states.push(state);
  55. }
  56. bool state_manager::run()
  57. {
  58. if (m_lib_detecter.changed())
  59. m_flag++;
  60. system::sleep_msec(sConfig->scripts.auto_update_sec);
  61. return true;
  62. }