Replace mentions of "writer" threads with "worker" threads

This commit is contained in:
Petar Kapriš 2025-10-24 10:20:20 +02:00
parent b2249f6639
commit baead47f04

View file

@ -31,7 +31,7 @@ struct threadInfo {
/* /*
* Concurrency model explained: * Concurrency model explained:
* One main thread, along with many workers in a thread pool, who split the work of * One main thread, along with many workers in a thread pool, who split the work of
* rendering roughly equally. The writers don't do any work until the pixel map which they * rendering roughly equally. The workers don't do any work until the pixel map which they
* are meant to work on is marked as available, and they are all singalled to start * are meant to work on is marked as available, and they are all singalled to start
* drawing. * drawing.
* *
@ -84,7 +84,7 @@ void color_lookup(int *r, int *g, int *b, double mu);
// worker code // worker code
gboolean queue_redraw_plane(void *arg); gboolean queue_redraw_plane(void *arg);
void *writer_thread(void *arg) void *worker_thread(void *arg)
{ {
struct threadInfo *info = arg; struct threadInfo *info = arg;
while (true) { while (true) {
@ -121,7 +121,7 @@ void draw_mandelbrot(struct threadInfo *info)
pthread_mutex_unlock(&pixmapMutex); pthread_mutex_unlock(&pixmapMutex);
return; return;
// after this, the thread will again enter the // after this, the thread will again enter the
// loop in the writer_thread function, and // loop in the worker_thread function, and
// enter a wait state // enter a wait state
} }
pthread_mutex_unlock(&pixmapMutex); pthread_mutex_unlock(&pixmapMutex);
@ -138,13 +138,13 @@ void draw_mandelbrot(struct threadInfo *info)
} }
pthread_mutex_lock(&pixmapMutex); pthread_mutex_lock(&pixmapMutex);
info->complete = true; info->complete = true;
bool allWritersComplete = true; bool allWorkersComplete = true;
for (int32_t i = 0; i < thread_count; i++) { for (int32_t i = 0; i < thread_count; i++) {
if (!threads[i].complete) { if (!threads[i].complete) {
allWritersComplete = false; allWorkersComplete = false;
} }
} }
if (allWritersComplete) { if (allWorkersComplete) {
cairo_surface_mark_dirty(surface); cairo_surface_mark_dirty(surface);
g_main_context_invoke(NULL, queue_redraw_plane, NULL); g_main_context_invoke(NULL, queue_redraw_plane, NULL);
} }
@ -259,7 +259,7 @@ int main(int argc, char **argv)
// integer, if the thread fails to start, it will simply be // integer, if the thread fails to start, it will simply be
// overwritten by the same value in the next run of the loop // overwritten by the same value in the next run of the loop
pthread_t id; pthread_t id;
if (pthread_create(&id, NULL, &writer_thread, &threads[count])) { if (pthread_create(&id, NULL, &worker_thread, &threads[count])) {
pthread_detach(id); pthread_detach(id);
fprintf(stderr, "Failed to create thread %d\n", i); fprintf(stderr, "Failed to create thread %d\n", i);
continue; continue;
@ -335,15 +335,15 @@ static void app_activate(GApplication *app)
gboolean queue_redraw_plane(void *arg) gboolean queue_redraw_plane(void *arg)
{ {
(void) arg; (void) arg;
bool allWritersHadCompleted = true; bool allWorkersHadCompleted = true;
for (int32_t i = 0; i < thread_count; i++) { for (int32_t i = 0; i < thread_count; i++) {
if (!threads[i].complete) { if (!threads[i].complete) {
allWritersHadCompleted = false; allWorkersHadCompleted = false;
break; break;
} }
} }
if (!allWritersHadCompleted) { if (!allWorkersHadCompleted) {
return G_SOURCE_REMOVE; return G_SOURCE_REMOVE;
} }
@ -357,14 +357,14 @@ void blit_plane(GtkDrawingArea *da, cairo_t *cr, int width, int height, gpointer
(void) width; (void) width;
(void) height; (void) height;
(void) data; (void) data;
bool allWritersHadCompleted = true; bool allWorkersHadCompleted = true;
for (int32_t i = 0; i < thread_count; i++) { for (int32_t i = 0; i < thread_count; i++) {
if (!threads[i].complete) { if (!threads[i].complete) {
allWritersHadCompleted = false; allWorkersHadCompleted = false;
break; break;
} }
} }
if (allWritersHadCompleted) { if (allWorkersHadCompleted) {
cairo_set_source_surface(cr, surface, 0, 0); cairo_set_source_surface(cr, surface, 0, 0);
cairo_paint(cr); cairo_paint(cr);
} }
@ -382,27 +382,27 @@ void create_surface(GtkWidget *widget)
pixmapAvailable = false; // Mark drawing area as unavailable pixmapAvailable = false; // Mark drawing area as unavailable
pthread_mutex_unlock(&pixmapMutex); pthread_mutex_unlock(&pixmapMutex);
bool allWritersStopped; bool allWorkersStopped;
do { // Wait until all writing threads have been stopped do { // Wait until all writing threads have been stopped
allWritersStopped = true; allWorkersStopped = true;
for (int32_t i = 0; i < thread_count; i++) { for (int32_t i = 0; i < thread_count; i++) {
if (threads[i].drawing) { if (threads[i].drawing) {
allWritersStopped = false; allWorkersStopped = false;
break; break;
} }
} }
} while (!allWritersStopped); } while (!allWorkersStopped);
// If all drawers hadn't completed, we need to call // If all drawers hadn't completed, we need to call
// cairo_surface_mark_dirty in the main thread // cairo_surface_mark_dirty in the main thread
bool allWritersHadCompleted = true; bool allWorkersHadCompleted = true;
for (int32_t i = 0; i < thread_count; i++) { for (int32_t i = 0; i < thread_count; i++) {
if (!threads[i].complete) { if (!threads[i].complete) {
allWritersHadCompleted = false; allWorkersHadCompleted = false;
break; break;
} }
} }
if (!allWritersHadCompleted && surface != NULL) { if (!allWorkersHadCompleted && surface != NULL) {
cairo_surface_mark_dirty(surface); cairo_surface_mark_dirty(surface);
} }