1796e1f6bSFrançois Tigeot /*-
2*09f141c4SFrançois Tigeot * Copyright (c) 2015-2019 François Tigeot <ftigeot@wolfpond.org>
3796e1f6bSFrançois Tigeot * All rights reserved.
4796e1f6bSFrançois Tigeot *
5796e1f6bSFrançois Tigeot * Redistribution and use in source and binary forms, with or without
6796e1f6bSFrançois Tigeot * modification, are permitted provided that the following conditions
7796e1f6bSFrançois Tigeot * are met:
8796e1f6bSFrançois Tigeot * 1. Redistributions of source code must retain the above copyright
9796e1f6bSFrançois Tigeot * notice unmodified, this list of conditions, and the following
10796e1f6bSFrançois Tigeot * disclaimer.
11796e1f6bSFrançois Tigeot * 2. Redistributions in binary form must reproduce the above copyright
12796e1f6bSFrançois Tigeot * notice, this list of conditions and the following disclaimer in the
13796e1f6bSFrançois Tigeot * documentation and/or other materials provided with the distribution.
14796e1f6bSFrançois Tigeot *
15796e1f6bSFrançois Tigeot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16796e1f6bSFrançois Tigeot * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17796e1f6bSFrançois Tigeot * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18796e1f6bSFrançois Tigeot * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19796e1f6bSFrançois Tigeot * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20796e1f6bSFrançois Tigeot * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21796e1f6bSFrançois Tigeot * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22796e1f6bSFrançois Tigeot * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23796e1f6bSFrançois Tigeot * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24796e1f6bSFrançois Tigeot * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25796e1f6bSFrançois Tigeot */
26796e1f6bSFrançois Tigeot
27796e1f6bSFrançois Tigeot #include <sys/spinlock2.h>
28796e1f6bSFrançois Tigeot #include <linux/async.h>
29796e1f6bSFrançois Tigeot
30*09f141c4SFrançois Tigeot #include <sys/kernel.h>
31*09f141c4SFrançois Tigeot
32796e1f6bSFrançois Tigeot static async_cookie_t cookie_counter = 1;
33796e1f6bSFrançois Tigeot
34796e1f6bSFrançois Tigeot /* Protects cookie_counter */
35796e1f6bSFrançois Tigeot static struct lock linux_async_lock;
36796e1f6bSFrançois Tigeot LOCK_SYSINIT(linux_async_lock, &linux_async_lock, "lasync", LK_CANRECURSE);
37796e1f6bSFrançois Tigeot
38796e1f6bSFrançois Tigeot /* async_schedule: schedule a function for asynchronous execution */
async_schedule(async_func_t func,void * data)39796e1f6bSFrançois Tigeot async_cookie_t async_schedule(async_func_t func, void *data)
40796e1f6bSFrançois Tigeot {
41796e1f6bSFrançois Tigeot async_cookie_t cookie;
42796e1f6bSFrançois Tigeot
43796e1f6bSFrançois Tigeot lockmgr(&linux_async_lock, LK_EXCLUSIVE);
44796e1f6bSFrançois Tigeot cookie_counter++;
45796e1f6bSFrançois Tigeot cookie = cookie_counter;
46796e1f6bSFrançois Tigeot lockmgr(&linux_async_lock, LK_RELEASE);
47796e1f6bSFrançois Tigeot
48796e1f6bSFrançois Tigeot /* XXX: Just run the function synchronously for now */
49796e1f6bSFrançois Tigeot func(data, cookie);
50796e1f6bSFrançois Tigeot
51796e1f6bSFrançois Tigeot return cookie;
52796e1f6bSFrançois Tigeot }
53