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