diff --git a/devicemodel/core/main.c b/devicemodel/core/main.c index 277c07dab..c00d606fd 100644 --- a/devicemodel/core/main.c +++ b/devicemodel/core/main.c @@ -770,6 +770,13 @@ main(int argc, char *argv[]) goto fail; } + err = mevent_init(); + if (err) { + fprintf(stderr, "Unable to initialize mevent (%d)\n", + errno); + goto mevent_fail; + } + init_mem(); init_inout(); pci_irq_init(ctx); @@ -848,6 +855,7 @@ main(int argc, char *argv[]) vrtc_deinit(ctx); atkbdc_deinit(ctx); vm_unsetup_memory(ctx); + mevent_deinit(); vm_destroy(ctx); vm_close(ctx); _ctx = 0; @@ -861,6 +869,7 @@ pci_fail: monitor_close(); vrtc_deinit(ctx); atkbdc_deinit(ctx); +mevent_fail: vm_unsetup_memory(ctx); fail: vm_destroy(ctx); diff --git a/devicemodel/core/mevent.c b/devicemodel/core/mevent.c index d4db826b6..382ce4c2d 100644 --- a/devicemodel/core/mevent.c +++ b/devicemodel/core/mevent.c @@ -58,6 +58,7 @@ #define MEV_DISABLE 3 #define MEV_DEL_PENDING 4 +static int epoll_fd; static pthread_t mevent_tid; static int mevent_pipefd[2]; static pthread_mutex_t mevent_lmutex = PTHREAD_MUTEX_INITIALIZER; @@ -341,6 +342,25 @@ mevent_set_name(void) pthread_setname_np(mevent_tid, "mevent"); } +int +mevent_init(void) +{ + epoll_fd = epoll_create1(0); + assert(epoll_fd >= 0); + + if (epoll_fd >= 0) + return 0; + else + return -1; +} + +void +mevent_deinit(void) +{ + mevent_destroy(); + close(epoll_fd); +} + void mevent_dispatch(void) { @@ -348,16 +368,12 @@ mevent_dispatch(void) struct epoll_event eventlist[MEVENT_MAX]; struct mevent *pipev; - int mfd; int numev; int ret; mevent_tid = pthread_self(); mevent_set_name(); - mfd = epoll_create1(0); - assert(mfd > 0); - /* * Open the pipe that will be used for other threads to force * the blocking kqueue call to exit by writing to it. Set the @@ -385,11 +401,11 @@ mevent_dispatch(void) int i; struct epoll_event *e; - numev = mevent_build(mfd, clist); + numev = mevent_build(epoll_fd, clist); for (i = 0; i < numev; i++) { e = &clist[i].ee; - ret = epoll_ctl(mfd, clist[i].op, clist[i].fd, e); + ret = epoll_ctl(epoll_fd, clist[i].op, clist[i].fd, e); if (ret == -1) perror("Error return from epoll_ctl"); } @@ -397,7 +413,7 @@ mevent_dispatch(void) /* * Block awaiting events */ - ret = epoll_wait(mfd, eventlist, MEVENT_MAX, -1); + ret = epoll_wait(epoll_fd, eventlist, MEVENT_MAX, -1); if (ret == -1 && errno != EINTR) perror("Error return from epoll_wait"); @@ -409,7 +425,5 @@ mevent_dispatch(void) if (vm_get_suspend_mode() != VM_SUSPEND_NONE) break; } - mevent_build(mfd, clist); - mevent_destroy(); - close(mfd); + mevent_build(epoll_fd, clist); } diff --git a/devicemodel/include/mevent.h b/devicemodel/include/mevent.h index e578e7f4a..924ec464e 100644 --- a/devicemodel/include/mevent.h +++ b/devicemodel/include/mevent.h @@ -49,6 +49,8 @@ int mevent_delete_close(struct mevent *evp); int mevent_notify(void); void mevent_dispatch(void); +int mevent_init(void); +void mevent_deinit(void); #define list_foreach_safe(var, head, field, tvar) \ for ((var) = LIST_FIRST((head)); \