Exemple #1
0
int main() {
  int tracklen = 10;
  uv_thread_t hare_id;
  uv_thread_t tortoise_id;
  uv_thread_create(&hare_id, hare, &tracklen);
  uv_thread_create(&tortoise_id, tortoise, &tracklen);

  uv_thread_join(&hare_id);
  uv_thread_join(&tortoise_id);
  return 0;
}
Exemple #2
0
int main()
{
	uv_thread_t threads[2];
	main_thread_id = uv_thread_self();	//获得当前线程的id
 
	uv_thread_create(threads + 0, check_thread, subthreads + 0);	//创建子线程
	uv_thread_create(threads + 1, check_thread, subthreads + 1);
 
	uv_thread_join(threads + 0);	//thread join,阻塞,直到目标子线程执行完成
	uv_thread_join(threads + 1);
 
	return 0;
}
Exemple #3
0
static int run_test(int inprocess) {
  uv_process_t process;
  uv_thread_t tid;
  int r;

  if (inprocess) {
    r = uv_thread_create(&tid, ipc_send_recv_helper_threadproc, (void *) 42);
    ASSERT(r == 0);

    uv_sleep(1000);

    r = uv_pipe_init(uv_default_loop(), &ctx.channel, 1);
    ASSERT(r == 0);

    uv_pipe_connect(&ctx.connect_req, &ctx.channel, TEST_PIPENAME_3, connect_cb);
  } else {
    spawn_helper(&ctx.channel, &process, "ipc_send_recv_helper");

    connect_cb(&ctx.connect_req, 0);
  }

  r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  ASSERT(r == 0);

  ASSERT(recv_cb_count == 2);

  if (inprocess) {
    r = uv_thread_join(&tid);
    ASSERT(r == 0);
  }

  return 0;
}
Exemple #4
0
int main(int argc, char **argv) {
  printf("starting...");
  int r = uv_thread_create(&thread, thread_cb, NULL);
  uv_thread_join(&thread);
  printf("done.\n");
  return 0;
}
Exemple #5
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));
}
Exemple #6
0
static void cleanup(void) {
#else
UV_DESTRUCTOR(static void cleanup(void)) {
#endif
  unsigned int i;

  if (initialized == 0)
    return;

  post(&exit_message);

  for (i = 0; i < nthreads; i++)
    if (uv_thread_join(threads + i))
      abort();

  if (threads != default_threads)
    uv__free(threads);

  uv_mutex_destroy(&mutex);
  uv_cond_destroy(&cond);

  threads = NULL;
  nthreads = 0;
  initialized = 0;
  // tuv
  // It is very important to initialize once again.
  // Without following once initialization,
  // test/run_pass/test_fs_xxx blocks on second running.
  once = UV_ONCE_INIT;
}
Exemple #7
0
void Log::stopWriter(bool waitThread) {
	if(this->stop == true) {
		return;
	}

#ifdef _WIN32
	if(!this->logWritterThreadStarted || this->logWritterThreadNativeId == GetCurrentThreadId())
#else /* unix */
	if(!this->logWritterThreadStarted || pthread_equal(pthread_self(), this->logWritterThreadId))
#endif
	{
		this->stop = true;
		return;
	}

	Object::log(LL_Debug, "Stopping log thread\n");
	log(LL_Info, this, "Log thread stopped\n");

	uv_mutex_lock(&this->messageListMutex);
	this->stop = true;
	uv_cond_signal(&this->messageListCond);
	uv_mutex_unlock(&this->messageListMutex);

	if(waitThread)
		uv_thread_join(&this->logWritterThreadId);
}
Exemple #8
0
static int luv_thread_join(lua_State* L) {
  luv_thread_t* tid = luv_check_thread(L, 1);
  int ret = uv_thread_join(&tid->handle);
  if (ret < 0) return luv_error(L, ret);
  lua_pushboolean(L, 1);
  return 1;
}
Exemple #9
0
/**
 * Close a number of sessions concurrently or sequentially
 *
 * @param sessions Session container for closing sessions
 * @param num_of_sessions Number of sessions to close concurrently
 * @param is_concurrently True if concurrent; false otherwise
 */
void close_sessions(SessionContainer* sessions, unsigned int num_of_sessions, bool is_concurrently = true) {
  //Close session threads (LIFO)
  std::vector<uv_thread_t> threads(num_of_sessions);
  for (unsigned int n = 0; n < num_of_sessions; ++n) {
    if (is_concurrently) {
      uv_thread_create(&threads[n], close_session, sessions->sessions[sessions->count() - n - 1].get());
    } else {
      close_session(sessions->sessions[sessions->count() - n - 1].get());
    }
  }

  //Ensure all threads have completed
  if (is_concurrently) {
    for (unsigned int n = 0; n < num_of_sessions; ++n) {
      uv_thread_join(&threads[n]);
    }
  }

  sessions->sessions.clear();

  //TODO: Remove sleep and create a timed wait for log messages (no boost)
  for (unsigned int n = 0; n < (SESSION_STRESS_NUMBER_OF_ITERATIONS * 20); ++n) {
    if (static_cast<unsigned int>(test_utils::CassLog::message_count()) != num_of_sessions) {
      boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
    } else {
      break;
    }
  }
  BOOST_TEST_MESSAGE("\t\tClosed: " << test_utils::CassLog::message_count());
}
Exemple #10
0
/**
 * Perform query operations using each session in multiple threads
 *
 * @param sessions Session container for closing sessions
 */
void query_sessions(const SessionContainer& sessions) {
  //Query session in multiple threads
  unsigned int thread_count = sessions.count() * SESSION_STRESS_NUMBER_OF_SHARED_SESSION_THREADS;
  BOOST_TEST_MESSAGE("\t\tThreads: " << thread_count);


  std::vector<uv_thread_t> threads(thread_count);
  std::vector<QuerySession> queries(thread_count);

  for (unsigned int iterations = 0; iterations < SESSION_STRESS_NUMBER_OF_SHARED_SESSION_THREADS; ++iterations) {
    for (unsigned int n = 0; n < sessions.count(); ++n) {
      int thread_index = (sessions.count() * iterations) + n;
      queries.push_back(QuerySession(sessions.sessions[n].get()));
      uv_thread_create(&threads[thread_index], query_session, &queries.back());
    }
  }

  //Ensure all threads have completed
  for (unsigned int n = 0; n < thread_count; ++n) {
    uv_thread_join(&threads[n]);
  }

  for (unsigned int n = 0; n < thread_count; ++n) {
    //Timeouts are OK (especially on the minor chaos test)
    CassError error_code = queries[n].error_code;
    if (error_code != CASS_OK && error_code != CASS_ERROR_LIB_REQUEST_TIMED_OUT) {
      BOOST_FAIL("Error occurred during query '" << std::string(cass_error_desc(error_code)));
    }
  }
}
 /**
  * Stop the executing query thread
  */
 void stop_query_execution() {
   is_running_ = false;
   uv_thread_join(&thread_);
   if (is_error_) {
     BOOST_FAIL("Error occurred during query execution");
   }
 }
Exemple #12
0
 void Thread::join()
 {
     int r = uv_thread_join(&thread);
     if (r != 0)
     {
         throw std::logic_error(uv_strerror(r));
     }
 }
Exemple #13
0
 void join() {
   if (is_joinable_) {
     is_joinable_ = false;
     int rc = uv_thread_join(&thread_);
     UNUSED_(rc);
     assert(rc == 0);
   }
 }
Exemple #14
0
int pc_client_join(pc_client_t *client) {
  if(PC_ST_WORKING != client->state) {
    LOGD( "Fail to join client for invalid state: %d.\n",
            client->state);
    return -1;
  }
  return uv_thread_join(&client->worker);
}
Exemple #15
0
Graph::~Graph() {
  printf("error: need to correctly shutdown graph; stop thread!\n");
  if(is_running) {
    stop();
    uv_thread_join(&thread);
  }
  is_running = false;
}
Exemple #16
0
int main() {
    uv_thread_t tid;
    loop = create_loop();
    async_handle = malloc(sizeof(uv_async_t));
    uv_async_init(loop, async_handle, async_callback);
    uv_thread_create(&tid, thread_main, NULL);
    uv_async_send(async_handle);
    uv_thread_join(&tid);
}
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);
}
Exemple #18
0
void Thread::join()
{
    TraceS(this) << "Joining" << std::endl;
    assert(this->tid() != Thread::currentID());
    //assert(this->cancelled()); // probably should be cancelled, but depends on impl
    uv_thread_join(&_handle);    
    assert(!this->running());
    assert(!this->started());
    TraceS(this) << "Joining: OK" << std::endl;
}
Exemple #19
0
static void ui_bridge_stop(UI *b)
{
  UI_CALL(b, stop, 1, b);
  UIBridgeData *bridge = (UIBridgeData *)b;
  uv_thread_join(&bridge->ui_thread);
  uv_mutex_destroy(&bridge->mutex);
  uv_cond_destroy(&bridge->cond);
  ui_detach(b);
  xfree(b);
}
Exemple #20
0
static mrb_value
mrb_uv_thread_join(mrb_state *mrb, mrb_value self)
{
  mrb_uv_thread* context = NULL;

  Data_Get_Struct(mrb, self, &mrb_uv_thread_type, context);

  uv_thread_join(&context->thread);
  return self;
}
Exemple #21
0
static void uv_finit(void)
{
	//uv_unref((uv_handle_t*)&g_timer);
	uv_timer_stop(&g_timer);
	uv_stop(&g_loop);
	uv_async_init(&g_loop, &g_async, NULL);
	uv_async_send(&g_async);
	uv_thread_join(&g_loop_thread);
	if (g_sys) delete g_sys;
	//if (g_remote) delete g_remote;
}
Exemple #22
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;
}
Exemple #23
0
Kinect::~Kinect() {
  printf("Error: need to free buffers.\n");

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

  uv_thread_join(&thread);

  if(ctx) {
    freenect_close_device(device);
    freenect_shutdown(ctx);
    ctx = NULL;
  }

  uv_mutex_destroy(&mutex);

  has_new_rgb = false;
  has_new_depth = false;

  if(depth_back) {
    delete[] depth_back;
  }

  if(depth_mid) {
    delete[] depth_mid;
  }

  if(depth_front) {
    delete[] depth_front;
  }

  depth_back = NULL;
  depth_mid = NULL;
  depth_front = NULL;

  if(rgb_back) {
    delete[] rgb_back;
  }

  if(rgb_mid) {
    delete[] rgb_mid;
  }

  if(rgb_front) {
    delete[] rgb_front;
  }

  rgb_back = NULL;
  rgb_mid = NULL;
  rgb_front = NULL;
  device = NULL;
}
Exemple #24
0
void Thread::stop()
{
	if (!this->is_started.exchange(false))
	{
		LOG(INFO) << "Thread " << static_cast<void*>(this) << " already stopped";
		return;
	}

	uv_stop(&this->loop);
	uv_async_send(&this->async);
	uv_thread_join(&this->thread_id);
}
Exemple #25
0
void ThreadManager::stopTask(const std::string& name) {
    task_map_t::iterator it = task_map.find(name);
    if (it != task_map.end()) {
        Task& task = it->second;
        uv_async_send(&task.cleanup);

        if (!adaptor) {
            uv_thread_join(&task.thread);
            uv_loop_close(task.loop);
            delete task.loop;
            task.loop = NULL;
        }
    }
}
TCPClient::~TCPClient()
{
    Close();
    uv_thread_join(&connect_threadhandle_);
    FreeTcpClientCtx(client_handle_);
    uv_loop_close(&loop_);
    uv_mutex_destroy(&mutex_writebuf_);
    for (auto it = writeparam_list_.begin(); it != writeparam_list_.end(); ++it) {
        FreeWriteParam(*it);
    }
    writeparam_list_.clear();

    LOG(INFO) << "client(" << this << ")exit";
}
int main() {
  assert(0 == uv_mutex_init(&mutex));
  assert(0 == uv_thread_create(&thread, thread_cb, NULL));

  uv_mutex_lock(&mutex);
  printf("main mutex start\n");
  sleep(1);
  printf("main mutex end\n");
  uv_mutex_unlock(&mutex);

  uv_thread_join(&thread);
  uv_mutex_destroy(&mutex);

  return 0;
}
static void timer_cb(uv_timer_t* handle) {
  unsigned i;

  done = 1;
  ASSERT(0 == uv_thread_join(&thread_id));

  for (i = 0; i < ARRAY_SIZE(container->async_handles); i++) {
    uv_async_t* handle = container->async_handles + i;

    if (handle->data != NULL)
      container->handles_seen++;

    uv_close((uv_handle_t*) handle, NULL);
  }

  uv_close((uv_handle_t*) handle, NULL);
}
Exemple #29
0
/* Lua API */
static int rava_thread_join(lua_State* L)
{
  rava_thread_t* self = (rava_thread_t*)luaL_checkudata(L, 1, RAVA_PROCESS_THREAD);
  rava_thread_t* curr = ravaL_thread_self(L);

  ravaL_thread_ready(self);
  ravaL_thread_suspend(curr);
  uv_thread_join(&self->tid); /* TODO: use async instead, this blocks hard */

  lua_settop(L, 0);

  int nret = lua_gettop(self->L);

  ravaL_serialize_encode(self->L, nret);
  lua_xmove(self->L, L, 1);
  ravaL_serialize_decode(L);

  return nret;
}
Exemple #30
0
__attribute__((destructor)) static void cleanup(void) {
  unsigned int i;

  if (initialized == 0) return;

  post(&exit_message);

  for (i = 0; i < nthreads; i++)
    if (uv_thread_join(threads + i)) abort();

  if (threads != default_threads) JX_FREE(threadpool, threads);

  uv_mutex_destroy(&mutex);
  uv_cond_destroy(&cond);

  threads = NULL;
  nthreads = 0;
  initialized = 0;
}