]>
git.proxmox.com Git - mirror_frr.git/blob - lib/workqueue.h
4 * Copyright (C) 2005 Sun Microsystems, Inc.
6 * This file is part of Quagga.
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
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.
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
23 #ifndef _QUAGGA_WORK_QUEUE_H
24 #define _QUAGGA_WORK_QUEUE_H
33 DECLARE_MTYPE(WORK_QUEUE
);
35 /* Hold time for the initial schedule of a queue run, in millisec */
36 #define WORK_QUEUE_DEFAULT_HOLD 50
38 /* Retry for queue that is 'blocked' or 'retry later' */
39 #define WORK_QUEUE_DEFAULT_RETRY 0
41 /* action value, for use by item processor and item error handlers */
44 WQ_RETRY_NOW
, /* retry immediately */
45 WQ_RETRY_LATER
, /* retry later, cease processing work queue */
46 WQ_REQUEUE
, /* requeue item, continue processing work queue */
47 WQ_QUEUE_BLOCKED
, /* Queue cant be processed at this time.
48 * Similar to WQ_RETRY_LATER, but doesn't penalise
49 * the particular item.. */
52 /* A single work queue item, unsurprisingly */
53 struct work_queue_item
{
54 STAILQ_ENTRY(work_queue_item
) wq
;
55 void *data
; /* opaque data */
56 unsigned short ran
; /* # of times item has been run */
59 #define WQ_UNPLUGGED (1 << 0) /* available for draining */
62 /* Everything but the specification struct is private
63 * the following may be read
65 struct thread_master
*master
; /* thread master */
66 struct thread
*thread
; /* thread, if one is active */
67 char *name
; /* work queue name */
69 /* Specification for this work queue.
70 * Public, must be set before use by caller. May be modified at will.
73 /* optional opaque user data, global to the queue. */
76 /* work function to process items with:
77 * First argument is the workqueue queue.
78 * Second argument is the item data
80 wq_item_status (*workfunc
)(struct work_queue
*, void *);
82 /* callback to delete user specific item data */
83 void (*del_item_data
)(struct work_queue
*, void *);
85 /* completion callback, called when queue is emptied, optional
87 void (*completion_func
)(struct work_queue
*);
89 /* max number of retries to make for item that errors */
90 unsigned int max_retries
;
92 unsigned int hold
; /* hold time for first run, in ms */
95 yield
; /* yield time in us for associated thread */
97 uint32_t retry
; /* Optional retry timeout if queue is blocked */
100 /* remaining fields should be opaque to users */
101 STAILQ_HEAD(work_queue_items
, work_queue_item
)
102 items
; /* queue item list */
103 int item_count
; /* queued items */
104 unsigned long runs
; /* runs count */
105 unsigned long yields
; /* yields count */
109 unsigned int granularity
;
111 } cycles
; /* cycle counts */
114 uint16_t flags
; /* user set flag */
119 static inline int work_queue_item_count(struct work_queue
*wq
)
121 return wq
->item_count
;
124 static inline bool work_queue_empty(struct work_queue
*wq
)
126 return (wq
->item_count
== 0) ? true : false;
129 static inline struct work_queue_item
*
130 work_queue_last_item(struct work_queue
*wq
)
132 return STAILQ_LAST(&wq
->items
, work_queue_item
, wq
);
135 static inline void work_queue_item_enqueue(struct work_queue
*wq
,
136 struct work_queue_item
*item
)
138 STAILQ_INSERT_TAIL(&wq
->items
, item
, wq
);
142 static inline void work_queue_item_dequeue(struct work_queue
*wq
,
143 struct work_queue_item
*item
)
145 assert(wq
->item_count
> 0);
148 STAILQ_REMOVE(&wq
->items
, item
, work_queue_item
, wq
);
151 /* create a new work queue, of given name.
152 * user must fill in the spec of the returned work queue before adding
155 extern struct work_queue
*work_queue_new(struct thread_master
*m
,
156 const char *queue_name
);
158 /* destroy work queue */
160 * The usage of work_queue_free is being transitioned to pass
161 * in the double pointer to remove use after free's.
163 extern void work_queue_free_and_null(struct work_queue
**wqp
);
165 /* Add the supplied data as an item onto the workqueue */
166 extern void work_queue_add(struct work_queue
*wq
, void *item
);
168 /* plug the queue, ie prevent it from being drained / processed */
169 extern void work_queue_plug(struct work_queue
*wq
);
170 /* unplug the queue, allow it to be drained again */
171 extern void work_queue_unplug(struct work_queue
*wq
);
173 bool work_queue_is_scheduled(struct work_queue
*wq
);
175 /* Helpers, exported for thread.c and command.c */
176 extern void work_queue_run(struct thread
*thread
);
178 extern void workqueue_cmd_init(void);
184 #endif /* _QUAGGA_WORK_QUEUE_H */