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 | |
paul | 306d889 | 2006-02-02 17:50:19 +0000 | [diff] [blame] | 27 | /* Hold time for the initial schedule of a queue run, in millisec */ |
| 28 | #define WORK_QUEUE_DEFAULT_HOLD 50 |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 29 | |
| 30 | /* action value, for use by item processor and item error handlers */ |
| 31 | typedef 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 */ |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 37 | 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.. */ |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 41 | } wq_item_status; |
| 42 | |
| 43 | /* A single work queue item, unsurprisingly */ |
| 44 | struct work_queue_item |
| 45 | { |
| 46 | void *data; /* opaque data */ |
paul | 8436968 | 2005-04-27 12:39:27 +0000 | [diff] [blame] | 47 | unsigned short ran; /* # of times item has been run */ |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
Denis Ovsienko | 6ce80bd | 2007-11-12 14:55:01 +0000 | [diff] [blame] | 50 | #define WQ_UNPLUGGED (1 << 0) /* available for draining */ |
| 51 | #define WQ_AIM_HEAD (1 << 1) /* add new items before list head, not after tail */ |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 52 | |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 53 | struct work_queue |
| 54 | { |
paul | 889e931 | 2005-11-14 14:46:35 +0000 | [diff] [blame] | 55 | /* Everything but the specification struct is private |
| 56 | * the following may be read |
| 57 | */ |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 58 | struct thread_master *master; /* thread master */ |
| 59 | struct thread *thread; /* thread, if one is active */ |
| 60 | char *name; /* work queue name */ |
| 61 | |
paul | 190880d | 2005-11-14 12:07:47 +0000 | [diff] [blame] | 62 | /* Specification for this work queue. |
| 63 | * Public, must be set before use by caller. May be modified at will. |
| 64 | */ |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 65 | struct { |
paul | 889e931 | 2005-11-14 14:46:35 +0000 | [diff] [blame] | 66 | /* optional opaque user data, global to the queue. */ |
| 67 | void *data; |
| 68 | |
| 69 | /* work function to process items with: |
| 70 | * First argument is the workqueue queue. |
| 71 | * Second argument is the item data |
| 72 | */ |
| 73 | wq_item_status (*workfunc) (struct work_queue *, void *); |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 74 | |
| 75 | /* error handling function, optional */ |
| 76 | void (*errorfunc) (struct work_queue *, struct work_queue_item *); |
| 77 | |
| 78 | /* callback to delete user specific item data */ |
paul | 889e931 | 2005-11-14 14:46:35 +0000 | [diff] [blame] | 79 | void (*del_item_data) (struct work_queue *, void *); |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 80 | |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 81 | /* completion callback, called when queue is emptied, optional */ |
| 82 | void (*completion_func) (struct work_queue *); |
| 83 | |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 84 | /* max number of retries to make for item that errors */ |
| 85 | unsigned int max_retries; |
| 86 | |
| 87 | unsigned int hold; /* hold time for first run, in ms */ |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 88 | } spec; |
| 89 | |
| 90 | /* remaining fields should be opaque to users */ |
| 91 | struct list *items; /* queue item list */ |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 92 | unsigned long runs; /* runs count */ |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 93 | |
| 94 | struct { |
| 95 | unsigned int best; |
| 96 | unsigned int granularity; |
| 97 | unsigned long total; |
| 98 | } cycles; /* cycle counts */ |
paul | 889e931 | 2005-11-14 14:46:35 +0000 | [diff] [blame] | 99 | |
| 100 | /* private state */ |
Denis Ovsienko | 6ce80bd | 2007-11-12 14:55:01 +0000 | [diff] [blame] | 101 | u_int16_t flags; /* user set flag */ |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /* User API */ |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 105 | |
| 106 | /* create a new work queue, of given name. |
| 107 | * user must fill in the spec of the returned work queue before adding |
| 108 | * anything to it |
| 109 | */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 110 | extern struct work_queue *work_queue_new (struct thread_master *, |
| 111 | const char *); |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 112 | /* destroy work queue */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 113 | extern void work_queue_free (struct work_queue *); |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 114 | |
| 115 | /* Add the supplied data as an item onto the workqueue */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 116 | extern void work_queue_add (struct work_queue *, void *); |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 117 | |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 118 | /* plug the queue, ie prevent it from being drained / processed */ |
| 119 | extern void work_queue_plug (struct work_queue *wq); |
| 120 | /* unplug the queue, allow it to be drained again */ |
| 121 | extern void work_queue_unplug (struct work_queue *wq); |
Denis Ovsienko | 6ce80bd | 2007-11-12 14:55:01 +0000 | [diff] [blame] | 122 | /* control the value for WQ_AIM_HEAD flag */ |
| 123 | extern void work_queue_aim_head (struct work_queue *wq, const unsigned); |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 124 | |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 125 | /* Helpers, exported for thread.c and command.c */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 126 | extern int work_queue_run (struct thread *); |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 127 | extern struct cmd_element show_work_queues_cmd; |
| 128 | #endif /* _QUAGGA_WORK_QUEUE_H */ |