| 1 |
/* mapped-ring-buffer-source.c |
| 2 |
* |
| 3 |
* Copyright 2020 Christian Hergert <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <glib.h> |
| 24 |
|
| 25 |
#include "mapped-ring-buffer-source-private.h" |
| 26 |
|
| 27 |
typedef struct _MappedRingSource |
| 28 |
{ |
| 29 |
GSource source; |
| 30 |
MappedRingBuffer *buffer; |
| 31 |
MappedRingBufferCallback callback; |
| 32 |
gpointer callback_data; |
| 33 |
GDestroyNotify callback_data_destroy; |
| 34 |
} MappedRingSource; |
| 35 |
|
| 36 |
static gboolean |
| 37 |
mapped_ring_source_dispatch (GSource *source, |
| 38 |
GSourceFunc callback, |
| 39 |
gpointer user_data) |
| 40 |
{ |
| 41 |
MappedRingSource *real_source = (MappedRingSource *)source; |
| 42 |
|
| 43 |
g_assert (source != NULL); |
| 44 |
|
| 45 |
return mapped_ring_buffer_drain (real_source->buffer, real_source->callback, real_source->callback_data); |
| 46 |
} |
| 47 |
|
| 48 |
static void |
| 49 |
mapped_ring_source_finalize (GSource *source) |
| 50 |
{ |
| 51 |
MappedRingSource *real_source = (MappedRingSource *)source; |
| 52 |
|
| 53 |
if (real_source != NULL) |
| 54 |
{ |
| 55 |
mapped_ring_buffer_drain (real_source->buffer, real_source->callback, real_source->callback_data); |
| 56 |
|
| 57 |
if (real_source->callback_data_destroy) |
| 58 |
real_source->callback_data_destroy (real_source->callback_data); |
| 59 |
|
| 60 |
real_source->callback = NULL; |
| 61 |
real_source->callback_data = NULL; |
| 62 |
real_source->callback_data_destroy = NULL; |
| 63 |
|
| 64 |
g_clear_pointer (&real_source->buffer, mapped_ring_buffer_unref); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
static gboolean |
| 69 |
mapped_ring_source_check (GSource *source) |
| 70 |
{ |
| 71 |
MappedRingSource *real_source = (MappedRingSource *)source; |
| 72 |
|
| 73 |
g_assert (real_source != NULL); |
| 74 |
g_assert (real_source->buffer != NULL); |
| 75 |
|
| 76 |
return !mapped_ring_buffer_is_empty (real_source->buffer); |
| 77 |
} |
| 78 |
|
| 79 |
static gboolean |
| 80 |
mapped_ring_source_prepare (GSource *source, |
| 81 |
gint *timeout_) |
| 82 |
{ |
| 83 |
MappedRingSource *real_source = (MappedRingSource *)source; |
| 84 |
|
| 85 |
g_assert (real_source != NULL); |
| 86 |
g_assert (real_source->buffer != NULL); |
| 87 |
|
| 88 |
if (!mapped_ring_buffer_is_empty (real_source->buffer)) |
| 89 |
return TRUE; |
| 90 |
|
| 91 |
*timeout_ = 5; |
| 92 |
|
| 93 |
return FALSE; |
| 94 |
} |
| 95 |
|
| 96 |
static GSourceFuncs mapped_ring_source_funcs = { |
| 97 |
.prepare = mapped_ring_source_prepare, |
| 98 |
.check = mapped_ring_source_check, |
| 99 |
.dispatch = mapped_ring_source_dispatch, |
| 100 |
.finalize = mapped_ring_source_finalize, |
| 101 |
}; |
| 102 |
|
| 103 |
guint |
| 104 |
mapped_ring_buffer_create_source_full (int priority, |
| 105 |
MappedRingBuffer *self, |
| 106 |
MappedRingBufferCallback source_func, |
| 107 |
gpointer user_data, |
| 108 |
GDestroyNotify destroy) |
| 109 |
{ |
| 110 |
MappedRingSource *source; |
| 111 |
guint ret; |
| 112 |
|
| 113 |
g_return_val_if_fail (self != NULL, 0); |
| 114 |
g_return_val_if_fail (source_func != NULL, 0); |
| 115 |
|
| 116 |
source = (MappedRingSource *)g_source_new (&mapped_ring_source_funcs, sizeof (MappedRingSource)); |
| 117 |
source->buffer = mapped_ring_buffer_ref (self); |
| 118 |
source->callback = source_func; |
| 119 |
source->callback_data = user_data; |
| 120 |
source->callback_data_destroy = destroy; |
| 121 |
g_source_set_static_name ((GSource *)source, "MappedRingSource"); |
| 122 |
g_source_set_priority ((GSource *)source, priority); |
| 123 |
ret = g_source_attach ((GSource *)source, g_main_context_default ()); |
| 124 |
g_source_unref ((GSource *)source); |
| 125 |
|
| 126 |
return ret; |
| 127 |
} |
| 128 |
|
| 129 |
guint |
| 130 |
mapped_ring_buffer_create_source (MappedRingBuffer *self, |
| 131 |
MappedRingBufferCallback source_func, |
| 132 |
gpointer user_data) |
| 133 |
{ |
| 134 |
return mapped_ring_buffer_create_source_full (G_PRIORITY_DEFAULT, self, source_func, user_data, NULL); |
| 135 |
} |