| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#include "dex-compat-private.h" |
| 31 |
#include "dex-future-private.h" |
| 32 |
#include "dex-scheduler.h" |
| 33 |
#include "dex-watch-private.h" |
| 34 |
|
| 35 |
typedef struct _DexWatch |
| 36 |
{ |
| 37 |
DexFuture parent_instance; |
| 38 |
GSource *source; |
| 39 |
gpointer tag; |
| 40 |
} DexWatch; |
| 41 |
|
| 42 |
typedef struct _DexWatchClass |
| 43 |
{ |
| 44 |
DexFutureClass parent_class; |
| 45 |
} DexWatchClass; |
| 46 |
|
| 47 |
DEX_DEFINE_FINAL_TYPE (DexWatch, dex_watch, DEX_TYPE_FUTURE) |
| 48 |
|
| 49 |
static void |
| 50 |
dex_watch_discard (DexFuture *future) |
| 51 |
{ |
| 52 |
DexWatch *watch = DEX_WATCH (future); |
| 53 |
|
| 54 |
if (watch->source != NULL) |
| 55 |
g_source_destroy (watch->source); |
| 56 |
} |
| 57 |
|
| 58 |
static void |
| 59 |
dex_watch_finalize (DexObject *object) |
| 60 |
{ |
| 61 |
DexWatch *watch = DEX_WATCH (object); |
| 62 |
|
| 63 |
if (watch->source != NULL) |
| 64 |
{ |
| 65 |
if (!g_source_is_destroyed (watch->source)) |
| 66 |
{ |
| 67 |
g_critical ("`%s` destroyed while source was active. " |
| 68 |
"This is likely a bug as no future is holding a reference to %p", |
| 69 |
DEX_OBJECT_TYPE_NAME (object), object); |
| 70 |
g_source_destroy (watch->source); |
| 71 |
} |
| 72 |
|
| 73 |
g_clear_pointer (&watch->source, g_source_unref); |
| 74 |
} |
| 75 |
|
| 76 |
DEX_OBJECT_CLASS (dex_watch_parent_class)->finalize (object); |
| 77 |
} |
| 78 |
|
| 79 |
static void |
| 80 |
dex_watch_class_init (DexWatchClass *watch_class) |
| 81 |
{ |
| 82 |
DexObjectClass *object_class = DEX_OBJECT_CLASS (watch_class); |
| 83 |
DexFutureClass *future_class = DEX_FUTURE_CLASS (watch_class); |
| 84 |
|
| 85 |
object_class->finalize = dex_watch_finalize; |
| 86 |
|
| 87 |
future_class->discard = dex_watch_discard; |
| 88 |
} |
| 89 |
|
| 90 |
static void |
| 91 |
dex_watch_init (DexWatch *watch) |
| 92 |
{ |
| 93 |
} |
| 94 |
|
| 95 |
static void |
| 96 |
clear_weak_ref (gpointer data) |
| 97 |
{ |
| 98 |
dex_weak_ref_clear (data); |
| 99 |
g_free (data); |
| 100 |
} |
| 101 |
|
| 102 |
static gboolean |
| 103 |
dex_watch_source_func (gpointer data) |
| 104 |
{ |
| 105 |
DexWeakRef *wr = data; |
| 106 |
DexWatch *watch = dex_weak_ref_get (wr); |
| 107 |
|
| 108 |
g_assert (!watch || DEX_IS_WATCH (watch)); |
| 109 |
|
| 110 |
if (watch != NULL) |
| 111 |
{ |
| 112 |
GValue value = G_VALUE_INIT; |
| 113 |
int revents; |
| 114 |
|
| 115 |
dex_object_lock (watch); |
| 116 |
|
| 117 |
if (watch->source && watch->tag) |
| 118 |
revents = g_source_query_unix_fd (watch->source, watch->tag); |
| 119 |
else |
| 120 |
revents = 0; |
| 121 |
|
| 122 |
dex_object_unlock (watch); |
| 123 |
|
| 124 |
g_value_init (&value, G_TYPE_INT); |
| 125 |
g_value_set_int (&value, revents); |
| 126 |
dex_future_complete (DEX_FUTURE (watch), &value, NULL); |
| 127 |
g_value_unset (&value); |
| 128 |
|
| 129 |
dex_object_lock (watch); |
| 130 |
g_clear_pointer (&watch->source, g_source_unref); |
| 131 |
dex_object_unlock (watch); |
| 132 |
|
| 133 |
dex_unref (watch); |
| 134 |
} |
| 135 |
|
| 136 |
return G_SOURCE_REMOVE; |
| 137 |
} |
| 138 |
|
| 139 |
static gboolean |
| 140 |
dex_watch_source_dispatch (GSource *source, |
| 141 |
GSourceFunc callback, |
| 142 |
gpointer user_data) |
| 143 |
{ |
| 144 |
return callback (user_data); |
| 145 |
} |
| 146 |
|
| 147 |
static GSourceFuncs source_funcs = { |
| 148 |
.dispatch = dex_watch_source_dispatch, |
| 149 |
}; |
| 150 |
|
| 151 |
DexFuture * |
| 152 |
dex_watch_new (int fd, |
| 153 |
int events) |
| 154 |
{ |
| 155 |
static const char *name; |
| 156 |
DexScheduler *scheduler; |
| 157 |
DexWatch *watch; |
| 158 |
DexWeakRef *wr; |
| 159 |
|
| 160 |
if G_UNLIKELY (name == NULL) |
| 161 |
name = g_intern_static_string ("[dex-watch]"); |
| 162 |
|
| 163 |
watch = (DexWatch *)dex_object_create_instance (DEX_TYPE_WATCH); |
| 164 |
|
| 165 |
wr = g_new0 (DexWeakRef, 1); |
| 166 |
dex_weak_ref_init (wr, watch); |
| 167 |
|
| 168 |
watch->source = g_source_new (&source_funcs, sizeof (GSource)); |
| 169 |
watch->tag = g_source_add_unix_fd (watch->source, fd, events); |
| 170 |
_g_source_set_static_name (watch->source, name); |
| 171 |
g_source_set_priority (watch->source, G_PRIORITY_DEFAULT); |
| 172 |
g_source_set_callback (watch->source, |
| 173 |
dex_watch_source_func, |
| 174 |
wr, clear_weak_ref); |
| 175 |
|
| 176 |
if (!(scheduler = dex_scheduler_get_thread_default ())) |
| 177 |
scheduler = dex_scheduler_get_default (); |
| 178 |
|
| 179 |
g_source_attach (watch->source, dex_scheduler_get_main_context (scheduler)); |
| 180 |
|
| 181 |
return DEX_FUTURE (watch); |
| 182 |
} |