watchlist.h (4169B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2016-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 typedef void (*watchlist_item_destroy_func_t)(void *data); 24 25 struct watchlist_item { 26 unsigned int id; 27 void *notify; 28 void *notify_data; 29 watchlist_item_destroy_func_t destroy; 30 }; 31 32 struct watchlist_ops { 33 void (*item_free)(struct watchlist_item *item); 34 }; 35 36 struct watchlist { 37 int next_id; 38 struct l_queue *items; 39 bool in_notify : 1; 40 bool stale_items : 1; 41 bool pending_destroy : 1; 42 const struct watchlist_ops *ops; 43 }; 44 45 void watchlist_init(struct watchlist *watchlist, 46 const struct watchlist_ops *ops); 47 unsigned int watchlist_add(struct watchlist *watchlist, void *notify, 48 void *notify_data, 49 watchlist_item_destroy_func_t destroy); 50 unsigned int watchlist_link(struct watchlist *watchlist, 51 struct watchlist_item *item, 52 void *notify, void *notify_data, 53 watchlist_item_destroy_func_t destroy); 54 bool watchlist_remove(struct watchlist *watchlist, unsigned int id); 55 void watchlist_destroy(struct watchlist *watchlist); 56 57 void __watchlist_prune_stale(struct watchlist *watchlist); 58 59 #define WATCHLIST_NOTIFY(list, type, args...) \ 60 do { \ 61 struct watchlist *watchlist = (list); \ 62 const struct l_queue_entry *entry = \ 63 l_queue_get_entries(watchlist->items); \ 64 \ 65 watchlist->in_notify = true; \ 66 for (; entry; entry = entry->next) { \ 67 struct watchlist_item *item = entry->data; \ 68 type t = item->notify; \ 69 if (item->id == 0) \ 70 continue; \ 71 t(args, item->notify_data); \ 72 if (watchlist->pending_destroy) \ 73 break; \ 74 } \ 75 watchlist->in_notify = false; \ 76 if (watchlist->pending_destroy) \ 77 watchlist_destroy(watchlist); \ 78 else if (watchlist->stale_items) \ 79 __watchlist_prune_stale(watchlist); \ 80 } while (false) \ 81 82 #define WATCHLIST_NOTIFY_MATCHES(list, match, match_data, type, args...) \ 83 do { \ 84 struct watchlist *watchlist = (list); \ 85 const struct l_queue_entry *entry = \ 86 l_queue_get_entries(watchlist->items); \ 87 \ 88 watchlist->in_notify = true; \ 89 for (; entry; entry = entry->next) { \ 90 struct watchlist_item *item = entry->data; \ 91 type t = item->notify; \ 92 \ 93 if (item->id == 0) \ 94 continue; \ 95 if (!match(item, match_data)) \ 96 continue; \ 97 \ 98 t(args, item->notify_data); \ 99 \ 100 if (watchlist->pending_destroy) \ 101 break; \ 102 } \ 103 watchlist->in_notify = false; \ 104 if (watchlist->pending_destroy) \ 105 watchlist_destroy(watchlist); \ 106 else if (watchlist->stale_items) \ 107 __watchlist_prune_stale(watchlist); \ 108 } while (false) 109 110 #define WATCHLIST_NOTIFY_NO_ARGS(list, type) \ 111 do { \ 112 struct watchlist *watchlist = (list); \ 113 const struct l_queue_entry *entry = \ 114 l_queue_get_entries(watchlist->items); \ 115 \ 116 watchlist->in_notify = true; \ 117 for (; entry; entry = entry->next) { \ 118 struct watchlist_item *item = entry->data; \ 119 type t = item->notify; \ 120 if (item->id == 0) \ 121 continue; \ 122 t(item->notify_data); \ 123 if (watchlist->pending_destroy) \ 124 break; \ 125 } \ 126 watchlist->in_notify = false; \ 127 if (watchlist->pending_destroy) \ 128 watchlist_destroy(watchlist); \ 129 else if (watchlist->stale_items) \ 130 __watchlist_prune_stale(watchlist); \ 131 } while (false)