From 49d11f0d1fae43ae1e5f61204ed051e889062767 Mon Sep 17 00:00:00 2001 From: NRK Date: Wed, 22 Jun 2022 09:28:07 +0600 Subject: spawn(): improve performance and simplify API posix_spawn() is designed especially for this purpose, and thus it's much more lightweight and efficient than manually fork/dup/exec-ing. on my system, it improves the performance of spawn() by about 10x. given that we make frequent calls to potentially multiple scripts, the increased efficiency will add up overtime. using posix_spawn() also simplifies the logic quite a bit, despite the very verbose function names. however it does make cleanup a bit more complicated. this patch uses the linux kernel style cleanup strategy [0] (which I'm personally not a huge fan of, but it fits this situation quite nicely) with a "stack-like" unwinding via `goto`-s. additionally simplify the spawn() API by taking in {read,write}fd pointers and returning the pid instead of using some custom struct. this coincidently also fixes #299 [0]: https://www.kernel.org/doc/html/v4.10/process/coding-style.html?highlight=goto#centralized-exiting-of-functions --- nsxiv.h | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) (limited to 'nsxiv.h') diff --git a/nsxiv.h b/nsxiv.h index 33a2bde..19e5399 100644 --- a/nsxiv.h +++ b/nsxiv.h @@ -326,17 +326,6 @@ typedef struct { int stlen; } r_dir_t; -typedef struct { - int readfd; - int writefd; - pid_t pid; -} spawn_t; - -enum { - X_READ = (1 << 0), - X_WRITE = (1 << 1) -}; - extern const char *progname; void* emalloc(size_t); @@ -349,7 +338,7 @@ int r_closedir(r_dir_t*); char* r_readdir(r_dir_t*, bool); int r_mkdir(char*); void construct_argv(char**, unsigned int, ...); -spawn_t spawn(const char*, char *const [], unsigned int); +pid_t spawn(int*, int*, char *const []); /* window.c */ -- cgit v1.2.3-54-g00ecf