Example #1
1
void thread_cb(void* arg) {
  thread_comm_t* comm = (thread_comm_t*) arg;

  printf("pre async send\n");
  uv_async_send(&comm->async);
  printf("post async send\n");
}
Example #2
0
void NodeMap::startRender(NodeMap::RenderOptions options) {
    view.resize(options.width, options.height);
    map->update(mbgl::Update::Dimensions);
    map->setClasses(options.classes);
    map->setLatLngZoom(mbgl::LatLng(options.latitude, options.longitude), options.zoom);
    map->setBearing(options.bearing);
    map->setPitch(options.pitch);
    map->setDebug(options.debugOptions);

    map->renderStill([this](const std::exception_ptr eptr, mbgl::PremultipliedImage&& result) {
        if (eptr) {
            error = std::move(eptr);
            uv_async_send(async);
        } else {
            assert(!image.data);
            image = std::move(result);
            uv_async_send(async);
        }
    });

    // Retain this object, otherwise it might get destructed before we are finished rendering the
    // still image.
    Ref();

    // Similarly, we're now waiting for the async to be called, so we need to make sure that it
    // keeps the loop alive.
    uv_ref(reinterpret_cast<uv_handle_t *>(async));
}
Example #3
0
static int unload_module(void) {
	unsigned int i = 0;

	LNOTICE("unloaded module %s", module_name);

	for (i = 0; i < profile_size; i++) {

		close_socket(i);
		free_profile(i);
	}
	
	                 
#if UV_VERSION_MAJOR == 0
	uv_async_send(&async_handle);
	uv_loop_delete(loop);
#else

	if (uv_loop_alive(loop)) {
        	uv_async_send(&async_handle);
	}
   
	uv_stop(loop);
	uv_loop_close(loop);
	free(loop);
#endif
	/* Close socket */
	return 0;
}
  void addEventData(myo::Myo* myo, uint64_t timestamp, std::string type, EventData *eventData)
  {
    std::map<myo::Myo*, std::string>::iterator iter = this->myo_ids.find(myo);
    if (iter == this->myo_ids.end()) {
      this->myo_ids[myo] = this->GUID();
      this->addEventData(myo, timestamp, "connect", new EventData());
    }

    EventHandle* eventHandle = this->getEventHandle(type);

    if (eventHandle == 0) {
      printf("no eventhandle for event %s\n", type);
      return;
    }

    std::string myo_id = this->myo_ids[myo];

    eventData->myo_id = myo_id;
    eventData->timestamp = timestamp;

    // uv_rwlock_wrlock(&eventHandle->lock);
    eventHandle->data.push(eventData);
    // uv_rwlock_wrunlock(&eventHandle->lock);

    uv_async_send(&eventHandle->async_handle);
  }
Example #5
0
	// shutdown shuts down the Node's event loop and cleans up resources.
	void
	shutdown()
	{
		uv_async_init(m_uv_loop.get(), &m_async, [](uv_async_t* handle) {
			auto self = (Node*)(handle->data);
			auto timer = self->m_timer.get();
			uv_timer_stop(timer);

			uv_close((uv_handle_t*)timer, [](uv_handle_t* handle) {
				auto self = (Node*)(handle->data);
				auto tcp = self->m_tcp.get();
				auto loop = self->m_uv_loop.get();

				self->m_peer_registry = nullptr;

				uv_close((uv_handle_t*)tcp, [](uv_handle_t* handle) {
					auto self = (Node*)(handle->data);
					auto loop = self->m_uv_loop.get();

					uv_walk(loop, [](uv_handle_t* handle, void* arg) {
						if (uv_is_closing(handle) == 0) {
							uv_close(handle, [](uv_handle_t* h){});
						}
					}, nullptr);
				});
			});
		});
		m_async.data = this;
		uv_async_send(&m_async);
	}
Example #6
0
/* Thread 3 calls uv_async_send on async_handle_2 8 times
 * after waiting half a second first.
 */
void thread3_entry(void *arg) {
  int i;

  for (i = 0; i < 8; i++) {
    uv_async_send(&async2_handle);
  }
}
Example #7
0
File: loop.c Project: nubjs/libnub
/* Should be run from spawned thread. */
int nub_loop_lock(nub_thread_t* thread) {
  fuq_queue_t* queue;
  uv_mutex_t* mutex;
  int er;
  int is_empty;

  ASSERT(NULL != thread);

  queue = &thread->nubloop->work_queue_;
  mutex = &thread->nubloop->work_lock_;

  if (NUB_LOOP_QUEUE_DISPOSE != thread->work.work_type)
    thread->work.work_type = NUB_LOOP_QUEUE_LOCK;

  uv_mutex_lock(mutex);
  is_empty = fuq_empty(queue);
  fuq_enqueue(queue, &thread->work);
  uv_mutex_unlock(mutex);

  if (is_empty)
    /* Send signal to event loop thread that work needs to be done. */
    er = uv_async_send(thread->async_signal_);
  else
    er = 0;

  /* Pause thread until uv_async_cb has run. */
  uv_sem_wait(&thread->thread_lock_sem_);

  return er;
}
Example #8
0
 void after_work(uv_work_t* work, int status) {
     ThreadPool* l = (ThreadPool*)((ThreadPool::work_data*)work->data)->tp_;
     delete (ThreadPool::work_data*)work->data;
     delete work;
     --(l->count_);
     uv_async_send(&l->async_);/*start thread_pool_async_cb*/
 }
Example #9
0
 Loop::~Loop() {
     stop_ = true;
     uv_async_send(&async_);
     thread_->join();
     UV_CHECK(uv_loop_close(loop_));
     free(loop_);
 }
Example #10
0
 void send(T &&data) {
     {
         std::lock_guard<std::mutex> lock(mutex);
         queue.push(std::make_unique<T>(std::move(data)));
     }
     uv_async_send(&async);
 }
Example #11
0
 void send(std::unique_ptr<T> data) {
     {
         std::lock_guard<std::mutex> lock(mutex);
         queue.push(std::move(data));
     }
     uv_async_send(&async);
 }
void HTTPRequestBaton::start(const util::ptr<HTTPRequestBaton> &baton) {
    assert(uv_thread_self() == baton->thread_id);

    std::string clean_url = util::percentDecode(baton->path);
    if (clean_url.find("local://") == 0) {
        clean_url = base_directory + clean_url.substr(8);
    }

    baton->response = std::make_unique<Response>();
    FILE *file = fopen(clean_url.c_str(),"rb");
    if (file != NULL) {
        fseek(file, 0, SEEK_END);
        const size_t size = ftell(file);
        fseek(file, 0, SEEK_SET);
        baton->response->data.resize(size);
        const size_t read = fread(&baton->response->data[0], 1, size, file);
        fclose(file);

        if (read == size) {
            baton->response->code = 200;
            baton->type = HTTPResponseType::Successful;
        } else {
            baton->response->code = 500;
            baton->type = HTTPResponseType::PermanentError;
            baton->response->message = "Read bytes differed from file size";
        }
    } else {
        baton->type = HTTPResponseType::PermanentError;
        baton->response->code = 404;
    }

    uv_async_send(baton->async);
}
Example #13
0
/* process individual kbd characters */
void readkbd(uv_stream_t* stream, ssize_t nread, const uv_buf_t *buf) {
    char* ch;
    int i;
    
    for (ch=buf->base, i=0; i<nread; i++) {
        if(nread < 0) {
            print("keyboard close (n=%d, %s)\n", nread, strerror(errno));
        }
        
        switch(*ch) {
        case '\r':
            *ch = '\n';
            break;
        case DELETE:
            *ch = '\b';
            break;
        }
        
        if(kbd.raw == 0){
            switch(*ch){
            case 0x15:
                write(1, "^U\n", 3);
                break;
            default:
                write(1, ch, 1);
                break;
            }
        }
        qproduce(kbdq, ch, 1);
        uv_async_send(&ev_conschar);
    }
    if (buf->base) {free(buf->base);}
}
bool
as_event_send_close_loop(as_event_loop* event_loop)
{
	// Send stop command through queue so it can be executed in event loop thread.
	pthread_mutex_lock(&event_loop->lock);
	as_uv_command qcmd = {.type = AS_UV_EXIT_LOOP, .ptr = 0};
	bool queued = as_queue_push(&event_loop->queue, &qcmd);
	pthread_mutex_unlock(&event_loop->lock);
	
	if (queued) {
		uv_async_send(event_loop->wakeup);
	}
	return queued;
}

void
as_event_close_loop(as_event_loop* event_loop)
{
	uv_close((uv_handle_t*)event_loop->wakeup, as_uv_wakeup_closed);
	
	// Only stop event loop if client created event loop.
	if (as_event_threads_created) {
		uv_stop(event_loop->loop);
	}
	
	// Cleanup event loop resources.
	as_queue_destroy(&event_loop->queue);
	as_queue_destroy(&event_loop->pipe_cb_queue);
	pthread_mutex_unlock(&event_loop->lock);
	pthread_mutex_destroy(&event_loop->lock);
}
Example #15
0
static int luv_queue_channel_send(lua_State* L)
{
	int type, ret;
	luv_msg_t* msg;
	luv_queue_t* queue = luv_queue_check_queue_t(L);
	if (lua_gettop(L) < 2) {
		luv_queue_lua_usage(L, queue_usage_send);
	}

	msg = (luv_msg_t*)malloc(sizeof(luv_msg_t));
	ret = luv_thread_arg_set(L, &msg->arg, 2, lua_gettop(L), 1);
	// printf("chan_send: %d\r\n", ret);

	ret = luv_queue_send(queue, msg, 0);
	if (!ret) {
		luv_queue_message_release(msg);

	} else {
		if (queue->async_cb != LUA_REFNIL) {
			uv_async_send(&(queue->async));
		}
	}

	lua_pushboolean(L, ret);
	return 1;
}
Example #16
0
Action ONSListenerV8::consume(Message& message, ConsumeContext& context)
{
    ONSConsumerACKInner* ack_inner = new ONSConsumerACKInner();
    MessageHandlerParam* param = new MessageHandlerParam();
    param->message = &message;

    if(consumer_env_v == "true")
    {
        printf(">>> ACK Inner Created: 0x%lX\n", (unsigned long)ack_inner);
        printf(">>> Message Handler Param Created: 0x%lX\n", (unsigned long)param);
    }

    param->ons = parent;
    param->ack_inner = ack_inner;
    async->data = (void*)param;
    uv_async_send(async);

    Action result = ack_inner->WaitResult();

    delete ack_inner;
    delete param;

    if(consumer_env_v == "true")
    {
        printf(">>> ACK Inner Deleted: 0x%lX\n", (unsigned long)ack_inner);
        printf(">>> Message Handler Param Deleted: 0x%lX\n", (unsigned long)param);
    }

    return result;
}
Example #17
0
void pc_client_destroy(pc_client_t *client) {
  if(PC_ST_INITED == client->state) {
    pc__client_clear(client);
    goto finally;
  }

  if(PC_ST_CLOSED == client->state) {
    pc__client_clear(client);
    goto finally;
  }

  // 1. asyn worker thread
  // 2. wait work thread exit
  // 3. free client
  uv_async_send(client->close_async);

  pc_client_join(client);

  if(PC_ST_CLOSED != client->state) {
    pc_client_stop(client);
    // wait uv_loop_t stop
    sleep(1);
    pc__client_clear(client);
  }

finally:
  if(client->uv_loop) {
    uv_loop_delete(client->uv_loop);
    client->uv_loop = NULL;
  }
  free(client);
}
unsigned VlcVideoOutput::video_format_cb( char* chroma,
                                          unsigned* width, unsigned* height,
                                          unsigned* pitches, unsigned* lines )
{
    std::unique_ptr<VideoEvent> frameSetupEvent;
    switch( _pixelFormat ) {
        case PixelFormat::RV32: {
            std::shared_ptr<RV32VideoFrame> videoFrame( new RV32VideoFrame() );
            frameSetupEvent.reset( new RV32FrameSetupEvent( videoFrame ) );
            _videoFrame = videoFrame;
            break;
        }
        case PixelFormat::I420:
        default: {
            std::shared_ptr<I420VideoFrame> videoFrame( new I420VideoFrame() );
            frameSetupEvent.reset( new I420FrameSetupEvent( videoFrame ) );
            _videoFrame = videoFrame;
            break;
        }
    }

    const unsigned planeCount = _videoFrame->video_format_cb( chroma,
                                                              width, height,
                                                              pitches, lines );

    _guard.lock();
    _videoEvents.push_back( std::move( frameSetupEvent ) );
    _guard.unlock();
    uv_async_send( &_async );

    _videoFrame->waitBuffer();

    return planeCount;
}
Example #19
0
// Schedule an event from another thread
void loop_schedule(Loop *loop, Event event)
{
  uv_mutex_lock(&loop->mutex);
  multiqueue_put_event(loop->thread_events, event);
  uv_async_send(&loop->async);
  uv_mutex_unlock(&loop->mutex);
}
Example #20
0
static void
signal_cb(uv_signal_t *handle, int signum) {
    if (signum == SIGINT || signum == SIGQUIT) {
        char *name = signum == SIGINT ? "SIGINT" : "SIGQUIT";
        logger_log(LOG_INFO, "Received %s, scheduling shutdown...", name);

        close_signal();

        if (concurrency > 1) {
            struct server_context *servers = handle->data;
            for (int i = 0; i < concurrency; i++) {
                struct server_context *server = &servers[i];
                uv_async_send(&server->async_handle);
            }

        } else {
            struct server_context *ctx = handle->data;
            uv_close((uv_handle_t *)&ctx->tcp, NULL);
            udprelay_close(ctx);
        }

    }
    if (signum == SIGTERM) {
        logger_log(LOG_INFO, "Received SIGTERM, scheduling shutdown...");
        if (daemon_mode) {
            delete_pidfile(pidfile);
        }
        exit(0);
    }
}
Example #21
0
static void embed_thread_runner(void* arg) {
  int r;
  int fd;
  int timeout;

  while (!embed_closed) {
    fd = uv_backend_fd(uv_default_loop());
    timeout = uv_backend_timeout(uv_default_loop());

    do {
#if defined(HAVE_KQUEUE)
      struct timespec ts;
      ts.tv_sec = timeout / 1000;
      ts.tv_nsec = (timeout % 1000) * 1000000;
      r = kevent(fd, NULL, 0, NULL, 0, &ts);
#elif defined(HAVE_EPOLL)
      {
        struct epoll_event ev;
        r = epoll_wait(fd, &ev, 1, timeout);
      }
#endif
    } while (r == -1 && errno == EINTR);
    uv_async_send(&embed_async);
    uv_sem_wait(&embed_sem);
  }
}
Example #22
0
//================================================================================================
//
//  DeviceRemoved
//
//  This routine will get called whenever any kIOGeneralInterest notification happens.  We are
//  interested in the kIOMessageServiceIsTerminated message so that's what we look for.  Other
//  messages are defined in IOMessage.h.
//
//================================================================================================
static void DeviceRemoved(void *refCon, io_service_t service, natural_t messageType, void *messageArgument) {
	kern_return_t kr;
	stDeviceListItem* deviceListItem = (stDeviceListItem *) refCon;
	DeviceItem_t* deviceItem = deviceListItem->deviceItem;

	if(messageType == kIOMessageServiceIsTerminated) {
		if(deviceListItem->deviceInterface) {
			kr = (*deviceListItem->deviceInterface)->Release(deviceListItem->deviceInterface);
		}

		kr = IOObjectRelease(deviceListItem->notification);


		ListResultItem_t* item = NULL;
		if(deviceItem) {
			item = CopyElement(&deviceItem->deviceParams);
			RemoveItemFromList(deviceItem);
			delete deviceItem;
		}
		else {
			item = new ListResultItem_t();
		}

		WaitForDeviceHandled();
		currentItem = item;
		isAdded = false;
		uv_async_send(&async_handler);
	}
}
Example #23
0
/**
 * disconnect will make the uv loop to exit
 */
void pc_client_disconnect(pc_client_t* client){
  assert(client);
  uv_mutex_lock(&client->state_mutex);
  client->state = PC_ST_DISCONNECTING;
  uv_mutex_unlock(&client->state_mutex);
  uv_async_send(client->close_async);
}
Example #24
0
void h2o_multithread_send_message(h2o_multithread_receiver_t *receiver, h2o_multithread_message_t *message)
{
    int do_send = 0;

    pthread_mutex_lock(&receiver->queue->mutex);
    if (message != NULL) {
        assert(!h2o_linklist_is_linked(&message->link));
        if (h2o_linklist_is_empty(&receiver->_messages)) {
            h2o_linklist_unlink(&receiver->_link);
            h2o_linklist_insert(&receiver->queue->receivers.active, &receiver->_link);
            do_send = 1;
        }
        h2o_linklist_insert(&receiver->_messages, &message->link);
    } else {
        if (h2o_linklist_is_empty(&receiver->_messages))
            do_send = 1;
    }
    pthread_mutex_unlock(&receiver->queue->mutex);

    if (do_send) {
#if H2O_USE_LIBUV
        uv_async_send(&receiver->queue->async);
#else
        while (write(receiver->queue->async.write, "", 1) == -1 && errno == EINTR)
            ;
#endif
    }
}
Example #25
0
int iotjs_tizen_bridge_native(const char* fn_name, unsigned fn_name_size,
                              const char* message, unsigned message_size,
                              user_callback_t cb) {
  iotjs_environment_t* env = iotjs_environment_get();

  if (env->state != kRunningMain && env->state != kRunningLoop) {
    return IOTJS_ERROR_RESULT_FAILED;
  }

  iotjs_call_jfunc_t* handle = IOTJS_ALLOC(iotjs_call_jfunc_t);

  if (handle == NULL) {
    return IOTJS_ERROR_OUT_OF_MEMORY;
  }

  handle->async.data = (void*)handle;
  handle->fn_name = create_string_buffer(fn_name, fn_name_size);
  handle->message = create_string_buffer(message, message_size);
  handle->module = create_string_buffer(IOTJS_MAGIC_STRING_TIZEN,
                                        sizeof(IOTJS_MAGIC_STRING_TIZEN));
  handle->cb = cb;

  uv_loop_t* loop = iotjs_environment_loop(env);
  uv_async_init(loop, &handle->async, bridge_native_async_handler);
  uv_async_send(&handle->async);

  return IOTJS_ERROR_NONE;
}
Example #26
0
void TCPClient::Close()
{
    if (isclosed_) {
        return;
    }
    isuseraskforclosed_ = true;
    uv_async_send(&async_handle_);
}
static void thread_cb(void* arg) {
  unsigned i;

  while (done == 0) {
    i = fastrand() % ARRAY_SIZE(container->async_handles);
    uv_async_send(container->async_handles + i);
  }
}
Example #28
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);
}
Example #29
0
static void uv_eio_done_poll(eio_channel *channel) {
  /*
   * Signal the main thread that we should stop calling eio_poll().
   * from the idle watcher.
   */
  uv_loop_t* loop = channel->data;
  uv_async_send(&loop->uv_eio_done_poll_notifier);
}
Example #30
0
// Request TLSConnection to close its connection.
void TLSConnectionPrivate::Disconnect() {
	unsigned long us = uv_thread_self();
	unsigned long it = thread_id_.load();
	if (us == it) {
		Shutdown(TLS_CONNECTION_STATE_DISCONNECTED_LOCAL);
	} else if (it > 0) {
		uv_async_send(&dcasync_);
	}
}