Пример #1
0
/*
 * note: the socket must be in non-blocking mode, or the call might block while the mutex is being locked
 */
int cloexec_accept(int socket, struct sockaddr *addr, socklen_t *addrlen)
{
    int fd = -1;
#ifndef _MSC_VER
    pthread_mutex_lock(&cloexec_mutex);
#else
	uv_mutex_lock(&cloexec_mutex);
#endif

    if ((fd = accept(socket, addr, addrlen)) == -1)
        goto Exit;
    if (set_cloexec(fd) != 0) {
        closesocket(fd);
        fd = -1;
        goto Exit;
    }

Exit:
#ifndef _MSC_VER
    pthread_mutex_unlock(&cloexec_mutex);
#else
	uv_mutex_unlock(&cloexec_mutex);
#endif
    return fd;
}
Пример #2
0
int pc_add_listener(pc_client_t *client, const char *event,
                    pc_event_cb event_cb) {
  if(PC_ST_CLOSED == client->state) {
    LOGD( "Pomelo client has closed.\n");
    return -1;
  }

  pc_listener_t *listener = pc_listener_new();
  if(listener == NULL) {
    LOGD( "Fail to create listener.\n");
    return -1;
  }
  listener->cb = event_cb;

  uv_mutex_lock(&client->listener_mutex);
  ngx_queue_t *head = pc_map_get(client->listeners, event);

  if(head == NULL) {
    head = (ngx_queue_t *)malloc(sizeof(ngx_queue_t));
    if(head == NULL) {
      LOGD( "Fail to create listener queue.\n");
      pc_listener_destroy(listener);
      return -1;
    }

    ngx_queue_init(head);

    pc_map_set(client->listeners, event, head);
  }

  ngx_queue_insert_tail(head, &listener->queue);
  uv_mutex_unlock(&client->listener_mutex);

  return 0;
}
Пример #3
0
void Adapter::cleanUpV8Resources()
{
    uv_mutex_lock(adapterCloseMutex);

    if (asyncStatus != nullptr)
    {
        auto handle = reinterpret_cast<uv_handle_t *>(asyncStatus);
        uv_close(handle, [](uv_handle_t *handle) {
            delete handle;
        });
        this->statusCallback.reset();

        asyncStatus = nullptr;
    }

    if (eventIntervalTimer != nullptr)
    {
        // Deallocate resources related to the event handling interval timer
        if (uv_timer_stop(eventIntervalTimer) != 0)
        {
            std::terminate();
        }

        auto handle = reinterpret_cast<uv_handle_t *>(eventIntervalTimer);
        uv_close(handle, [](uv_handle_t *handle)
        {
            delete handle;
        });

        eventIntervalTimer = nullptr;
    }

    if (asyncEvent != nullptr)
    {
        auto handle = reinterpret_cast<uv_handle_t *>(asyncEvent);

        uv_close(handle, [](uv_handle_t *handle)
        {
            delete handle;
        });
        this->eventCallback.reset();

        asyncEvent = nullptr;
    }

    if (asyncLog != nullptr)
    {
        auto logHandle = reinterpret_cast<uv_handle_t *>(asyncLog);
        uv_close(logHandle, [](uv_handle_t *handle)
        {
            delete handle;
        });
        this->logCallback.reset();

        asyncLog = nullptr;
    }

    uv_mutex_unlock(adapterCloseMutex);
}
Пример #4
0
int status_wait(Status* status, uint64_t timeout_secs) {
  int count;
  uv_mutex_lock(&status->mutex);
  uv_cond_timedwait(&status->cond, &status->mutex, timeout_secs * 1000 * 1000 * 1000);
  count = status->count;
  uv_mutex_unlock(&status->mutex);
  return count;
}
Пример #5
0
static int uv__custom_sem_trywait(uv_sem_t* sem_) {
  uv_semaphore_t* sem;

  sem = *(uv_semaphore_t**)sem_;
  if (uv_mutex_trylock(&sem->mutex) != 0)
    return UV_EAGAIN;

  if (sem->value == 0) {
    uv_mutex_unlock(&sem->mutex);
    return UV_EAGAIN;
  }

  sem->value--;
  uv_mutex_unlock(&sem->mutex);

  return 0;
}
Пример #6
0
inline static void uv__rwlock_fallback_rdlock(uv_rwlock_t* rwlock) {
  uv_mutex_lock(&rwlock->fallback_.read_mutex_);

  if (++rwlock->fallback_.num_readers_ == 1)
    uv_mutex_lock(&rwlock->fallback_.write_mutex_);

  uv_mutex_unlock(&rwlock->fallback_.read_mutex_);
}
Пример #7
0
void
udprelay_close(struct server_context *server) {
    uv_poll_stop(&server->watcher);
    close(server->udp_fd);
    uv_mutex_lock(&mutex);
    cache_removeall(cache, server->udp.loop, select_cb);
    uv_mutex_unlock(&mutex);
}
Пример #8
0
void Workers::submit(const JobResult &result)
{
    uv_mutex_lock(&m_mutex);
    m_queue.push_back(result);
    uv_mutex_unlock(&m_mutex);

    uv_async_send(&m_async);
}
Пример #9
0
size_t Workers::hugePages()
{
    uv_mutex_lock(&m_mutex);
    const size_t hugePages = m_status.hugePages;
    uv_mutex_unlock(&m_mutex);

    return hugePages;
}
Пример #10
0
size_t Workers::threads()
{
    uv_mutex_lock(&m_mutex);
    const size_t threads = m_status.threads;
    uv_mutex_unlock(&m_mutex);

    return threads;
}
Пример #11
0
static void WaitForDeviceHandled() {
	uv_mutex_lock(&notify_mutex);
	if(deviceHandled == false) {
		uv_cond_wait(&notifyDeviceHandled, &notify_mutex);
	}
	deviceHandled = false;
	uv_mutex_unlock(&notify_mutex);
}
Пример #12
0
void RecursiveLockGuard::release() {
    void* lkey = uv_key_get(mutex_key);
    if (lkey) {
        uv_mutex_unlock(mutex);
        uv_key_set(mutex_key, 0);
    }
    locked = false;
}
Пример #13
0
/*
 * TODO: last waiter frees descriptor ?
 */
void pg_cache_punch_hole(struct rrdengine_instance *ctx, struct rrdeng_page_cache_descr *descr)
{
    struct page_cache *pg_cache = &ctx->pg_cache;
    Pvoid_t *PValue;
    struct pg_cache_page_index *page_index;
    int ret;

    uv_rwlock_rdlock(&pg_cache->metrics_index.lock);
    PValue = JudyHSGet(pg_cache->metrics_index.JudyHS_array, descr->id, sizeof(uuid_t));
    assert(NULL != PValue);
    page_index = *PValue;
    uv_rwlock_rdunlock(&pg_cache->metrics_index.lock);

    uv_rwlock_wrlock(&page_index->lock);
    ret = JudyLDel(&page_index->JudyL_array, (Word_t)(descr->start_time / USEC_PER_SEC), PJE0);
    uv_rwlock_wrunlock(&page_index->lock);
    if (unlikely(0 == ret)) {
        error("Page under deletion was not in index.");
        if (unlikely(debug_flags & D_RRDENGINE))
            print_page_cache_descr(descr);
        goto destroy;
    }
    assert(1 == ret);

    uv_rwlock_wrlock(&pg_cache->pg_cache_rwlock);
    ++ctx->stats.pg_cache_deletions;
    --pg_cache->page_descriptors;
    uv_rwlock_wrunlock(&pg_cache->pg_cache_rwlock);

    uv_mutex_lock(&descr->mutex);
    while (!pg_cache_try_get_unsafe(descr, 1)) {
        debug(D_RRDENGINE, "%s: Waiting for locked page:", __func__);
        if(unlikely(debug_flags & D_RRDENGINE))
            print_page_cache_descr(descr);
        pg_cache_wait_event_unsafe(descr);
    }
    /* even a locked page could be dirty */
    while (unlikely(descr->flags & RRD_PAGE_DIRTY)) {
        debug(D_RRDENGINE, "%s: Found dirty page, waiting for it to be flushed:", __func__);
        if (unlikely(debug_flags & D_RRDENGINE))
            print_page_cache_descr(descr);
        pg_cache_wait_event_unsafe(descr);
    }
    uv_mutex_unlock(&descr->mutex);

    if (descr->flags & RRD_PAGE_POPULATED) {
        /* only after locking can it be safely deleted from LRU */
        pg_cache_replaceQ_delete(ctx, descr);

        uv_rwlock_wrlock(&pg_cache->pg_cache_rwlock);
        pg_cache_evict_unsafe(ctx, descr);
        uv_rwlock_wrunlock(&pg_cache->pg_cache_rwlock);
    }
    pg_cache_put(descr);
destroy:
    pg_cache_destroy_descr(descr);
    pg_cache_update_metric_times(page_index);
}
Пример #14
0
// send data to the grapher app 
void graph_thread(void* user) {
  Graph* g = static_cast<Graph*>(user);

  int sock = nn_socket(AF_SP, NN_PUSH);
  if(sock == -1) {
    printf("error: cannot create client socket.\n");
    return;
  }

  int rc = nn_connect(sock, g->address.c_str());
  if(rc < 0) {
    printf("error: cannot connect, %s\n", nn_strerror(errno));
    return;
  }
  std::vector<GraphPacket*> todo;

  while(true) {
    
    // get the work!
    uv_mutex_lock(&g->mutex);
    {
      while(g->work.size() == 0) {
        uv_cond_wait(&g->cv, &g->mutex);
      }
      std::copy(g->work.begin(), g->work.end(), std::back_inserter(todo));
      g->work.clear();
    }
    uv_mutex_unlock(&g->mutex);

    // perform work
    bool must_stop = false;
    for(std::vector<GraphPacket*>::iterator it = todo.begin(); it != todo.end(); ++it) {
      GraphPacket* p = *it;

      if(p->data[0] == PKT_TYPE_STOP) {
        must_stop = true;
        break;
      }
      else {
        int rc = nn_send(sock, (char*)&p->data.front(), p->data.size(), 0);
        if(rc < 0) {
          printf("error: cannot send to grapher: %s\n", nn_strerror(rc));
        }
      }

      delete p;
      p = NULL;
    } // for

    todo.clear();

    if(must_stop) {
      break;
    }

  }
  printf("@todo -> cleanup socket.\n");
}
Пример #15
0
static void worker(void* arg) {
  printf("worker\n");

  worker_config* wc = (worker_config*) arg;

  uv_mutex_lock(&wc->mutex);
  uv_cond_signal(&wc->cond);
  uv_mutex_unlock(&wc->mutex);
}
Пример #16
0
void EventStream::add(Event * event) {
  uv_mutex_lock(&mutex);

  eventVector.push_back(event);
  uv_ref(reinterpret_cast<uv_handle_t *>(&async));
  uv_mutex_unlock(&mutex);

  uv_async_send(&async);
}
Пример #17
0
void nub_thread_dispose(nub_thread_t* thread, nub_thread_disposed_cb cb) {
  thread->disposed = 1;
  thread->disposed_cb_ = cb;
  thread->nubloop->disposed_ = 1;
  uv_mutex_lock(&thread->nubloop->thread_dispose_lock_);
  fuq_enqueue(&thread->nubloop->thread_dispose_queue_, thread);
  uv_mutex_unlock(&thread->nubloop->thread_dispose_lock_);
  uv_async_send(thread->nubloop->work_ping_);
}
Пример #18
0
void wait_exit() {
  uv_mutex_lock(&mutex);
  while (close_future == NULL) {
    uv_cond_wait(&cond, &mutex);
  }
  uv_mutex_unlock(&mutex);
  cass_future_wait(close_future);
  cass_future_free(close_future);
}
Пример #19
0
void iobuf_remove(struct iobuf_t *io, size_t n) {
	uv_mutex_lock(&io->lock);
  if(n > 0 && n <= io->len) {
    memmove(io->buf, io->buf + n, io->len - n);
    io->len -= n;
		io->buf[io->len] = 0;
  }
	uv_mutex_unlock(&io->lock);
}
Пример #20
0
void signal_exit(CassSession* session) {
  uv_mutex_lock(&mutex);
  if (session) {
    close_future = cass_session_close(session);
  }
  exit_flag = 1;
  uv_cond_signal(&cond);
  uv_mutex_unlock(&mutex);
}
Пример #21
0
void uv_chan_send(uv_chan_t *chan, void *data) {
    uv__chan_item_t *item = (uv__chan_item_t *)malloc(sizeof(uv__chan_item_t));
    item->data = data;

    uv_mutex_lock(&chan->mutex);
    QUEUE_INSERT_TAIL(&chan->q, &item->active_queue);
    uv_cond_signal(&chan->cond);
    uv_mutex_unlock(&chan->mutex);
}
Пример #22
0
void
rtb_window_unlock(struct rtb_window *rwin)
{
    struct xrtb_window *self = RTB_WINDOW_AS(rwin, xrtb_window);

    glXMakeContextCurrent(self->xrtb->dpy, None, None, NULL);
    XUnlockDisplay(self->xrtb->dpy);
    uv_mutex_unlock(&self->lock);
}
Пример #23
0
static void lock_callback(int mode, int type, const char *file, int line)
{
	if (mode & CRYPTO_LOCK) {
		uv_mutex_lock(&(g_pMutex[type]));
	}
	else {
		uv_mutex_unlock(&(g_pMutex[type]));
	}
}
Пример #24
0
bool Kinect::setup() {

  if(ctx) {
    printf("Error: the freenect context has been setup already.\n");
    return false;
  }

  if(freenect_init(&ctx, NULL) < 0) {
    printf("Error: cannot init libfreenect.\n");
    return false;
  }

  freenect_set_log_level(ctx, FREENECT_LOG_DEBUG);

  int ndevices = freenect_num_devices(ctx);
  if(ndevices < 1) {
    printf("Error: cannot find a kinect. @todo cleanup mem.\n");
    freenect_shutdown(ctx);
    ctx = NULL;
    return false;
  }

  printf("Number of found kinect devices: %d\n", ndevices);

  freenect_select_subdevices(ctx, (freenect_device_flags)(FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA));
  //freenect_select_subdevices(ctx, (freenect_device_flags)( FREENECT_DEVICE_CAMERA));

  int devnum = 0;
  if(freenect_open_device(ctx, &device, devnum) < 0) {
    printf("Error: cannot open device: %d\n", devnum);
    freenect_shutdown(ctx);
    ctx = NULL;
    return false;
  }

  freenect_set_user(device, this);

  uint32_t w = 640;
  uint32_t h = 480;
  uint32_t nbytes = w * h * 3;
  nbytes_rgb = nbytes;
  rgb_back = new uint8_t[nbytes];
  rgb_mid = new uint8_t[nbytes];
  rgb_front = new uint8_t[nbytes];
  depth_back = new uint8_t[nbytes];
  depth_mid = new uint8_t[nbytes];
  depth_front = new uint8_t[nbytes];

  uv_mutex_lock(&mutex);
    must_stop = false;
  uv_mutex_unlock(&mutex);

  uv_thread_create(&thread, kinect_thread, this);

  return true;
}
Пример #25
0
int CAudioDecoder::put(cc_src_sample_t buf)
{
	int ret = 0;
	uv_mutex_lock(&queue_mutex);
	if (buf_deque.size() > 50){
		uv_mutex_unlock(&queue_mutex);
		free(buf.buf);
		return ret;
	}
	buf_deque.push_back(buf);
	iFrame++;
	uv_mutex_unlock(&queue_mutex);
	uv_cond_signal(&queue_not_empty);

	if (!bOpen && buf_deque.size() > 0){
		open();
	}
	return ret;
}
Пример #26
0
MVMObject * MVM_dll_find_symbol(MVMThreadContext *tc, MVMString *lib,
        MVMString *sym) {
    MVMDLLRegistry *entry;
    MVMDLLSym *obj;
    char *csym;
    void *address;

    uv_mutex_lock(&tc->instance->mutex_dll_registry);

    MVM_string_flatten(tc, lib);
    MVM_HASH_GET(tc, tc->instance->dll_registry, lib, entry);

    if (!entry) {
        uv_mutex_unlock(&tc->instance->mutex_dll_registry);
        MVM_exception_throw_adhoc(tc,
                "cannot find symbol in non-existent library");
    }

    if (!entry->lib) {
        uv_mutex_unlock(&tc->instance->mutex_dll_registry);
        MVM_exception_throw_adhoc(tc,
                "cannot find symbol in unloaded library");
    }

    csym = MVM_string_utf8_encode_C_string(tc, sym);
    address = dlFindSymbol(entry->lib, csym);
    free(csym);

    if (!address) {
        uv_mutex_unlock(&tc->instance->mutex_dll_registry);
        return NULL;
    }

    obj = (MVMDLLSym *)MVM_repr_alloc_init(tc,
            tc->instance->raw_types.RawDLLSym);
    obj->body.address = address;
    obj->body.dll = entry;

    entry->refcount++;

    uv_mutex_unlock(&tc->instance->mutex_dll_registry);
    return (MVMObject *)obj;
}
Пример #27
0
static void async_cb(uv_async_t *handle)
{
  Loop *l = handle->loop->data;
  uv_mutex_lock(&l->mutex);
  while (!multiqueue_empty(l->thread_events)) {
    Event ev = multiqueue_get(l->thread_events);
    multiqueue_put_event(l->fast_events, ev);
  }
  uv_mutex_unlock(&l->mutex);
}
Пример #28
0
static void uv__custom_sem_post(uv_sem_t* sem_) {
  uv_semaphore_t* sem;

  sem = *(uv_semaphore_t**)sem_;
  uv_mutex_lock(&sem->mutex);
  sem->value++;
  if (sem->value == 1)
    uv_cond_signal(&sem->cond);
  uv_mutex_unlock(&sem->mutex);
}
Пример #29
0
static void
_dynlock_lock_callback (int mode, CRYPTO_dynlock_value* self, const char* file, int line)
{
	if (mode & CRYPTO_LOCK) {
		uv_mutex_lock(self->lock);
	}
	else {
		uv_mutex_unlock(self->lock);
	}
}
Пример #30
0
static void uv__custom_sem_wait(uv_sem_t* sem_) {
  uv_semaphore_t* sem;

  sem = *(uv_semaphore_t**)sem_;
  uv_mutex_lock(&sem->mutex);
  while (sem->value == 0)
    uv_cond_wait(&sem->cond, &sem->mutex);
  sem->value--;
  uv_mutex_unlock(&sem->mutex);
}