paul@575 | 1 | /* |
paul@575 | 2 | * Notification-related functions. |
paul@575 | 3 | * |
paul@611 | 4 | * Copyright (C) 2023, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@575 | 5 | * |
paul@575 | 6 | * This program is free software; you can redistribute it and/or |
paul@575 | 7 | * modify it under the terms of the GNU General Public License as |
paul@575 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@575 | 9 | * the License, or (at your option) any later version. |
paul@575 | 10 | * |
paul@575 | 11 | * This program is distributed in the hope that it will be useful, |
paul@575 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@575 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@575 | 14 | * GNU General Public License for more details. |
paul@575 | 15 | * |
paul@575 | 16 | * You should have received a copy of the GNU General Public License |
paul@575 | 17 | * along with this program; if not, write to the Free Software |
paul@575 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@575 | 19 | * Boston, MA 02110-1301, USA |
paul@575 | 20 | */ |
paul@575 | 21 | |
paul@611 | 22 | #include <mutex> |
paul@575 | 23 | #include <notifier/notifier.h> |
paul@575 | 24 | |
paul@575 | 25 | #include "notify.h" |
paul@575 | 26 | |
paul@575 | 27 | |
paul@575 | 28 | |
paul@575 | 29 | /* Opaque notifier type for notifier_t. */ |
paul@575 | 30 | |
paul@575 | 31 | struct notifier |
paul@575 | 32 | { |
paul@575 | 33 | ObjectNotifier *obj; |
paul@575 | 34 | }; |
paul@575 | 35 | |
paul@611 | 36 | /* Lock protecting per-task notifier access. */ |
paul@611 | 37 | |
paul@611 | 38 | static std::mutex _task_lock; |
paul@611 | 39 | |
paul@611 | 40 | /* Per-task storage for the wrapped notifier. */ |
paul@611 | 41 | |
paul@611 | 42 | static struct notifier *_notifier = NULL; |
paul@611 | 43 | |
paul@611 | 44 | |
paul@611 | 45 | |
paul@575 | 46 | /* Return the notifiable details from a compatible object. */ |
paul@575 | 47 | |
paul@575 | 48 | notifiable_t *notify_notifiable(notifiable_base_t *notifiable) |
paul@575 | 49 | { |
paul@575 | 50 | return ¬ifiable->notifiable; |
paul@575 | 51 | } |
paul@575 | 52 | |
paul@575 | 53 | /* Return the notification flags for an object. */ |
paul@575 | 54 | |
paul@575 | 55 | notify_flags_t notify_notifications(notifiable_base_t *notifiable) |
paul@575 | 56 | { |
paul@575 | 57 | return notifiable->notifiable.notifications; |
paul@575 | 58 | } |
paul@575 | 59 | |
paul@575 | 60 | /* Return the notification values for an object. */ |
paul@575 | 61 | |
paul@575 | 62 | notify_values_t notify_notification_values(notifiable_base_t *notifiable) |
paul@575 | 63 | { |
paul@575 | 64 | return notifiable->notifiable.values; |
paul@575 | 65 | } |
paul@575 | 66 | |
paul@575 | 67 | /* Close a notifier object. */ |
paul@575 | 68 | |
paul@575 | 69 | void notify_close(notifier_t *notifier) |
paul@575 | 70 | { |
paul@611 | 71 | std::lock_guard<std::mutex> guard(_task_lock); |
paul@611 | 72 | |
paul@575 | 73 | delete notifier->obj; |
paul@575 | 74 | delete notifier; |
paul@611 | 75 | |
paul@611 | 76 | if (notifier == _notifier) |
paul@611 | 77 | _notifier = NULL; |
paul@575 | 78 | } |
paul@575 | 79 | |
paul@575 | 80 | /* Obtain a local notifier object. */ |
paul@575 | 81 | |
paul@575 | 82 | notifier_t *notify_get_local() |
paul@575 | 83 | { |
paul@575 | 84 | notifier_t *notifier = new notifier_t; |
paul@575 | 85 | |
paul@575 | 86 | notifier->obj = notifier_get_local_notifier(); |
paul@575 | 87 | return notifier; |
paul@575 | 88 | } |
paul@575 | 89 | |
paul@575 | 90 | /* Obtain the task-wide notifier object. */ |
paul@575 | 91 | |
paul@575 | 92 | notifier_t *notify_get_task() |
paul@575 | 93 | { |
paul@611 | 94 | std::lock_guard<std::mutex> guard(_task_lock); |
paul@575 | 95 | |
paul@611 | 96 | if (_notifier == NULL) |
paul@611 | 97 | _notifier = new notifier_t; |
paul@611 | 98 | |
paul@611 | 99 | _notifier->obj = notifier_get_task_notifier(); |
paul@611 | 100 | return _notifier; |
paul@575 | 101 | } |
paul@575 | 102 | |
paul@575 | 103 | /* Subscribe to notification events. */ |
paul@575 | 104 | |
paul@575 | 105 | long notify_subscribe(notifiable_t *notifiable, notify_flags_t flags, notifier_t *notifier) |
paul@575 | 106 | { |
paul@575 | 107 | return notifier->obj->subscribe(notifiable, flags); |
paul@575 | 108 | } |
paul@575 | 109 | |
paul@575 | 110 | /* Unsubscribe from notification events. */ |
paul@575 | 111 | |
paul@575 | 112 | long notify_unsubscribe(notifiable_t *notifiable, notifier_t *notifier) |
paul@575 | 113 | { |
paul@575 | 114 | return notifier->obj->unsubscribe(notifiable); |
paul@575 | 115 | } |
paul@575 | 116 | |
paul@575 | 117 | /* Wait for a notification event on a single object. */ |
paul@575 | 118 | |
paul@575 | 119 | long notify_wait(notifiable_t *notifiable, notifier_t *notifier) |
paul@575 | 120 | { |
paul@575 | 121 | return notifier->obj->wait_object(notifiable); |
paul@575 | 122 | } |
paul@575 | 123 | |
paul@575 | 124 | /* Wait for notification events on multiple objects. */ |
paul@575 | 125 | |
paul@575 | 126 | long notify_wait_many(notifiable_t **notifiable, notifier_t *notifier) |
paul@575 | 127 | { |
paul@575 | 128 | return notifier->obj->wait(notifiable); |
paul@575 | 129 | } |
paul@575 | 130 | |
paul@575 | 131 | // vim: tabstop=2 expandtab shiftwidth=2 |