about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/omemo/omemo.c38
-rw-r--r--src/omemo/omemo.h2
2 files changed, 36 insertions, 4 deletions
diff --git a/src/omemo/omemo.c b/src/omemo/omemo.c
index 871f3bfc..96e07d1c 100644
--- a/src/omemo/omemo.c
+++ b/src/omemo/omemo.c
@@ -1,13 +1,24 @@
+#include <pthread.h>
 #include <signal/signal_protocol.h>
 
 #include "config/account.h"
 #include "ui/ui.h"
+#include "omemo/omemo.h"
 #include "omemo/crypto.h"
 
+static void lock(void *user_data);
+static void unlock(void *user_data);
+
+struct omemo_context_t {
+    pthread_mutexattr_t attr;
+    pthread_mutex_t lock;
+};
+
 void
 omemo_init(ProfAccount *account)
 {
-    signal_context *global_context;
+    signal_context *signal_ctx;
+    omemo_context *ctx = malloc(sizeof(omemo_context));
     signal_crypto_provider crypto_provider = {
         .random_func = omemo_random_func,
         .hmac_sha256_init_func = omemo_hmac_sha256_init_func,
@@ -27,14 +38,33 @@ omemo_init(ProfAccount *account)
         cons_show("Error initializing Omemo crypto");
     }
 
-    if (signal_context_create(&global_context, NULL) != 0) {
+    pthread_mutexattr_init(&ctx->attr);
+    pthread_mutexattr_settype(&ctx->attr, PTHREAD_MUTEX_RECURSIVE);
+    pthread_mutex_init(&ctx->lock, &ctx->attr);
+
+    if (signal_context_create(&signal_ctx, ctx) != 0) {
         cons_show("Error initializing Omemo context");
         return;
     }
 
-    if (signal_context_set_crypto_provider(global_context, &crypto_provider) != 0) {
+    if (signal_context_set_crypto_provider(signal_ctx, &crypto_provider) != 0) {
         cons_show("Error initializing Omemo crypto");
         return;
     }
-    //signal_context_set_locking_functions(global_context, lock_function, unlock_function);
+
+    signal_context_set_locking_functions(signal_ctx, lock, unlock);
+}
+
+static void
+lock(void *user_data)
+{
+    omemo_context *ctx = (omemo_context *)user_data;
+    pthread_mutex_lock(&ctx->lock);
+}
+
+static void
+unlock(void *user_data)
+{
+    omemo_context *ctx = (omemo_context *)user_data;
+    pthread_mutex_unlock(&ctx->lock);
 }
diff --git a/src/omemo/omemo.h b/src/omemo/omemo.h
index 2ca1c221..d34b90bf 100644
--- a/src/omemo/omemo.h
+++ b/src/omemo/omemo.h
@@ -1,3 +1,5 @@
 #include "config/account.h"
 
+typedef struct omemo_context_t omemo_context;
+
 void omemo_init(ProfAccount *account);