+/* Store the threads in a global variable so they are not cleaned up by the
+ * garbage collector. */
+static int clua_store_thread (lua_State *l, int idx, int callback_id) /* {{{ */
+{
+ if (idx < 0)
+ idx += lua_gettop (l) + 1;
+
+ lua_getfield (l, LUA_REGISTRYINDEX, "collectd_threads"); /* +1 = 1 */
+ if (!lua_istable (l, /* idx = */ -1))
+ {
+ lua_pop (l, /* nelems = */ 1); /* -1 = 0 */
+ lua_newtable (l); /* +1 = 1 */
+ lua_pushvalue (l, -1); /* +1 = 2 */
+ lua_setfield (l, LUA_REGISTRYINDEX, "collectd_threads"); /* -1 = 1 */
+ }
+
+ /* The table is now on top of the stack */
+ lua_pushinteger (l, (lua_Integer) callback_id); /* +1 = 2 */
+
+ /* Copy the thread pointer */
+ lua_pushvalue (l, idx); /* +1 = 3 */
+
+ if (!lua_isthread (l, /* idx = */ -1))
+ {
+ lua_pop (l, /* nelems = */ 3); /* -3 = 0 */
+ return (-1);
+ }
+
+ lua_settable (l, /* idx = */ -3); /* -2 = 1 */
+ lua_pop (l, /* nelems = */ 1); /* -1 = 0 */
+ return (0);
+} /* }}} int clua_store_thread */
+