mod_lua_shared.inl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* Copyright (c) 2018-2020 CivetWeb developers
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. /* Interface functions */
  22. LUA_SHARED_INTERFACE void lua_shared_init(void);
  23. LUA_SHARED_INTERFACE void lua_shared_exit(void);
  24. LUA_SHARED_INTERFACE void lua_shared_register(struct lua_State *L);
  25. /* Shared data for all Lua states */
  26. static lua_State *L_shared;
  27. static pthread_mutex_t lua_shared_lock;
  28. /* Library init.
  29. * This function must be called before all other functions. Not thread-safe. */
  30. LUA_SHARED_INTERFACE void
  31. lua_shared_init(void)
  32. {
  33. /* Create a new Lua state to store all shared data.
  34. * In fact, this is used as a hashmap. */
  35. L_shared = lua_newstate(lua_allocator, NULL);
  36. lua_newtable(L_shared);
  37. lua_setglobal(L_shared, "shared");
  38. /* Mutex for locking access to the shared state from different threads. */
  39. pthread_mutex_init(&lua_shared_lock, &pthread_mutex_attr);
  40. }
  41. /* Library exit.
  42. * This function should be called for cleanup. Not thread-safe. */
  43. LUA_SHARED_INTERFACE void
  44. lua_shared_exit(void)
  45. {
  46. /* Destroy Lua state */
  47. lua_close(L_shared);
  48. L_shared = 0;
  49. /* Destroy mutex. */
  50. pthread_mutex_destroy(&lua_shared_lock);
  51. }
  52. #if defined(MG_EXPERIMENTAL_INTERFACES)
  53. static double
  54. shared_locked_add(const char *name, size_t namlen, double value, int op)
  55. {
  56. double ret;
  57. pthread_mutex_lock(&lua_shared_lock);
  58. lua_getglobal(L_shared, "shared");
  59. lua_pushlstring(L_shared, name, namlen);
  60. lua_rawget(L_shared, -2);
  61. ret = lua_tonumber(L_shared, -1);
  62. if (op > 0) {
  63. ret += value;
  64. } else {
  65. ret = value;
  66. }
  67. lua_getglobal(L_shared, "shared");
  68. lua_pushlstring(L_shared, name, namlen);
  69. lua_pushnumber(L_shared, ret);
  70. lua_rawset(L_shared, -3);
  71. lua_pop(L_shared, 3);
  72. pthread_mutex_unlock(&lua_shared_lock);
  73. return ret;
  74. }
  75. static int
  76. lua_shared_add(struct lua_State *L)
  77. {
  78. size_t symlen = 0;
  79. const char *sym = lua_tolstring(L, 1, &symlen);
  80. double num = lua_tonumber(L, 2);
  81. double ret = shared_locked_add(sym, symlen, num, 1);
  82. lua_pushnumber(L, ret);
  83. return 1;
  84. }
  85. static int
  86. lua_shared_inc(struct lua_State *L)
  87. {
  88. size_t symlen = 0;
  89. const char *sym = lua_tolstring(L, 1, &symlen);
  90. double ret = shared_locked_add(sym, symlen, +1.0, 1);
  91. lua_pushnumber(L, ret);
  92. return 1;
  93. }
  94. static int
  95. lua_shared_dec(struct lua_State *L)
  96. {
  97. size_t symlen = 0;
  98. const char *sym = lua_tolstring(L, 1, &symlen);
  99. double ret = shared_locked_add(sym, symlen, -1.0, 1);
  100. lua_pushnumber(L, ret);
  101. return 1;
  102. }
  103. static int
  104. lua_shared_exchange(struct lua_State *L)
  105. {
  106. size_t namlen = 0;
  107. const char *name = lua_tolstring(L, 1, &namlen);
  108. double num = lua_tonumber(L, 2);
  109. double ret;
  110. pthread_mutex_lock(&lua_shared_lock);
  111. lua_getglobal(L_shared, "shared");
  112. lua_pushlstring(L_shared, name, namlen);
  113. lua_rawget(L_shared, -2);
  114. ret = lua_tonumber(L_shared, -1);
  115. lua_getglobal(L_shared, "shared");
  116. lua_pushlstring(L_shared, name, namlen);
  117. lua_pushnumber(L_shared, num);
  118. lua_rawset(L_shared, -3);
  119. lua_pop(L_shared, 3);
  120. pthread_mutex_unlock(&lua_shared_lock);
  121. lua_pushnumber(L, ret);
  122. return 1;
  123. }
  124. #endif
  125. /* Read access to shared element (x = shared.element) */
  126. static int
  127. lua_shared_index(struct lua_State *L)
  128. {
  129. int key_type = lua_type(L, 2);
  130. int val_type;
  131. if ((key_type != LUA_TNUMBER) && (key_type != LUA_TSTRING)
  132. && (key_type != LUA_TBOOLEAN)) {
  133. return luaL_error(L, "shared index must be string, number or boolean");
  134. }
  135. if (key_type == LUA_TNUMBER) {
  136. double num = lua_tonumber(L, 2);
  137. pthread_mutex_lock(&lua_shared_lock);
  138. lua_getglobal(L_shared, "shared");
  139. lua_pushnumber(L_shared, num);
  140. } else if (key_type == LUA_TBOOLEAN) {
  141. int i = lua_toboolean(L, 2);
  142. pthread_mutex_lock(&lua_shared_lock);
  143. lua_getglobal(L_shared, "shared");
  144. lua_pushboolean(L_shared, i);
  145. } else {
  146. size_t len = 0;
  147. const char *str = lua_tolstring(L, 2, &len);
  148. if ((len > 1) && (0 == memcmp(str, "__", 2))) {
  149. #if defined(MG_EXPERIMENTAL_INTERFACES)
  150. /* Return functions */
  151. if (0 == strcmp(str, "__add")) {
  152. lua_pushcclosure(L, lua_shared_add, 0);
  153. } else if (0 == strcmp(str, "__inc")) {
  154. lua_pushcclosure(L, lua_shared_inc, 0);
  155. } else if (0 == strcmp(str, "__dec")) {
  156. lua_pushcclosure(L, lua_shared_dec, 0);
  157. } else if (0 == strcmp(str, "__exchange")) {
  158. lua_pushcclosure(L, lua_shared_exchange, 0);
  159. } else
  160. #endif
  161. {
  162. /* Unknown reserved index */
  163. lua_pushnil(L);
  164. }
  165. return 1;
  166. }
  167. pthread_mutex_lock(&lua_shared_lock);
  168. lua_getglobal(L_shared, "shared");
  169. lua_pushlstring(L_shared, str, len);
  170. }
  171. lua_rawget(L_shared, -2);
  172. val_type = lua_type(L_shared, -1);
  173. if (val_type == LUA_TNUMBER) {
  174. double num = lua_tonumber(L_shared, -1);
  175. lua_pushnumber(L, num);
  176. } else if (val_type == LUA_TBOOLEAN) {
  177. int i = lua_toboolean(L_shared, -1);
  178. lua_pushboolean(L, i);
  179. } else if (val_type == LUA_TNIL) {
  180. lua_pushnil(L);
  181. } else {
  182. size_t len = 0;
  183. const char *str = lua_tolstring(L_shared, -1, &len);
  184. lua_pushlstring(L, str, len);
  185. }
  186. lua_pop(L_shared, 2);
  187. pthread_mutex_unlock(&lua_shared_lock);
  188. return 1;
  189. }
  190. /* Write access to shared element (shared.element = x) */
  191. static int
  192. lua_shared_newindex(struct lua_State *L)
  193. {
  194. int key_type = lua_type(L, 2);
  195. int val_type = lua_type(L, 3);
  196. if ((key_type != LUA_TNUMBER) && (key_type != LUA_TSTRING)
  197. && (key_type != LUA_TBOOLEAN)) {
  198. return luaL_error(L, "shared index must be string, number or boolean");
  199. }
  200. if ((val_type != LUA_TNUMBER) && (val_type != LUA_TSTRING)
  201. && (val_type != LUA_TBOOLEAN) && (val_type != LUA_TNIL)) {
  202. return luaL_error(L, "shared value must be string, number or boolean");
  203. }
  204. if (key_type == LUA_TNUMBER) {
  205. double num = lua_tonumber(L, 2);
  206. pthread_mutex_lock(&lua_shared_lock);
  207. lua_getglobal(L_shared, "shared");
  208. lua_pushnumber(L_shared, num);
  209. } else if (key_type == LUA_TBOOLEAN) {
  210. int i = lua_toboolean(L, 2);
  211. pthread_mutex_lock(&lua_shared_lock);
  212. lua_getglobal(L_shared, "shared");
  213. lua_pushboolean(L_shared, i);
  214. } else {
  215. size_t len = 0;
  216. const char *str = lua_tolstring(L, 2, &len);
  217. if ((len > 1) && (0 == memcmp(str, "__", 2))) {
  218. return luaL_error(L, "shared index is reserved");
  219. }
  220. pthread_mutex_lock(&lua_shared_lock);
  221. lua_getglobal(L_shared, "shared");
  222. lua_pushlstring(L_shared, str, len);
  223. }
  224. if (val_type == LUA_TNUMBER) {
  225. double num = lua_tonumber(L, 3);
  226. lua_pushnumber(L_shared, num);
  227. } else if (val_type == LUA_TBOOLEAN) {
  228. int i = lua_toboolean(L, 3);
  229. lua_pushboolean(L_shared, i);
  230. } else if (val_type == LUA_TNIL) {
  231. lua_pushnil(L_shared);
  232. } else {
  233. size_t len = 0;
  234. const char *str = lua_tolstring(L, 3, &len);
  235. lua_pushlstring(L_shared, str, len);
  236. }
  237. lua_rawset(L_shared, -3);
  238. lua_pop(L_shared, 1);
  239. pthread_mutex_unlock(&lua_shared_lock);
  240. return 0;
  241. }
  242. /* Register the "shared" library in a new Lua state.
  243. * Call it once for every Lua state accessing "shared" elements. */
  244. LUA_SHARED_INTERFACE void
  245. lua_shared_register(struct lua_State *L)
  246. {
  247. lua_newuserdata(L, 0);
  248. lua_newtable(L);
  249. lua_pushliteral(L, "__index");
  250. lua_pushcclosure(L, lua_shared_index, 0);
  251. lua_rawset(L, -3);
  252. lua_pushliteral(L, "__newindex");
  253. lua_pushcclosure(L, lua_shared_newindex, 0);
  254. lua_rawset(L, -3);
  255. lua_setmetatable(L, -2);
  256. lua_setglobal(L, "shared");
  257. }