#############################################################################*/
#include <errno.h>
+#include <pthread.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
struct timespec time_start;
timer_t timer;
+ pthread_mutex_t drawlock;
// Widgets
STAILQ_HEAD(widgets, pakfire_progressbar_widget) widgets;
if (p->timer)
timer_delete(p->timer);
+ // Free muted
+ pthread_mutex_destroy(&p->drawlock);
+
pakfire_progressbar_free_widgets(p);
free(p);
}
static int pakfire_progressbar_draw(struct pakfire_progressbar* p) {
struct pakfire_progressbar_widget* widget;
+ int r;
+
+ // Acquire the lock
+ r = pthread_mutex_lock(&p->drawlock);
+ if (r)
+ return r;
// Update terminal size if not set
if (!terminal.cols)
// Flush everything
fflush(p->f);
+ // Release lock
+ pthread_mutex_unlock(&p->drawlock);
+
return 0;
}
PAKFIRE_EXPORT int pakfire_progressbar_create(
struct pakfire_progressbar** progressbar, FILE* f) {
+ int r;
+
+ // Allocate main object
struct pakfire_progressbar* p = calloc(1, sizeof(*p));
if (!p)
return ENOMEM;
// Store output
p->f = stdout;
+ // Acquire lock
+ r = pthread_mutex_init(&p->drawlock, NULL);
+ if (r)
+ goto ERROR;
+
// Register signal handler to update terminal size
signal(SIGWINCH, pakfire_progressbar_update_terminal_size);
};
// Create timer
- int r = timer_create(CLOCK_REALTIME, &sigevent, &p->timer);
+ r = timer_create(CLOCK_REALTIME, &sigevent, &p->timer);
if (r)
goto ERROR;