thread.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // sol2
  2. // The MIT License (MIT)
  3. // Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. // this software and associated documentation files (the "Software"), to deal in
  6. // the Software without restriction, including without limitation the rights to
  7. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. // the Software, and to permit persons to whom the Software is furnished to do so,
  9. // subject to the following conditions:
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. #ifndef SOL_THREAD_HPP
  19. #define SOL_THREAD_HPP
  20. #include <sol/reference.hpp>
  21. #include <sol/object.hpp>
  22. #include <sol/stack.hpp>
  23. #include <sol/state_view.hpp>
  24. namespace sol {
  25. struct lua_thread_state {
  26. lua_State* L;
  27. lua_thread_state(lua_State* Ls) : L(Ls) {
  28. }
  29. lua_State* lua_state() const noexcept {
  30. return L;
  31. }
  32. operator lua_State*() const noexcept {
  33. return lua_state();
  34. }
  35. lua_State* operator->() const noexcept {
  36. return lua_state();
  37. }
  38. };
  39. namespace stack {
  40. template <>
  41. struct unqualified_pusher<lua_thread_state> {
  42. int push(lua_State*, lua_thread_state lts) {
  43. lua_pushthread(lts.L);
  44. return 1;
  45. }
  46. };
  47. template <>
  48. struct unqualified_getter<lua_thread_state> {
  49. lua_thread_state get(lua_State* L, int index, record& tracking) {
  50. tracking.use(1);
  51. lua_thread_state lts(lua_tothread(L, index));
  52. return lts;
  53. }
  54. };
  55. template <>
  56. struct unqualified_check_getter<lua_thread_state> {
  57. template <typename Handler>
  58. optional<lua_thread_state> get(lua_State* L, int index, Handler&& handler, record& tracking) {
  59. lua_thread_state lts(lua_tothread(L, index));
  60. if (lts.lua_state() == nullptr) {
  61. handler(L, index, type::thread, type_of(L, index), "value is not a valid thread type");
  62. return nullopt;
  63. }
  64. tracking.use(1);
  65. return lts;
  66. }
  67. };
  68. } // namespace stack
  69. template <typename ref_t>
  70. class basic_thread : public basic_object<ref_t> {
  71. private:
  72. using base_t = basic_object<ref_t>;
  73. public:
  74. using base_t::lua_state;
  75. basic_thread() noexcept = default;
  76. basic_thread(const basic_thread&) = default;
  77. basic_thread(basic_thread&&) = default;
  78. template <typename T,
  79. meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_thread>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  80. basic_thread(T&& r) : base_t(std::forward<T>(r)) {
  81. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  82. auto pp = stack::push_pop(*this);
  83. constructor_handler handler {};
  84. stack::check<basic_thread>(lua_state(), -1, handler);
  85. #endif // Safety
  86. }
  87. basic_thread(const stack_reference& r) : basic_thread(r.lua_state(), r.stack_index()) {};
  88. basic_thread(stack_reference&& r) : basic_thread(r.lua_state(), r.stack_index()) {};
  89. basic_thread& operator=(const basic_thread&) = default;
  90. basic_thread& operator=(basic_thread&&) = default;
  91. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  92. basic_thread(lua_State* L, T&& r) : base_t(L, std::forward<T>(r)) {
  93. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  94. auto pp = stack::push_pop(*this);
  95. constructor_handler handler {};
  96. stack::check<basic_thread>(lua_state(), -1, handler);
  97. #endif // Safety
  98. }
  99. basic_thread(lua_State* L, int index = -1) : base_t(L, index) {
  100. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  101. constructor_handler handler {};
  102. stack::check<basic_thread>(L, index, handler);
  103. #endif // Safety
  104. }
  105. basic_thread(lua_State* L, ref_index index) : base_t(L, index) {
  106. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  107. auto pp = stack::push_pop(*this);
  108. constructor_handler handler {};
  109. stack::check<basic_thread>(lua_state(), -1, handler);
  110. #endif // Safety
  111. }
  112. basic_thread(lua_State* L, lua_State* actualthread) : basic_thread(L, lua_thread_state { actualthread }) {
  113. }
  114. basic_thread(lua_State* L, this_state actualthread) : basic_thread(L, lua_thread_state { actualthread.L }) {
  115. }
  116. basic_thread(lua_State* L, lua_thread_state actualthread) : base_t(L, -stack::push(L, actualthread)) {
  117. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  118. constructor_handler handler {};
  119. stack::check<basic_thread>(lua_state(), -1, handler);
  120. #endif // Safety
  121. if (!is_stack_based<base_t>::value) {
  122. lua_pop(lua_state(), 1);
  123. }
  124. }
  125. state_view state() const {
  126. return state_view(this->thread_state());
  127. }
  128. bool is_main_thread() const {
  129. return stack::is_main_thread(this->thread_state());
  130. }
  131. lua_State* thread_state() const {
  132. auto pp = stack::push_pop(*this);
  133. lua_State* lthread = lua_tothread(lua_state(), -1);
  134. return lthread;
  135. }
  136. thread_status status() const {
  137. lua_State* lthread = thread_state();
  138. auto lstat = static_cast<thread_status>(lua_status(lthread));
  139. if (lstat == thread_status::ok) {
  140. lua_Debug ar;
  141. if (lua_getstack(lthread, 0, &ar) > 0)
  142. return thread_status::ok;
  143. else if (lua_gettop(lthread) == 0)
  144. return thread_status::dead;
  145. else
  146. return thread_status::yielded;
  147. }
  148. return lstat;
  149. }
  150. basic_thread create() {
  151. return create(lua_state());
  152. }
  153. static basic_thread create(lua_State* L) {
  154. lua_newthread(L);
  155. basic_thread result(L);
  156. if (!is_stack_based<base_t>::value) {
  157. lua_pop(L, 1);
  158. }
  159. return result;
  160. }
  161. };
  162. typedef basic_thread<reference> thread;
  163. typedef basic_thread<stack_reference> stack_thread;
  164. } // namespace sol
  165. #endif // SOL_THREAD_HPP