diff --git a/cmdline/elem.c b/cmdline/elem.c index bd255a4d..c33baee2 100644 --- a/cmdline/elem.c +++ b/cmdline/elem.c @@ -737,7 +737,9 @@ struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev /* ensure that the dir terminate with "/" if it isn't empty */ pathslash(disk->dir, sizeof(disk->dir)); +#if HAVE_PTHREAD thread_mutex_init(&disk->fs_mutex, 0); +#endif disk->smartctl[0] = 0; disk->device = dev; @@ -782,19 +784,29 @@ void disk_free(struct snapraid_disk* disk) tommy_list_foreach(&disk->dirlist, (tommy_foreach_func*)dir_free); tommy_hashdyn_done(&disk->dirset); +#if HAVE_PTHREAD thread_mutex_destroy(&disk->fs_mutex); +#endif free(disk); } static inline void fs_lock(struct snapraid_disk* disk) { +#if HAVE_PTHREAD thread_mutex_lock(&disk->fs_mutex); +#else + (void)disk; +#endif } static inline void fs_unlock(struct snapraid_disk* disk) { +#if HAVE_PTHREAD thread_mutex_unlock(&disk->fs_mutex); +#else + (void)disk; +#endif } struct chunk_disk_empty { diff --git a/cmdline/elem.h b/cmdline/elem.h index ba005329..c9f13974 100644 --- a/cmdline/elem.h +++ b/cmdline/elem.h @@ -355,6 +355,7 @@ struct snapraid_disk { int mapping_idx; /**< Index in the mapping vector. Used only as buffer when writing the content file. */ int skip_access; /**< If the disk is inaccessible and it should be skipped. */ +#if HAVE_PTHREAD /** * Mutex for protecting the filesystem structure. * @@ -365,6 +366,7 @@ struct snapraid_disk { * change during multithread processing. */ pthread_mutex_t fs_mutex; +#endif /** * Mapping of chunks in the parity. diff --git a/cmdline/io.c b/cmdline/io.c index d4af5fb5..614a1a6a 100644 --- a/cmdline/io.c +++ b/cmdline/io.c @@ -19,18 +19,6 @@ #include "io.h" -/** - * At now pthread is required. - */ -#if !HAVE_PTHREAD -#error Pthread library is required for InputOutput! -#endif - -static inline int io_thread(struct snapraid_io* io) -{ - return io->io_max > 1; -} - /** * Get the next block position to process. */ @@ -273,6 +261,9 @@ static void io_stop_mono(struct snapraid_io* io) /*****************************************************************************/ /* multi thread */ +/* disable multithread if pthread is not present */ +#if HAVE_PTHREAD + /** * Get the next task to work on for a reader. * @@ -847,6 +838,8 @@ static void io_stop_thread(struct snapraid_io* io) } } +#endif + /*****************************************************************************/ /* global */ @@ -862,6 +855,8 @@ void io_init(struct snapraid_io* io, struct snapraid_state* state, size_t allocated; io->state = state; + +#if HAVE_PTHREAD if (io_cache == 0) { /* default is 8 MiB of cache */ /* this seems to be a good tradeoff between speed and memory usage */ @@ -873,6 +868,12 @@ void io_init(struct snapraid_io* io, struct snapraid_state* state, } else { io->io_max = io_cache; } +#else + (void)io_cache; + + /* without pthread force the mono thread mode */ + io->io_max = 1; +#endif assert(io->io_max == 1 || (io->io_max >= IO_MIN && io->io_max <= IO_MAX)); @@ -943,7 +944,8 @@ void io_init(struct snapraid_io* io, struct snapraid_state* state, worker->buffer_skew = handle_max; } - if (io_thread(io)) { +#if HAVE_PTHREAD + if (io->io_max > 1) { io_read_next = io_read_next_thread; io_write_preset = io_write_preset_thread; io_write_next = io_write_next_thread; @@ -959,7 +961,9 @@ void io_init(struct snapraid_io* io, struct snapraid_state* state, thread_cond_init(&io->read_sched, 0); thread_cond_init(&io->write_done, 0); thread_cond_init(&io->write_sched, 0); - } else { + } else +#endif + { io_read_next = io_read_next_mono; io_write_preset = io_write_preset_mono; io_write_next = io_write_next_mono; @@ -986,12 +990,14 @@ void io_done(struct snapraid_io* io) free(io->writer_map); free(io->writer_list); - if (io_thread(io)) { +#if HAVE_PTHREAD + if (io->io_max > 1) { thread_mutex_destroy(&io->io_mutex); thread_cond_destroy(&io->read_done); thread_cond_destroy(&io->read_sched); thread_cond_destroy(&io->write_done); thread_cond_destroy(&io->write_sched); } +#endif } diff --git a/cmdline/snapraid.c b/cmdline/snapraid.c index eeffb5fe..38b89b1a 100644 --- a/cmdline/snapraid.c +++ b/cmdline/snapraid.c @@ -814,7 +814,9 @@ int main(int argc, char* argv[]) opt.force_stats = 1; break; case OPT_TEST_COND_SIGNAL_OUTSIDE : +#if HAVE_PTHREAD thread_cond_signal_outside = 1; +#endif break; default : /* LCOV_EXCL_START */