blob: b245b9a9cc0bbdd6cdb3e74e6da90e009f6c515a [file] [log] [blame]
David Howells07fe7cb2009-04-03 16:42:35 +01001/* Worker thread pool for slow items, such as filesystem lookups or mkdirs
2 *
3 * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
David Howells8f0aa2f2009-04-03 16:42:35 +010010 *
11 * See Documentation/slow-work.txt
David Howells07fe7cb2009-04-03 16:42:35 +010012 */
13
14#ifndef _LINUX_SLOW_WORK_H
15#define _LINUX_SLOW_WORK_H
16
17#ifdef CONFIG_SLOW_WORK
18
David Howells12e22c52009-04-03 16:42:35 +010019#include <linux/sysctl.h>
Jens Axboe6b8268b2009-11-19 18:10:47 +000020#include <linux/timer.h>
David Howells12e22c52009-04-03 16:42:35 +010021
David Howells07fe7cb2009-04-03 16:42:35 +010022struct slow_work;
23
24/*
25 * The operations used to support slow work items
26 */
27struct slow_work_ops {
David Howells3d7a6412009-11-19 18:10:23 +000028 /* owner */
29 struct module *owner;
30
David Howells07fe7cb2009-04-03 16:42:35 +010031 /* get a ref on a work item
32 * - return 0 if successful, -ve if not
33 */
34 int (*get_ref)(struct slow_work *work);
35
36 /* discard a ref to a work item */
37 void (*put_ref)(struct slow_work *work);
38
39 /* execute a work item */
40 void (*execute)(struct slow_work *work);
41};
42
43/*
44 * A slow work item
45 * - A reference is held on the parent object by the thread pool when it is
46 * queued
47 */
48struct slow_work {
David Howells3d7a6412009-11-19 18:10:23 +000049 struct module *owner; /* the owning module */
David Howells07fe7cb2009-04-03 16:42:35 +010050 unsigned long flags;
51#define SLOW_WORK_PENDING 0 /* item pending (further) execution */
52#define SLOW_WORK_EXECUTING 1 /* item currently executing */
53#define SLOW_WORK_ENQ_DEFERRED 2 /* item enqueue deferred */
54#define SLOW_WORK_VERY_SLOW 3 /* item is very slow */
Jens Axboe01609502009-11-19 18:10:43 +000055#define SLOW_WORK_CANCELLING 4 /* item is being cancelled, don't enqueue */
Jens Axboe6b8268b2009-11-19 18:10:47 +000056#define SLOW_WORK_DELAYED 5 /* item is struct delayed_slow_work with active timer */
David Howells07fe7cb2009-04-03 16:42:35 +010057 const struct slow_work_ops *ops; /* operations table for this item */
58 struct list_head link; /* link in queue */
59};
60
Jens Axboe6b8268b2009-11-19 18:10:47 +000061struct delayed_slow_work {
62 struct slow_work work;
63 struct timer_list timer;
64};
65
David Howells07fe7cb2009-04-03 16:42:35 +010066/**
67 * slow_work_init - Initialise a slow work item
68 * @work: The work item to initialise
69 * @ops: The operations to use to handle the slow work item
70 *
71 * Initialise a slow work item.
72 */
73static inline void slow_work_init(struct slow_work *work,
74 const struct slow_work_ops *ops)
75{
76 work->flags = 0;
77 work->ops = ops;
78 INIT_LIST_HEAD(&work->link);
79}
80
81/**
Jens Axboe6b8268b2009-11-19 18:10:47 +000082 * slow_work_init - Initialise a delayed slow work item
83 * @work: The work item to initialise
84 * @ops: The operations to use to handle the slow work item
85 *
86 * Initialise a delayed slow work item.
87 */
88static inline void delayed_slow_work_init(struct delayed_slow_work *dwork,
89 const struct slow_work_ops *ops)
90{
91 init_timer(&dwork->timer);
92 slow_work_init(&dwork->work, ops);
93}
94
95/**
Jonathan Corbet5dd559f2009-04-21 16:30:32 -060096 * vslow_work_init - Initialise a very slow work item
David Howells07fe7cb2009-04-03 16:42:35 +010097 * @work: The work item to initialise
98 * @ops: The operations to use to handle the slow work item
99 *
100 * Initialise a very slow work item. This item will be restricted such that
101 * only a certain number of the pool threads will be able to execute items of
102 * this type.
103 */
104static inline void vslow_work_init(struct slow_work *work,
105 const struct slow_work_ops *ops)
106{
107 work->flags = 1 << SLOW_WORK_VERY_SLOW;
108 work->ops = ops;
109 INIT_LIST_HEAD(&work->link);
110}
111
112extern int slow_work_enqueue(struct slow_work *work);
Jens Axboe01609502009-11-19 18:10:43 +0000113extern void slow_work_cancel(struct slow_work *work);
David Howells3d7a6412009-11-19 18:10:23 +0000114extern int slow_work_register_user(struct module *owner);
115extern void slow_work_unregister_user(struct module *owner);
David Howells07fe7cb2009-04-03 16:42:35 +0100116
Jens Axboe6b8268b2009-11-19 18:10:47 +0000117extern int delayed_slow_work_enqueue(struct delayed_slow_work *dwork,
118 unsigned long delay);
119
120static inline void delayed_slow_work_cancel(struct delayed_slow_work *dwork)
121{
122 slow_work_cancel(&dwork->work);
123}
124
David Howells12e22c52009-04-03 16:42:35 +0100125#ifdef CONFIG_SYSCTL
126extern ctl_table slow_work_sysctls[];
127#endif
David Howells07fe7cb2009-04-03 16:42:35 +0100128
129#endif /* CONFIG_SLOW_WORK */
130#endif /* _LINUX_SLOW_WORK_H */