Whamcloud - gitweb
LU-9680 net: Netlink improvements
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_workitem.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * libcfs/include/libcfs/libcfs_workitem.h
32  *
33  * Author: Isaac Huang  <he.h.huang@oracle.com>
34  *         Liang Zhen   <zhen.liang@sun.com>
35  *
36  * A workitems is deferred work with these semantics:
37  * - a workitem always runs in thread context.
38  * - a workitem can be concurrent with other workitems but is strictly
39  *   serialized with respect to itself.
40  * - no CPU affinity, a workitem does not necessarily run on the same CPU
41  *   that schedules it. However, this might change in the future.
42  * - if a workitem is scheduled again before it has a chance to run, it
43  *   runs only once.
44  * - if a workitem is scheduled while it runs, it runs again after it
45  *   completes; this ensures that events occurring while other events are
46  *   being processed receive due attention. This behavior also allows a
47  *   workitem to reschedule itself.
48  *
49  * Usage notes:
50  * - a workitem can sleep but it should be aware of how that sleep might
51  *   affect others.
52  * - a workitem runs inside a kernel thread so there's no user space to access.
53  * - do not use a workitem if the scheduling latency can't be tolerated.
54  *
55  * When wi_action returns non-zero, it means the workitem has either been
56  * freed or reused and workitem scheduler won't touch it any more.
57  */
58
59 #ifndef __LIBCFS_WORKITEM_H__
60 #define __LIBCFS_WORKITEM_H__
61
62 struct cfs_wi_sched;
63
64 void cfs_wi_sched_destroy(struct cfs_wi_sched *);
65 int cfs_wi_sched_create(char *name, struct cfs_cpt_table *cptab, int cpt,
66                         int nthrs, struct cfs_wi_sched **);
67
68 struct cfs_workitem;
69
70 typedef int (*cfs_wi_action_t) (struct cfs_workitem *);
71
72 struct cfs_workitem {
73         /** chain on runq or rerunq */
74         struct list_head        wi_list;
75         /** working function */
76         cfs_wi_action_t         wi_action;
77         /** in running */
78         unsigned short          wi_running:1;
79         /** scheduled */
80         unsigned short          wi_scheduled:1;
81 };
82
83 static inline void
84 cfs_wi_init(struct cfs_workitem *wi, cfs_wi_action_t action)
85 {
86         INIT_LIST_HEAD(&wi->wi_list);
87
88         wi->wi_running   = 0;
89         wi->wi_scheduled = 0;
90         wi->wi_action    = action;
91 }
92
93 void cfs_wi_schedule(struct cfs_wi_sched *sched, struct cfs_workitem *wi);
94 int  cfs_wi_deschedule(struct cfs_wi_sched *sched, struct cfs_workitem *wi);
95 void cfs_wi_exit(struct cfs_wi_sched *sched, struct cfs_workitem *wi);
96
97 int  cfs_wi_startup(void);
98 void cfs_wi_shutdown(void);
99
100 /** # workitem scheduler loops before reschedule */
101 #define CFS_WI_RESCHED    128
102
103 #endif /* __LIBCFS_WORKITEM_H__ */