1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 int asynchronous_job(void* (*func)(void *p), void *arg) {
34 /* It kinda sucks that we have to resort to threads to
35 * implement an asynchronous sync(), but well, such is
38 * Note that issuing this command right before exiting a
39 * process will cause the process to wait for the sync() to
40 * complete. This function hence is nicely asynchronous really
41 * only in long running processes. */
43 r = pthread_attr_init(&a);
47 r = pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
53 r = pthread_create(&t, &a, func, arg);
60 pthread_attr_destroy(&a);
64 static void *sync_thread(void *p) {
69 int asynchronous_sync(void) {
70 log_debug("Spawning new thread for sync");
72 return asynchronous_job(sync_thread, NULL);
75 static void *close_thread(void *p) {
76 assert_se(close_nointr(PTR_TO_INT(p)) != -EBADF);
80 int asynchronous_close(int fd) {
83 /* This is supposed to behave similar to safe_close(), but
84 * actually invoke close() asynchronously, so that it will
85 * never block. Ideally the kernel would have an API for this,
86 * but it doesn't, so we work around it, and hide this as a
87 * far away as we can. */
92 r = asynchronous_job(close_thread, INT_TO_PTR(fd));
94 assert_se(close_nointr(fd) != -EBADF);