]>
git.proxmox.com Git - mirror_frr.git/blob - lib/workqueue.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2005 Sun Microsystems, Inc.
8 #ifndef _QUAGGA_WORK_QUEUE_H
9 #define _QUAGGA_WORK_QUEUE_H
18 DECLARE_MTYPE(WORK_QUEUE
);
20 /* Hold time for the initial schedule of a queue run, in millisec */
21 #define WORK_QUEUE_DEFAULT_HOLD 50
23 /* Retry for queue that is 'blocked' or 'retry later' */
24 #define WORK_QUEUE_DEFAULT_RETRY 0
26 /* action value, for use by item processor and item error handlers */
29 WQ_RETRY_NOW
, /* retry immediately */
30 WQ_RETRY_LATER
, /* retry later, cease processing work queue */
31 WQ_REQUEUE
, /* requeue item, continue processing work queue */
32 WQ_QUEUE_BLOCKED
, /* Queue cant be processed at this time.
33 * Similar to WQ_RETRY_LATER, but doesn't penalise
34 * the particular item.. */
37 /* A single work queue item, unsurprisingly */
38 struct work_queue_item
{
39 STAILQ_ENTRY(work_queue_item
) wq
;
40 void *data
; /* opaque data */
41 unsigned short ran
; /* # of times item has been run */
44 #define WQ_UNPLUGGED (1 << 0) /* available for draining */
47 /* Everything but the specification struct is private
48 * the following may be read
50 struct event_loop
*master
; /* thread master */
51 struct event
*thread
; /* thread, if one is active */
52 char *name
; /* work queue name */
54 /* Specification for this work queue.
55 * Public, must be set before use by caller. May be modified at will.
58 /* optional opaque user data, global to the queue. */
61 /* work function to process items with:
62 * First argument is the workqueue queue.
63 * Second argument is the item data
65 wq_item_status (*workfunc
)(struct work_queue
*, void *);
67 /* callback to delete user specific item data */
68 void (*del_item_data
)(struct work_queue
*, void *);
70 /* completion callback, called when queue is emptied, optional
72 void (*completion_func
)(struct work_queue
*);
74 /* max number of retries to make for item that errors */
75 unsigned int max_retries
;
77 unsigned int hold
; /* hold time for first run, in ms */
80 yield
; /* yield time in us for associated thread */
82 uint32_t retry
; /* Optional retry timeout if queue is blocked */
85 /* remaining fields should be opaque to users */
86 STAILQ_HEAD(work_queue_items
, work_queue_item
)
87 items
; /* queue item list */
88 int item_count
; /* queued items */
89 unsigned long runs
; /* runs count */
90 unsigned long yields
; /* yields count */
94 unsigned int granularity
;
96 } cycles
; /* cycle counts */
99 uint16_t flags
; /* user set flag */
104 static inline int work_queue_item_count(struct work_queue
*wq
)
106 return wq
->item_count
;
109 static inline bool work_queue_empty(struct work_queue
*wq
)
111 return (wq
->item_count
== 0) ? true : false;
114 static inline struct work_queue_item
*
115 work_queue_last_item(struct work_queue
*wq
)
117 return STAILQ_LAST(&wq
->items
, work_queue_item
, wq
);
120 static inline void work_queue_item_enqueue(struct work_queue
*wq
,
121 struct work_queue_item
*item
)
123 STAILQ_INSERT_TAIL(&wq
->items
, item
, wq
);
127 static inline void work_queue_item_dequeue(struct work_queue
*wq
,
128 struct work_queue_item
*item
)
130 assert(wq
->item_count
> 0);
133 STAILQ_REMOVE(&wq
->items
, item
, work_queue_item
, wq
);
136 /* create a new work queue, of given name.
137 * user must fill in the spec of the returned work queue before adding
140 extern struct work_queue
*work_queue_new(struct event_loop
*m
,
141 const char *queue_name
);
143 /* destroy work queue */
145 * The usage of work_queue_free is being transitioned to pass
146 * in the double pointer to remove use after free's.
148 extern void work_queue_free_and_null(struct work_queue
**wqp
);
150 /* Add the supplied data as an item onto the workqueue */
151 extern void work_queue_add(struct work_queue
*wq
, void *item
);
153 /* plug the queue, ie prevent it from being drained / processed */
154 extern void work_queue_plug(struct work_queue
*wq
);
155 /* unplug the queue, allow it to be drained again */
156 extern void work_queue_unplug(struct work_queue
*wq
);
158 bool work_queue_is_scheduled(struct work_queue
*wq
);
160 /* Helpers, exported for thread.c and command.c */
161 extern void work_queue_run(struct event
*thread
);
163 extern void workqueue_cmd_init(void);
169 #endif /* _QUAGGA_WORK_QUEUE_H */