Skip to content

Instantly share code, notes, and snippets.

@enginespot
Created April 3, 2014 11:25
Show Gist options
  • Save enginespot/9952669 to your computer and use it in GitHub Desktop.
Save enginespot/9952669 to your computer and use it in GitHub Desktop.
// Compiling from toplevel of built node.js repo on OSX:
// CC -g -o issue5564 issue5564.cc -I deps/uv/include/ \
// -L out/Debug/ -luv -framework CoreFoundation -framework Carbon
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include "uv.h"
uint64_t usec;
#define L(x) do { struct timeval __tv; gettimeofday(&__tv, 0); \
uint64_t __usec = (1000000 * __tv.tv_sec) + __tv.tv_usec - usec; \
printf("%03llu.%03llu %s\n", __usec / 1000000, (__usec % 1000000) / 1000, x); \
fflush(stdout); } while(0)
static uv_loop_t* loop_;
static uv_async_t async_;
static uv_timer_t timer_;
static uv_thread_t thread_;
static uv_cond_t cond_;
static uv_mutex_t mutex_;
void async_cb(uv_async_t* async, int status) {
L("async_cb fired");
}
void timer_cb(uv_timer_t* timer, int status) {
L("timer_cb fired, cond pre-signal");
uv_cond_signal(&cond_);
}
void run_cb(void* arg) {
L("run_cb pre-UV_RUN_ONCE 1");
uv_run(loop_, UV_RUN_ONCE);
L("run_cb post-UV_RUN_ONCE 1");
L("run_cb pre-UV_RUN_ONCE 2");
uv_run(loop_, UV_RUN_ONCE);
L("run_cb post-UV_RUN_ONCE 2");
uv_close(reinterpret_cast<uv_handle_t*>(&async_), NULL);
uv_close(reinterpret_cast<uv_handle_t*>(&timer_), NULL);
L("run_cb pre-UV_RUN_DEFAULT");
uv_run(loop_, UV_RUN_DEFAULT);
L("run_cb post-UV_RUN_DEFAULT");
}
int main(int argc, char** argv) {
struct timeval start;
gettimeofday(&start, 0);
usec = (1000000 * start.tv_sec) + start.tv_usec;
if (3 != argc) {
fprintf(stderr, "issue5564 <timer_timeout> <code_delay>\n");
exit(1);
}
uint64_t timeout = strtoull(argv[1], NULL, 10);
uint64_t delay = 1000000 * strtoull(argv[2], NULL, 10);
uv_cond_init(&cond_);
uv_mutex_init(&mutex_);
uv_mutex_lock(&mutex_);
loop_ = uv_loop_new();
uv_async_init(loop_, &async_, &async_cb);
uv_timer_init(loop_, &timer_);
uv_timer_start(&timer_, &timer_cb, timeout, 0);
L("main thread pre-create");
uv_thread_create(&thread_, &run_cb, NULL);
L("main thread post-create");
L("main cond pre-timedwait");
uv_cond_timedwait(&cond_, &mutex_, delay);
L("main cond post-timedwait");
L("main async pre-send");
uv_async_send(&async_);
L("main async post-send");
L("main thread pre-join");
uv_thread_join(&thread_);
L("main thread post-join");
uv_loop_delete(loop_);
L("hello world");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment