paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 27 | /* Work queue default hold and cycle times - millisec */ |
paul | 190880d | 2005-11-14 12:07:47 +0000 | [diff] [blame] | 28 | #define WORK_QUEUE_DEFAULT_HOLD 50 /* hold-time between runs of a queue */ |
| 29 | #define WORK_QUEUE_DEFAULT_DELAY 10 /* minimum delay for queue runs */ |
| 30 | #define WORK_QUEUE_DEFAULT_FLOOD 40 /* flood factor, ~2s with prev values */ |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 31 | |
| 32 | /* action value, for use by item processor and item error handlers */ |
| 33 | typedef enum |
| 34 | { |
| 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 */ |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 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.. */ |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 43 | } wq_item_status; |
| 44 | |
| 45 | /* A single work queue item, unsurprisingly */ |
| 46 | struct work_queue_item |
| 47 | { |
| 48 | void *data; /* opaque data */ |
paul | 8436968 | 2005-04-27 12:39:27 +0000 | [diff] [blame] | 49 | unsigned short ran; /* # of times item has been run */ |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 52 | enum work_queue_flags |
| 53 | { |
| 54 | WQ_UNPLUGGED = 0, |
| 55 | WQ_PLUGGED = 1, |
| 56 | }; |
| 57 | |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 58 | struct work_queue |
| 59 | { |
paul | 889e931 | 2005-11-14 14:46:35 +0000 | [diff] [blame] | 60 | /* Everything but the specification struct is private |
| 61 | * the following may be read |
| 62 | */ |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 63 | struct thread_master *master; /* thread master */ |
| 64 | struct thread *thread; /* thread, if one is active */ |
| 65 | char *name; /* work queue name */ |
| 66 | |
paul | 190880d | 2005-11-14 12:07:47 +0000 | [diff] [blame] | 67 | /* Specification for this work queue. |
| 68 | * Public, must be set before use by caller. May be modified at will. |
| 69 | */ |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 70 | struct { |
paul | 889e931 | 2005-11-14 14:46:35 +0000 | [diff] [blame] | 71 | /* optional opaque user data, global to the queue. */ |
| 72 | void *data; |
| 73 | |
| 74 | /* work function to process items with: |
| 75 | * First argument is the workqueue queue. |
| 76 | * Second argument is the item data |
| 77 | */ |
| 78 | wq_item_status (*workfunc) (struct work_queue *, void *); |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 79 | |
| 80 | /* error handling function, optional */ |
| 81 | void (*errorfunc) (struct work_queue *, struct work_queue_item *); |
| 82 | |
| 83 | /* callback to delete user specific item data */ |
paul | 889e931 | 2005-11-14 14:46:35 +0000 | [diff] [blame] | 84 | void (*del_item_data) (struct work_queue *, void *); |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 85 | |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 86 | /* completion callback, called when queue is emptied, optional */ |
| 87 | void (*completion_func) (struct work_queue *); |
| 88 | |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 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 | unsigned int delay; /* min delay between queue runs, in ms */ |
paul | 190880d | 2005-11-14 12:07:47 +0000 | [diff] [blame] | 94 | |
| 95 | unsigned int flood; /* number of queue runs after which we consider |
| 96 | * queue to be flooded, where the runs are |
| 97 | * consecutive and each has used its full slot, |
| 98 | * and the queue has still not been cleared. If |
| 99 | * the queue is flooded, then we try harder to |
| 100 | * clear it by ignoring the hold and delay |
| 101 | * times. No point sparing CPU resources just |
| 102 | * to use ever more memory resources. |
| 103 | */ |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 104 | } spec; |
| 105 | |
| 106 | /* remaining fields should be opaque to users */ |
| 107 | struct list *items; /* queue item list */ |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 108 | unsigned long runs; /* runs count */ |
paul | 190880d | 2005-11-14 12:07:47 +0000 | [diff] [blame] | 109 | unsigned int runs_since_clear; /* number of runs since queue was |
| 110 | * last cleared |
| 111 | */ |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 112 | |
| 113 | struct { |
| 114 | unsigned int best; |
| 115 | unsigned int granularity; |
| 116 | unsigned long total; |
| 117 | } cycles; /* cycle counts */ |
paul | 889e931 | 2005-11-14 14:46:35 +0000 | [diff] [blame] | 118 | |
| 119 | /* private state */ |
| 120 | enum work_queue_flags flags; /* user set flag */ |
| 121 | char status; /* internal status */ |
| 122 | #define WQ_STATE_FLOODED (1 << 0) |
| 123 | |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | /* User API */ |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 127 | |
| 128 | /* create a new work queue, of given name. |
| 129 | * user must fill in the spec of the returned work queue before adding |
| 130 | * anything to it |
| 131 | */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 132 | extern struct work_queue *work_queue_new (struct thread_master *, |
| 133 | const char *); |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 134 | /* destroy work queue */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 135 | extern void work_queue_free (struct work_queue *); |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 136 | |
| 137 | /* Add the supplied data as an item onto the workqueue */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 138 | extern void work_queue_add (struct work_queue *, void *); |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 139 | |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 140 | /* plug the queue, ie prevent it from being drained / processed */ |
| 141 | extern void work_queue_plug (struct work_queue *wq); |
| 142 | /* unplug the queue, allow it to be drained again */ |
| 143 | extern void work_queue_unplug (struct work_queue *wq); |
| 144 | |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 145 | /* Helpers, exported for thread.c and command.c */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 146 | extern int work_queue_run (struct thread *); |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 147 | extern struct cmd_element show_work_queues_cmd; |
| 148 | #endif /* _QUAGGA_WORK_QUEUE_H */ |