mod_lua_shared.inl 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* Copyright (c) 2018 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. /*
  125. static int
  126. lua_shared_push(struct lua_State *L)
  127. {
  128. int val_type = lua_type(L, 1);
  129. if ((val_type != LUA_TNUMBER) && (val_type != LUA_TSTRING)
  130. && (val_type != LUA_TBOOLEAN)) {
  131. return luaL_error(L, "shared value must be string, number or boolean");
  132. }
  133. pthread_mutex_lock(&lua_shared_lock);
  134. lua_getglobal(L_shared, "shared");
  135. lua_pushnumber(L_shared, num);
  136. if (val_type == LUA_TNUMBER) {
  137. double num = lua_tonumber(L, 3);
  138. lua_pushnumber(L_shared, num);
  139. } else if (val_type == LUA_TBOOLEAN) {
  140. int i = lua_toboolean(L, 3);
  141. lua_pushboolean(L_shared, i);
  142. } else {
  143. size_t len = 0;
  144. const char *str = lua_tolstring(L, 3, &len);
  145. lua_pushlstring(L_shared, str, len);
  146. }
  147. lua_rawset(L_shared, -3);
  148. lua_pop(L_shared, 1);
  149. pthread_mutex_unlock(&lua_shared_lock);
  150. return 0;
  151. }
  152. */
  153. #endif
  154. /* Read access to shared element (x = shared.element) */
  155. static int
  156. lua_shared_index(struct lua_State *L)
  157. {
  158. int key_type = lua_type(L, 2);
  159. int val_type;
  160. if ((key_type != LUA_TNUMBER) && (key_type != LUA_TSTRING)
  161. && (key_type != LUA_TBOOLEAN)) {
  162. return luaL_error(L, "shared index must be string, number or boolean");
  163. }
  164. if (key_type == LUA_TNUMBER) {
  165. double num = lua_tonumber(L, 2);
  166. pthread_mutex_lock(&lua_shared_lock);
  167. lua_getglobal(L_shared, "shared");
  168. lua_pushnumber(L_shared, num);
  169. } else if (key_type == LUA_TBOOLEAN) {
  170. int i = lua_toboolean(L, 2);
  171. pthread_mutex_lock(&lua_shared_lock);
  172. lua_getglobal(L_shared, "shared");
  173. lua_pushboolean(L_shared, i);
  174. } else {
  175. size_t len = 0;
  176. const char *str = lua_tolstring(L, 2, &len);
  177. if ((len > 1) && (0 == memcmp(str, "__", 2))) {
  178. #if defined(MG_EXPERIMENTAL_INTERFACES)
  179. /* Return functions */
  180. if (0 == strcmp(str, "__add")) {
  181. lua_pushcclosure(L, lua_shared_add, 0);
  182. } else if (0 == strcmp(str, "__inc")) {
  183. lua_pushcclosure(L, lua_shared_inc, 0);
  184. } else if (0 == strcmp(str, "__dec")) {
  185. lua_pushcclosure(L, lua_shared_dec, 0);
  186. } else if (0 == strcmp(str, "__exchange")) {
  187. lua_pushcclosure(L, lua_shared_exchange, 0);
  188. /*
  189. } else if (0 == strcmp(str, "__push")) {
  190. lua_pushcclosure(L, lua_shared_push, 0);
  191. } else if (0 == strcmp(str, "__pop")) {
  192. lua_pushcclosure(L, lua_shared_pop, 0);
  193. */
  194. } else
  195. #endif
  196. {
  197. /* Unknown reserved index */
  198. lua_pushnil(L);
  199. }
  200. return 1;
  201. }
  202. pthread_mutex_lock(&lua_shared_lock);
  203. lua_getglobal(L_shared, "shared");
  204. lua_pushlstring(L_shared, str, len);
  205. }
  206. lua_rawget(L_shared, -2);
  207. val_type = lua_type(L_shared, -1);
  208. if (val_type == LUA_TNUMBER) {
  209. double num = lua_tonumber(L_shared, -1);
  210. lua_pushnumber(L, num);
  211. } else if (val_type == LUA_TBOOLEAN) {
  212. int i = lua_toboolean(L_shared, -1);
  213. lua_pushboolean(L, i);
  214. } else if (val_type == LUA_TNIL) {
  215. lua_pushnil(L);
  216. } else {
  217. size_t len = 0;
  218. const char *str = lua_tolstring(L_shared, -1, &len);
  219. lua_pushlstring(L, str, len);
  220. }
  221. lua_pop(L_shared, 2);
  222. pthread_mutex_unlock(&lua_shared_lock);
  223. return 1;
  224. }
  225. /* Write access to shared element (shared.element = x) */
  226. static int
  227. lua_shared_newindex(struct lua_State *L)
  228. {
  229. int key_type = lua_type(L, 2);
  230. int val_type = lua_type(L, 3);
  231. if ((key_type != LUA_TNUMBER) && (key_type != LUA_TSTRING)
  232. && (key_type != LUA_TBOOLEAN)) {
  233. return luaL_error(L, "shared index must be string, number or boolean");
  234. }
  235. if ((val_type != LUA_TNUMBER) && (val_type != LUA_TSTRING)
  236. && (val_type != LUA_TBOOLEAN) && (val_type != LUA_TNIL)) {
  237. return luaL_error(L, "shared value must be string, number or boolean");
  238. }
  239. if (key_type == LUA_TNUMBER) {
  240. double num = lua_tonumber(L, 2);
  241. pthread_mutex_lock(&lua_shared_lock);
  242. lua_getglobal(L_shared, "shared");
  243. lua_pushnumber(L_shared, num);
  244. } else if (key_type == LUA_TBOOLEAN) {
  245. int i = lua_toboolean(L, 2);
  246. pthread_mutex_lock(&lua_shared_lock);
  247. lua_getglobal(L_shared, "shared");
  248. lua_pushboolean(L_shared, i);
  249. } else {
  250. size_t len = 0;
  251. const char *str = lua_tolstring(L, 2, &len);
  252. if ((len > 1) && (0 == memcmp(str, "__", 2))) {
  253. return luaL_error(L, "shared index is reserved");
  254. }
  255. pthread_mutex_lock(&lua_shared_lock);
  256. lua_getglobal(L_shared, "shared");
  257. lua_pushlstring(L_shared, str, len);
  258. }
  259. if (val_type == LUA_TNUMBER) {
  260. double num = lua_tonumber(L, 3);
  261. lua_pushnumber(L_shared, num);
  262. } else if (val_type == LUA_TBOOLEAN) {
  263. int i = lua_toboolean(L, 3);
  264. lua_pushboolean(L_shared, i);
  265. } else if (val_type == LUA_TNIL) {
  266. lua_pushnil(L_shared);
  267. } else {
  268. size_t len = 0;
  269. const char *str = lua_tolstring(L, 3, &len);
  270. lua_pushlstring(L_shared, str, len);
  271. }
  272. lua_rawset(L_shared, -3);
  273. lua_pop(L_shared, 1);
  274. pthread_mutex_unlock(&lua_shared_lock);
  275. return 0;
  276. }
  277. /* Register the "shared" library in a new Lua state.
  278. * Call it once for every Lua state accessing "shared" elements. */
  279. LUA_SHARED_INTERFACE void
  280. lua_shared_register(struct lua_State *L)
  281. {
  282. lua_newuserdata(L, 0);
  283. lua_newtable(L);
  284. lua_pushliteral(L, "__index");
  285. lua_pushcclosure(L, lua_shared_index, 0);
  286. lua_rawset(L, -3);
  287. lua_pushliteral(L, "__newindex");
  288. lua_pushcclosure(L, lua_shared_newindex, 0);
  289. lua_rawset(L, -3);
  290. lua_setmetatable(L, -2);
  291. lua_setglobal(L, "shared");
  292. }