[njs] Allowing to create repeatable events.

Dmitry Volyntsev xeioex at nginx.com
Tue Sep 11 12:36:07 UTC 2018


details:   http://hg.nginx.org/njs/rev/39c741a9994c
branches:  
changeset: 599:39c741a9994c
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Sep 11 15:34:25 2018 +0300
description:
Allowing to create repeatable events.

njs_vm_add_event() prototype is extended to allow creating
oneshot vs repeatable events.

diffstat:

 nginx/ngx_http_js_module.c |   2 +-
 njs/njs.c                  |  11 +++++++++--
 njs/njs.h                  |   2 +-
 njs/njs_event.h            |   1 +
 njs/njs_time.c             |   1 +
 5 files changed, 13 insertions(+), 4 deletions(-)

diffs (81 lines):

diff -r 1a02c6a3bdd9 -r 39c741a9994c nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Mon Sep 03 14:29:52 2018 +0300
+++ b/nginx/ngx_http_js_module.c	Tue Sep 11 15:34:25 2018 +0300
@@ -2074,7 +2074,7 @@ ngx_http_js_subrequest(ngx_http_request_
             return NJS_ERROR;
         }
 
-        vm_event = njs_vm_add_event(ctx->vm, callback, NULL, NULL);
+        vm_event = njs_vm_add_event(ctx->vm, callback, 1, NULL, NULL);
         if (vm_event == NULL) {
             njs_vm_error(ctx->vm, "internal error");
             return NJS_ERROR;
diff -r 1a02c6a3bdd9 -r 39c741a9994c njs/njs.c
--- a/njs/njs.c	Mon Sep 03 14:29:52 2018 +0300
+++ b/njs/njs.c	Tue Sep 11 15:34:25 2018 +0300
@@ -486,7 +486,7 @@ njs_vm_call(njs_vm_t *vm, njs_function_t
 
 
 njs_vm_event_t
-njs_vm_add_event(njs_vm_t *vm, njs_function_t *function,
+njs_vm_add_event(njs_vm_t *vm, njs_function_t *function, nxt_uint_t once,
     njs_host_event_t host_ev, njs_event_destructor destructor)
 {
     njs_event_t  *event;
@@ -499,6 +499,7 @@ njs_vm_add_event(njs_vm_t *vm, njs_funct
     event->host_event = host_ev;
     event->destructor = destructor;
     event->function = function;
+    event->once = once;
     event->posted = 0;
     event->nargs = 0;
     event->args = NULL;
@@ -633,7 +634,13 @@ njs_vm_handle_events(njs_vm_t *vm)
 
         ev = nxt_queue_link_data(link, njs_event_t, link);
 
-        njs_del_event(vm, ev, NJS_EVENT_DELETE);
+        if (ev->once) {
+            njs_del_event(vm, ev, NJS_EVENT_DELETE);
+
+        } else {
+            ev->posted = 0;
+            nxt_queue_remove(&ev->link);
+        }
 
         ret = njs_vm_call(vm, ev->function, ev->args, ev->nargs);
 
diff -r 1a02c6a3bdd9 -r 39c741a9994c njs/njs.h
--- a/njs/njs.h	Mon Sep 03 14:29:52 2018 +0300
+++ b/njs/njs.h	Tue Sep 11 15:34:25 2018 +0300
@@ -164,7 +164,7 @@ NXT_EXPORT nxt_int_t njs_vm_call(njs_vm_
     const njs_value_t *args, nxt_uint_t nargs);
 
 NXT_EXPORT njs_vm_event_t njs_vm_add_event(njs_vm_t *vm,
-    njs_function_t *function, njs_host_event_t host_ev,
+    njs_function_t *function, nxt_uint_t once, njs_host_event_t host_ev,
     njs_event_destructor destructor);
 NXT_EXPORT void njs_vm_del_event(njs_vm_t *vm, njs_vm_event_t vm_event);
 NXT_EXPORT nxt_int_t njs_vm_pending(njs_vm_t *vm);
diff -r 1a02c6a3bdd9 -r 39c741a9994c njs/njs_event.h
--- a/njs/njs_event.h	Mon Sep 03 14:29:52 2018 +0300
+++ b/njs/njs_event.h	Tue Sep 11 15:34:25 2018 +0300
@@ -26,6 +26,7 @@ typedef struct {
     nxt_queue_link_t      link;
 
     unsigned              posted:1;
+    unsigned              once:1;
 } njs_event_t;
 
 
diff -r 1a02c6a3bdd9 -r 39c741a9994c njs/njs_time.c
--- a/njs/njs_time.c	Mon Sep 03 14:29:52 2018 +0300
+++ b/njs/njs_time.c	Tue Sep 11 15:34:25 2018 +0300
@@ -48,6 +48,7 @@ njs_set_timeout(njs_vm_t *vm, njs_value_
     event->destructor = ops->clear_timer;
     event->function = args[1].data.u.function;
     event->nargs = (nargs >= 3) ? nargs - 3 : 0;
+    event->once = 1;
     event->posted = 0;
 
     if (event->nargs != 0) {


More information about the nginx-devel mailing list