Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
add uv_run_once()
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn authored and bnoordhuis committed Dec 14, 2011
1 parent 4c6008f commit e53cecb
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
5 changes: 5 additions & 0 deletions include/uv.h
Expand Up @@ -212,6 +212,11 @@ UV_EXTERN uv_loop_t* uv_default_loop(void);
*/
UV_EXTERN int uv_run (uv_loop_t*);

/*
* This function polls for new events without blocking.
*/
UV_EXTERN int uv_run_once (uv_loop_t*);

/*
* Manually modify the event loop's reference count. Useful if the user wants
* to have a handle or timeout that doesn't keep the loop alive.
Expand Down
6 changes: 6 additions & 0 deletions src/unix/core.c
Expand Up @@ -202,6 +202,12 @@ int uv_run(uv_loop_t* loop) {
}


int uv_run_once(uv_loop_t* loop) {
ev_run(loop->ev, EVRUN_NOWAIT);
return 0;
}


void uv__handle_init(uv_loop_t* loop, uv_handle_t* handle,
uv_handle_type type) {
loop->counters.handle_init++;
Expand Down
20 changes: 17 additions & 3 deletions src/win/core.c
Expand Up @@ -192,9 +192,8 @@ static void uv_poll_ex(uv_loop_t* loop, int block) {
}
}


#define UV_LOOP(loop, poll) \
while ((loop)->refs > 0) { \
#define UV_LOOP_ONCE(loop, poll) \
do { \
uv_update_time((loop)); \
uv_process_timers((loop)); \
\
Expand All @@ -219,9 +218,24 @@ static void uv_poll_ex(uv_loop_t* loop, int block) {
(loop)->refs > 0); \
\
uv_check_invoke((loop)); \
} while (0);

#define UV_LOOP(loop, poll) \
while ((loop)->refs > 0) { \
UV_LOOP_ONCE(loop, poll) \
}


int uv_run_once(uv_loop_t* loop) {
if (pGetQueuedCompletionStatusEx) {
UV_LOOP_ONCE(loop, uv_poll_ex);
} else {
UV_LOOP_ONCE(loop, uv_poll);
}
return 0;
}


int uv_run(uv_loop_t* loop) {
if (pGetQueuedCompletionStatusEx) {
UV_LOOP(loop, uv_poll_ex);
Expand Down
44 changes: 44 additions & 0 deletions test/test-run-once.c
@@ -0,0 +1,44 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#include "uv.h"
#include "task.h"

static idle_counter = 0;

static void idle_cb(uv_idle_t* handle, int status) {
ASSERT(handle != NULL);
ASSERT(status == 0);
idle_counter ++;
}


TEST_IMPL(run_once) {
int n;
uv_idle_t h;
uv_idle_init(uv_default_loop(), &h);
uv_idle_start(&h, idle_cb);
for (n = 0; n < 500; n++) {
uv_run_once(uv_default_loop());
}
ASSERT(n == 500);
return 0;
}

0 comments on commit e53cecb

Please sign in to comment.