Blob Blame History Raw
From 075d60ceb2efdafcdf5586653b912eeed84194e0 Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Fri, 2 May 2014 17:56:05 +0200
Subject: [PATCH] async: add asynchronous close() call

(cherry picked from commit 8a474b0c04a5a3608dda53edc1ddaa932ba177bf)
(cherry picked from commit 43f1b28bc457c92f85332dda752d5426cde466ae)
---
 src/core/async.c | 22 ++++++++++++++++++++++
 src/core/async.h |  2 ++
 2 files changed, 24 insertions(+)

diff --git a/src/core/async.c b/src/core/async.c
index af527be..3876ded 100644
--- a/src/core/async.c
+++ b/src/core/async.c
@@ -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;
+}
diff --git a/src/core/async.h b/src/core/async.h
index 6601b4d..7f1ef79 100644
--- a/src/core/async.h
+++ b/src/core/async.h
@@ -22,4 +22,6 @@
 ***/
 
 int asynchronous_job(void* (*func)(void *p), void *arg);
+
 int asynchronous_sync(void);
+int asynchronous_close(int fd);