blob: aac786058334d93641d0a9aa32941f52382e1072 [file] [log] [blame]
paul354d1192005-04-25 16:26:42 +00001/*
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 *
18 * You should have received a copy of the GNU General Public License
19 * along with Quagga; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * 02111-1307, USA.
22 */
23
24#ifndef _QUAGGA_WORK_QUEUE_H
25#define _QUAGGA_WORK_QUEUE_H
26
paul306d8892006-02-02 17:50:19 +000027/* Hold time for the initial schedule of a queue run, in millisec */
28#define WORK_QUEUE_DEFAULT_HOLD 50
paul354d1192005-04-25 16:26:42 +000029
30/* action value, for use by item processor and item error handlers */
31typedef enum
32{
33 WQ_SUCCESS = 0,
34 WQ_ERROR, /* Error, run error handler if provided */
35 WQ_RETRY_NOW, /* retry immediately */
36 WQ_RETRY_LATER, /* retry later, cease processing work queue */
paul269d74f2005-05-23 13:42:46 +000037 WQ_REQUEUE, /* requeue item, continue processing work queue */
38 WQ_QUEUE_BLOCKED, /* Queue cant be processed at this time.
39 * Similar to WQ_RETRY_LATER, but doesn't penalise
40 * the particular item.. */
paul354d1192005-04-25 16:26:42 +000041} wq_item_status;
42
43/* A single work queue item, unsurprisingly */
44struct work_queue_item
45{
46 void *data; /* opaque data */
paul84369682005-04-27 12:39:27 +000047 unsigned short ran; /* # of times item has been run */
paul354d1192005-04-25 16:26:42 +000048};
49
Denis Ovsienko6ce80bd2007-11-12 14:55:01 +000050#define WQ_UNPLUGGED (1 << 0) /* available for draining */
paul269d74f2005-05-23 13:42:46 +000051
paul354d1192005-04-25 16:26:42 +000052struct work_queue
53{
paul889e9312005-11-14 14:46:35 +000054 /* Everything but the specification struct is private
55 * the following may be read
56 */
paul354d1192005-04-25 16:26:42 +000057 struct thread_master *master; /* thread master */
58 struct thread *thread; /* thread, if one is active */
59 char *name; /* work queue name */
60
paul190880d2005-11-14 12:07:47 +000061 /* Specification for this work queue.
62 * Public, must be set before use by caller. May be modified at will.
63 */
paul354d1192005-04-25 16:26:42 +000064 struct {
paul889e9312005-11-14 14:46:35 +000065 /* optional opaque user data, global to the queue. */
66 void *data;
67
68 /* work function to process items with:
69 * First argument is the workqueue queue.
70 * Second argument is the item data
71 */
72 wq_item_status (*workfunc) (struct work_queue *, void *);
paul354d1192005-04-25 16:26:42 +000073
74 /* error handling function, optional */
75 void (*errorfunc) (struct work_queue *, struct work_queue_item *);
76
77 /* callback to delete user specific item data */
paul889e9312005-11-14 14:46:35 +000078 void (*del_item_data) (struct work_queue *, void *);
paul354d1192005-04-25 16:26:42 +000079
paul269d74f2005-05-23 13:42:46 +000080 /* completion callback, called when queue is emptied, optional */
81 void (*completion_func) (struct work_queue *);
82
paul354d1192005-04-25 16:26:42 +000083 /* max number of retries to make for item that errors */
84 unsigned int max_retries;
85
86 unsigned int hold; /* hold time for first run, in ms */
paul354d1192005-04-25 16:26:42 +000087 } spec;
88
89 /* remaining fields should be opaque to users */
90 struct list *items; /* queue item list */
paul269d74f2005-05-23 13:42:46 +000091 unsigned long runs; /* runs count */
Paul Jakmaa2f0db22016-02-25 16:41:56 +000092 unsigned long worst_usec;
paul354d1192005-04-25 16:26:42 +000093
94 struct {
95 unsigned int best;
Paul Jakmaa2f0db22016-02-25 16:41:56 +000096 unsigned int worst;
paul354d1192005-04-25 16:26:42 +000097 unsigned int granularity;
98 unsigned long total;
99 } cycles; /* cycle counts */
paul889e9312005-11-14 14:46:35 +0000100
101 /* private state */
Denis Ovsienko6ce80bd2007-11-12 14:55:01 +0000102 u_int16_t flags; /* user set flag */
paul354d1192005-04-25 16:26:42 +0000103};
104
105/* User API */
paul269d74f2005-05-23 13:42:46 +0000106
107/* create a new work queue, of given name.
108 * user must fill in the spec of the returned work queue before adding
109 * anything to it
110 */
paul8cc41982005-05-06 21:25:49 +0000111extern struct work_queue *work_queue_new (struct thread_master *,
112 const char *);
paul269d74f2005-05-23 13:42:46 +0000113/* destroy work queue */
paul8cc41982005-05-06 21:25:49 +0000114extern void work_queue_free (struct work_queue *);
paul269d74f2005-05-23 13:42:46 +0000115
116/* Add the supplied data as an item onto the workqueue */
paul8cc41982005-05-06 21:25:49 +0000117extern void work_queue_add (struct work_queue *, void *);
paul354d1192005-04-25 16:26:42 +0000118
paul269d74f2005-05-23 13:42:46 +0000119/* plug the queue, ie prevent it from being drained / processed */
120extern void work_queue_plug (struct work_queue *wq);
121/* unplug the queue, allow it to be drained again */
122extern void work_queue_unplug (struct work_queue *wq);
123
Paul Jakma13c2a3d2015-09-15 16:16:42 +0100124bool work_queue_is_scheduled (struct work_queue *);
125
paul354d1192005-04-25 16:26:42 +0000126/* Helpers, exported for thread.c and command.c */
paul8cc41982005-05-06 21:25:49 +0000127extern int work_queue_run (struct thread *);
paul354d1192005-04-25 16:26:42 +0000128extern struct cmd_element show_work_queues_cmd;
129#endif /* _QUAGGA_WORK_QUEUE_H */