xref: /netbsd-src/sys/external/bsd/common/include/linux/workqueue.h (revision 888678ac50a237bd3f8a2dc113a33f54a5e73e5d)
1*888678acSriastradh /*	$NetBSD: workqueue.h,v 1.25 2021/12/19 11:40:13 riastradh Exp $	*/
262976e36Sskrll 
362976e36Sskrll /*-
49ee469e1Sriastradh  * Copyright (c) 2013, 2018 The NetBSD Foundation, Inc.
562976e36Sskrll  * All rights reserved.
662976e36Sskrll  *
762976e36Sskrll  * This code is derived from software contributed to The NetBSD Foundation
862976e36Sskrll  * by Taylor R. Campbell.
962976e36Sskrll  *
1062976e36Sskrll  * Redistribution and use in source and binary forms, with or without
1162976e36Sskrll  * modification, are permitted provided that the following conditions
1262976e36Sskrll  * are met:
1362976e36Sskrll  * 1. Redistributions of source code must retain the above copyright
1462976e36Sskrll  *    notice, this list of conditions and the following disclaimer.
1562976e36Sskrll  * 2. Redistributions in binary form must reproduce the above copyright
1662976e36Sskrll  *    notice, this list of conditions and the following disclaimer in the
1762976e36Sskrll  *    documentation and/or other materials provided with the distribution.
1862976e36Sskrll  *
1962976e36Sskrll  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2062976e36Sskrll  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2162976e36Sskrll  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2262976e36Sskrll  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2362976e36Sskrll  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2462976e36Sskrll  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2562976e36Sskrll  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2662976e36Sskrll  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2762976e36Sskrll  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2862976e36Sskrll  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2962976e36Sskrll  * POSSIBILITY OF SUCH DAMAGE.
3062976e36Sskrll  */
3162976e36Sskrll 
3262976e36Sskrll #ifndef _LINUX_WORKQUEUE_H_
3362976e36Sskrll #define _LINUX_WORKQUEUE_H_
3462976e36Sskrll 
3562976e36Sskrll #include <sys/queue.h>
369ee469e1Sriastradh #include <sys/stdbool.h>
3762976e36Sskrll 
389ee469e1Sriastradh #include <linux/kernel.h>	/* container_of */
39883a141fSriastradh #include <linux/stringify.h>
4062976e36Sskrll 
4162976e36Sskrll #define	INIT_DELAYED_WORK		linux_INIT_DELAYED_WORK
42*888678acSriastradh #define	INIT_RCU_WORK			linux_INIT_RCU_WORK
4362976e36Sskrll #define	INIT_WORK			linux_INIT_WORK
4462976e36Sskrll #define	alloc_ordered_workqueue		linux_alloc_ordered_workqueue
45*888678acSriastradh #define	alloc_workqueue			linux_alloc_workqueue
4662976e36Sskrll #define	cancel_delayed_work		linux_cancel_delayed_work
4762976e36Sskrll #define	cancel_delayed_work_sync	linux_cancel_delayed_work_sync
4862976e36Sskrll #define	cancel_work			linux_cancel_work
4962976e36Sskrll #define	cancel_work_sync		linux_cancel_work_sync
5035ff64e8Sriastradh #define	current_work			linux_current_work
51f7bdb0aaSriastradh #define	delayed_work_pending		linux_delayed_work_pending
5262976e36Sskrll #define	destroy_workqueue		linux_destroy_workqueue
53504d2eb4Sriastradh #define	drain_workqueue			linux_drain_workqueue
5494359bcaSriastradh #define	flush_delayed_work		linux_flush_delayed_work
55ddd95367Sriastradh #define	flush_scheduled_work		linux_flush_scheduled_work
5662976e36Sskrll #define	flush_work			linux_flush_work
5762976e36Sskrll #define	flush_workqueue			linux_flush_workqueue
5862976e36Sskrll #define	mod_delayed_work		linux_mod_delayed_work
59*888678acSriastradh #define	queue_delayed_work		linux_queue_delayed_work
60*888678acSriastradh #define	queue_rcu_work			linux_queue_rcu_work
6162976e36Sskrll #define	queue_work			linux_queue_work
6262976e36Sskrll #define	schedule_delayed_work		linux_schedule_delayed_work
6362976e36Sskrll #define	schedule_work			linux_schedule_work
64dfa0e026Sriastradh #define	system_highpri_wq		linux_system_highpri_wq
652ccb6811Sriastradh #define	system_long_wq			linux_system_long_wq
66876a52a2Sriastradh #define	system_power_efficient_wq	linux_system_power_efficient_wq
67740d45f7Sriastradh #define	system_unbound_wq		linux_system_unbound_wq
6862976e36Sskrll #define	system_wq			linux_system_wq
6962976e36Sskrll #define	to_delayed_work			linux_to_delayed_work
70f7bdb0aaSriastradh #define	work_pending			linux_work_pending
7162976e36Sskrll 
7262976e36Sskrll struct workqueue_struct;
7362976e36Sskrll 
7462976e36Sskrll struct work_struct {
75e27b3435Sriastradh 	volatile uintptr_t		work_owner;
769ee469e1Sriastradh 	TAILQ_ENTRY(work_struct)	work_entry;
779ee469e1Sriastradh 	void	(*func)(struct work_struct *); /* Linux API name */
7862976e36Sskrll };
7962976e36Sskrll 
8062976e36Sskrll struct delayed_work {
819ee469e1Sriastradh 	struct work_struct		work; /* Linux API name */
8262976e36Sskrll 	struct callout			dw_callout;
8362976e36Sskrll 	TAILQ_ENTRY(delayed_work)	dw_entry;
841fca7189Sriastradh 	int				dw_resched;
859ee469e1Sriastradh 	enum {
869ee469e1Sriastradh 		DELAYED_WORK_IDLE,
879ee469e1Sriastradh 		DELAYED_WORK_SCHEDULED,
889ee469e1Sriastradh 		DELAYED_WORK_RESCHEDULED,
899ee469e1Sriastradh 		DELAYED_WORK_CANCELLED,
909ee469e1Sriastradh 	}				dw_state;
9162976e36Sskrll };
9262976e36Sskrll 
9301bf3487Sriastradh struct rcu_work {
94*888678acSriastradh 	struct work_struct		work; /* Linux API name */
95*888678acSriastradh 	struct rcu_head			rw_rcu;
9601bf3487Sriastradh };
9701bf3487Sriastradh 
98181b0344Sriastradh #define	WQ_FREEZABLE		__BIT(0)
99181b0344Sriastradh #define	WQ_HIGHPRI		__BIT(1)
100181b0344Sriastradh #define	WQ_MEM_RECLAIM		__BIT(2)
101181b0344Sriastradh #define	WQ_UNBOUND		__BIT(3)
102181b0344Sriastradh 
103dfa0e026Sriastradh #define	WQ_UNBOUND_MAX_ACTIVE	0
104dfa0e026Sriastradh 
10562976e36Sskrll static inline struct delayed_work *
to_delayed_work(struct work_struct * work)10662976e36Sskrll to_delayed_work(struct work_struct *work)
10762976e36Sskrll {
10862976e36Sskrll 	return container_of(work, struct delayed_work, work);
10962976e36Sskrll }
11062976e36Sskrll 
111dfa0e026Sriastradh extern struct workqueue_struct	*system_highpri_wq;
112ffba132aSriastradh extern struct workqueue_struct	*system_long_wq;
113876a52a2Sriastradh extern struct workqueue_struct	*system_power_efficient_wq;
1146fe9f2d2Sriastradh extern struct workqueue_struct	*system_unbound_wq;
115dfa0e026Sriastradh extern struct workqueue_struct	*system_wq;
11662976e36Sskrll 
11762976e36Sskrll int	linux_workqueue_init(void);
11862976e36Sskrll void	linux_workqueue_fini(void);
11962976e36Sskrll 
12062976e36Sskrll #define	create_singlethread_workqueue(name)				      \
12162976e36Sskrll 	alloc_ordered_workqueue((name), 0)
12262976e36Sskrll 
12362976e36Sskrll struct workqueue_struct *
124181b0344Sriastradh 	alloc_workqueue(const char *, int, unsigned);
125181b0344Sriastradh struct workqueue_struct *
12662976e36Sskrll 	alloc_ordered_workqueue(const char *, int);
12762976e36Sskrll void	destroy_workqueue(struct workqueue_struct *);
12862976e36Sskrll void	flush_workqueue(struct workqueue_struct *);
129504d2eb4Sriastradh void	drain_workqueue(struct workqueue_struct *);
13062976e36Sskrll void	flush_scheduled_work(void);
13162976e36Sskrll 
13262976e36Sskrll void	INIT_WORK(struct work_struct *, void (*)(struct work_struct *));
13362976e36Sskrll bool	schedule_work(struct work_struct *);
13462976e36Sskrll bool	queue_work(struct workqueue_struct *, struct work_struct *);
1359ee469e1Sriastradh bool	cancel_work(struct work_struct *);
13662976e36Sskrll bool	cancel_work_sync(struct work_struct *);
1372ba00858Sriastradh bool	flush_work(struct work_struct *);
13823727e60Sriastradh bool	work_pending(const struct work_struct *);
13962976e36Sskrll 
14062976e36Sskrll void	INIT_DELAYED_WORK(struct delayed_work *,
14162976e36Sskrll 	    void (*)(struct work_struct *));
14262976e36Sskrll bool	schedule_delayed_work(struct delayed_work *, unsigned long);
14362976e36Sskrll bool	queue_delayed_work(struct workqueue_struct *, struct delayed_work *,
14462976e36Sskrll 	    unsigned long ticks);
14562976e36Sskrll bool	mod_delayed_work(struct workqueue_struct *, struct delayed_work *,
14662976e36Sskrll 	    unsigned long ticks);
14762976e36Sskrll bool	cancel_delayed_work(struct delayed_work *);
14862976e36Sskrll bool	cancel_delayed_work_sync(struct delayed_work *);
1492ba00858Sriastradh bool	flush_delayed_work(struct delayed_work *);
15023727e60Sriastradh bool	delayed_work_pending(const struct delayed_work *);
15162976e36Sskrll 
152*888678acSriastradh void	INIT_RCU_WORK(struct rcu_work *, void (*fn)(struct work_struct *));
153*888678acSriastradh void	queue_rcu_work(struct workqueue_struct *, struct rcu_work *);
154*888678acSriastradh 
15535ff64e8Sriastradh struct work_struct *
15635ff64e8Sriastradh 	current_work(void);
15735ff64e8Sriastradh 
15806daeb9eSriastradh #define	INIT_WORK_ONSTACK		INIT_WORK
1593c752b2fSriastradh #define	INIT_DELAYED_WORK_ONSTACK	INIT_DELAYED_WORK
1603c752b2fSriastradh #define	destroy_delayed_work_on_stack(dw)	__nothing
16106daeb9eSriastradh 
16206daeb9eSriastradh static inline void
destroy_work_on_stack(struct work_struct * work)16306daeb9eSriastradh destroy_work_on_stack(struct work_struct *work)
16406daeb9eSriastradh {
16506daeb9eSriastradh }
16606daeb9eSriastradh 
16762976e36Sskrll #endif  /* _LINUX_WORKQUEUE_H_ */
168