Skip to content

Commit

Permalink
Refactor variable naming and enhance error handling 🎨🔧
Browse files Browse the repository at this point in the history
- Modified function parameter names from `at` to `x` for consistency.
- Used `athr_logger_error()` for enhanced error logging in `athr.c`, `athr_terminal_ioctl.c`, and `athr_terminal_win32.c`.
- Removed unnamed function macros in `athr_widget.h` for clarity and standardization.
- Updated `athr_widget_*` creations to remove old prefixes for consistency across files.

These changes standardize the codebase, improve code readability, and ensure consistent error handling across different modules. Keep pushing for better practices! 🚀
  • Loading branch information
horta committed Nov 7, 2024
1 parent 02935a1 commit c3dccef
Show file tree
Hide file tree
Showing 20 changed files with 248 additions and 275 deletions.
140 changes: 71 additions & 69 deletions athr.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "athr_ovs_atomic_posix.h"
#endif

#define error(msg) athr_logger_error(athr_logger_format(msg))

static atomic_bool disable_thread = false;

static void lock(struct athr *at)
Expand All @@ -26,38 +28,38 @@ static void lock(struct athr *at)
/* spin until the lock is acquired */;
}

static void unlock(struct athr *at) { atomic_flag_clear(&at->lock); }
static void unlock(struct athr *x) { atomic_flag_clear(&x->lock); }

static void update(struct athr *at)
static void update(struct athr *x)
{
lock(at);
uint_fast64_t consumed = atomic_load_uint_fast64(&at->consumed);
if (consumed > at->total) consumed = at->total;
if (consumed == at->last_consumed) goto cleanup;
uint_fast64_t delta = consumed - at->last_consumed;
at->last_consumed = consumed;
lock(x);
uint_fast64_t consumed = atomic_load_uint_fast64(&x->consumed);
if (consumed > x->total) consumed = x->total;
if (consumed == x->last_consumed) goto cleanup;
uint_fast64_t delta = consumed - x->last_consumed;
x->last_consumed = consumed;

if (athr_elapsed_stop(at->elapsed)) error("failed to elapsed_stop");
if (athr_elapsed_stop(x->elapsed)) error("failed to elapsed_stop");

double seconds = ((double)athr_elapsed_milliseconds(at->elapsed)) / 1000.;
double progress = ((double)delta) / ((double)at->total);
double seconds = ((double)athr_elapsed_milliseconds(x->elapsed)) / 1000.;
double progress = ((double)delta) / ((double)x->total);

if (progress < 0.005f && at->timestep < ATHR_TIMESTEP_LIMIT)
if (progress < 0.005f && x->timestep < ATHR_TIMESTEP_LIMIT)
{
at->timestep += ATHR_TIMESTEP;
if (at->timestep > ATHR_TIMESTEP_LIMIT)
at->timestep = ATHR_TIMESTEP_LIMIT;
x->timestep += ATHR_TIMESTEP;
if (x->timestep > ATHR_TIMESTEP_LIMIT)
x->timestep = ATHR_TIMESTEP_LIMIT;
}

athr_ema_add(&at->speed, progress / seconds);
athr_ema_add(&x->speed, progress / seconds);

double consumed_fraction = ((double)consumed) / ((double)at->total);
at->main.super.vtable->update(&at->main.super, consumed_fraction,
athr_ema_get(&at->speed));
double consumed_fraction = ((double)consumed) / ((double)x->total);
x->main.super.vtable->update(&x->main.super, consumed_fraction,
athr_ema_get(&x->speed));

if (athr_elapsed_start(at->elapsed)) error("failed to elapsed_start");
if (athr_elapsed_start(x->elapsed)) error("failed to elapsed_start");
cleanup:
unlock(at);
unlock(x);
}

static void thread_start(void *args)
Expand All @@ -70,86 +72,86 @@ static void thread_start(void *args)
}
}

int athr_start(struct athr *at, uint64_t total, const char *desc,
int athr_start(struct athr *x, uint64_t total, const char *desc,
enum athr_option opts)
{
if (desc == NULL) desc = "";
at->timestep = ATHR_TIMESTEP;
at->total = total;
at->consumed = 0;
at->last_consumed = 0;
at->speed = ATHR_EMA_INIT;
at->elapsed = athr_elapsed_new();
at->total_elapsed = athr_elapsed_new();
if (!at->elapsed || !at->total_elapsed)
x->timestep = ATHR_TIMESTEP;
x->total = total;
x->consumed = 0;
x->last_consumed = 0;
x->speed = ATHR_EMA_INIT;
x->elapsed = athr_elapsed_new();
x->total_elapsed = athr_elapsed_new();
if (!x->elapsed || !x->total_elapsed)
{
athr_elapsed_del(at->elapsed);
athr_elapsed_del(at->total_elapsed);
at->elapsed = NULL;
at->total_elapsed = NULL;
athr_elapsed_del(x->elapsed);
athr_elapsed_del(x->total_elapsed);
x->elapsed = NULL;
x->total_elapsed = NULL;
error("failed to allocate elapsed struct");
return 1;
}
if (athr_elapsed_start(at->elapsed) || athr_elapsed_start(at->total_elapsed))
if (athr_elapsed_start(x->elapsed) || athr_elapsed_start(x->total_elapsed))
{
athr_elapsed_del(at->elapsed);
athr_elapsed_del(at->total_elapsed);
at->elapsed = NULL;
at->total_elapsed = NULL;
athr_elapsed_del(x->elapsed);
athr_elapsed_del(x->total_elapsed);
x->elapsed = NULL;
x->total_elapsed = NULL;
error("failed to elapsed_start");
return 1;
}

at->opts = opts;
__athr_widget_main_create(&at->main);
__athr_widget_text_create(__athr_widget_main_add_text(&at->main), desc);
x->opts = opts;
athr_widget_main_create(&x->main);
athr_widget_text_create(athr_widget_main_add_text(&x->main), desc);
if (opts & ATHR_PERC)
__athr_widget_perc_create(__athr_widget_main_add_perc(&at->main));
athr_widget_perc_create(athr_widget_main_add_perc(&x->main));
if (opts & ATHR_BAR)
__athr_widget_bar_create(__athr_widget_main_add_bar(&at->main));
athr_widget_bar_create(athr_widget_main_add_bar(&x->main));
if (opts & ATHR_ETA)
__athr_widget_eta_create(__athr_widget_main_add_eta(&at->main));
__athr_widget_main_setup(&at->main);
athr_widget_eta_create(athr_widget_main_add_eta(&x->main));
athr_widget_main_setup(&x->main);

atomic_store(&at->stop, false);
atomic_store(&x->stop, false);

if (!atomic_load_bool(&disable_thread))
{
int rc = athr_thread_create(&at->thr, thread_start, at);
int rc = athr_thread_create(&x->thr, thread_start, x);
if (rc)
{
athr_elapsed_del(at->elapsed);
athr_elapsed_del(at->total_elapsed);
at->elapsed = NULL;
at->total_elapsed = NULL;
athr_elapsed_del(x->elapsed);
athr_elapsed_del(x->total_elapsed);
x->elapsed = NULL;
x->total_elapsed = NULL;
}
return rc;
}

return 0;
}

void athr_eat(struct athr *at, uint64_t amount)
void athr_eat(struct athr *x, uint64_t amount)
{
atomic_fetch_add_uint_fast64(&at->consumed, amount);
if (atomic_load_bool(&disable_thread)) update(at);
atomic_fetch_add_uint_fast64(&x->consumed, amount);
if (atomic_load_bool(&disable_thread)) update(x);
}

void athr_stop(struct athr *at)
void athr_stop(struct athr *x)
{
atomic_store(&at->stop, true);
update(at);
athr_thread_join(&at->thr);

if (athr_elapsed_stop(at->total_elapsed)) error("failed to elapsed_stop");

double seconds = ((double)athr_elapsed_milliseconds(at->total_elapsed)) / 1000.;
at->main.super.vtable->finish(&at->main.super, seconds);
athr_canvas_close(&at->main.canvas);
athr_elapsed_del(at->elapsed);
athr_elapsed_del(at->total_elapsed);
at->elapsed = NULL;
at->total_elapsed = NULL;
atomic_store(&x->stop, true);
update(x);
athr_thread_join(&x->thr);

if (athr_elapsed_stop(x->total_elapsed)) error("failed to elapsed_stop");

double seconds = ((double)athr_elapsed_milliseconds(x->total_elapsed)) / 1000.;
x->main.super.vtable->finish(&x->main.super, seconds);
athr_canvas_close(&x->main.canvas);
athr_elapsed_del(x->elapsed);
athr_elapsed_del(x->total_elapsed);
x->elapsed = NULL;
x->total_elapsed = NULL;
}

void athr_stop_wait(struct athr *at)
Expand Down
4 changes: 2 additions & 2 deletions athr.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ struct athr
atomic_uint_fast64_t consumed;
uint_fast64_t last_consumed;
struct athr_ema speed;
struct athr_elapsed *elapsed;
struct athr_elapsed *total_elapsed;
struct athr_elapsed *elapsed;
struct athr_elapsed *total_elapsed;

enum athr_option opts;
struct athr_widget_main main;
Expand Down
42 changes: 21 additions & 21 deletions athr_canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,53 @@ static atomic_bool use_stderr = false;

void athr_canvas_use_stderr(bool use) { atomic_store(&use_stderr, use); }

void athr_canvas_create(struct athr_canvas *canvas)
void athr_canvas_create(struct athr_canvas *x)
{
canvas->len = 0;
canvas->min_len = 0;
canvas->max_len = 0;
x->len = 0;
x->min_len = 0;
x->max_len = 0;
}

void athr_canvas_draw(struct athr_canvas const *canvas)
void athr_canvas_draw(struct athr_canvas const *x)
{
if (use_stderr)
{
fprintf(stderr, "%.*s", canvas->len, canvas->buff);
fprintf(stderr, "%.*s", x->len, x->buff);
fflush(stderr);
}
else
{
fprintf(stdout, "%.*s", canvas->len, canvas->buff);
fprintf(stdout, "%.*s", x->len, x->buff);
fflush(stdout);
}
}

bool athr_canvas_resize(struct athr_canvas *canvas)
bool athr_canvas_resize(struct athr_canvas *x)
{
unsigned ncols = athr_terminal_width() + 1;
unsigned prev_len = canvas->len;
canvas->len = ncols;
canvas->len = athr_min(canvas->len, canvas->max_len);
canvas->len = athr_max(canvas->len, canvas->min_len);
return prev_len != canvas->len;
unsigned prev_len = x->len;
x->len = ncols;
x->len = athr_min(x->len, x->max_len);
x->len = athr_max(x->len, x->min_len);
return prev_len != x->len;
}

void athr_canvas_setup(struct athr_canvas *canvas, unsigned min_len,
void athr_canvas_setup(struct athr_canvas *x, unsigned min_len,
unsigned max_len)
{
canvas->min_len = min_len;
canvas->max_len = athr_min(max_len, ATHR_CANVAS_MAX_LEN);
x->min_len = min_len;
x->max_len = athr_min(max_len, ATHR_CANVAS_MAX_LEN);
}

void athr_canvas_clean(struct athr_canvas *canvas)
void athr_canvas_clean(struct athr_canvas *x)
{
memset(canvas->buff, ' ', canvas->len - 1);
canvas->buff[canvas->len - 1] = '\r';
memset(x->buff, ' ', x->len - 1);
x->buff[x->len - 1] = '\r';
}

void athr_canvas_close(struct athr_canvas *canvas)
void athr_canvas_close(struct athr_canvas *x)
{
(void)canvas;
(void)x;
if (use_stderr)
{
fprintf(stderr, "\n");
Expand Down
22 changes: 10 additions & 12 deletions athr_elapsed.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,26 @@ struct athr_elapsed* athr_elapsed_new(void)
return x;
}

void athr_elapsed_del(struct athr_elapsed* elapsed)
void athr_elapsed_del(struct athr_elapsed* x)
{
free(elapsed);
free(x);
}

static int
get_any_timespec(struct timespec* ts)
static int get_any_timespec(struct timespec *ts)
{
return timespec_get(ts, TIME_UTC) != TIME_UTC;
}

static void
die(char const* msg)
static void die(char const *msg)
{
fprintf(stderr, "%s", msg);
fflush(stderr);
abort();
}

int athr_elapsed_start(struct athr_elapsed* elapsed)
int athr_elapsed_start(struct athr_elapsed* x)
{
return get_any_timespec(&elapsed->start);
return get_any_timespec(&x->start);
}

/* source: libbsd */
Expand All @@ -82,10 +80,10 @@ int athr_elapsed_start(struct athr_elapsed* elapsed)
#define MICROSECONDS_IN_A_SECOND (MILLISECONDS_IN_A_SECOND * 1000L)
#define NANOSECONDS_IN_A_SECOND (MICROSECONDS_IN_A_SECOND * 1000L)

long athr_elapsed_milliseconds(struct athr_elapsed const* elapsed)
long athr_elapsed_milliseconds(struct athr_elapsed const* x)
{
struct timespec diff = { 0 };
timespecsub(&elapsed->stop, &elapsed->start, &diff);
timespecsub(&x->stop, &x->start, &diff);
if (diff.tv_sec < 0 || diff.tv_nsec < 0)
die("unexpected negative time duration");

Expand All @@ -94,9 +92,9 @@ long athr_elapsed_milliseconds(struct athr_elapsed const* elapsed)
return ms + re;
}

int athr_elapsed_stop(struct athr_elapsed* elapsed)
int athr_elapsed_stop(struct athr_elapsed* x)
{
return get_any_timespec(&elapsed->stop);
return get_any_timespec(&x->stop);
}

int athr_elapsed_sleep(long ms)
Expand Down
1 change: 0 additions & 1 deletion athr_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ void athr_logger_setup(athr_logger_print_t *, void *arg);
void athr_logger_error(char const *msg);

#define athr_logger_format(msg) __FILE__ ":" athr_stringify(__LINE__) ": " msg
#define error(msg) athr_logger_error(athr_logger_format(msg))

#endif
4 changes: 2 additions & 2 deletions athr_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
enum athr_option
{
ATHR_NONE = 0,
ATHR_BAR = 1,
ATHR_ETA = 2,
ATHR_BAR = 1,
ATHR_ETA = 2,
ATHR_PERC = 4
};

Expand Down
2 changes: 2 additions & 0 deletions athr_terminal_ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <windows.h>
#endif

#define error(msg) athr_logger_error(athr_logger_format(msg))

/* Source: wget2 */
unsigned athr_terminal_ioctl_width(void)
{
Expand Down
2 changes: 2 additions & 0 deletions athr_terminal_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <stdlib.h>
#include <windows.h>

#define error(msg) athr_logger_error(athr_logger_format(msg))

static long tput_cols(void)
{
char buff[16] = {0};
Expand Down
Loading

0 comments on commit c3dccef

Please sign in to comment.