Exemplo n.º 1
0
void nub_thread_join(nub_thread_t* thread) {
  ASSERT(NULL != thread);
  thread->disposed = 1;
  uv_sem_post(&thread->sem_wait_);
  uv_thread_join(&thread->uvthread);
  uv_close((uv_handle_t*) thread->async_signal_, nub__free_handle_cb);
  uv_sem_destroy(&thread->thread_lock_sem_);
  uv_sem_destroy(&thread->sem_wait_);
  --thread->nubloop->ref_;
  thread->nubloop = NULL;
}
Exemplo n.º 2
0
int uv_barrier_init(uv_barrier_t* barrier, unsigned int count) {
  int err;

  barrier->n = count;
  barrier->count = 0;

  err = uv_mutex_init(&barrier->mutex);
  if (err)
    return -err;

  err = uv_sem_init(&barrier->turnstile1, 0);
  if (err)
    goto error2;

  err = uv_sem_init(&barrier->turnstile2, 1);
  if (err)
    goto error;

  return 0;

error:
  uv_sem_destroy(&barrier->turnstile1);
error2:
  uv_mutex_destroy(&barrier->mutex);
  return -err;

}
Exemplo n.º 3
0
/* kproc exit */
void
pexit(char *msg, int t)
{
	Osenv *e;
	kproc_t* p;
	Osdep *os;

	USED(t);

    /* get our process context and remove ourselves from proc list
     */
    trace(TRACE_DEBUG, "kernel/pexit: process shutdown");
    
	p = (kproc_t*)up;

	lock(&procs.l);
	if(p->prev)
		p->prev->next = p->next;
	else
		procs.head = p->next;

	if(p->next)
		p->next->prev = p->prev;
	else
		procs.tail = p->prev;
	unlock(&procs.l);

	if(0)
		print("pexit: %s: %s\n", p->text, msg);

    trace(TRACE_DEBUG, "kernel/pexit: releasing resources"); 
    
    /* release name,fid and env groups */
	e = p->env;
	if(e != nil) {
    trace(TRACE_DEBUG, "kernel/pexit: closing fgrp"); 
		closefgrp(e->fgrp);
    trace(TRACE_DEBUG, "kernel/pexit: closing pgrp"); 
		closepgrp(e->pgrp);
    trace(TRACE_DEBUG, "kernel/pexit: closing egrp"); 
		closeegrp(e->egrp);
    trace(TRACE_DEBUG, "kernel/pexit: closing signatures"); 
		closesigs(e->sigs);
    trace(TRACE_DEBUG, "kernel/pexit: freeing user");
		free(e->user);
	}
	
    /* release os dependencies */
	os = p->os;
    if(os != nil){
        trace(TRACE_DEBUG, "kernel/pexit: deleting semaphore"); 
		uv_sem_destroy(&os->sem);
        trace(TRACE_DEBUG, "kernel/pexit: freeing os dependencies"); 
		free(os);
	}
       
    /* release the process itself */
    trace(TRACE_DEBUG, "kernel/pexit: freeing process"); 
	free(p);
}
Exemplo n.º 4
0
void uv__stream_close(uv_stream_t* handle) {
#if defined(__APPLE__)
  /* Terminate select loop first */
  if (handle->select != NULL) {
    uv__stream_select_t* s;

    s = handle->select;

    uv_sem_post(&s->sem);
    uv__stream_osx_interrupt_select(handle);
    uv_thread_join(&s->thread);
    uv_sem_destroy(&s->sem);
    uv_mutex_destroy(&s->mutex);
    close(s->fake_fd);
    close(s->int_fd);
    uv_close((uv_handle_t*) &s->async, uv__stream_osx_cb_close);

    handle->select = NULL;
  }
#endif /* defined(__APPLE__) */

  uv_read_stop(handle);
  uv__io_close(handle->loop, &handle->io_watcher);

  close(handle->io_watcher.fd);
  handle->io_watcher.fd = -1;

  if (handle->accepted_fd >= 0) {
    close(handle->accepted_fd);
    handle->accepted_fd = -1;
  }

  assert(!uv__io_active(&handle->io_watcher, UV__POLLIN | UV__POLLOUT));
}
Exemplo n.º 5
0
void
mrb_uv_sem_free(mrb_state *mrb, void *p)
{
  if(!p) { return; }

  uv_sem_destroy((uv_sem_t*)p);
  mrb_free(mrb, p);
}
Exemplo n.º 6
0
static void
Semaphore_tp_dealloc(Semaphore *self)
{
    if (self->initialized) {
        uv_sem_destroy(&self->uv_semaphore);
    }
    Py_TYPE(self)->tp_free(self);
}
void async_worker_free(async_worker_t *const worker) {
	if(!worker) return;
	assert(!worker->work);
	uv_sem_post(worker->sem);
	uv_thread_join(worker->thread);
	uv_sem_destroy(worker->sem);
	uv_close((uv_handle_t *)&worker->async);
	free(worker);
}
Exemplo n.º 8
0
static mrb_value
mrb_uv_sem_destroy(mrb_state *mrb, mrb_value self)
{
  uv_sem_t *sem = (uv_sem_t*)mrb_uv_get_ptr(mrb, self, &sem_type);
  uv_sem_destroy(sem);
  mrb_free(mrb, DATA_PTR(self));
  DATA_PTR(self) = NULL;
  return self;
}
Exemplo n.º 9
0
void Thread::start()
{
	if (this->is_started.exchange(true))
	{
		LOG(INFO) << "Thread " << static_cast<void*>(this) << " already started";
		return;
	}

	uv_sem_init(&this->semaphore, 0);
	uv_thread_create(&this->thread_id, &Thread::thread_worker, static_cast<void*>(this));
	uv_sem_wait(&this->semaphore);
	uv_sem_destroy(&this->semaphore);
}
Exemplo n.º 10
0
int uv_barrier_init(uv_barrier_t* barrier, unsigned int count) {
    barrier->n = count;
    barrier->count = 0;

    if (uv_mutex_init(&barrier->mutex)) return -1;

    if (uv_sem_init(&barrier->turnstile1, 0)) goto error2;

    if (uv_sem_init(&barrier->turnstile2, 1)) goto error;

    return 0;

error:
    uv_sem_destroy(&barrier->turnstile1);
error2:
    uv_mutex_destroy(&barrier->mutex);
    return -1;
}
Exemplo n.º 11
0
static void init_threads(void) {
  unsigned int i;
  const char* val;
  uv_sem_t sem;

  nthreads = ARRAY_SIZE(default_threads);
  val = getenv("UV_THREADPOOL_SIZE");
  if (val != NULL)
    nthreads = atoi(val);
  if (nthreads == 0)
    nthreads = 1;
  if (nthreads > MAX_THREADPOOL_SIZE)
    nthreads = MAX_THREADPOOL_SIZE;

  threads = default_threads;
  if (nthreads > ARRAY_SIZE(default_threads)) {
    threads = (uv_thread_t*)uv__malloc(nthreads * sizeof(threads[0]));
    if (threads == NULL) {
      nthreads = ARRAY_SIZE(default_threads);
      threads = default_threads;
    }
  }

  if (uv_cond_init(&cond))
    abort();

  if (uv_mutex_init(&mutex))
    abort();

  QUEUE_INIT(&wq);

  if (uv_sem_init(&sem, 0))
    abort();

  for (i = 0; i < nthreads; i++)
    if (uv_thread_create(threads + i, worker, &sem))
      abort();

  for (i = 0; i < nthreads; i++)
    uv_sem_wait(&sem);

  uv_sem_destroy(&sem);
}
Exemplo n.º 12
0
void uv__platform_loop_delete(uv_loop_t* loop) {
  QUEUE* item;
  uv__cf_loop_signal_t* s;

  assert(loop->cf_loop != NULL);
  uv__cf_loop_signal(loop, NULL, NULL);
  uv_thread_join(&loop->cf_thread);

  uv_sem_destroy(&loop->cf_sem);
  uv_mutex_destroy(&loop->cf_mutex);

  /* Free any remaining data */
  while (!QUEUE_EMPTY(&loop->cf_signals)) {
    item = QUEUE_HEAD(&loop->cf_signals);

    s = QUEUE_DATA(item, uv__cf_loop_signal_t, member);

    QUEUE_REMOVE(item);
    free(s);
  }
}
Exemplo n.º 13
0
void uv__platform_loop_delete(uv_loop_t* loop) {
  ngx_queue_t* item;
  uv__cf_loop_signal_t* s;

  assert(loop->cf_loop != NULL);
  uv__cf_loop_signal(loop, NULL, NULL);
  uv_thread_join(&loop->cf_thread);

  uv_sem_destroy(&loop->cf_sem);
  uv_mutex_destroy(&loop->cf_mutex);

  /* Free any remaining data */
  while (!ngx_queue_empty(&loop->cf_signals)) {
    item = ngx_queue_head(&loop->cf_signals);

    s = ngx_queue_data(item, uv__cf_loop_signal_t, member);

    ngx_queue_remove(item);
    free(s);
  }
}
Exemplo n.º 14
0
Arquivo: loop.c Projeto: nubjs/libnub
void nub_loop_dispose(nub_loop_t* loop) {
  ASSERT(0 == uv_loop_alive(&loop->uvloop));
  ASSERT(1 == fuq_empty(&loop->blocking_queue_));
  ASSERT(0 == loop->ref_);
  ASSERT(NULL != loop->work_ping_);
  ASSERT(0 == uv_has_ref((uv_handle_t*) loop->work_ping_));
  ASSERT(1 == fuq_empty(&loop->thread_dispose_queue_));

  uv_close((uv_handle_t*) loop->work_ping_, nub__free_handle_cb);
  uv_close((uv_handle_t*) &loop->queue_processor_, NULL);
  ASSERT(0 == uv_is_active((uv_handle_t*) loop->work_ping_));

  fuq_dispose(&loop->thread_dispose_queue_);
  uv_mutex_destroy(&loop->thread_dispose_lock_);
  uv_sem_destroy(&loop->loop_lock_sem_);
  fuq_dispose(&loop->blocking_queue_);
  uv_mutex_destroy(&loop->queue_processor_lock_);
  fuq_dispose(&loop->work_queue_);
  uv_mutex_destroy(&loop->work_lock_);

  CHECK_EQ(0, uv_run(&loop->uvloop, UV_RUN_NOWAIT));
  CHECK_NE(UV_EBUSY, uv_loop_close(&loop->uvloop));
}
Exemplo n.º 15
0
int uv__stream_try_select(uv_stream_t* stream, int fd) {
  /*
   * kqueue doesn't work with some files from /dev mount on osx.
   * select(2) in separate thread for those fds
   */

  struct kevent filter[1];
  struct kevent events[1];
  struct timespec timeout;
  uv__stream_select_t* s;
  int fds[2];
  int ret;
  int kq;

  kq = kqueue();
  if (kq == -1) {
    fprintf(stderr, "(libuv) Failed to create kqueue (%d)\n", errno);
    return uv__set_sys_error(stream->loop, errno);
  }

  EV_SET(&filter[0], fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);

  /* Use small timeout, because we only want to capture EINVALs */
  timeout.tv_sec = 0;
  timeout.tv_nsec = 1;

  ret = kevent(kq, filter, 1, events, 1, &timeout);
  SAVE_ERRNO(close(kq));

  if (ret == -1)
    return uv__set_sys_error(stream->loop, errno);

  if ((events[0].flags & EV_ERROR) == 0 || events[0].data != EINVAL)
    return 0;

  /* At this point we definitely know that this fd won't work with kqueue */
  s = malloc(sizeof(*s));
  if (s == NULL)
    return uv__set_artificial_error(stream->loop, UV_ENOMEM);

  s->fd = fd;

  if (uv_async_init(stream->loop, &s->async, uv__stream_osx_select_cb)) {
    SAVE_ERRNO(free(s));
    return uv__set_sys_error(stream->loop, errno);
  }

  s->async.flags |= UV__HANDLE_INTERNAL;
  uv__handle_unref(&s->async);

  if (uv_sem_init(&s->sem, 0))
    goto fatal1;

  if (uv_mutex_init(&s->mutex))
    goto fatal2;

  /* Create fds for io watcher and to interrupt the select() loop. */
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds))
    goto fatal3;

  s->fake_fd = fds[0];
  s->int_fd = fds[1];

  if (uv_thread_create(&s->thread, uv__stream_osx_select, stream))
    goto fatal4;

  s->stream = stream;
  stream->select = s;

  return 0;

fatal4:
  close(s->fake_fd);
  close(s->int_fd);
  s->fake_fd = -1;
  s->int_fd = -1;
fatal3:
  uv_mutex_destroy(&s->mutex);
fatal2:
  uv_sem_destroy(&s->sem);
fatal1:
  uv_close((uv_handle_t*) &s->async, uv__stream_osx_cb_close);
  return uv__set_sys_error(stream->loop, errno);
}
Exemplo n.º 16
0
void
uvm_chime_destroy(uvm_chime_t* c){
    uv_sem_destroy(&c->sem);
}
Exemplo n.º 17
0
void uv_barrier_destroy(uv_barrier_t* barrier) {
  uv_sem_destroy(&barrier->turnstile2);
  uv_sem_destroy(&barrier->turnstile1);
  uv_mutex_destroy(&barrier->mutex);
}
Exemplo n.º 18
0
 Semaphore::~Semaphore()
 {
     uv_sem_destroy(&sem);
 }
Exemplo n.º 19
0
static void done_cb(uv_work_t* req, int status) {
  uv_sem_destroy(pause_sems + (req - pause_reqs));
}