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