From ccda76bb7dba9d5775c7b38153f1aa1d28857a0e Mon Sep 17 00:00:00 2001 From: Chia-I Wu Date: Tue, 6 Oct 2020 11:04:01 -0700 Subject: [PATCH] vrend: move eventfd manipulations to virgl_util.c So that it can be shared by different renderers (there is only vrend right now). Signed-off-by: Chia-I Wu Reviewed-by: Gert Wollny --- src/virgl_util.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ src/virgl_util.h | 5 ++++ src/vrend_renderer.c | 42 ++------------------------------ 3 files changed, 64 insertions(+), 40 deletions(-) diff --git a/src/virgl_util.c b/src/virgl_util.c index bf8ce88..2f30be8 100644 --- a/src/virgl_util.c +++ b/src/virgl_util.c @@ -22,8 +22,18 @@ * **************************************************************************/ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include "virgl_util.h" +#include +#ifdef HAVE_EVENTFD_H +#include +#endif +#include + #include "util/u_pointer.h" unsigned hash_func_u32(void *key) @@ -41,3 +51,50 @@ int compare_func(void *key1, void *key2) else return 0; } + +bool has_eventfd(void) +{ +#ifdef HAVE_EVENTFD_H + return true; +#else + return false; +#endif +} + +int create_eventfd(unsigned int initval) +{ +#ifdef HAVE_EVENTFD_H + return eventfd(initval, EFD_CLOEXEC | EFD_NONBLOCK); +#else + return -1; +#endif +} + +int write_eventfd(int fd, uint64_t val) +{ + const char *buf = (const char *)&val; + size_t count = sizeof(val); + ssize_t ret = 0; + + while (count) { + ret = write(fd, buf, count); + if (ret < 0) { + if (errno == EINTR) + continue; + break; + } + count -= ret; + buf += ret; + } + + return count ? -1 : 0; +} + +void flush_eventfd(int fd) +{ + ssize_t len; + uint64_t value; + do { + len = read(fd, &value, sizeof(value)); + } while ((len == -1 && errno == EINTR) || len == sizeof(value)); +} diff --git a/src/virgl_util.h b/src/virgl_util.h index 84d6ef8..cdb5778 100644 --- a/src/virgl_util.h +++ b/src/virgl_util.h @@ -49,4 +49,9 @@ unsigned hash_func_u32(void *key); int compare_func(void *key1, void *key2); +bool has_eventfd(void); +int create_eventfd(unsigned int initval); +int write_eventfd(int fd, uint64_t val); +void flush_eventfd(int fd); + #endif /* VIRGL_UTIL_H */ diff --git a/src/vrend_renderer.c b/src/vrend_renderer.c index b8b1104..f104ba8 100644 --- a/src/vrend_renderer.c +++ b/src/vrend_renderer.c @@ -57,10 +57,6 @@ #include "tgsi/tgsi_text.h" -#ifdef HAVE_EVENTFD_H -#include -#endif - static const uint32_t fake_occlusion_query_samples_passed_default = 1024; const struct vrend_if_cbs *vrend_clicbs; @@ -5771,40 +5767,15 @@ static bool do_wait(struct vrend_fence *fence, bool can_block) } #ifdef HAVE_EVENTFD_H -static ssize_t -write_full(int fd, const void *ptr, size_t count) -{ - const char *buf = ptr; - ssize_t ret = 0; - ssize_t total = 0; - - while (count) { - ret = write(fd, buf, count); - if (ret < 0) { - if (errno == EINTR) - continue; - break; - } - count -= ret; - buf += ret; - total += ret; - } - return total; -} - static void wait_sync(struct vrend_fence *fence) { - ssize_t n; - uint64_t value = 1; - do_wait(fence, /* can_block */ true); pipe_mutex_lock(vrend_state.fence_mutex); list_addtail(&fence->fences, &vrend_state.fence_list); pipe_mutex_unlock(vrend_state.fence_mutex); - n = write_full(vrend_state.eventfd, &value, sizeof(value)); - if (n != sizeof(value)) { + if (write_eventfd(vrend_state.eventfd, 1)) { perror("failed to write to eventfd\n"); } } @@ -5860,7 +5831,7 @@ static void vrend_renderer_use_threaded_sync(void) return; } - vrend_state.eventfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); + vrend_state.eventfd = create_eventfd(0); if (vrend_state.eventfd == -1) { vrend_printf( "Failed to create eventfd\n"); vrend_clicbs->destroy_gl_context(vrend_state.sync_context); @@ -9034,15 +9005,6 @@ int vrend_renderer_create_fence(int client_fence_id, uint32_t ctx_id) return ENOMEM; } -static void flush_eventfd(int fd) -{ - ssize_t len; - uint64_t value; - do { - len = read(fd, &value, sizeof(value)); - } while ((len == -1 && errno == EINTR) || len == sizeof(value)); -} - static void vrend_renderer_check_queries(void); void vrend_renderer_check_fences(void)