Skip to content

Commit

Permalink
Shameful fix of missing mutexes
Browse files Browse the repository at this point in the history
  • Loading branch information
BatchDrake committed Sep 5, 2024
1 parent 06706a6 commit d3419b6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
66 changes: 50 additions & 16 deletions analyzer/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ suscan_source_destroy(suscan_source_t *self)
if (self->throttle_mutex_init)
pthread_mutex_destroy(&self->throttle_mutex);

if (self->history_mutex_init)
pthread_mutex_destroy(&self->history_mutex);

free(self);
}

Expand Down Expand Up @@ -311,6 +314,8 @@ suscan_source_history_write(
const SUCOMPLEX *buffer,
SUSCOUNT len)
{
(void) pthread_mutex_lock(&self->history_mutex);

SUSCOUNT ptr = self->history_ptr;

if (len > self->history_alloc) {
Expand Down Expand Up @@ -345,6 +350,8 @@ suscan_source_history_write(
}

self->history_ptr = ptr;

(void) pthread_mutex_unlock(&self->history_mutex);
}

SUINLINE SUSDIFF
Expand All @@ -356,9 +363,15 @@ suscan_source_history_read(
SUSCOUNT avail;
SUBOOL ptrside_before = self->rp <= self->history_ptr;
SUBOOL ptrside_after;
SUBOOL mutex_acquired = SU_FALSE;

SU_TRYZ(pthread_mutex_lock(&self->history_mutex));
mutex_acquired = SU_TRUE;

if (self->history_size == 0)
return 0;
if (self->history_size == 0) {
len = 0;
goto done;
}

avail = self->history_size - self->rp;

Expand All @@ -376,7 +389,11 @@ suscan_source_history_read(

if (ptrside_before != ptrside_after)
suscan_source_mark_looped(self);


done:
if (mutex_acquired)
pthread_mutex_unlock(&self->history_mutex);

return len;
}

Expand Down Expand Up @@ -1072,9 +1089,14 @@ suscan_source_set_history_alloc(suscan_source_t *self, size_t bytes)
SUBOOL
suscan_source_set_history_length(suscan_source_t *self, SUSCOUNT length)
{
size_t new_alloc = length * sizeof(SUCOMPLEX);
size_t old_alloc = self->history_alloc * sizeof(SUCOMPLEX);
size_t new_alloc = length;
size_t old_alloc = self->history_alloc;
SUBOOL mutex_acquired = SU_TRUE;
SUCOMPLEX *new_bytes;
SUBOOL ok = SU_FALSE;

SU_TRYZ(pthread_mutex_lock(&self->history_mutex));
mutex_acquired = SU_TRUE;

if (new_alloc == 0) {
/* Clear previous history */
Expand All @@ -1085,20 +1107,22 @@ suscan_source_set_history_length(suscan_source_t *self, SUSCOUNT length)
self->history_replay = SU_FALSE;
self->info.history_length = 0;
self->info.replay = SU_FALSE;
return SU_TRUE;

ok = SU_TRUE;
goto done;
}

new_bytes = mmap(
NULL,
new_alloc,
new_alloc * sizeof(SUCOMPLEX),
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS,
0, 0);

if (new_bytes == (SUCOMPLEX *) -1) {
SU_ERROR(
"Cannot mmap %lld bytes of memory for history: %s\n", new_alloc, strerror(errno));
return SU_FALSE;
"Cannot mmap %lld bytes of memory for history: %s\n", new_alloc * sizeof(SUCOMPLEX), strerror(errno));
goto done;
}

/*
Expand All @@ -1111,18 +1135,19 @@ suscan_source_set_history_length(suscan_source_t *self, SUSCOUNT length)
/* Partially full buffer, guess where to start */
copy_size = self->history_size;
p = copy_size < old_alloc ? 0 : self->history_ptr;

/* Adjust start according to new alloc */
if (copy_size > new_alloc) {

/* If the new alloc is smaller than the existing data,
we will need to advance the pointer */
if (new_alloc < copy_size) {
p += copy_size - new_alloc;
p %= old_alloc;
copy_size = new_alloc;
}

/* If p + copy_size surpasses the buffer size, divide in two */
if (p + copy_size > old_alloc) {
SUSCOUNT size1 = old_alloc - p;
SUSCOUNT size2 = copy_size - size1;

memcpy(new_bytes, self->history + p, size1 * sizeof(SUCOMPLEX));
memcpy(new_bytes + size1, self->history, size2 * sizeof(SUCOMPLEX));
} else {
Expand All @@ -1144,7 +1169,13 @@ suscan_source_set_history_length(suscan_source_t *self, SUSCOUNT length)

self->info.history_length = self->history_alloc;

return SU_TRUE;
ok = SU_TRUE;

done:
if (mutex_acquired)
pthread_mutex_unlock(&self->history_mutex);

return ok;
}

SUSCOUNT
Expand Down Expand Up @@ -1214,7 +1245,7 @@ void
suscan_source_clear_history(suscan_source_t *self)
{
if (self->history_alloc > 0) {
munmap(self->history, self->history_alloc);
munmap(self->history, self->history_alloc * sizeof(SUCOMPLEX));
self->history_alloc = 0;
self->history = NULL;
}
Expand All @@ -1228,6 +1259,9 @@ suscan_source_new(suscan_source_config_t *config)
SU_TRY_FAIL(suscan_source_config_check(config));
SU_ALLOCATE_FAIL(new, suscan_source_t);

SU_TRYZ_FAIL(pthread_mutex_init(&new->history_mutex, NULL));
new->history_mutex_init = SU_TRUE;

SU_TRY_FAIL(new->config = suscan_source_config_clone(config));

new->decim = 1;
Expand Down
5 changes: 4 additions & 1 deletion analyzer/source.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ struct suscan_source {
suscan_throttle_t throttle; /* For non-realtime sources */
SUBOOL throttle_mutex_init;
pthread_mutex_t throttle_mutex;

/* Source state */
SUBOOL capturing;
void *src_priv; /* Opaque source object */
Expand Down Expand Up @@ -152,6 +152,9 @@ struct suscan_source {
SUSCOUNT history_ptr;
SUSCOUNT rp; /* Replay pointer */
SUCOMPLEX *history;

pthread_mutex_t history_mutex;
SUBOOL history_mutex_init;
};

typedef struct suscan_source suscan_source_t;
Expand Down

0 comments on commit d3419b6

Please sign in to comment.