chiark / gitweb /
async: add asynchronous close() call
authorLennart Poettering <lennart@poettering.net>
Fri, 2 May 2014 15:56:05 +0000 (17:56 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 2 May 2014 15:57:37 +0000 (17:57 +0200)
src/core/async.c
src/core/async.h

index af527bea4ec62ff46c92ec0f0b547618f737cc09..3876deda70c9954eefa3c43ba5915b3471a4a48f 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "async.h"
 #include "log.h"
+#include "util.h"
 
 int asynchronous_job(void* (*func)(void *p), void *arg) {
         pthread_attr_t a;
@@ -70,3 +71,24 @@ int asynchronous_sync(void) {
 
         return asynchronous_job(sync_thread, NULL);
 }
+
+static void *close_thread(void *p) {
+        safe_close(PTR_TO_INT(p));
+        return NULL;
+}
+
+int asynchronous_close(int fd) {
+        int r;
+
+        /* This is supposed to behave similar to safe_close(), but
+         * actually invoke close() asynchronously, so that it will
+         * never block. Ideally the kernel would have an API for this,
+         * but it doesn't, so we work around it, and hide this as a
+         * far away as we can. */
+
+        r = asynchronous_job(close_thread, INT_TO_PTR(fd));
+        if (r < 0)
+                safe_close(fd);
+
+        return -1;
+}
index 6601b4dc4b313c741b91b9ac8a838a83520cfb09..7f1ef79532d81b5526a81f6632452ea2a5fe0477 100644 (file)
@@ -22,4 +22,6 @@
 ***/
 
 int asynchronous_job(void* (*func)(void *p), void *arg);
+
 int asynchronous_sync(void);
+int asynchronous_close(int fd);