Whamcloud - gitweb
744883671e34dd21cf7fa5fdd4afb41b04c3e44e
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_workitem.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/include/libcfs/libcfs_workitem.h
37  *
38  * Author: Isaac Huang  <he.h.huang@oracle.com>
39  *         Liang Zhen   <zhen.liang@sun.com>
40  *
41  * A workitems is deferred work with these semantics:
42  * - a workitem always runs in thread context.
43  * - a workitem can be concurrent with other workitems but is strictly
44  *   serialized with respect to itself.
45  * - no CPU affinity, a workitem does not necessarily run on the same CPU
46  *   that schedules it. However, this might change in the future.
47  * - if a workitem is scheduled again before it has a chance to run, it
48  *   runs only once.
49  * - if a workitem is scheduled while it runs, it runs again after it
50  *   completes; this ensures that events occurring while other events are
51  *   being processed receive due attention. This behavior also allows a
52  *   workitem to reschedule itself.
53  *
54  * Usage notes:
55  * - a workitem can sleep but it should be aware of how that sleep might
56  *   affect others.
57  * - a workitem runs inside a kernel thread so there's no user space to access.
58  * - do not use a workitem if the scheduling latency can't be tolerated.
59  *
60  * When wi_action returns non-zero, it means the workitem has either been
61  * freed or reused and workitem scheduler won't touch it any more.
62  */
63
64 #ifndef __LIBCFS_WORKITEM_H__
65 #define __LIBCFS_WORKITEM_H__
66
67 struct cfs_workitem;
68
69 typedef int (*cfs_wi_action_t) (struct cfs_workitem *);
70 typedef struct cfs_workitem {
71         /** chain on runq or rerunq */
72         cfs_list_t       wi_list;
73         /** working function */
74         cfs_wi_action_t  wi_action;
75         /** arg for working function */
76         void            *wi_data;
77         /** scheduler id, can be negative */
78         short            wi_sched_id;
79         /** in running */
80         unsigned short   wi_running:1;
81         /** scheduled */
82         unsigned short   wi_scheduled:1;
83 } cfs_workitem_t;
84
85 /**
86  * positive values are reserved as CPU id of future implementation of
87  * per-cpu scheduler, so user can "bind" workitem on specific CPU.
88  */
89 #define CFS_WI_SCHED_ANY        (-1)
90 #define CFS_WI_SCHED_SERIAL     (-2)
91
92 static inline void
93 cfs_wi_init(cfs_workitem_t *wi, void *data,
94             cfs_wi_action_t action, short sched_id)
95 {
96         CFS_INIT_LIST_HEAD(&wi->wi_list);
97
98         wi->wi_sched_id  = sched_id;
99         wi->wi_running   = 0;
100         wi->wi_scheduled = 0;
101         wi->wi_data      = data;
102         wi->wi_action    = action;
103 }
104
105 void cfs_wi_exit(cfs_workitem_t *wi);
106 int  cfs_wi_cancel(cfs_workitem_t *wi);
107 void cfs_wi_schedule(cfs_workitem_t *wi);
108 int  cfs_wi_startup(void);
109 void cfs_wi_shutdown(void);
110
111 #ifdef __KERNEL__
112 /** # workitem scheduler loops before reschedule */
113 #define CFS_WI_RESCHED    128
114 #else
115 int cfs_wi_check_events(void);
116 #endif
117
118 #endif /* __LIBCFS_WORKITEM_H__ */