ithewei 5 years ago
parent
commit
e13e3d02f7
4 changed files with 32 additions and 24 deletions
  1. 2 2
      event/epoll.c
  2. 2 7
      event/hevent.h
  3. 21 15
      event/hloop.c
  4. 7 0
      event/hloop.h

+ 2 - 2
event/epoll.c

@@ -43,7 +43,7 @@ int iowatcher_add_event(hloop_t* loop, int fd, int events) {
     hio_t* io = loop->ios.ptr[fd];
 
     struct epoll_event ee;
-    ee.events = 0;
+    memset(&ee, 0, sizeof(ee));
     ee.data.fd = fd;
     // pre events
     if (io->events & HV_READ) {
@@ -76,7 +76,7 @@ int iowatcher_del_event(hloop_t* loop, int fd, int events) {
     hio_t* io = loop->ios.ptr[fd];
 
     struct epoll_event ee;
-    ee.events = 0;
+    memset(&ee, 0, sizeof(ee));
     ee.data.fd = fd;
     // pre events
     if (io->events & HV_READ) {

+ 2 - 7
event/hevent.h

@@ -3,6 +3,7 @@
 
 #include "hloop.h"
 #include "hbuf.h"
+#include "hatomic.h"
 #include "hmutex.h"
 
 #include "array.h"
@@ -12,12 +13,6 @@
 
 #define HLOOP_READ_BUFSIZE  8192
 
-typedef enum {
-    HLOOP_STATUS_STOP,
-    HLOOP_STATUS_RUNNING,
-    HLOOP_STATUS_PAUSE
-} hloop_status_e;
-
 ARRAY_DECL(hio_t*, io_array);
 QUEUE_DECL(hevent_t, event_queue);
 
@@ -34,7 +29,7 @@ struct hloop_s {
     void*       userdata;
 //private:
     // events
-    uint64_t                    event_counter;
+    hatomic_t                   event_counter;
     uint32_t                    nactives;
     uint32_t                    npendings;
     // pendings: with priority as array.index

+ 21 - 15
event/hloop.c

@@ -212,11 +212,11 @@ void hloop_post_event(hloop_t* loop, hevent_t* ev) {
     if (ev->event_type == 0) {
         ev->event_type = HEVENT_TYPE_CUSTOM;
     }
-
-    hmutex_lock(&loop->custom_events_mutex);
     if (ev->event_id == 0) {
         ev->event_id = ++loop->event_counter;
     }
+
+    hmutex_lock(&loop->custom_events_mutex);
     hwrite(loop, loop->sockpair[SOCKPAIR_WRITE_INDEX], &buf, 1, NULL);
     event_queue_push_back(&loop->custom_events, ev);
     hmutex_unlock(&loop->custom_events_mutex);
@@ -224,6 +224,8 @@ void hloop_post_event(hloop_t* loop, hevent_t* ev) {
 
 static void hloop_init(hloop_t* loop) {
     loop->status = HLOOP_STATUS_STOP;
+    loop->pid = hv_getpid();
+    loop->tid = hv_gettid();
 
     // idles
     list_init(&loop->idles);
@@ -408,6 +410,10 @@ int hloop_resume(hloop_t* loop) {
     return 0;
 }
 
+hloop_status_e hloop_status(hloop_t* loop) {
+    return loop->status;
+}
+
 void hloop_update_time(hloop_t* loop) {
     loop->cur_hrtime = gethrtime_us();
     if (ABS((int64_t)hloop_now(loop) - (int64_t)time(NULL)) > 1) {
@@ -640,9 +646,9 @@ void hio_ready(hio_t* io) {
     // callbacks
     io->read_cb = NULL;
     io->write_cb = NULL;
-    io->close_cb = 0;
-    io->accept_cb = 0;
-    io->connect_cb = 0;
+    io->close_cb = NULL;
+    io->accept_cb = NULL;
+    io->connect_cb = NULL;
     // timers
     io->connect_timeout = 0;
     io->connect_timer = NULL;
@@ -718,7 +724,7 @@ int hio_add(hio_t* io, hio_cb cb, int events) {
     printd("hio_add fd=%d events=%d\n", io->fd, events);
 #ifdef OS_WIN
     // Windows iowatcher not work on stdio
-    if (io->fd < 3) return 0;
+    if (io->fd < 3) return -1;
 #endif
     hloop_t* loop = io->loop;
     if (!io->active) {
@@ -743,16 +749,16 @@ int hio_del(hio_t* io, int events) {
     printd("hio_del fd=%d io->events=%d events=%d\n", io->fd, io->events, events);
 #ifdef OS_WIN
     // Windows iowatcher not work on stdio
-    if (io->fd < 3) return 0;
+    if (io->fd < 3) return -1;
 #endif
-    if (io->active) {
-        iowatcher_del_event(io->loop, io->fd, events);
-        io->events &= ~events;
-        if (io->events == 0) {
-            io->loop->nios--;
-            // NOTE: not EVENT_DEL, avoid free
-            EVENT_INACTIVE(io);
-        }
+    if (!io->active) return -1;
+
+    iowatcher_del_event(io->loop, io->fd, events);
+    io->events &= ~events;
+    if (io->events == 0) {
+        io->loop->nios--;
+        // NOTE: not EVENT_DEL, avoid free
+        EVENT_INACTIVE(io);
     }
     return 0;
 }

+ 7 - 0
event/hloop.h

@@ -27,6 +27,12 @@ typedef void (*hread_cb)    (hio_t* io, void* buf, int readbytes);
 typedef void (*hwrite_cb)   (hio_t* io, const void* buf, int writebytes);
 typedef void (*hclose_cb)   (hio_t* io);
 
+typedef enum {
+    HLOOP_STATUS_STOP,
+    HLOOP_STATUS_RUNNING,
+    HLOOP_STATUS_PAUSE
+} hloop_status_e;
+
 typedef enum {
     HEVENT_TYPE_NONE    = 0,
     HEVENT_TYPE_IO      = 0x00000001,
@@ -115,6 +121,7 @@ HV_EXPORT int hloop_stop(hloop_t* loop);
 HV_EXPORT int hloop_pause(hloop_t* loop);
 HV_EXPORT int hloop_resume(hloop_t* loop);
 HV_EXPORT int hloop_wakeup(hloop_t* loop);
+HV_EXPORT hloop_status_e hloop_status(hloop_t* loop);
 
 HV_EXPORT void     hloop_update_time(hloop_t* loop);
 HV_EXPORT uint64_t hloop_now(hloop_t* loop);          // s