Skip to content

Commit

Permalink
filestr: fix for multi thread
Browse files Browse the repository at this point in the history
  • Loading branch information
rockeet committed Sep 23, 2022
1 parent ef7fb9e commit da9d074
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/lua/internal/sysbench.rand.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ function sysbench.rand.uniform_double()
return ffi.C.sb_rand_uniform_double()
end

buf1 = ffi.new("char[?]", 1024)
buf2 = ffi.new("char[?]", 4194304)

function sysbench.rand.filestr()
local buf1 = ffi.new("char[?]", 1024)
local buf2 = ffi.new("char[?]", 4194304)
ffi.C.sb_file_str(buf1, buf2)
return ffi.string(buf1), ffi.string(buf2)
end
6 changes: 6 additions & 0 deletions src/sb_rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include "sb_logger.h"

#include "sb_ck_pr.h"
#include "pthread.h"

TLS sb_rng_state_t sb_rng_state CK_CC_CACHELINE;

Expand Down Expand Up @@ -462,6 +463,7 @@ uint32_t sb_rand_zipfian(uint32_t a, uint32_t b)
static FILE* file = NULL;
static char* str_buf;
static size_t len;
static pthread_mutex_t file_mtx;

int sb_file_init()
{
Expand All @@ -473,6 +475,7 @@ int sb_file_init()
log_text(LOG_FATAL, "Invalid filename: %s", sb_globals.filename);
return 1;
}
pthread_mutex_init(&file_mtx, NULL);

len = 4194304 * sizeof(char);
str_buf = (char *)malloc(len);
Expand All @@ -483,6 +486,7 @@ void sb_file_str(char* buf1, char* buf2)
{
assert(file != NULL);

pthread_mutex_lock(&file_mtx);
ssize_t read = getline(&str_buf, &len, file);
if (read != -1)
{
Expand All @@ -502,6 +506,7 @@ void sb_file_str(char* buf1, char* buf2)
memcpy(buf2, str_buf, read);
}
}
pthread_mutex_unlock(&file_mtx);
}

void sb_file_done()
Expand All @@ -515,6 +520,7 @@ void sb_file_done()
{
free(str_buf);
}
pthread_mutex_destroy(&file_mtx);
}


Expand Down

0 comments on commit da9d074

Please sign in to comment.