Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * libcfs/include/libcfs/libcfs_workitem.h
35  *
36  * Author: Isaac Huang  <he.h.huang@oracle.com>
37  *         Liang Zhen   <zhen.liang@sun.com>
38  *
39  * A workitems is deferred work with these semantics:
40  * - a workitem always runs in thread context.
41  * - a workitem can be concurrent with other workitems but is strictly
42  *   serialized with respect to itself.
43  * - no CPU affinity, a workitem does not necessarily run on the same CPU
44  *   that schedules it. However, this might change in the future.
45  * - if a workitem is scheduled again before it has a chance to run, it
46  *   runs only once.
47  * - if a workitem is scheduled while it runs, it runs again after it
48  *   completes; this ensures that events occurring while other events are
49  *   being processed receive due attention. This behavior also allows a
50  *   workitem to reschedule itself.
51  *
52  * Usage notes:
53  * - a workitem can sleep but it should be aware of how that sleep might
54  *   affect others.
55  * - a workitem runs inside a kernel thread so there's no user space to access.
56  * - do not use a workitem if the scheduling latency can't be tolerated.
57  *
58  * When wi_action returns non-zero, it means the workitem has either been
59  * freed or reused and workitem scheduler won't touch it any more.
60  */
61
62 #ifndef __LIBCFS_WORKITEM_H__
63 #define __LIBCFS_WORKITEM_H__
64
65 struct cfs_workitem;
66
67 typedef int (*cfs_wi_action_t) (struct cfs_workitem *);
68 typedef struct cfs_workitem {
69         /** chain on runq or rerunq */
70         cfs_list_t       wi_list;
71         /** working function */
72         cfs_wi_action_t  wi_action;
73         /** arg for working function */
74         void            *wi_data;
75         /** scheduler id, can be negative */
76         short            wi_sched_id;
77         /** in running */
78         unsigned short   wi_running:1;
79         /** scheduled */
80         unsigned short   wi_scheduled:1;
81 } cfs_workitem_t;
82
83 /**
84  * positive values are reserved as CPU id of future implementation of
85  * per-cpu scheduler, so user can "bind" workitem on specific CPU.
86  */
87 #define CFS_WI_SCHED_ANY        (-1)
88 #define CFS_WI_SCHED_SERIAL     (-2)
89
90 static inline void
91 cfs_wi_init(cfs_workitem_t *wi, void *data,
92             cfs_wi_action_t action, short sched_id)
93 {
94         CFS_INIT_LIST_HEAD(&wi->wi_list);
95
96         wi->wi_sched_id  = sched_id;
97         wi->wi_running   = 0;
98         wi->wi_scheduled = 0;
99         wi->wi_data      = data;
100         wi->wi_action    = action;
101 }
102
103 void cfs_wi_exit(cfs_workitem_t *wi);
104 int  cfs_wi_cancel(cfs_workitem_t *wi);
105 void cfs_wi_schedule(cfs_workitem_t *wi);
106 int  cfs_wi_startup(void);
107 void cfs_wi_shutdown(void);
108
109 #ifdef __KERNEL__
110 /** # workitem scheduler loops before reschedule */
111 #define CFS_WI_RESCHED    128
112 #else
113 int cfs_wi_check_events(void);
114 #endif
115
116 #endif /* __LIBCFS_WORKITEM_H__ */