{"action":"created","issue":{"url":"https://api.github.com/repos/libuv/libuv/issues/356","labels_url":"https://api.github.com/repos/libuv/libuv/issues/356/labels{/name}","comments_url":"https://api.github.com/repos/libuv/libuv/issues/356/comments","events_url":"https://api.github.com/repos/libuv/libuv/issues/356/events","html_url":"https://github.com/libuv/libuv/pull/356","id":77345831,"number":356,"title":"unix: Add uv_pending function","user":{"login":"szyusong","id":12484869,"avatar_url":"https://avatars.githubusercontent.com/u/12484869?v=3","gravatar_id":"","url":"https://api.github.com/users/szyusong","html_url":"https://github.com/szyusong","followers_url":"https://api.github.com/users/szyusong/followers","following_url":"https://api.github.com/users/szyusong/following{/other_user}","gists_url":"https://api.github.com/users/szyusong/gists{/gist_id}","starred_url":"https://api.github.com/users/szyusong/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/szyusong/subscriptions","organizations_url":"https://api.github.com/users/szyusong/orgs","repos_url":"https://api.github.com/users/szyusong/repos","events_url":"https://api.github.com/users/szyusong/events{/privacy}","received_events_url":"https://api.github.com/users/szyusong/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2015-05-17T16:40:07Z","updated_at":"2015-05-19T10:36:24Z","closed_at":"2015-05-19T09:33:30Z","pull_request":{"url":"https://api.github.com/repos/libuv/libuv/pulls/356","html_url":"https://github.com/libuv/libuv/pull/356","diff_url":"https://github.com/libuv/libuv/pull/356.diff","patch_url":"https://github.com/libuv/libuv/pull/356.patch"},"body":"It need the loop pending_queue state for integrate with glib, so add this function to export the loop pending_queue state."},"comment":{"url":"https://api.github.com/repos/libuv/libuv/issues/comments/103432303","html_url":"https://github.com/libuv/libuv/pull/356#issuecomment-103432303","issue_url":"https://api.github.com/repos/libuv/libuv/issues/356","id":103432303,"user":{"login":"szyusong","id":12484869,"avatar_url":"https://avatars.githubusercontent.com/u/12484869?v=3","gravatar_id":"","url":"https://api.github.com/users/szyusong","html_url":"https://github.com/szyusong","followers_url":"https://api.github.com/users/szyusong/followers","following_url":"https://api.github.com/users/szyusong/following{/other_user}","gists_url":"https://api.github.com/users/szyusong/gists{/gist_id}","starred_url":"https://api.github.com/users/szyusong/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/szyusong/subscriptions","organizations_url":"https://api.github.com/users/szyusong/orgs","repos_url":"https://api.github.com/users/szyusong/repos","events_url":"https://api.github.com/users/szyusong/events{/privacy}","received_events_url":"https://api.github.com/users/szyusong/received_events","type":"User","site_admin":false},"created_at":"2015-05-19T10:36:24Z","updated_at":"2015-05-19T10:36:24Z","body":"The following test code is just a echo server using TCP. \r\n\r\n```C\r\n#include <stdlib.h>\r\n#include <stdio.h>\r\n#include <string.h>\r\n#include <netinet/in.h>  /* sockaddr_in, sockaddr_in6 */\r\n#include <stddef.h>      /* size_t, ssize_t */\r\n#include <stdint.h>\r\n#include <sys/socket.h>  /* sockaddr */\r\n\r\n#include <sys/types.h>\r\n#include <sys/socket.h>\r\n#include <netinet/in.h>\r\n#include <arpa/inet.h>\r\n#include <netdb.h>\r\n#include <unistd.h>\r\n\r\n#include <glib.h>\r\n#include <glibuv.h>\r\n\r\nstatic GMainLoop *g_loop;\r\nstatic uv_loop_t *uv_loop;\r\n\r\n//==============================================================================\r\n\r\nvoid alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t* buf)\r\n{\r\n  static char buffer[1024];\r\n\r\n  buf->base = buffer;\r\n  buf->len = sizeof(buffer);\r\n}\r\n\r\nvoid echo_read(uv_stream_t *stream, ssize_t nread, const uv_buf_t* buf)\r\n{\r\n  uv_write_t write_req;\r\n  uv_buf_t buf_req;\r\n  \r\n  if (nread < 0)\r\n  {\r\n    uv_close((uv_handle_t*)stream, NULL);\r\n    return;\r\n  }\r\n  //printf(buf->base);\r\n  buf_req = uv_buf_init(buf->base, nread);\r\n  uv_write(&write_req, stream, buf, 1, NULL);\r\n}\r\n\r\nvoid on_new_connection(uv_stream_t *server, int status)\r\n{\r\n  static int count = 0;\r\n\r\n  if (status == -1)\r\n  {\r\n    // error!\r\n    return;\r\n  }\r\n\r\n  uv_tcp_t *client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));\r\n\r\n  printf(\"connect: %d\\n\", ++count);\r\n\r\n  uv_tcp_init(uv_loop, client);\r\n  if (uv_accept(server, (uv_stream_t*) client) == 0)\r\n  {\r\n    uv_read_start((uv_stream_t*) client, alloc_buffer, echo_read);\r\n  }\r\n  else\r\n  {\r\n    uv_close((uv_handle_t*) client, NULL);\r\n  }\r\n}\r\n\r\nvoid on_timeout(uv_timer_t *timer)\r\n{\r\n  static int count = 0;\r\n  \r\n  fprintf(stderr, \".\");\r\n  fflush(stderr);\r\n\r\n  ++count;\r\n  char buf[64];\r\n  snprintf(buf, sizeof(buf), \"%d\", count);\r\n}\r\n\r\nint main()\r\n{\r\n  GSource *source;\r\n  struct sockaddr_in bind_addr;\r\n  int r;\r\n  uv_timer_t timer;\r\n  uv_tcp_t server;\r\n\r\n  g_type_init();\r\n\r\n  g_loop = g_main_loop_new(NULL, FALSE);\r\n\r\n  uv_loop = uv_default_loop();\r\n\r\n  //g_assert(! uv_timer_init(uv_loop, &timer));\r\n  //g_assert(! uv_timer_start(&timer, on_timeout, 1000, 1000));\r\n\r\n  uv_tcp_init(uv_loop, &server);\r\n\r\n  uv_ip4_addr(\"0.0.0.0\", 7003, &bind_addr);\r\n  uv_tcp_bind(&server, (struct sockaddr*)&bind_addr, 0);\r\n  r = uv_listen((uv_stream_t*) &server, 128, on_new_connection);\r\n  if (r)\r\n  {\r\n    fprintf(stderr, \"Listen error %s\\n\", uv_err_name(r));\r\n    return 1;\r\n  }\r\n\r\n  g_source_attach(g_uv_source_new(uv_loop), NULL);\r\n\r\n  g_main_loop_run(g_loop);\r\n\r\n  //return uv_run(uv_loop, UV_RUN_DEFAULT);\r\n\r\n  return 0;\r\n}\r\n```\r\n\r\nIf uv_source_dispatch is as following:\r\n```C\r\nstatic gboolean\r\nuv_source_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)\r\n{\r\n  uv_source_t *uv_source = (uv_source_t*)source;\r\n\r\n  uv_run(uv_source->loop, UV_RUN_ONCE);\r\n#if 0\r\n  do\r\n  {\r\n    uv_run(uv_source->loop, UV_RUN_NOWAIT);\r\n  }\r\n  while(uv_pending(uv_source->loop));\r\n#endif\r\n  return TRUE;\r\n}\r\n```\r\nand do not uncomment the following\r\n```C\r\n  //g_assert(! uv_timer_init(uv_loop, &timer));\r\n  //g_assert(! uv_timer_start(&timer, on_timeout, 1000, 1000));\r\n```\r\nthe echo server dose not work as expect, the echo_read will never be invoked.\r\n"}}