fix: prevent multi-threaded server race conditions

This commit is contained in:
ngn
2024-08-16 00:22:44 +03:00
parent 9f0665ce64
commit 7b1bee0b99
4 changed files with 206 additions and 193 deletions

View File

@ -40,7 +40,7 @@ bool lm_ctx_init(lm_ctx_t *ctx) {
bzero(ctx, sizeof(lm_ctx_t));
ctx->version = LM_VERSION;
lm_error_clear();
lm_error_init();
return true;
}

View File

@ -1,12 +1,14 @@
#include "../include/error.h"
#include "../include/util.h"
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
lm_error_t lm_error_code = LM_ERR_NoError;
char *lm_error_str = NULL;
lm_error_t lm_error_code = LM_ERR_NoError; // error code
char *lm_error_str = NULL; // error string
pthread_t lm_error_thread = 0; // thread that is using the error system
void lm_error_clear() {
free(lm_error_str);
@ -14,7 +16,17 @@ void lm_error_clear() {
lm_error_str = NULL;
}
void lm_error_init() {
lm_error_clear();
lm_error_code = LM_ERR_NoError;
lm_error_str = NULL;
lm_error_thread = pthread_self();
}
void lm_error_set(lm_error_t code, ...) {
if (!pthread_equal(pthread_self(), lm_error_thread)) // ignore error_set outside the main thread
return;
lm_error_desc_t errors[] = {
{.code = LM_ERR_NoError, .desc = _("no error") },
{.code = LM_ERR_URLBadChar, .desc = _("URL contains an invalid character") },