diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2020-02-12 02:22:31 +0100 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2020-02-12 02:26:24 +0100 |
commit | 303680ebcd5adaac2934b63a0edc2d9d1a36d7fb (patch) | |
tree | bd2012dc6fa56f7259dbe2e5edd7ab3042f8e0a0 /lib/sqfs/block_processor/winpthread.c | |
parent | ec7a522a520017327dd73b4d8e3787016ee1a31e (diff) |
Implement a more explicit object system
Make every dynamically allocated, opaque data structure inherit from
a common sqfs_object_t structure with common entry points (e.g. destroy).
This removes tons of public API functions and replaces them with a
simple sqfs_destroy instead. If semantics of the (until now implicit)
object system need to be extended, it can be much more conveniantely
done this way.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfs/block_processor/winpthread.c')
-rw-r--r-- | lib/sqfs/block_processor/winpthread.c | 93 |
1 files changed, 48 insertions, 45 deletions
diff --git a/lib/sqfs/block_processor/winpthread.c b/lib/sqfs/block_processor/winpthread.c index 85416ab..a91f001 100644 --- a/lib/sqfs/block_processor/winpthread.c +++ b/lib/sqfs/block_processor/winpthread.c @@ -94,6 +94,35 @@ static THREAD_TYPE worker_proc(THREAD_ARG arg) } #if defined(_WIN32) || defined(__WINDOWS__) +static void block_processor_destroy(sqfs_object_t *obj) +{ + sqfs_block_processor_t *proc = (sqfs_block_processor_t *)obj; + unsigned int i; + + EnterCriticalSection(&proc->mtx); + proc->status = -1; + WakeAllConditionVariable(&proc->queue_cond); + LeaveCriticalSection(&proc->mtx); + + for (i = 0; i < proc->num_workers; ++i) { + if (proc->workers[i] == NULL) + continue; + + if (proc->workers[i]->thread != NULL) { + WaitForSingleObject(proc->workers[i]->thread, INFINITE); + CloseHandle(proc->workers[i]->thread); + } + + if (proc->workers[i]->cmp != NULL) + sqfs_destroy(proc->workers[i]->cmp); + + free(proc->workers[i]); + } + + DeleteCriticalSection(&proc->mtx); + block_processor_cleanup(proc); +} + sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, sqfs_compressor_t *cmp, unsigned int num_workers, @@ -112,6 +141,8 @@ sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, if (proc == NULL) return NULL; + ((sqfs_object_t *)obj)->destroy = block_processor_destroy; + InitializeCriticalSection(&proc->mtx); InitializeConditionVariable(&proc->queue_cond); InitializeConditionVariable(&proc->done_cond); @@ -142,38 +173,34 @@ sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, return proc; fail: - sqfs_block_processor_destroy(proc); + block_processor_destroy(proc); return NULL; } - -void sqfs_block_processor_destroy(sqfs_block_processor_t *proc) +#else +static void block_processor_destroy(sqfs_object_t *obj) { + sqfs_block_processor_t *proc = (sqfs_block_processor_t *)obj; unsigned int i; - EnterCriticalSection(&proc->mtx); + pthread_mutex_lock(&proc->mtx); proc->status = -1; - WakeAllConditionVariable(&proc->queue_cond); - LeaveCriticalSection(&proc->mtx); + pthread_cond_broadcast(&proc->queue_cond); + pthread_mutex_unlock(&proc->mtx); for (i = 0; i < proc->num_workers; ++i) { - if (proc->workers[i] == NULL) - continue; - - if (proc->workers[i]->thread != NULL) { - WaitForSingleObject(proc->workers[i]->thread, INFINITE); - CloseHandle(proc->workers[i]->thread); - } - - if (proc->workers[i]->cmp != NULL) - proc->workers[i]->cmp->destroy(proc->workers[i]->cmp); + pthread_join(proc->workers[i]->thread, NULL); + sqfs_destroy(proc->workers[i]->cmp); free(proc->workers[i]); } - DeleteCriticalSection(&proc->mtx); + pthread_cond_destroy(&proc->done_cond); + pthread_cond_destroy(&proc->queue_cond); + pthread_mutex_destroy(&proc->mtx); + block_processor_cleanup(proc); } -#else + sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, sqfs_compressor_t *cmp, unsigned int num_workers, @@ -194,6 +221,7 @@ sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, if (proc == NULL) return NULL; + ((sqfs_object_t *)proc)->destroy = block_processor_destroy; proc->mtx = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; proc->queue_cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER; proc->done_cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER; @@ -246,10 +274,8 @@ fail_thread: fail_init: for (i = 0; i < num_workers; ++i) { if (proc->workers[i] != NULL) { - if (proc->workers[i]->cmp != NULL) { - proc->workers[i]->cmp-> - destroy(proc->workers[i]->cmp); - } + if (proc->workers[i]->cmp != NULL) + sqfs_destroy(proc->workers[i]->cmp); free(proc->workers[i]); } @@ -260,29 +286,6 @@ fail_init: block_processor_cleanup(proc); return NULL; } - -void sqfs_block_processor_destroy(sqfs_block_processor_t *proc) -{ - unsigned int i; - - pthread_mutex_lock(&proc->mtx); - proc->status = -1; - pthread_cond_broadcast(&proc->queue_cond); - pthread_mutex_unlock(&proc->mtx); - - for (i = 0; i < proc->num_workers; ++i) { - pthread_join(proc->workers[i]->thread, NULL); - - proc->workers[i]->cmp->destroy(proc->workers[i]->cmp); - free(proc->workers[i]); - } - - pthread_cond_destroy(&proc->done_cond); - pthread_cond_destroy(&proc->queue_cond); - pthread_mutex_destroy(&proc->mtx); - - block_processor_cleanup(proc); -} #endif int append_to_work_queue(sqfs_block_processor_t *proc, sqfs_block_t *block, |