Avoid busy-wait by sleep 1ms on non-blocking io

This commit is contained in:
Stephen Seo 2024-10-16 13:54:29 +09:00
parent 3dfc258fa4
commit 9d84b28efe

View file

@ -64,6 +64,8 @@ void handle_sig_int(int sig) {
is_sig_int_occurred = 1; is_sig_int_occurred = 1;
} }
} }
const struct timespec nonblock_sleep = {.tv_sec = 0, .tv_nsec = 1000000};
#endif #endif
typedef struct SDArchiverInternalToWrite { typedef struct SDArchiverInternalToWrite {
@ -297,6 +299,7 @@ int write_files_fn(void *data, void *ud) {
ret = write(pipe_into_cmd[1], write_buf, write_count); ret = write(pipe_into_cmd[1], write_buf, write_count);
if (ret == -1) { if (ret == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
nanosleep(&nonblock_sleep, NULL);
write_again = 1; write_again = 1;
} else { } else {
// Error during write. // Error during write.
@ -354,7 +357,7 @@ int write_files_fn(void *data, void *ud) {
// fprintf(stderr, "read_done\n"); // fprintf(stderr, "read_done\n");
} else if (ret == -1) { } else if (ret == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
// Nop. nanosleep(&nonblock_sleep, NULL);
} else { } else {
// Read error. // Read error.
fprintf(stderr, fprintf(stderr,
@ -1171,6 +1174,7 @@ int read_decomp_to_out_file(const char *out_filename, int in_pipe,
} else { } else {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
// Non-blocking read from pipe. // Non-blocking read from pipe.
nanosleep(&nonblock_sleep, NULL);
continue; continue;
} else { } else {
// Error. // Error.
@ -1210,6 +1214,7 @@ int read_decomp_to_out_file(const char *out_filename, int in_pipe,
} else { } else {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
// Non-blocking read from pipe. // Non-blocking read from pipe.
nanosleep(&nonblock_sleep, NULL);
continue; continue;
} else { } else {
// Error. // Error.
@ -2436,6 +2441,7 @@ int simple_archiver_write_v1(FILE *out_f, SDArchiverState *state,
// Non-blocking write. // Non-blocking write.
has_hold = (int)fread_ret; has_hold = (int)fread_ret;
memcpy(hold_buf, buf, fread_ret); memcpy(hold_buf, buf, fread_ret);
nanosleep(&nonblock_sleep, NULL);
} else { } else {
fprintf( fprintf(
stderr, stderr,
@ -2462,6 +2468,7 @@ int simple_archiver_write_v1(FILE *out_f, SDArchiverState *state,
if (write_ret < 0) { if (write_ret < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
// Non-blocking write. // Non-blocking write.
nanosleep(&nonblock_sleep, NULL);
} else { } else {
return SDAS_INTERNAL_ERROR; return SDAS_INTERNAL_ERROR;
} }
@ -2484,6 +2491,7 @@ int simple_archiver_write_v1(FILE *out_f, SDArchiverState *state,
if (read_ret < 0) { if (read_ret < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
// Non-blocking read. // Non-blocking read.
nanosleep(&nonblock_sleep, NULL);
} else { } else {
fprintf(stderr, fprintf(stderr,
"ERROR: Reading from compressor, pipe read error!\n"); "ERROR: Reading from compressor, pipe read error!\n");
@ -2518,6 +2526,7 @@ int simple_archiver_write_v1(FILE *out_f, SDArchiverState *state,
if (read_ret < 0) { if (read_ret < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
// Non-blocking read. // Non-blocking read.
nanosleep(&nonblock_sleep, NULL);
} else { } else {
fprintf(stderr, fprintf(stderr,
"ERROR: Reading from compressor, pipe read error!\n"); "ERROR: Reading from compressor, pipe read error!\n");
@ -3154,6 +3163,7 @@ int simple_archiver_parse_archive_version_0(FILE *in_f, int_fast8_t do_extract,
} }
} else if (write_ret == -1) { } else if (write_ret == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
nanosleep(&nonblock_sleep, NULL);
write_again = 1; write_again = 1;
} else { } else {
// Error. // Error.
@ -3197,6 +3207,7 @@ int simple_archiver_parse_archive_version_0(FILE *in_f, int_fast8_t do_extract,
} else if (read_ret == -1) { } else if (read_ret == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
// No bytes to read yet. // No bytes to read yet.
nanosleep(&nonblock_sleep, NULL);
} else { } else {
// Error. // Error.
fprintf(stderr, fprintf(stderr,