pull: Rework threading communication model

Previously, I've observed bugs where we either:
1) Exit too early, leaving undownloaded objects
2) Hang while downloading

This rewrite hopefully fixes both.
This commit is contained in:
Colin Walters committed 2013-04-01 18:56:38 -04:00
1 parent a268b53dc9
commit bac4d7a0d2
6 files changed
+499 -384

No files matched your search

+120
View File
@@ -0,0 +1,120 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2012 Colin Walters <walters@verbum.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Colin Walters <walters@verbum.org>
*/
#include "config.h"
#include "otutil.h"
#include <glib-unix.h>
#include <string.h>
#include <sys/eventfd.h>
#include <errno.h>
#include <unistd.h>
struct OtWaitableQueue {
volatile gint refcount;
GMutex mutex;
int fd;
gboolean read_empty;
GQueue queue;
};
OtWaitableQueue *
ot_waitable_queue_new (void)
{
OtWaitableQueue *queue = g_new0 (OtWaitableQueue, 1);
queue->refcount = 1;
g_mutex_init (&queue->mutex);
g_queue_init (&queue->queue);
queue->fd = eventfd (0, EFD_CLOEXEC | EFD_NONBLOCK);
g_assert (queue->fd >= 0);
return queue;
}
void
ot_waitable_queue_push (OtWaitableQueue *queue,
gpointer data)
{
const guint64 val = 1;
int rval;
g_mutex_lock (&queue->mutex);
g_queue_push_head (&queue->queue, data);
do
rval = write (queue->fd, &val, sizeof (val));
while (G_UNLIKELY (rval == -1 && errno == EINTR));
queue->read_empty = FALSE;
g_mutex_unlock (&queue->mutex);
}
gboolean
ot_waitable_queue_pop (OtWaitableQueue *queue,
gpointer *out_data)
{
gpointer ret = NULL;
gboolean empty = TRUE;
int rval;
guint64 val;
g_mutex_lock (&queue->mutex);
if (g_queue_peek_tail_link (&queue->queue) != NULL)
{
ret = g_queue_pop_tail (&queue->queue);
empty = FALSE;
}
else if (!queue->read_empty)
{
do
rval = read (queue->fd, &val, sizeof (val));
while (G_UNLIKELY (rval == -1 && errno == EINTR));
queue->read_empty = TRUE;
}
g_mutex_unlock (&queue->mutex);
*out_data = ret;
return !empty;
}
void
ot_waitable_queue_ref (OtWaitableQueue *queue)
{
g_atomic_int_inc (&queue->refcount);
}
void
ot_waitable_queue_unref (OtWaitableQueue *queue)
{
if (!g_atomic_int_dec_and_test (&queue->refcount))
return;
g_mutex_clear (&queue->mutex);
g_queue_clear (&queue->queue);
(void) close (queue->fd);
g_free (queue);
}
GSource *
ot_waitable_queue_create_source (OtWaitableQueue *queue)
{
return g_unix_fd_source_new (queue->fd, G_IO_IN);
}
@@ -20,30 +20,27 @@
* Author: Colin Walters <walters@verbum.org>
*/
#ifndef __OSTREE_WORKER_QUEUE_H__
#define __OSTREE_WORKER_QUEUE_H__
#ifndef __OSTREE_WAITABLE_QUEUE_H__
#define __OSTREE_WAITABLE_QUEUE_H__
#include <gio/gio.h>
G_BEGIN_DECLS
typedef struct OtWorkerQueue OtWorkerQueue;
typedef struct OtWaitableQueue OtWaitableQueue;
typedef void (*OtWorkerQueueFunc) (gpointer data,
gpointer user_data);
OtWaitableQueue *ot_waitable_queue_new (void);
OtWorkerQueue *ot_worker_queue_new (const char *thread_name,
OtWorkerQueueFunc func,
gpointer data);
void ot_waitable_queue_push (OtWaitableQueue *queue,
gpointer data);
void ot_worker_queue_start (OtWorkerQueue *queue);
GSource *ot_waitable_queue_create_source (OtWaitableQueue *queue);
gboolean ot_worker_queue_is_idle (OtWorkerQueue *queue);
gboolean ot_waitable_queue_pop (OtWaitableQueue *queue,
gpointer *out_val);
void ot_worker_queue_push (OtWorkerQueue *queue,
gpointer data);
void ot_worker_queue_unref (OtWorkerQueue *queue);
void ot_waitable_queue_ref (OtWaitableQueue *queue);
void ot_waitable_queue_unref (OtWaitableQueue *queue);
G_END_DECLS
-140
View File
@@ -1,140 +0,0 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2012 Colin Walters <walters@verbum.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Colin Walters <walters@verbum.org>
*/
#include "config.h"
#include "otutil.h"
#include <string.h>
struct OtWorkerQueue {
GMutex mutex;
GCond cond;
GQueue queue;
char *thread_name;
gboolean complete;
gboolean is_idle;
gboolean destroyed;
GThread *worker;
OtWorkerQueueFunc work_func;
OtWorkerQueueFunc work_data;
};
static gpointer
ot_worker_queue_thread_main (gpointer user_data);
OtWorkerQueue *
ot_worker_queue_new (const char *thread_name,
OtWorkerQueueFunc func,
gpointer data)
{
OtWorkerQueue *queue = g_new0 (OtWorkerQueue, 1);
g_mutex_init (&queue->mutex);
g_cond_init (&queue->cond);
g_queue_init (&queue->queue);
queue->is_idle = TRUE;
queue->thread_name = g_strdup (thread_name);
queue->work_func = func;
queue->work_data = data;
return queue;
}
void
ot_worker_queue_start (OtWorkerQueue *queue)
{
queue->worker = g_thread_new (queue->thread_name, ot_worker_queue_thread_main, queue);
}
void
ot_worker_queue_push (OtWorkerQueue *queue,
gpointer data)
{
g_mutex_lock (&queue->mutex);
g_queue_push_head (&queue->queue, data);
queue->is_idle = FALSE;
g_cond_signal (&queue->cond);
g_mutex_unlock (&queue->mutex);
}
static gpointer
ot_worker_queue_thread_main (gpointer user_data)
{
OtWorkerQueue *queue = user_data;
while (TRUE)
{
gpointer item;
g_mutex_lock (&queue->mutex);
while (!g_queue_peek_tail_link (&queue->queue))
{
queue->is_idle = TRUE;
g_cond_wait (&queue->cond, &queue->mutex);
}
item = g_queue_pop_tail (&queue->queue);
g_mutex_unlock (&queue->mutex);
if (!item)
break;
queue->work_func (item, queue->work_data);
}
return NULL;
}
gboolean
ot_worker_queue_is_idle (OtWorkerQueue *queue)
{
gboolean ret;
g_mutex_lock (&queue->mutex);
ret = queue->is_idle;
g_mutex_unlock (&queue->mutex);
return ret;
}
void
ot_worker_queue_unref (OtWorkerQueue *queue)
{
if (queue->worker)
{
ot_worker_queue_push (queue, NULL);
g_thread_join (queue->worker);
}
g_free (queue->thread_name);
g_mutex_clear (&queue->mutex);
g_cond_clear (&queue->cond);
g_queue_clear (&queue->queue);
g_free (queue);
}
+1 -1
View File
@@ -37,7 +37,7 @@
} \
} G_STMT_END;
#include <ot-worker-queue.h>
#include <ot-waitable-queue.h>
#include <ot-local-alloc.h>
#include <ot-keyfile-utils.h>
#include <ot-gio-utils.h>