]>
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" |
4a1ab8e4 DL |
28 | DECLARE_MTYPE(WORK_QUEUE) |
29 | ||
306d8890 | 30 | /* Hold time for the initial schedule of a queue run, in millisec */ |
e208c8f9 | 31 | #define WORK_QUEUE_DEFAULT_HOLD 50 |
354d119a | 32 | |
33 | /* action value, for use by item processor and item error handlers */ | |
d62a17ae | 34 | typedef enum { |
35 | WQ_SUCCESS = 0, | |
36 | WQ_ERROR, /* Error, run error handler if provided */ | |
37 | WQ_RETRY_NOW, /* retry immediately */ | |
38 | WQ_RETRY_LATER, /* retry later, cease processing work queue */ | |
39 | WQ_REQUEUE, /* requeue item, continue processing work queue */ | |
40 | WQ_QUEUE_BLOCKED, /* Queue cant be processed at this time. | |
41 | * Similar to WQ_RETRY_LATER, but doesn't penalise | |
42 | * the particular item.. */ | |
354d119a | 43 | } wq_item_status; |
44 | ||
45 | /* A single work queue item, unsurprisingly */ | |
d62a17ae | 46 | struct work_queue_item { |
f104f6c1 | 47 | STAILQ_ENTRY(work_queue_item) wq; |
d62a17ae | 48 | void *data; /* opaque data */ |
49 | unsigned short ran; /* # of times item has been run */ | |
354d119a | 50 | }; |
51 | ||
6ce80bdb | 52 | #define WQ_UNPLUGGED (1 << 0) /* available for draining */ |
269d74fd | 53 | |
d62a17ae | 54 | struct work_queue { |
55 | /* Everything but the specification struct is private | |
56 | * the following may be read | |
57 | */ | |
58 | struct thread_master *master; /* thread master */ | |
59 | struct thread *thread; /* thread, if one is active */ | |
60 | char *name; /* work queue name */ | |
61 | ||
62 | /* Specification for this work queue. | |
63 | * Public, must be set before use by caller. May be modified at will. | |
64 | */ | |
65 | struct { | |
66 | /* optional opaque user data, global to the queue. */ | |
67 | void *data; | |
68 | ||
69 | /* work function to process items with: | |
70 | * First argument is the workqueue queue. | |
71 | * Second argument is the item data | |
72 | */ | |
73 | wq_item_status (*workfunc)(struct work_queue *, void *); | |
74 | ||
75 | /* error handling function, optional */ | |
76 | void (*errorfunc)(struct work_queue *, | |
77 | struct work_queue_item *); | |
78 | ||
79 | /* callback to delete user specific item data */ | |
80 | void (*del_item_data)(struct work_queue *, void *); | |
81 | ||
82 | /* completion callback, called when queue is emptied, optional | |
83 | */ | |
84 | void (*completion_func)(struct work_queue *); | |
85 | ||
86 | /* max number of retries to make for item that errors */ | |
87 | unsigned int max_retries; | |
88 | ||
89 | unsigned int hold; /* hold time for first run, in ms */ | |
90 | ||
91 | unsigned long | |
92 | yield; /* yield time in us for associated thread */ | |
93 | } spec; | |
94 | ||
95 | /* remaining fields should be opaque to users */ | |
996c9314 LB |
96 | STAILQ_HEAD(work_queue_items, work_queue_item) |
97 | items; /* queue item list */ | |
98 | int item_count; /* queued items */ | |
d62a17ae | 99 | unsigned long runs; /* runs count */ |
100 | unsigned long yields; /* yields count */ | |
101 | ||
102 | struct { | |
103 | unsigned int best; | |
104 | unsigned int granularity; | |
105 | unsigned long total; | |
106 | } cycles; /* cycle counts */ | |
107 | ||
108 | /* private state */ | |
d7c0a89a | 109 | uint16_t flags; /* user set flag */ |
354d119a | 110 | }; |
111 | ||
112 | /* User API */ | |
269d74fd | 113 | |
f104f6c1 JB |
114 | static inline int work_queue_item_count(struct work_queue *wq) |
115 | { | |
116 | return wq->item_count; | |
117 | } | |
118 | ||
119 | static inline bool work_queue_empty(struct work_queue *wq) | |
120 | { | |
121 | return (wq->item_count == 0) ? true : false; | |
122 | } | |
123 | ||
996c9314 LB |
124 | static inline struct work_queue_item * |
125 | work_queue_last_item(struct work_queue *wq) | |
f104f6c1 JB |
126 | { |
127 | return STAILQ_LAST(&wq->items, work_queue_item, wq); | |
128 | } | |
129 | ||
130 | static inline void work_queue_item_enqueue(struct work_queue *wq, | |
131 | struct work_queue_item *item) | |
132 | { | |
133 | STAILQ_INSERT_TAIL(&wq->items, item, wq); | |
134 | wq->item_count++; | |
135 | } | |
136 | ||
137 | static inline void work_queue_item_dequeue(struct work_queue *wq, | |
138 | struct work_queue_item *item) | |
139 | { | |
140 | assert(wq->item_count > 0); | |
141 | ||
142 | wq->item_count--; | |
143 | STAILQ_REMOVE(&wq->items, item, work_queue_item, wq); | |
144 | } | |
145 | ||
d62a17ae | 146 | /* create a new work queue, of given name. |
269d74fd | 147 | * user must fill in the spec of the returned work queue before adding |
148 | * anything to it | |
149 | */ | |
d62a17ae | 150 | extern struct work_queue *work_queue_new(struct thread_master *, const char *); |
e208c8f9 | 151 | |
269d74fd | 152 | /* destroy work queue */ |
e208c8f9 DS |
153 | /* |
154 | * The usage of work_queue_free is being transitioned to pass | |
155 | * in the double pointer to remove use after free's. | |
156 | */ | |
308fea4c | 157 | #if CONFDATE > 20190205 |
e208c8f9 DS |
158 | CPP_NOTICE("work_queue_free without double pointer is deprecated, please fixup") |
159 | #endif | |
160 | extern void work_queue_free_and_null(struct work_queue **); | |
161 | extern void work_queue_free_original(struct work_queue *); | |
162 | #define work_queue_free(X) \ | |
163 | do { \ | |
164 | work_queue_free_original((X)); \ | |
165 | CPP_WARN("Please use work_queue_free_and_null"); \ | |
166 | } while (0) | |
269d74fd | 167 | |
168 | /* Add the supplied data as an item onto the workqueue */ | |
d62a17ae | 169 | extern void work_queue_add(struct work_queue *, void *); |
354d119a | 170 | |
269d74fd | 171 | /* plug the queue, ie prevent it from being drained / processed */ |
d62a17ae | 172 | extern void work_queue_plug(struct work_queue *wq); |
269d74fd | 173 | /* unplug the queue, allow it to be drained again */ |
d62a17ae | 174 | extern void work_queue_unplug(struct work_queue *wq); |
269d74fd | 175 | |
d62a17ae | 176 | bool work_queue_is_scheduled(struct work_queue *); |
86582682 | 177 | |
354d119a | 178 | /* Helpers, exported for thread.c and command.c */ |
d62a17ae | 179 | extern int work_queue_run(struct thread *); |
0b84f294 | 180 | |
d62a17ae | 181 | extern void workqueue_cmd_init(void); |
0b84f294 | 182 | |
354d119a | 183 | #endif /* _QUAGGA_WORK_QUEUE_H */ |