Ejemplo n.º 1
0
/*
 * Arguments: pipe_udata
 */
static int
pipe_close (lua_State *L)
{
    struct pipe_ref *pr = checkudata(L, 1, PIPE_TYPENAME);
    struct pipe *pp = pr->pipe;

    if (pp) {
	thread_critsect_t *csp = pipe_critsect_ptr(pp);
	int nref;

	thread_critsect_enter(csp);
	nref = pp->nref--;
	thread_critsect_leave(csp);

	if (!nref) {
	    thread_critsect_del(&pp->cs);
	    thread_cond_del(&pp->put_cond);
	    thread_cond_del(&pp->get_cond);

	    /* deallocate buffers */
	    if (pp->rbuf) {
		struct pipe_buf *rpb = pp->rbuf, *wpb = pp->wbuf;
		do {
		    struct pipe_buf *pb = rpb->next_buf;
		    free(rpb);
		    rpb = pb;
		} while (rpb != wpb);
	    }
	    free(pp);
	}
	pr->pipe = NULL;
    }
    return 0;
}
Ejemplo n.º 2
0
/*
 * Arguments: thread_udata
 */
static int
thread_done (lua_State *L)
{
  struct sys_thread *td = checkudata(L, 1, THREAD_TYPENAME);

  if (td->L) {
    if (thread_isvm(td)) {
      thread_critsect_leave(td->vmcsp);
      thread_critsect_del(td->vmcsp);
    } else {
      sys_vm2_leave(td);
#ifndef _WIN32
      {
        THREAD_FUNC_RES v;
        pthread_join(td->tid, &v);
      }
#else
      WaitForSingleObject(td->tid, INFINITE);
      CloseHandle(td->tid);
#endif
      sys_vm2_enter(td);
    }
    (void) thread_cond_del(&td->cond);

    lua_rawgetp(L, LUA_REGISTRYINDEX, THREAD_KEY_ADDRESS);
    lua_pushnil(L);
    lua_rawsetp(L, -2, td->L); /* coroutine */
    lua_pop(L, 1);

    td->L = NULL;
  }
  return 0;
}
Ejemplo n.º 3
0
static int
channel_done (lua_State *L)
{
    struct channel *chan = checkudata(L, 1, CHANNEL_TYPENAME);

    thread_cond_del(&chan->put);
    thread_cond_del(&chan->get);
    thread_critsect_del(&chan->mutex);
    return 0;
}
Ejemplo n.º 4
0
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;
}
Ejemplo n.º 5
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;
}