| 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 |
#include "dex-compat-private.h" |
| 24 |
#include "dex-future-private.h" |
| 25 |
#include "dex-scheduler.h" |
| 26 |
#include "dex-socket-wait-private.h" |
| 27 |
|
| 28 |
struct _DexSocketWait |
| 29 |
{ |
| 30 |
DexFuture parent_instance; |
| 31 |
GSource *source; |
| 32 |
}; |
| 33 |
|
| 34 |
typedef struct _DexSocketWaitClass |
| 35 |
{ |
| 36 |
DexFutureClass parent_class; |
| 37 |
} DexSocketWaitClass; |
| 38 |
|
| 39 |
DEX_DEFINE_FINAL_TYPE (DexSocketWait, dex_socket_wait, DEX_TYPE_FUTURE) |
| 40 |
|
| 41 |
static void |
| 42 |
dex_socket_wait_discard (DexFuture *future) |
| 43 |
{ |
| 44 |
DexSocketWait *self = DEX_SOCKET_WAIT (future); |
| 45 |
|
| 46 |
if (self->source != NULL) |
| 47 |
g_source_destroy (self->source); |
| 48 |
} |
| 49 |
|
| 50 |
static void |
| 51 |
dex_socket_wait_finalize (DexObject *object) |
| 52 |
{ |
| 53 |
DexSocketWait *self = DEX_SOCKET_WAIT (object); |
| 54 |
|
| 55 |
if (self->source != NULL) |
| 56 |
{ |
| 57 |
if (!g_source_is_destroyed (self->source)) |
| 58 |
{ |
| 59 |
g_critical ("`%s` destroyed while source was active. " |
| 60 |
"This is likely a bug as no future is holding a reference to %p", |
| 61 |
DEX_OBJECT_TYPE_NAME (object), object); |
| 62 |
g_source_destroy (self->source); |
| 63 |
} |
| 64 |
|
| 65 |
g_clear_pointer (&self->source, g_source_unref); |
| 66 |
} |
| 67 |
|
| 68 |
DEX_OBJECT_CLASS (dex_socket_wait_parent_class)->finalize (object); |
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
dex_socket_wait_class_init (DexSocketWaitClass *klass) |
| 73 |
{ |
| 74 |
DexObjectClass *object_class = DEX_OBJECT_CLASS (klass); |
| 75 |
DexFutureClass *future_class = DEX_FUTURE_CLASS (klass); |
| 76 |
|
| 77 |
object_class->finalize = dex_socket_wait_finalize; |
| 78 |
|
| 79 |
future_class->discard = dex_socket_wait_discard; |
| 80 |
} |
| 81 |
|
| 82 |
static void |
| 83 |
dex_socket_wait_init (DexSocketWait *self) |
| 84 |
{ |
| 85 |
} |
| 86 |
|
| 87 |
static void |
| 88 |
dex_socket_wait_clear_weak_ref (gpointer data) |
| 89 |
{ |
| 90 |
dex_weak_ref_clear (data); |
| 91 |
g_free (data); |
| 92 |
} |
| 93 |
|
| 94 |
static gboolean |
| 95 |
dex_socket_wait_source_func (GSocket *socket, |
| 96 |
GIOCondition condition, |
| 97 |
gpointer data) |
| 98 |
{ |
| 99 |
DexWeakRef *wr = data; |
| 100 |
DexSocketWait *self = dex_weak_ref_get (wr); |
| 101 |
|
| 102 |
g_assert (!self || DEX_IS_SOCKET_WAIT (self)); |
| 103 |
g_assert (!self || G_IS_SOCKET (socket)); |
| 104 |
|
| 105 |
if (self != NULL) |
| 106 |
{ |
| 107 |
GValue value = G_VALUE_INIT; |
| 108 |
|
| 109 |
g_value_init (&value, G_TYPE_IO_CONDITION); |
| 110 |
g_value_set_flags (&value, condition); |
| 111 |
dex_future_complete (DEX_FUTURE (self), &value, NULL); |
| 112 |
g_value_unset (&value); |
| 113 |
|
| 114 |
dex_object_lock (self); |
| 115 |
g_clear_pointer (&self->source, g_source_unref); |
| 116 |
dex_object_unlock (self); |
| 117 |
|
| 118 |
dex_unref (self); |
| 119 |
} |
| 120 |
|
| 121 |
return G_SOURCE_REMOVE; |
| 122 |
} |
| 123 |
|
| 124 |
DexFuture * |
| 125 |
dex_socket_wait_new (GSocket *socket, |
| 126 |
GIOCondition condition) |
| 127 |
{ |
| 128 |
static const char *name; |
| 129 |
DexScheduler *scheduler; |
| 130 |
DexSocketWait *self; |
| 131 |
DexWeakRef *wr; |
| 132 |
|
| 133 |
g_return_val_if_fail (G_IS_SOCKET (socket), NULL); |
| 134 |
g_return_val_if_fail (condition != 0, NULL); |
| 135 |
|
| 136 |
if G_UNLIKELY (name == NULL) |
| 137 |
name = g_intern_static_string ("[dex-socket-wait]"); |
| 138 |
|
| 139 |
self = (DexSocketWait *)dex_object_create_instance (DEX_TYPE_SOCKET_WAIT); |
| 140 |
|
| 141 |
wr = g_new0 (DexWeakRef, 1); |
| 142 |
dex_weak_ref_init (wr, self); |
| 143 |
|
| 144 |
self->source = g_socket_create_source (socket, condition, NULL); |
| 145 |
_g_source_set_static_name (self->source, name); |
| 146 |
g_source_set_priority (self->source, G_PRIORITY_DEFAULT); |
| 147 |
g_source_set_callback (self->source, |
| 148 |
(GSourceFunc)dex_socket_wait_source_func, |
| 149 |
wr, |
| 150 |
dex_socket_wait_clear_weak_ref); |
| 151 |
|
| 152 |
if (!(scheduler = dex_scheduler_get_thread_default ())) |
| 153 |
scheduler = dex_scheduler_get_default (); |
| 154 |
|
| 155 |
g_source_attach (self->source, dex_scheduler_get_main_context (scheduler)); |
| 156 |
|
| 157 |
return DEX_FUTURE (self); |
| 158 |
} |