:: commit 104c886c5882019010672845599db43b66a1ddc9

Kamila Szewczyk <kspalaiologos@gmail.com> — 2022-05-07 12:47

parents: 1bc4d17167

parallel compression/decompression

diff --git a/include/libbz3.h b/include/libbz3.h
index e0e080a..3c795a9 100644
--- a/include/libbz3.h
+++ b/include/libbz3.h
@@ -81,7 +81,7 @@ void bz3_encode_blocks(struct bz3_state * states[], uint8_t * buffers[], int32_t
 
 /**
  * @brief Decode `n' blocks, all in parallel.
- * Same specifics as `bz3_encode_blocks'
+ * Same specifics as `bz3_encode_blocks', but doesn't overwrite `sizes'.
  */
 void bz3_decode_blocks(struct bz3_state * states[], uint8_t * buffers[], int32_t sizes[], int32_t orig_sizes[], int32_t n);
 
diff --git a/src/libbz3.c b/src/libbz3.c
index 745daab..d2fcb27 100644
--- a/src/libbz3.c
+++ b/src/libbz3.c
@@ -256,10 +256,52 @@ s32 bz3_decode_block(struct bz3_state * state, u8 * buffer, s32 data_size, s32 o
 
 #undef swap
 
-void bz3_encode_blocks(struct bz3_state * states[], uint8_t * buffers[], int32_t sizes[], int32_t n) {
+#include <pthread.h>
+
+typedef struct {
+    struct bz3_state * state;
+    uint8_t * buffer;
+    int32_t size;
+} encode_thread_msg;
+
+typedef struct {
+    struct bz3_state * state;
+    uint8_t * buffer;
+    int32_t size;
+    int32_t orig_size;
+} decode_thread_msg;
+
+static void bz3_init_encode_thread(encode_thread_msg * msg) {
+    msg->size = bz3_encode_block(msg->state, msg->buffer, msg->size);
+}
 
+static void bz3_init_decode_thread(decode_thread_msg * msg) {
+    bz3_decode_block(msg->state, msg->buffer, msg->size, msg->orig_size);
+}
+
+void bz3_encode_blocks(struct bz3_state * states[], uint8_t * buffers[], int32_t sizes[], int32_t n) {
+    encode_thread_msg messages[n];
+    pthread_t threads[n];
+    for(int32_t i = 0; i < n; i++) {
+        messages[i].state = states[i];
+        messages[i].buffer = buffers[i];
+        messages[i].size = sizes[i];
+        pthread_create(&threads[i], NULL, (void *(*)(void *)) bz3_init_encode_thread, &messages[i]);
+    }
+    for(int32_t i = 0; i < n; i++)
+        pthread_join(threads[i], NULL);
 }
 
 void bz3_decode_blocks(struct bz3_state * states[], uint8_t * buffers[], int32_t sizes[], int32_t orig_sizes[], int32_t n) {
-    
+    decode_thread_msg messages[n];
+    pthread_t threads[n];
+    for(int32_t i = 0; i < n; i++) {
+        messages[i].state = states[i];
+        messages[i].buffer = buffers[i];
+        messages[i].size = sizes[i];
+        messages[i].orig_size = orig_sizes[i];
+        pthread_create(&threads[i], NULL, (void *(*)(void *)) bz3_init_decode_thread, &messages[i]);
+    }
+    for(int32_t i = 0; i < n; i++)
+        pthread_join(threads[i], NULL);
 }
tab: 248 wrap: offon