fixed arena + some niceties
This commit is contained in:
parent
df0187bdb3
commit
b601ace92b
3 changed files with 258 additions and 54 deletions
97
colla.c
97
colla.c
|
|
@ -762,6 +762,11 @@ strview_t istr_get_line(instream_t *ctx) {
|
|||
return line;
|
||||
}
|
||||
|
||||
strview_t istr_get_word(instream_t *ctx) {
|
||||
strview_t word = istr_get_view_either(ctx, strv(" \t\v\r\n"));
|
||||
return word;
|
||||
}
|
||||
|
||||
// == OUTPUT STREAM ================================================
|
||||
|
||||
outstream_t ostr_init(arena_t *exclusive_arena) {
|
||||
|
|
@ -802,6 +807,11 @@ strview_t ostr_as_view(outstream_t *ctx) {
|
|||
return strv(ctx->beg, ostr_tell(ctx));
|
||||
}
|
||||
|
||||
void ostr_rewind(outstream_t *ctx, usize from_beg) {
|
||||
if (!ctx->arena) return;
|
||||
ctx->arena->cur = (u8*)ctx->beg + from_beg;
|
||||
}
|
||||
|
||||
void ostr_pop(outstream_t *ctx, usize count) {
|
||||
if (!ctx->arena) return;
|
||||
arena_pop(ctx->arena, count);
|
||||
|
|
@ -1183,7 +1193,7 @@ static void *arena__alloc_common(const arena_alloc_desc_t *desc) {
|
|||
|
||||
if (new_cur > page_end) {
|
||||
usize page_size = os_get_system_info().page_size;
|
||||
usize prev_page = os_pad_to_page(allocated - page_size);
|
||||
usize prev_page = os_pad_to_page(allocated ? allocated - page_size : 0);
|
||||
usize next_page = os_pad_to_page(new_cur);
|
||||
usize num_of_pages = (next_page - prev_page) / page_size;
|
||||
colla_assert(num_of_pages > 0);
|
||||
|
|
@ -1257,13 +1267,13 @@ os_log_colour_e os_log_level_colours[LOG_COL__COUNT] = {
|
|||
[LOG_FATAL] = LOG_COL_RED,
|
||||
};
|
||||
|
||||
os_log_options_e log_opts = OS_LOG_SIMPLE;
|
||||
log_callback_t log_cbs[COLLA_LOG_MAX_CALLBACKS] = {0};
|
||||
int log_cbs_count = 0;
|
||||
os_log_options_e os__log_opts = OS_LOG_SIMPLE;
|
||||
log_callback_t os__log_cbs[COLLA_LOG_MAX_CALLBACKS] = {0};
|
||||
i64 os__log_cbs_count = 0;
|
||||
|
||||
void os_log__stdout(log_event_t *ev) {
|
||||
bool notime = log_opts & OS_LOG_NOTIME;
|
||||
bool nofile = log_opts & OS_LOG_NOFILE;
|
||||
bool notime = os__log_opts & OS_LOG_NOTIME;
|
||||
bool nofile = os__log_opts & OS_LOG_NOFILE;
|
||||
|
||||
if (!notime) {
|
||||
os_log_set_colour(LOG_COL_DARK_GREY);
|
||||
|
|
@ -1298,8 +1308,8 @@ void os_log__fp(log_event_t *ev) {
|
|||
oshandle_t fp = {0};
|
||||
fp.data = (uptr)ev->udata;
|
||||
|
||||
bool notime = log_opts & OS_LOG_NOTIME;
|
||||
bool nofile = log_opts & OS_LOG_NOFILE;
|
||||
bool notime = os__log_opts & OS_LOG_NOTIME;
|
||||
bool nofile = os__log_opts & OS_LOG_NOFILE;
|
||||
|
||||
if (!notime) {
|
||||
os_file_print(
|
||||
|
|
@ -1324,9 +1334,17 @@ void os_log__fp(log_event_t *ev) {
|
|||
os_file_putc(fp, '\n');
|
||||
}
|
||||
|
||||
void os_log_set_options(os_log_options_e opt) {
|
||||
os__log_opts = opt;
|
||||
}
|
||||
|
||||
os_log_options_e os_log_get_options(void) {
|
||||
return os__log_opts;
|
||||
}
|
||||
|
||||
void os_log_add_callback(log_callback_t cb) {
|
||||
colla_assert(log_cbs_count < arrlen(log_cbs));
|
||||
log_cbs[log_cbs_count++] = cb;
|
||||
colla_assert(os__log_cbs_count < arrlen(os__log_cbs));
|
||||
os__log_cbs[os__log_cbs_count++] = cb;
|
||||
}
|
||||
|
||||
void os_log_add_fp(oshandle_t fp, os_log_level_e level) {
|
||||
|
|
@ -1362,15 +1380,15 @@ void os_log_printv(const char *file, int line, os_log_level_e level, const char
|
|||
};
|
||||
|
||||
localtime_s(&log_time, &t);
|
||||
for (int i = 0; i < log_cbs_count; ++i) {
|
||||
if (log_cbs[i].level > level) {
|
||||
for (int i = 0; i < os__log_cbs_count; ++i) {
|
||||
if (os__log_cbs[i].level > level) {
|
||||
continue;
|
||||
}
|
||||
ev.udata = log_cbs[i].udata;
|
||||
log_cbs[i].fn(&ev);
|
||||
ev.udata = os__log_cbs[i].udata;
|
||||
os__log_cbs[i].fn(&ev);
|
||||
}
|
||||
|
||||
bool nocrash = log_opts & OS_LOG_NOCRASH;
|
||||
bool nocrash = os__log_opts & OS_LOG_NOCRASH;
|
||||
|
||||
if (!nocrash && level == LOG_FATAL) {
|
||||
os_abort(1);
|
||||
|
|
@ -1544,11 +1562,55 @@ usize os_pad_to_page(usize byte_count) {
|
|||
return byte_count + padding;
|
||||
}
|
||||
|
||||
// == THREAD ====================================
|
||||
|
||||
void os_barrier_sync(os_barrier_t *b) {
|
||||
atomic_inc_i64(&b->thread_value);
|
||||
|
||||
while (true) {
|
||||
if (atomic_cmp_i64(&b->has_completed, 1, 1)) {
|
||||
break;
|
||||
}
|
||||
i64 completed =
|
||||
atomic_cmp_i64(
|
||||
&b->thread_value,
|
||||
b->thread_count,
|
||||
b->thread_count
|
||||
) == b->thread_count;
|
||||
if (completed) {
|
||||
atomic_set_i64(&b->has_completed, completed);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (atomic_dec_i64(&b->thread_value) == 0) {
|
||||
b->has_completed = 0;
|
||||
}
|
||||
}
|
||||
|
||||
i64range_t os_lane_range(u64 values_count) {
|
||||
i64 thread_id = os_thread_id;
|
||||
i64 thread_count = os_thread_count;
|
||||
|
||||
i64 values_per_thread = values_count / thread_count;
|
||||
i64 leftover_values_count = values_count % thread_count;
|
||||
bool thread_has_leftover = thread_id < leftover_values_count;
|
||||
i64 leftover_before_this_thread_id = thread_has_leftover ?
|
||||
thread_id : leftover_values_count;
|
||||
i64 thread_first_value =
|
||||
values_per_thread * thread_id + leftover_before_this_thread_id;
|
||||
i64 thread_last_value =
|
||||
thread_first_value + values_per_thread + thread_has_leftover;
|
||||
|
||||
return (i64range_t){ thread_first_value, thread_last_value };
|
||||
}
|
||||
|
||||
#if !COLLA_NO_CONDITION_VARIABLE
|
||||
|
||||
// == JOB QUEUE =================================
|
||||
|
||||
int jq__worker_function(u64 id, void *udata) {
|
||||
int jq__worker_function(u64 thread_id, void *udata) {
|
||||
COLLA_UNUSED(thread_id);
|
||||
job_queue_t *q = udata;
|
||||
|
||||
// TODO: is this safe to not have an atomic variable?
|
||||
|
|
@ -1617,6 +1679,7 @@ void jq_cleanup(job_queue_t *queue) {
|
|||
void jq_push(arena_t *arena, job_queue_t *queue, job_func_f *func, void *userdata) {
|
||||
os_mutex_lock(queue->mutex);
|
||||
job_t *job = queue->freelist;
|
||||
list_pop(queue->freelist);
|
||||
if (!job) {
|
||||
job = alloc(arena, job_t);
|
||||
}
|
||||
|
|
@ -1633,7 +1696,7 @@ job_t *jq_pop_job(job_queue_t *queue) {
|
|||
while (!queue->jobs && !queue->should_stop && !queue->stop_when_finished) {
|
||||
os_cond_wait(queue->condvar, queue->mutex, OS_WAIT_INFINITE);
|
||||
}
|
||||
job = queue->jobs;
|
||||
job = queue->jobs ;
|
||||
list_pop(queue->jobs);
|
||||
os_mutex_unlock(queue->mutex);
|
||||
return job;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue