summaryrefslogtreecommitdiff
path: root/lib/sqfs/block_processor
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-02-12 02:22:31 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-02-12 02:26:24 +0100
commit303680ebcd5adaac2934b63a0edc2d9d1a36d7fb (patch)
treebd2012dc6fa56f7259dbe2e5edd7ab3042f8e0a0 /lib/sqfs/block_processor
parentec7a522a520017327dd73b4d8e3787016ee1a31e (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')
-rw-r--r--lib/sqfs/block_processor/internal.h2
-rw-r--r--lib/sqfs/block_processor/serial.c11
-rw-r--r--lib/sqfs/block_processor/winpthread.c93
3 files changed, 56 insertions, 50 deletions
diff --git a/lib/sqfs/block_processor/internal.h b/lib/sqfs/block_processor/internal.h
index b3d76ba..71f8fab 100644
--- a/lib/sqfs/block_processor/internal.h
+++ b/lib/sqfs/block_processor/internal.h
@@ -50,6 +50,8 @@ typedef struct sqfs_block_t {
} sqfs_block_t;
struct sqfs_block_processor_t {
+ sqfs_object_t obj;
+
/* synchronization primitives */
#ifdef WITH_PTHREAD
pthread_mutex_t mtx;
diff --git a/lib/sqfs/block_processor/serial.c b/lib/sqfs/block_processor/serial.c
index e18b67e..469388c 100644
--- a/lib/sqfs/block_processor/serial.c
+++ b/lib/sqfs/block_processor/serial.c
@@ -7,6 +7,11 @@
#define SQFS_BUILDING_DLL
#include "internal.h"
+static void block_processor_destroy(sqfs_object_t *obj)
+{
+ block_processor_cleanup((sqfs_block_processor_t *)obj);
+}
+
sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size,
sqfs_compressor_t *cmp,
unsigned int num_workers,
@@ -27,14 +32,10 @@ sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size,
return NULL;
}
+ ((sqfs_object_t *)proc)->destroy = block_processor_destroy;
return proc;
}
-void sqfs_block_processor_destroy(sqfs_block_processor_t *proc)
-{
- block_processor_cleanup(proc);
-}
-
int test_and_set_status(sqfs_block_processor_t *proc, int status)
{
if (proc->status == 0)
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,