Skip to content

Commit

Permalink
Increase the robustness of hloop_free and hio_free
Browse files Browse the repository at this point in the history
  • Loading branch information
ithewei committed Apr 12, 2024
1 parent 21a06e7 commit a4e51af
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
3 changes: 2 additions & 1 deletion event/hevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ void hio_done(hio_t* io) {
}

void hio_free(hio_t* io) {
if (io == NULL) return;
if (io == NULL || io->destroy) return;
io->destroy = 1;
hio_close(io);
hrecursive_mutex_destroy(&io->write_mutex);
HV_FREE(io->localaddr);
Expand Down
21 changes: 14 additions & 7 deletions event/hloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,15 +417,19 @@ hloop_t* hloop_new(int flags) {
HV_ALLOC_SIZEOF(loop);
hloop_init(loop);
loop->flags |= flags;
hlogd("hloop_new tid=%ld", loop->tid);
return loop;
}

void hloop_free(hloop_t** pp) {
if (pp && *pp) {
hloop_cleanup(*pp);
HV_FREE(*pp);
*pp = NULL;
}
if (pp == NULL || *pp == NULL) return;
hloop_t* loop = *pp;
if (loop->status == HLOOP_STATUS_DESTROY) return;
loop->status = HLOOP_STATUS_DESTROY;
hlogd("hloop_free tid=%ld", hv_gettid());
hloop_cleanup(loop);
HV_FREE(loop);
*pp = NULL;
}

// while (loop->status) { hloop_process_events(loop); }
Expand All @@ -436,6 +440,7 @@ int hloop_run(hloop_t* loop) {
loop->status = HLOOP_STATUS_RUNNING;
loop->pid = hv_getpid();
loop->tid = hv_gettid();
hlogd("hloop_run tid=%ld", loop->tid);

if (loop->intern_nevents == 0) {
hmutex_lock(&loop->custom_events_mutex);
Expand Down Expand Up @@ -471,8 +476,7 @@ int hloop_run(hloop_t* loop) {
loop->end_hrtime = gethrtime_us();

if (loop->flags & HLOOP_FLAG_AUTO_FREE) {
hloop_cleanup(loop);
HV_FREE(loop);
hloop_free(&loop);
}
return 0;
}
Expand All @@ -485,6 +489,9 @@ int hloop_wakeup(hloop_t* loop) {
}

int hloop_stop(hloop_t* loop) {
if (loop == NULL) return -1;
if (loop->status == HLOOP_STATUS_STOP) return -2;
hlogd("hloop_stop tid=%ld", hv_gettid());
if (hv_gettid() != loop->tid) {
hloop_wakeup(loop);
}
Expand Down
3 changes: 2 additions & 1 deletion event/hloop.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ typedef void (*hclose_cb) (hio_t* io);
typedef enum {
HLOOP_STATUS_STOP,
HLOOP_STATUS_RUNNING,
HLOOP_STATUS_PAUSE
HLOOP_STATUS_PAUSE,
HLOOP_STATUS_DESTROY
} hloop_status_e;

typedef enum {
Expand Down
4 changes: 2 additions & 2 deletions event/nio.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ int hio_write (hio_t* io, const void* buf, size_t len) {

int hio_close (hio_t* io) {
if (io->closed) return 0;
if (hv_gettid() != io->loop->tid) {
if (io->destroy == 0 && hv_gettid() != io->loop->tid) {
return hio_close_async(io);
}

Expand All @@ -575,7 +575,7 @@ int hio_close (hio_t* io) {
hrecursive_mutex_unlock(&io->write_mutex);
return 0;
}
if (!write_queue_empty(&io->write_queue) && io->error == 0 && io->close == 0) {
if (!write_queue_empty(&io->write_queue) && io->error == 0 && io->close == 0 && io->destroy == 0) {
io->close = 1;
hrecursive_mutex_unlock(&io->write_mutex);
hlogw("write_queue not empty, close later.");
Expand Down

0 comments on commit a4e51af

Please sign in to comment.