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 * Copyright (c) 2012, Intel Corporation.
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_wi_sched;
68
69 void cfs_wi_sched_destroy(struct cfs_wi_sched *);
70 int cfs_wi_sched_create(char *name, struct cfs_cpt_table *cptab, int cpt,
71 int nthrs, struct cfs_wi_sched **);
72
73 struct cfs_workitem;
74
75 typedef int (*cfs_wi_action_t) (struct cfs_workitem *);
76 typedef struct cfs_workitem {
77 /** chain on runq or rerunq */
78 struct list_head wi_list;
79 /** working function */
80 cfs_wi_action_t wi_action;
81 /** arg for working function */
82 void *wi_data;
83 /** in running */
84 unsigned short wi_running:1;
85 /** scheduled */
86 unsigned short wi_scheduled:1;
87 } cfs_workitem_t;
88
89 static inline void
cfs_wi_init(cfs_workitem_t * wi,void * data,cfs_wi_action_t action)90 cfs_wi_init(cfs_workitem_t *wi, void *data, cfs_wi_action_t action)
91 {
92 INIT_LIST_HEAD(&wi->wi_list);
93
94 wi->wi_running = 0;
95 wi->wi_scheduled = 0;
96 wi->wi_data = data;
97 wi->wi_action = action;
98 }
99
100 void cfs_wi_schedule(struct cfs_wi_sched *sched, cfs_workitem_t *wi);
101 int cfs_wi_deschedule(struct cfs_wi_sched *sched, cfs_workitem_t *wi);
102 void cfs_wi_exit(struct cfs_wi_sched *sched, cfs_workitem_t *wi);
103
104 int cfs_wi_startup(void);
105 void cfs_wi_shutdown(void);
106
107 /** # workitem scheduler loops before reschedule */
108 #define CFS_WI_RESCHED 128
109
110 #endif /* __LIBCFS_WORKITEM_H__ */
111