| 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 <errno.h> |
| 24 |
#include <fcntl.h> |
| 25 |
|
| 26 |
#ifdef HAVE_UNISTD_H |
| 27 |
#include <unistd.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
#include "dex-compat-private.h" |
| 31 |
#include "dex-future-private.h" |
| 32 |
#include "dex-posix-aio-backend-private.h" |
| 33 |
#include "dex-posix-aio-future-private.h" |
| 34 |
|
| 35 |
#define N_IO_WORKERS 8 |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
struct _DexPosixAioBackend |
| 53 |
{ |
| 54 |
DexAioBackend parent_instance; |
| 55 |
}; |
| 56 |
|
| 57 |
struct _DexPosixAioBackendClass |
| 58 |
{ |
| 59 |
DexAioBackendClass parent_class; |
| 60 |
}; |
| 61 |
|
| 62 |
typedef struct _DexPosixAioContext |
| 63 |
{ |
| 64 |
DexAioContext parent; |
| 65 |
GMutex mutex; |
| 66 |
GQueue completed; |
| 67 |
} DexPosixAioContext; |
| 68 |
|
| 69 |
DEX_DEFINE_FINAL_TYPE (DexPosixAioBackend, dex_posix_aio_backend, DEX_TYPE_AIO_BACKEND) |
| 70 |
|
| 71 |
static struct { |
| 72 |
GThreadPool *thread_pool; |
| 73 |
GError *error; |
| 74 |
gsize initialized; |
| 75 |
} io_thread_pool; |
| 76 |
static DexAioBackend *fallback; |
| 77 |
static G_LOCK_DEFINE (fallback); |
| 78 |
|
| 79 |
static void |
| 80 |
dex_posix_aio_context_take (DexPosixAioContext *posix_aio_context, |
| 81 |
DexPosixAioFuture *posix_aio_future) |
| 82 |
{ |
| 83 |
GMainContext *main_context; |
| 84 |
|
| 85 |
g_assert (posix_aio_context != NULL); |
| 86 |
g_assert (DEX_IS_POSIX_AIO_FUTURE (posix_aio_future)); |
| 87 |
|
| 88 |
g_mutex_lock (&posix_aio_context->mutex); |
| 89 |
g_queue_push_tail (&posix_aio_context->completed, posix_aio_future); |
| 90 |
main_context = dex_posix_aio_future_get_main_context (posix_aio_future); |
| 91 |
g_mutex_unlock (&posix_aio_context->mutex); |
| 92 |
|
| 93 |
if (main_context) |
| 94 |
g_main_context_wakeup (main_context); |
| 95 |
} |
| 96 |
|
| 97 |
static void |
| 98 |
dex_posix_aio_backend_worker (gpointer data, |
| 99 |
gpointer user_data) |
| 100 |
{ |
| 101 |
DexPosixAioFuture *posix_aio_future = data; |
| 102 |
DexPosixAioContext *posix_aio_context; |
| 103 |
|
| 104 |
g_assert (DEX_IS_POSIX_AIO_FUTURE (posix_aio_future)); |
| 105 |
|
| 106 |
posix_aio_context = dex_posix_aio_future_get_aio_context (posix_aio_future); |
| 107 |
dex_posix_aio_future_run (posix_aio_future); |
| 108 |
dex_posix_aio_context_take (posix_aio_context, posix_aio_future); |
| 109 |
} |
| 110 |
|
| 111 |
static GThreadPool * |
| 112 |
dex_posix_aio_backend_get_thread_pool (GError **error) |
| 113 |
{ |
| 114 |
if (g_once_init_enter (&io_thread_pool.initialized)) |
| 115 |
{ |
| 116 |
io_thread_pool.thread_pool = g_thread_pool_new (dex_posix_aio_backend_worker, |
| 117 |
NULL, |
| 118 |
N_IO_WORKERS, |
| 119 |
FALSE, |
| 120 |
&io_thread_pool.error); |
| 121 |
|
| 122 |
g_once_init_leave (&io_thread_pool.initialized, TRUE); |
| 123 |
} |
| 124 |
|
| 125 |
if (io_thread_pool.thread_pool == NULL) |
| 126 |
{ |
| 127 |
if (io_thread_pool.error != NULL) |
| 128 |
g_propagate_error (error, g_error_copy (io_thread_pool.error)); |
| 129 |
else |
| 130 |
g_set_error_literal (error, |
| 131 |
G_THREAD_ERROR, |
| 132 |
G_THREAD_ERROR_AGAIN, |
| 133 |
"Failed to create thread pool"); |
| 134 |
} |
| 135 |
|
| 136 |
return io_thread_pool.thread_pool; |
| 137 |
} |
| 138 |
|
| 139 |
static DexFuture * |
| 140 |
dex_posix_aio_backend_enqueue (DexPosixAioFuture *posix_aio_future) |
| 141 |
{ |
| 142 |
GThreadPool *thread_pool; |
| 143 |
GError *error = NULL; |
| 144 |
|
| 145 |
g_assert (DEX_IS_POSIX_AIO_FUTURE (posix_aio_future)); |
| 146 |
|
| 147 |
if (!(thread_pool = dex_posix_aio_backend_get_thread_pool (&error))) |
| 148 |
{ |
| 149 |
dex_future_complete (DEX_FUTURE (posix_aio_future), NULL, g_steal_pointer (&error)); |
| 150 |
return DEX_FUTURE (posix_aio_future); |
| 151 |
} |
| 152 |
|
| 153 |
if (!g_thread_pool_push (thread_pool, dex_ref (posix_aio_future), &error)) |
| 154 |
{ |
| 155 |
dex_unref (posix_aio_future); |
| 156 |
dex_future_complete (DEX_FUTURE (posix_aio_future), NULL, g_steal_pointer (&error)); |
| 157 |
} |
| 158 |
|
| 159 |
return DEX_FUTURE (posix_aio_future); |
| 160 |
} |
| 161 |
|
| 162 |
static inline void |
| 163 |
steal_queue (GQueue *from_queue, |
| 164 |
GQueue *to_queue) |
| 165 |
{ |
| 166 |
*to_queue = *from_queue; |
| 167 |
*from_queue = (GQueue) {NULL, NULL, 0}; |
| 168 |
} |
| 169 |
|
| 170 |
static gboolean |
| 171 |
dex_posix_aio_context_dispatch (GSource *source, |
| 172 |
GSourceFunc callback, |
| 173 |
gpointer user_data) |
| 174 |
{ |
| 175 |
DexPosixAioContext *aio_context = (DexPosixAioContext *)source; |
| 176 |
GQueue completed; |
| 177 |
|
| 178 |
g_mutex_lock (&aio_context->mutex); |
| 179 |
steal_queue (&aio_context->completed, &completed); |
| 180 |
g_mutex_unlock (&aio_context->mutex); |
| 181 |
|
| 182 |
while (completed.length > 0) |
| 183 |
{ |
| 184 |
DexPosixAioFuture *posix_aio_future = g_queue_pop_head (&completed); |
| 185 |
dex_posix_aio_future_complete (posix_aio_future); |
| 186 |
dex_unref (posix_aio_future); |
| 187 |
} |
| 188 |
|
| 189 |
return G_SOURCE_CONTINUE; |
| 190 |
} |
| 191 |
|
| 192 |
static gboolean |
| 193 |
dex_posix_aio_context_check (GSource *source) |
| 194 |
{ |
| 195 |
DexPosixAioContext *aio_context = (DexPosixAioContext *)source; |
| 196 |
gboolean ret; |
| 197 |
|
| 198 |
g_assert (aio_context != NULL); |
| 199 |
g_assert (DEX_IS_POSIX_AIO_BACKEND (aio_context->parent.aio_backend)); |
| 200 |
|
| 201 |
g_mutex_lock (&aio_context->mutex); |
| 202 |
ret = aio_context->completed.length > 0; |
| 203 |
g_mutex_unlock (&aio_context->mutex); |
| 204 |
|
| 205 |
return ret; |
| 206 |
} |
| 207 |
|
| 208 |
static gboolean |
| 209 |
dex_posix_aio_context_prepare (GSource *source, |
| 210 |
int *timeout) |
| 211 |
{ |
| 212 |
*timeout = -1; |
| 213 |
return dex_posix_aio_context_check (source); |
| 214 |
} |
| 215 |
|
| 216 |
static void |
| 217 |
dex_posix_aio_context_finalize (GSource *source) |
| 218 |
{ |
| 219 |
DexPosixAioContext *aio_context = (DexPosixAioContext *)source; |
| 220 |
|
| 221 |
g_assert (aio_context != NULL); |
| 222 |
g_assert (DEX_IS_POSIX_AIO_BACKEND (aio_context->parent.aio_backend)); |
| 223 |
g_assert (aio_context->completed.length == 0); |
| 224 |
|
| 225 |
g_mutex_clear (&aio_context->mutex); |
| 226 |
} |
| 227 |
|
| 228 |
static GSourceFuncs dex_posix_aio_context_source_funcs = { |
| 229 |
.check = dex_posix_aio_context_check, |
| 230 |
.prepare = dex_posix_aio_context_prepare, |
| 231 |
.dispatch = dex_posix_aio_context_dispatch, |
| 232 |
.finalize = dex_posix_aio_context_finalize, |
| 233 |
}; |
| 234 |
|
| 235 |
static DexAioContext * |
| 236 |
dex_posix_aio_backend_create_context (DexAioBackend *aio_backend) |
| 237 |
{ |
| 238 |
DexPosixAioContext *aio_context; |
| 239 |
|
| 240 |
g_assert (DEX_IS_POSIX_AIO_BACKEND (aio_backend)); |
| 241 |
|
| 242 |
aio_context = (DexPosixAioContext *) |
| 243 |
g_source_new (&dex_posix_aio_context_source_funcs, |
| 244 |
sizeof *aio_context); |
| 245 |
_g_source_set_static_name ((GSource *)aio_context, "[dex-posix-aio-backend]"); |
| 246 |
g_source_set_can_recurse ((GSource *)aio_context, TRUE); |
| 247 |
aio_context->parent.aio_backend = dex_ref (aio_backend); |
| 248 |
g_mutex_init (&aio_context->mutex); |
| 249 |
|
| 250 |
return (DexAioContext *)aio_context; |
| 251 |
} |
| 252 |
|
| 253 |
static DexFuture * |
| 254 |
dex_posix_aio_backend_close (DexAioBackend *aio_backend, |
| 255 |
DexAioContext *aio_context, |
| 256 |
int fd) |
| 257 |
{ |
| 258 |
return dex_posix_aio_backend_enqueue (dex_posix_aio_future_new_close ((DexPosixAioContext *)aio_context, fd)); |
| 259 |
} |
| 260 |
|
| 261 |
static DexFuture * |
| 262 |
dex_posix_aio_backend_open (DexAioBackend *aio_backend, |
| 263 |
DexAioContext *aio_context, |
| 264 |
const char *path, |
| 265 |
int flags, |
| 266 |
int mode) |
| 267 |
{ |
| 268 |
return dex_posix_aio_backend_enqueue (dex_posix_aio_future_new_open ((DexPosixAioContext *)aio_context, path, flags, mode)); |
| 269 |
} |
| 270 |
|
| 271 |
static DexFuture * |
| 272 |
dex_posix_aio_backend_read (DexAioBackend *aio_backend, |
| 273 |
DexAioContext *aio_context, |
| 274 |
int fd, |
| 275 |
gpointer buffer, |
| 276 |
gsize count, |
| 277 |
goffset offset) |
| 278 |
{ |
| 279 |
return dex_posix_aio_backend_enqueue (dex_posix_aio_future_new_read ((DexPosixAioContext *)aio_context, fd, buffer, count, offset)); |
| 280 |
} |
| 281 |
|
| 282 |
static DexFuture * |
| 283 |
dex_posix_aio_backend_write (DexAioBackend *aio_backend, |
| 284 |
DexAioContext *aio_context, |
| 285 |
int fd, |
| 286 |
gconstpointer buffer, |
| 287 |
gsize count, |
| 288 |
goffset offset) |
| 289 |
{ |
| 290 |
return dex_posix_aio_backend_enqueue (dex_posix_aio_future_new_write ((DexPosixAioContext *)aio_context, fd, buffer, count, offset)); |
| 291 |
} |
| 292 |
|
| 293 |
static void |
| 294 |
dex_posix_aio_backend_class_init (DexPosixAioBackendClass *posix_aio_backend_class) |
| 295 |
{ |
| 296 |
DexAioBackendClass *aio_backend_class = DEX_AIO_BACKEND_CLASS (posix_aio_backend_class); |
| 297 |
|
| 298 |
aio_backend_class->create_context = dex_posix_aio_backend_create_context; |
| 299 |
aio_backend_class->close = dex_posix_aio_backend_close; |
| 300 |
aio_backend_class->open = dex_posix_aio_backend_open; |
| 301 |
aio_backend_class->read = dex_posix_aio_backend_read; |
| 302 |
aio_backend_class->write = dex_posix_aio_backend_write; |
| 303 |
|
| 304 |
g_type_ensure (DEX_TYPE_POSIX_AIO_FUTURE); |
| 305 |
} |
| 306 |
|
| 307 |
static void |
| 308 |
dex_posix_aio_backend_init (DexPosixAioBackend *posix_aio_backend) |
| 309 |
{ |
| 310 |
} |
| 311 |
|
| 312 |
DexAioBackend * |
| 313 |
dex_posix_aio_backend_new (void) |
| 314 |
{ |
| 315 |
return dex_ref (dex_posix_aio_backend_get_fallback ()); |
| 316 |
} |
| 317 |
|
| 318 |
DexAioBackend * |
| 319 |
dex_posix_aio_backend_get_fallback (void) |
| 320 |
{ |
| 321 |
G_LOCK (fallback); |
| 322 |
if (fallback == NULL) |
| 323 |
fallback = (DexAioBackend *)dex_object_create_instance (DEX_TYPE_POSIX_AIO_BACKEND); |
| 324 |
G_UNLOCK (fallback); |
| 325 |
|
| 326 |
return fallback; |
| 327 |
} |