static int vmthread_new (lua_State *L, struct sys_vmthread **vmtdp) { struct sys_vmthread *vmtd; lua_pushthread(L); vmtd = lua_newuserdata(L, sizeof(struct sys_vmthread)); memset(vmtd, 0, sizeof(struct sys_vmthread)); vmtd->td.L = L; vmtd->td.tid = thread_getid(); vmtd->td.vmtd = vmtd; /* vm-mutex destructor */ lua_pushlightuserdata(L, &g_TLSIndex); lua_rawget(L, LUA_REGISTRYINDEX); lua_pushvalue(L, -1); lua_setmetatable(L, -3); lua_pushlightuserdata(L, vmtd); lua_pushvalue(L, -3); /* thread_udata */ lua_rawset(L, -3); lua_pop(L, 1); if (thread_event_new(&vmtd->bufev)) return -1; #ifndef _WIN32 vmtd->td.mutex = &vmtd->vmmutex; if (thread_critsect_new(vmtd->td.mutex)) { thread_event_del(&vmtd->bufev); return -1; } #else if (thread_critsect_new(&vmtd->bufcs)) { thread_event_del(&vmtd->bufev); return -1; } vmtd->td.mutex = CreateMutex(NULL, FALSE, NULL); if (!vmtd->td.mutex) { thread_event_del(&vmtd->bufev); thread_critsect_del(&vmtd->bufcs); return -1; } #endif *vmtdp = vmtd; return 0; }
/* * Arguments: dpool_udata */ static int dpool_done (lua_State *L) { struct data_pool *dp = checkudata(L, 1, DPOOL_TYPENAME); thread_event_del(&dp->tev); return 0; }
/* * Arguments: dpool_udata */ static int dpool_close (lua_State *L) { struct data_pool *dp = checkudata(L, 1, DPOOL_TYPENAME); if (dp->flags & DPOOL_OPEN) { dp->flags ^= DPOOL_OPEN; thread_event_del(&dp->tev); } return 0; }
static int vmthread_del (lua_State *L) { struct sys_vmthread *vmtd = lua_touserdata(L, 1); if (vmtd->td.mutex) { thread_event_del(&vmtd->bufev); #ifdef _WIN32 thread_critsect_del(&vmtd->bufcs); #endif free(vmtd->buffer.ptr); #ifndef _WIN32 pthread_mutex_destroy(vmtd->td.mutex); #else CloseHandle(vmtd->td.mutex); #endif vmtd->td.mutex = NULL; } return 0; }