]>
Commit | Line | Data |
---|---|---|
d62a17ae | 1 | /* |
354d119a | 2 | * Quagga Work Queues. |
3 | * | |
4 | * Copyright (C) 2005 Sun Microsystems, Inc. | |
5 | * | |
6 | * This file is part of Quagga. | |
7 | * | |
8 | * Quagga is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * Quagga is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
896014f4 DL |
18 | * You should have received a copy of the GNU General Public License along |
19 | * with this program; see the file COPYING; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
354d119a | 21 | */ |
22 | ||
23 | #ifndef _QUAGGA_WORK_QUEUE_H | |
24 | #define _QUAGGA_WORK_QUEUE_H | |
25 | ||
4a1ab8e4 | 26 | #include "memory.h" |
f104f6c1 | 27 | #include "queue.h" |
5e244469 RW |
28 | |
29 | #ifdef __cplusplus | |
30 | extern "C" { | |
31 | #endif | |
32 | ||
4a1ab8e4 DL |
33 | DECLARE_MTYPE(WORK_QUEUE) |
34 | ||
306d8890 | 35 | /* Hold time for the initial schedule of a queue run, in millisec */ |
e208c8f9 | 36 | #define WORK_QUEUE_DEFAULT_HOLD 50 |
354d119a | 37 | |
5418f988 MS |
38 | /* Retry for queue that is 'blocked' or 'retry later' */ |
39 | #define WORK_QUEUE_DEFAULT_RETRY 0 | |
40 | ||
354d119a | 41 | /* action value, for use by item processor and item error handlers */ |
d62a17ae | 42 | typedef enum { |
43 | WQ_SUCCESS = 0, | |
44 | WQ_ERROR, /* Error, run error handler if provided */ | |
45 | WQ_RETRY_NOW, /* retry immediately */ | |
46 | WQ_RETRY_LATER, /* retry later, cease processing work queue */ | |
47 | WQ_REQUEUE, /* requeue item, continue processing work queue */ | |
48 | WQ_QUEUE_BLOCKED, /* Queue cant be processed at this time. | |
49 | * Similar to WQ_RETRY_LATER, but doesn't penalise | |
50 | * the particular item.. */ | |
354d119a | 51 | } wq_item_status; |
52 | ||
53 | /* A single work queue item, unsurprisingly */ | |
d62a17ae | 54 | struct work_queue_item { |
f104f6c1 | 55 | STAILQ_ENTRY(work_queue_item) wq; |
d62a17ae | 56 | void *data; /* opaque data */ |
57 | unsigned short ran; /* # of times item has been run */ | |
354d119a | 58 | }; |
59 | ||
6ce80bdb | 60 | #define WQ_UNPLUGGED (1 << 0) /* available for draining */ |
269d74fd | 61 | |
d62a17ae | 62 | struct work_queue { |
63 | /* Everything but the specification struct is private | |
64 | * the following may be read | |
65 | */ | |
66 | struct thread_master *master; /* thread master */ | |
67 | struct thread *thread; /* thread, if one is active */ | |
68 | char *name; /* work queue name */ | |
69 | ||
70 | /* Specification for this work queue. | |
71 | * Public, must be set before use by caller. May be modified at will. | |
72 | */ | |
73 | struct { | |
74 | /* optional opaque user data, global to the queue. */ | |
75 | void *data; | |
76 | ||
77 | /* work function to process items with: | |
78 | * First argument is the workqueue queue. | |
79 | * Second argument is the item data | |
80 | */ | |
81 | wq_item_status (*workfunc)(struct work_queue *, void *); | |
82 | ||
83 | /* error handling function, optional */ | |
84 | void (*errorfunc)(struct work_queue *, | |
85 | struct work_queue_item *); | |
86 | ||
87 | /* callback to delete user specific item data */ | |
88 | void (*del_item_data)(struct work_queue *, void *); | |
89 | ||
90 | /* completion callback, called when queue is emptied, optional | |
91 | */ | |
92 | void (*completion_func)(struct work_queue *); | |
93 | ||
94 | /* max number of retries to make for item that errors */ | |
95 | unsigned int max_retries; | |
96 | ||
97 | unsigned int hold; /* hold time for first run, in ms */ | |
98 | ||
99 | unsigned long | |
100 | yield; /* yield time in us for associated thread */ | |
5418f988 MS |
101 | |
102 | uint32_t retry; /* Optional retry timeout if queue is blocked */ | |
d62a17ae | 103 | } spec; |
104 | ||
105 | /* remaining fields should be opaque to users */ | |
996c9314 LB |
106 | STAILQ_HEAD(work_queue_items, work_queue_item) |
107 | items; /* queue item list */ | |
108 | int item_count; /* queued items */ | |
d62a17ae | 109 | unsigned long runs; /* runs count */ |
110 | unsigned long yields; /* yields count */ | |
111 | ||
112 | struct { | |
113 | unsigned int best; | |
114 | unsigned int granularity; | |
115 | unsigned long total; | |
116 | } cycles; /* cycle counts */ | |
117 | ||
118 | /* private state */ | |
d7c0a89a | 119 | uint16_t flags; /* user set flag */ |
354d119a | 120 | }; |
121 | ||
122 | /* User API */ | |
269d74fd | 123 | |
f104f6c1 JB |
124 | static inline int work_queue_item_count(struct work_queue *wq) |
125 | { | |
126 | return wq->item_count; | |
127 | } | |
128 | ||
129 | static inline bool work_queue_empty(struct work_queue *wq) | |
130 | { | |
131 | return (wq->item_count == 0) ? true : false; | |
132 | } | |
133 | ||
996c9314 LB |
134 | static inline struct work_queue_item * |
135 | work_queue_last_item(struct work_queue *wq) | |
f104f6c1 JB |
136 | { |
137 | return STAILQ_LAST(&wq->items, work_queue_item, wq); | |
138 | } | |
139 | ||
140 | static inline void work_queue_item_enqueue(struct work_queue *wq, | |
141 | struct work_queue_item *item) | |
142 | { | |
143 | STAILQ_INSERT_TAIL(&wq->items, item, wq); | |
144 | wq->item_count++; | |
145 | } | |
146 | ||
147 | static inline void work_queue_item_dequeue(struct work_queue *wq, | |
148 | struct work_queue_item *item) | |
149 | { | |
150 | assert(wq->item_count > 0); | |
151 | ||
152 | wq->item_count--; | |
153 | STAILQ_REMOVE(&wq->items, item, work_queue_item, wq); | |
154 | } | |
155 | ||
d62a17ae | 156 | /* create a new work queue, of given name. |
269d74fd | 157 | * user must fill in the spec of the returned work queue before adding |
158 | * anything to it | |
159 | */ | |
d62a17ae | 160 | extern struct work_queue *work_queue_new(struct thread_master *, const char *); |
e208c8f9 | 161 | |
269d74fd | 162 | /* destroy work queue */ |
e208c8f9 DS |
163 | /* |
164 | * The usage of work_queue_free is being transitioned to pass | |
165 | * in the double pointer to remove use after free's. | |
166 | */ | |
49f36252 | 167 | extern void work_queue_free_and_null(struct work_queue **wqp); |
269d74fd | 168 | |
169 | /* Add the supplied data as an item onto the workqueue */ | |
49f36252 | 170 | extern void work_queue_add(struct work_queue *wq, void *item); |
354d119a | 171 | |
269d74fd | 172 | /* plug the queue, ie prevent it from being drained / processed */ |
d62a17ae | 173 | extern void work_queue_plug(struct work_queue *wq); |
269d74fd | 174 | /* unplug the queue, allow it to be drained again */ |
d62a17ae | 175 | extern void work_queue_unplug(struct work_queue *wq); |
269d74fd | 176 | |
d62a17ae | 177 | bool work_queue_is_scheduled(struct work_queue *); |
86582682 | 178 | |
354d119a | 179 | /* Helpers, exported for thread.c and command.c */ |
d62a17ae | 180 | extern int work_queue_run(struct thread *); |
0b84f294 | 181 | |
d62a17ae | 182 | extern void workqueue_cmd_init(void); |
0b84f294 | 183 | |
5e244469 RW |
184 | #ifdef __cplusplus |
185 | } | |
186 | #endif | |
187 | ||
354d119a | 188 | #endif /* _QUAGGA_WORK_QUEUE_H */ |