Add mutexes to string buffer allocator uses

This commit is contained in:
Ginger Bill
2017-08-01 14:24:40 +01:00
parent 2db971eedd
commit 10cc9cf661
2 changed files with 35 additions and 29 deletions

View File

@@ -69,8 +69,11 @@ String odin_root_dir(void) {
}
len += 1; // NOTE(bill): It needs an extra 1 for some reason
gb_mutex_lock(&string_buffer_mutex);
defer (gb_mutex_unlock(&string_buffer_mutex));
tmp = gb_temp_arena_memory_begin(&string_buffer_arena);
defer (gb_temp_arena_memory_end(tmp));
text = gb_alloc_array(string_buffer_allocator, wchar_t, len+1);
@@ -88,7 +91,6 @@ String odin_root_dir(void) {
global_module_path = path;
global_module_path_set = true;
gb_temp_arena_memory_end(tmp);
array_free(&path_buf);
@@ -124,8 +126,12 @@ String odin_root_dir(void) {
}
}
gb_mutex_lock(&string_buffer_mutex);
defer (gb_mutex_unlock(&string_buffer_mutex));
tmp = gb_temp_arena_memory_begin(&string_buffer_arena);
defer (gb_temp_arena_memory_end(tmp));
text = gb_alloc_array(string_buffer_allocator, u8, len + 1);
gb_memmove(text, &path_buf[0], len);
@@ -141,7 +147,6 @@ String odin_root_dir(void) {
global_module_path = path;
global_module_path_set = true;
gb_temp_arena_memory_end(tmp);
// array_free(&path_buf);
@@ -182,6 +187,8 @@ String odin_root_dir(void) {
array_resize(&path_buf, 2*path_buf.count + 300);
}
gb_mutex_lock(&string_buffer_mutex);
defer (gb_mutex_unlock(&string_buffer_mutex));
tmp = gb_temp_arena_memory_begin(&string_buffer_arena);
defer (gb_temp_arena_memory_end(tmp));
@@ -211,27 +218,28 @@ String odin_root_dir(void) {
String path_to_fullpath(gbAllocator a, String s) {
String result = {};
gb_mutex_lock(&string_buffer_mutex);
{
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&string_buffer_arena);
String16 string16 = string_to_string16(string_buffer_allocator, s);
defer (gb_mutex_unlock(&string_buffer_mutex));
DWORD len = GetFullPathNameW(&string16[0], 0, nullptr, nullptr);
if (len != 0) {
wchar_t *text = gb_alloc_array(string_buffer_allocator, wchar_t, len+1);
GetFullPathNameW(&string16[0], len, text, nullptr);
text[len] = 0;
result = string16_to_string(a, make_string16(text, len));
}
gb_temp_arena_memory_end(tmp);
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&string_buffer_arena);
String16 string16 = string_to_string16(string_buffer_allocator, s);
DWORD len = GetFullPathNameW(&string16[0], 0, nullptr, nullptr);
if (len != 0) {
wchar_t *text = gb_alloc_array(string_buffer_allocator, wchar_t, len+1);
GetFullPathNameW(&string16[0], len, text, nullptr);
text[len] = 0;
result = string16_to_string(a, make_string16(text, len));
}
gb_mutex_unlock(&string_buffer_mutex);
gb_temp_arena_memory_end(tmp);
return result;
}
#elif defined(GB_SYSTEM_OSX) || defined(GB_SYSTEM_UNIX)
String path_to_fullpath(gbAllocator a, String s) {
char *p = realpath(cast(char *)&s[0], 0);
char *p;
gb_mutex_lock(&string_buffer_mutex);
p = realpath(cast(char *)s.data, 0);
gb_mutex_unlock(&string_buffer_mutex);
if(p == nullptr) return make_string_c("");
return make_string_c(p);
}
#else

View File

@@ -79,8 +79,6 @@ struct Parser {
String init_fullpath;
Array<AstFile> files;
Array<ImportedFile> imports;
isize curr_import_index;
gbAtomic32 import_index;
isize total_token_count;
isize total_line_count;
gbMutex file_add_mutex;
@@ -4921,7 +4919,7 @@ void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array<AstNod
String collection_name = {};
String oirignal_string = id->relpath.string;
String file_str = id->relpath.string;
gbAllocator allocator = heap_allocator(); // TODO(bill): Change this allocator
gbAllocator a = heap_allocator(); // TODO(bill): Change this allocator
String import_file = {};
String rel_path = {};
@@ -4936,15 +4934,13 @@ void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array<AstNod
continue;
}
gb_mutex_lock(&p->file_decl_mutex);
defer (gb_mutex_unlock(&p->file_decl_mutex));
rel_path = get_fullpath_relative(allocator, base_dir, file_str);
rel_path = get_fullpath_relative(a, base_dir, file_str);
import_file = rel_path;
if (!gb_file_exists(cast(char *)rel_path.text)) { // NOTE(bill): This should be null terminated
String abs_path = get_fullpath_core(allocator, file_str);
String abs_path = get_fullpath_core(a, file_str);
if (gb_file_exists(cast(char *)abs_path.text)) {
import_file = abs_path;
}
@@ -5105,6 +5101,7 @@ ParseFileError parse_files(Parser *p, String init_filename) {
gbThread *t = &worker_threads[i];
gb_thread_init(t);
}
isize curr_import_index = 0;
// NOTE(bill): Make sure that these are in parsed in this order
for (isize i = 0; i < shared_file_count; i++) {
@@ -5112,7 +5109,7 @@ ParseFileError parse_files(Parser *p, String init_filename) {
if (err != ParseFile_None) {
return err;
}
p->curr_import_index++;
curr_import_index++;
}
for (;;) {
@@ -5121,19 +5118,20 @@ ParseFileError parse_files(Parser *p, String init_filename) {
gbThread *t = &worker_threads[i];
if (gb_thread_is_running(t)) {
are_any_alive = true;
} else if (p->curr_import_index < p->imports.count) {
if (t->return_value != 0) {
} else if (curr_import_index < p->imports.count) {
auto err = cast(ParseFileError)t->return_value;
if (err != ParseFile_None) {
for_array(i, worker_threads) {
gb_thread_destroy(&worker_threads[i]);
}
return cast(ParseFileError)t->return_value;
return err;
}
t->user_index = p->curr_import_index++;
t->user_index = curr_import_index++;
gb_thread_start(t, parse_worker_file_proc, p);
are_any_alive = true;
}
}
if (!are_any_alive && p->curr_import_index >= p->imports.count) {
if (!are_any_alive && curr_import_index >= p->imports.count) {
break;
}
}