| 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 <gio/gio.h> |
| 31 |
|
| 32 |
#include "dex-fd-private.h" |
| 33 |
#include "dex-future-private.h" |
| 34 |
#include "dex-posix-aio-backend-private.h" |
| 35 |
#include "dex-posix-aio-future-private.h" |
| 36 |
|
| 37 |
typedef enum _DexPosixAioFutureKind |
| 38 |
{ |
| 39 |
DEX_POSIX_AIO_FUTURE_CLOSE = 1, |
| 40 |
DEX_POSIX_AIO_FUTURE_OPEN, |
| 41 |
DEX_POSIX_AIO_FUTURE_READ, |
| 42 |
DEX_POSIX_AIO_FUTURE_WRITE, |
| 43 |
} DexPosixAioFutureKind; |
| 44 |
|
| 45 |
struct _DexPosixAioFuture |
| 46 |
{ |
| 47 |
DexFuture parent_instance; |
| 48 |
GMainContext *main_context; |
| 49 |
DexPosixAioContext *aio_context; |
| 50 |
DexPosixAioFutureKind kind; |
| 51 |
int errsv; |
| 52 |
union { |
| 53 |
struct { |
| 54 |
DexFD *fd; |
| 55 |
int res; |
| 56 |
} close; |
| 57 |
struct { |
| 58 |
char *path; |
| 59 |
int flags; |
| 60 |
int mode; |
| 61 |
int res; |
| 62 |
} open; |
| 63 |
struct { |
| 64 |
int fd; |
| 65 |
gpointer buffer; |
| 66 |
gsize count; |
| 67 |
goffset offset; |
| 68 |
gssize res; |
| 69 |
} read; |
| 70 |
struct { |
| 71 |
int fd; |
| 72 |
gconstpointer buffer; |
| 73 |
gsize count; |
| 74 |
goffset offset; |
| 75 |
gssize res; |
| 76 |
} write; |
| 77 |
}; |
| 78 |
}; |
| 79 |
|
| 80 |
typedef struct _DexPosixAioFutureClass |
| 81 |
{ |
| 82 |
DexFutureClass parent_class; |
| 83 |
} DexPosixAioFutureClass; |
| 84 |
|
| 85 |
DEX_DEFINE_FINAL_TYPE (DexPosixAioFuture, dex_posix_aio_future, DEX_TYPE_FUTURE) |
| 86 |
|
| 87 |
#undef DEX_TYPE_POSIX_AIO_FUTURE |
| 88 |
#define DEX_TYPE_POSIX_AIO_FUTURE dex_posix_aio_future_type |
| 89 |
|
| 90 |
static void |
| 91 |
dex_posix_aio_future_finalize (DexObject *object) |
| 92 |
{ |
| 93 |
DexPosixAioFuture *posix_aio_future = DEX_POSIX_AIO_FUTURE (object); |
| 94 |
|
| 95 |
if (posix_aio_future->kind == DEX_POSIX_AIO_FUTURE_CLOSE) |
| 96 |
g_clear_pointer (&posix_aio_future->close.fd, dex_fd_free); |
| 97 |
else if (posix_aio_future->kind == DEX_POSIX_AIO_FUTURE_OPEN) |
| 98 |
g_clear_pointer (&posix_aio_future->open.path, g_free); |
| 99 |
|
| 100 |
g_clear_pointer ((GSource **)&posix_aio_future->aio_context, g_source_unref); |
| 101 |
g_clear_pointer (&posix_aio_future->main_context, g_main_context_unref); |
| 102 |
|
| 103 |
DEX_OBJECT_CLASS (dex_posix_aio_future_parent_class)->finalize (object); |
| 104 |
} |
| 105 |
|
| 106 |
static void |
| 107 |
dex_posix_aio_future_class_init (DexPosixAioFutureClass *posix_aio_future_class) |
| 108 |
{ |
| 109 |
DexObjectClass *object_class = DEX_OBJECT_CLASS (posix_aio_future_class); |
| 110 |
|
| 111 |
object_class->finalize = dex_posix_aio_future_finalize; |
| 112 |
} |
| 113 |
|
| 114 |
static void |
| 115 |
dex_posix_aio_future_init (DexPosixAioFuture *posix_aio_future) |
| 116 |
{ |
| 117 |
} |
| 118 |
|
| 119 |
static DexPosixAioFuture * |
| 120 |
dex_posix_aio_future_new (DexPosixAioFutureKind kind, |
| 121 |
DexPosixAioContext *aio_context) |
| 122 |
{ |
| 123 |
DexPosixAioFuture *posix_aio_future; |
| 124 |
GMainContext *main_context; |
| 125 |
|
| 126 |
if ((main_context = g_source_get_context ((GSource *)aio_context))) |
| 127 |
g_main_context_ref (main_context); |
| 128 |
|
| 129 |
posix_aio_future = (DexPosixAioFuture *)dex_object_create_instance (DEX_TYPE_POSIX_AIO_FUTURE); |
| 130 |
posix_aio_future->kind = kind; |
| 131 |
posix_aio_future->aio_context = (DexPosixAioContext *)g_source_ref ((GSource *)aio_context); |
| 132 |
posix_aio_future->main_context = main_context; |
| 133 |
|
| 134 |
return posix_aio_future; |
| 135 |
} |
| 136 |
|
| 137 |
DexPosixAioFuture * |
| 138 |
dex_posix_aio_future_new_close (DexPosixAioContext *posix_aio_context, |
| 139 |
int fd) |
| 140 |
{ |
| 141 |
DexPosixAioFuture *posix_aio_future; |
| 142 |
|
| 143 |
posix_aio_future = dex_posix_aio_future_new (DEX_POSIX_AIO_FUTURE_CLOSE, posix_aio_context); |
| 144 |
posix_aio_future->close.fd = g_memdup2 (&fd, sizeof fd); |
| 145 |
posix_aio_future->close.res = -1; |
| 146 |
|
| 147 |
return posix_aio_future; |
| 148 |
} |
| 149 |
|
| 150 |
DexPosixAioFuture * |
| 151 |
dex_posix_aio_future_new_open (DexPosixAioContext *posix_aio_context, |
| 152 |
const char *path, |
| 153 |
int flags, |
| 154 |
int mode) |
| 155 |
{ |
| 156 |
DexPosixAioFuture *posix_aio_future; |
| 157 |
|
| 158 |
posix_aio_future = dex_posix_aio_future_new (DEX_POSIX_AIO_FUTURE_OPEN, posix_aio_context); |
| 159 |
posix_aio_future->open.path = g_strdup (path); |
| 160 |
posix_aio_future->open.flags = flags; |
| 161 |
posix_aio_future->open.mode = mode; |
| 162 |
posix_aio_future->open.res = -1; |
| 163 |
|
| 164 |
return posix_aio_future; |
| 165 |
} |
| 166 |
|
| 167 |
DexPosixAioFuture * |
| 168 |
dex_posix_aio_future_new_read (DexPosixAioContext *posix_aio_context, |
| 169 |
int fd, |
| 170 |
gpointer buffer, |
| 171 |
gsize count, |
| 172 |
goffset offset) |
| 173 |
{ |
| 174 |
DexPosixAioFuture *posix_aio_future; |
| 175 |
|
| 176 |
posix_aio_future = dex_posix_aio_future_new (DEX_POSIX_AIO_FUTURE_READ, posix_aio_context); |
| 177 |
posix_aio_future->read.fd = fd; |
| 178 |
posix_aio_future->read.buffer = buffer; |
| 179 |
posix_aio_future->read.count = count; |
| 180 |
posix_aio_future->read.offset = offset; |
| 181 |
posix_aio_future->read.res = -1; |
| 182 |
|
| 183 |
return posix_aio_future; |
| 184 |
} |
| 185 |
|
| 186 |
DexPosixAioFuture * |
| 187 |
dex_posix_aio_future_new_write (DexPosixAioContext *posix_aio_context, |
| 188 |
int fd, |
| 189 |
gconstpointer buffer, |
| 190 |
gsize count, |
| 191 |
goffset offset) |
| 192 |
{ |
| 193 |
DexPosixAioFuture *posix_aio_future; |
| 194 |
|
| 195 |
posix_aio_future = dex_posix_aio_future_new (DEX_POSIX_AIO_FUTURE_WRITE, posix_aio_context); |
| 196 |
posix_aio_future->write.fd = fd; |
| 197 |
posix_aio_future->write.buffer = buffer; |
| 198 |
posix_aio_future->write.count = count; |
| 199 |
posix_aio_future->write.offset = offset; |
| 200 |
posix_aio_future->write.res = -1; |
| 201 |
|
| 202 |
return posix_aio_future; |
| 203 |
} |
| 204 |
|
| 205 |
void |
| 206 |
dex_posix_aio_future_run (DexPosixAioFuture *posix_aio_future) |
| 207 |
{ |
| 208 |
g_return_if_fail (DEX_IS_POSIX_AIO_FUTURE (posix_aio_future)); |
| 209 |
|
| 210 |
errno = 0; |
| 211 |
|
| 212 |
switch (posix_aio_future->kind) |
| 213 |
{ |
| 214 |
case DEX_POSIX_AIO_FUTURE_CLOSE: |
| 215 |
posix_aio_future->close.res = |
| 216 |
close (dex_fd_steal (posix_aio_future->close.fd)); |
| 217 |
|
| 218 |
|
| 219 |
(void)posix_aio_future->close.res; |
| 220 |
break; |
| 221 |
|
| 222 |
case DEX_POSIX_AIO_FUTURE_OPEN: |
| 223 |
posix_aio_future->open.res = |
| 224 |
open (posix_aio_future->open.path, |
| 225 |
posix_aio_future->open.flags, |
| 226 |
posix_aio_future->open.mode); |
| 227 |
|
| 228 |
|
| 229 |
(void)posix_aio_future->open.res; |
| 230 |
break; |
| 231 |
|
| 232 |
case DEX_POSIX_AIO_FUTURE_READ: |
| 233 |
if (posix_aio_future->read.offset >= 0) |
| 234 |
posix_aio_future->read.res = |
| 235 |
pread (posix_aio_future->read.fd, |
| 236 |
posix_aio_future->read.buffer, |
| 237 |
posix_aio_future->read.count, |
| 238 |
posix_aio_future->read.offset); |
| 239 |
else |
| 240 |
posix_aio_future->read.res = |
| 241 |
read (posix_aio_future->read.fd, |
| 242 |
posix_aio_future->read.buffer, |
| 243 |
posix_aio_future->read.count); |
| 244 |
|
| 245 |
|
| 246 |
(void)posix_aio_future->read.res; |
| 247 |
break; |
| 248 |
|
| 249 |
case DEX_POSIX_AIO_FUTURE_WRITE: |
| 250 |
if (posix_aio_future->write.offset >= 0) |
| 251 |
posix_aio_future->write.res = |
| 252 |
pwrite (posix_aio_future->write.fd, |
| 253 |
posix_aio_future->write.buffer, |
| 254 |
posix_aio_future->write.count, |
| 255 |
posix_aio_future->write.offset); |
| 256 |
else |
| 257 |
posix_aio_future->write.res = |
| 258 |
write (posix_aio_future->write.fd, |
| 259 |
posix_aio_future->write.buffer, |
| 260 |
posix_aio_future->write.count); |
| 261 |
|
| 262 |
|
| 263 |
(void)posix_aio_future->write.res; |
| 264 |
break; |
| 265 |
|
| 266 |
default: |
| 267 |
g_assert_not_reached (); |
| 268 |
} |
| 269 |
|
| 270 |
posix_aio_future->errsv = errno; |
| 271 |
} |
| 272 |
|
| 273 |
static void |
| 274 |
dex_posix_aio_future_complete_boolean (DexPosixAioFuture *posix_aio_future, |
| 275 |
int res) |
| 276 |
{ |
| 277 |
g_assert (DEX_IS_POSIX_AIO_FUTURE (posix_aio_future)); |
| 278 |
|
| 279 |
if (res < 0) |
| 280 |
dex_future_complete (DEX_FUTURE (posix_aio_future), |
| 281 |
NULL, |
| 282 |
g_error_new_literal (G_IO_ERROR, |
| 283 |
g_io_error_from_errno (posix_aio_future->errsv), |
| 284 |
g_strerror (posix_aio_future->errsv))); |
| 285 |
else |
| 286 |
dex_future_complete (DEX_FUTURE (posix_aio_future), |
| 287 |
&(GValue) { G_TYPE_BOOLEAN, {{.v_int = TRUE}}}, |
| 288 |
NULL); |
| 289 |
} |
| 290 |
|
| 291 |
static void |
| 292 |
dex_posix_aio_future_complete_fd (DexPosixAioFuture *posix_aio_future, |
| 293 |
int res) |
| 294 |
{ |
| 295 |
g_assert (DEX_IS_POSIX_AIO_FUTURE (posix_aio_future)); |
| 296 |
|
| 297 |
if (res < 0) |
| 298 |
dex_future_complete (DEX_FUTURE (posix_aio_future), |
| 299 |
NULL, |
| 300 |
g_error_new_literal (G_IO_ERROR, |
| 301 |
g_io_error_from_errno (posix_aio_future->errsv), |
| 302 |
g_strerror (posix_aio_future->errsv))); |
| 303 |
else |
| 304 |
{ |
| 305 |
GValue value = G_VALUE_INIT; |
| 306 |
|
| 307 |
g_value_init (&value, DEX_TYPE_FD); |
| 308 |
g_value_take_boxed (&value, g_memdup2 (&res, sizeof res)); |
| 309 |
dex_future_complete_steal (DEX_FUTURE (posix_aio_future), &value, NULL); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
static void |
| 314 |
dex_posix_aio_future_complete_int64 (DexPosixAioFuture *posix_aio_future, |
| 315 |
gint64 res) |
| 316 |
{ |
| 317 |
g_assert (DEX_IS_POSIX_AIO_FUTURE (posix_aio_future)); |
| 318 |
|
| 319 |
if (res < 0) |
| 320 |
dex_future_complete (DEX_FUTURE (posix_aio_future), |
| 321 |
NULL, |
| 322 |
g_error_new_literal (G_IO_ERROR, |
| 323 |
g_io_error_from_errno (posix_aio_future->errsv), |
| 324 |
g_strerror (posix_aio_future->errsv))); |
| 325 |
else |
| 326 |
dex_future_complete (DEX_FUTURE (posix_aio_future), |
| 327 |
&(GValue) { G_TYPE_INT64, {{.v_int64 = res}}}, |
| 328 |
NULL); |
| 329 |
} |
| 330 |
|
| 331 |
void |
| 332 |
dex_posix_aio_future_complete (DexPosixAioFuture *posix_aio_future) |
| 333 |
{ |
| 334 |
g_return_if_fail (DEX_IS_POSIX_AIO_FUTURE (posix_aio_future)); |
| 335 |
|
| 336 |
switch (posix_aio_future->kind) |
| 337 |
{ |
| 338 |
case DEX_POSIX_AIO_FUTURE_CLOSE: |
| 339 |
dex_posix_aio_future_complete_boolean (posix_aio_future, |
| 340 |
posix_aio_future->close.res); |
| 341 |
break; |
| 342 |
|
| 343 |
case DEX_POSIX_AIO_FUTURE_OPEN: |
| 344 |
dex_posix_aio_future_complete_fd (posix_aio_future, posix_aio_future->open.res); |
| 345 |
break; |
| 346 |
|
| 347 |
case DEX_POSIX_AIO_FUTURE_READ: |
| 348 |
dex_posix_aio_future_complete_int64 (posix_aio_future, posix_aio_future->read.res); |
| 349 |
break; |
| 350 |
|
| 351 |
case DEX_POSIX_AIO_FUTURE_WRITE: |
| 352 |
dex_posix_aio_future_complete_int64 (posix_aio_future, posix_aio_future->write.res); |
| 353 |
break; |
| 354 |
|
| 355 |
default: |
| 356 |
g_assert_not_reached (); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
DexPosixAioContext * |
| 361 |
dex_posix_aio_future_get_aio_context (DexPosixAioFuture *posix_aio_future) |
| 362 |
{ |
| 363 |
g_return_val_if_fail (DEX_IS_POSIX_AIO_FUTURE (posix_aio_future), NULL); |
| 364 |
|
| 365 |
return posix_aio_future->aio_context; |
| 366 |
} |
| 367 |
|
| 368 |
GMainContext * |
| 369 |
dex_posix_aio_future_get_main_context (DexPosixAioFuture *posix_aio_future) |
| 370 |
{ |
| 371 |
g_return_val_if_fail (DEX_IS_POSIX_AIO_FUTURE (posix_aio_future), NULL); |
| 372 |
|
| 373 |
return posix_aio_future->main_context; |
| 374 |
} |