paul@124 | 1 | /* |
paul@124 | 2 | * File event notification support. |
paul@124 | 3 | * |
paul@124 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@124 | 5 | * |
paul@124 | 6 | * This program is free software; you can redistribute it and/or |
paul@124 | 7 | * modify it under the terms of the GNU General Public License as |
paul@124 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@124 | 9 | * the License, or (at your option) any later version. |
paul@124 | 10 | * |
paul@124 | 11 | * This program is distributed in the hope that it will be useful, |
paul@124 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@124 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@124 | 14 | * GNU General Public License for more details. |
paul@124 | 15 | * |
paul@124 | 16 | * You should have received a copy of the GNU General Public License |
paul@124 | 17 | * along with this program; if not, write to the Free Software |
paul@124 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@124 | 19 | * Boston, MA 02110-1301, USA |
paul@124 | 20 | */ |
paul@124 | 21 | |
paul@124 | 22 | #include <map> |
paul@124 | 23 | #include <mutex> |
paul@124 | 24 | |
paul@124 | 25 | #include <ipc/cap_alloc.h> |
paul@124 | 26 | #include <ipc/server.h> |
paul@124 | 27 | |
paul@124 | 28 | #include <pthread.h> |
paul@124 | 29 | #include <pthread-l4.h> |
paul@124 | 30 | |
paul@124 | 31 | #include "notification_client.h" |
paul@124 | 32 | #include "notifier.h" |
paul@124 | 33 | |
paul@124 | 34 | |
paul@124 | 35 | |
paul@124 | 36 | /* Thread-local storage workaround. */ |
paul@124 | 37 | |
paul@124 | 38 | static std::mutex lock; |
paul@124 | 39 | static std::map<l4_cap_idx_t, FileNotifier *> notifiers; |
paul@124 | 40 | |
paul@124 | 41 | FileNotifier *get_notifier() |
paul@124 | 42 | { |
paul@124 | 43 | std::lock_guard<std::mutex> guard(lock); |
paul@124 | 44 | |
paul@124 | 45 | l4_cap_idx_t thread = pthread_l4_cap(pthread_self()); |
paul@124 | 46 | FileNotifier *notifier = notifiers[thread]; |
paul@124 | 47 | |
paul@124 | 48 | /* Start any new notifier. */ |
paul@124 | 49 | |
paul@124 | 50 | if (notifier == NULL) |
paul@124 | 51 | { |
paul@124 | 52 | notifier = new FileNotifier; |
paul@124 | 53 | notifiers[thread] = notifier; |
paul@124 | 54 | notifier->start(); |
paul@124 | 55 | } |
paul@124 | 56 | |
paul@124 | 57 | return notifier; |
paul@124 | 58 | } |
paul@124 | 59 | |
paul@124 | 60 | |
paul@124 | 61 | |
paul@124 | 62 | /* Subscribe to notification events on a file. */ |
paul@124 | 63 | |
paul@124 | 64 | long FileNotifier::subscribe(file_t *file, notify_flags_t flags) |
paul@124 | 65 | { |
paul@124 | 66 | /* Create a notification endpoint, if necessary. */ |
paul@124 | 67 | |
paul@124 | 68 | if (l4_is_invalid_cap(file->notifier)) |
paul@124 | 69 | { |
paul@124 | 70 | long err = ipc_server_new_for_thread(&file->notifier, file, _thread); |
paul@124 | 71 | |
paul@124 | 72 | if (err) |
paul@124 | 73 | return err; |
paul@124 | 74 | } |
paul@124 | 75 | |
paul@124 | 76 | client_Notification notify(file->ref); |
paul@124 | 77 | |
paul@124 | 78 | return notify.subscribe(file->notifier, flags); |
paul@124 | 79 | } |
paul@124 | 80 | |
paul@124 | 81 | /* Unsubscribe from notification events on a file. */ |
paul@124 | 82 | |
paul@124 | 83 | long FileNotifier::unsubscribe(file_t *file) |
paul@124 | 84 | { |
paul@124 | 85 | if (l4_is_invalid_cap(file->notifier)) |
paul@124 | 86 | return -L4_EINVAL; |
paul@124 | 87 | |
paul@124 | 88 | ipc_cap_free_um(file->notifier); |
paul@124 | 89 | |
paul@124 | 90 | client_Notification notify(file->ref); |
paul@124 | 91 | |
paul@124 | 92 | return notify.unsubscribe(); |
paul@124 | 93 | } |
paul@124 | 94 | |
paul@124 | 95 | /* Handle a notification event for a file. Ideally, this would be invoked by the |
paul@124 | 96 | generic server dispatch mechanism, with the gate label being interpreted and |
paul@124 | 97 | provided as the first parameter. */ |
paul@124 | 98 | |
paul@124 | 99 | void FileNotifier::_notify(file_t *file, notify_flags_t flags) |
paul@124 | 100 | { |
paul@124 | 101 | std::unique_lock<std::mutex> guard(_lock); |
paul@124 | 102 | |
paul@125 | 103 | /* Record the flags for the file object. */ |
paul@124 | 104 | |
paul@126 | 105 | _affected_flags[file] = _affected_flags[file] | flags; |
paul@124 | 106 | _affected.push_back(file); |
paul@124 | 107 | |
paul@124 | 108 | /* Notify any waiting caller. */ |
paul@124 | 109 | |
paul@124 | 110 | _notified.notify_one(); |
paul@124 | 111 | } |
paul@124 | 112 | |
paul@124 | 113 | /* Listen for notifications. */ |
paul@124 | 114 | |
paul@124 | 115 | void FileNotifier::mainloop() |
paul@124 | 116 | { |
paul@124 | 117 | ipc_message_t msg; |
paul@124 | 118 | l4_umword_t label; |
paul@124 | 119 | |
paul@124 | 120 | while (1) |
paul@124 | 121 | { |
paul@124 | 122 | ipc_message_wait(&msg, &label); |
paul@124 | 123 | |
paul@124 | 124 | /* Clear lower label bits. */ |
paul@124 | 125 | |
paul@124 | 126 | label = label & ~3UL; |
paul@124 | 127 | |
paul@124 | 128 | /* Ignore erroneous messages. */ |
paul@124 | 129 | |
paul@124 | 130 | if (l4_ipc_error(msg.tag, l4_utcb())) |
paul@124 | 131 | continue; |
paul@124 | 132 | |
paul@126 | 133 | /* Interpret gate labels as file objects. */ |
paul@126 | 134 | |
paul@126 | 135 | file_t *file = (file_t *) label; |
paul@126 | 136 | |
paul@126 | 137 | /* Obtain message details. */ |
paul@126 | 138 | |
paul@126 | 139 | ipc_message_open(&msg); |
paul@126 | 140 | |
paul@126 | 141 | notify_flags_t flags = ipc_message_get_word(&msg, 0); |
paul@126 | 142 | |
paul@124 | 143 | /* Reply to notifications. */ |
paul@124 | 144 | |
paul@124 | 145 | ipc_message_reply(&msg); |
paul@126 | 146 | ipc_message_discard(&msg); |
paul@124 | 147 | |
paul@124 | 148 | /* Register the notification. */ |
paul@124 | 149 | |
paul@124 | 150 | _notify(file, flags); |
paul@124 | 151 | } |
paul@124 | 152 | |
paul@124 | 153 | ipc_message_free(&msg); |
paul@124 | 154 | } |
paul@124 | 155 | |
paul@124 | 156 | /* Invoke the mainloop in a thread. */ |
paul@124 | 157 | |
paul@124 | 158 | static void *notifier_mainloop(void *data) |
paul@124 | 159 | { |
paul@124 | 160 | FileNotifier *notifier = reinterpret_cast<FileNotifier *>(data); |
paul@124 | 161 | |
paul@124 | 162 | notifier->mainloop(); |
paul@124 | 163 | return 0; |
paul@124 | 164 | } |
paul@124 | 165 | |
paul@124 | 166 | /* Start listening for notifications. */ |
paul@124 | 167 | |
paul@124 | 168 | long FileNotifier::start() |
paul@124 | 169 | { |
paul@124 | 170 | if (_started) |
paul@124 | 171 | return L4_EOK; |
paul@124 | 172 | |
paul@124 | 173 | pthread_t thread; |
paul@124 | 174 | pthread_attr_t attr; |
paul@124 | 175 | long err; |
paul@124 | 176 | |
paul@124 | 177 | pthread_attr_init(&attr); |
paul@124 | 178 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
paul@124 | 179 | |
paul@124 | 180 | err = pthread_create(&thread, &attr, notifier_mainloop, this); |
paul@124 | 181 | if (err) |
paul@124 | 182 | return err; |
paul@124 | 183 | |
paul@124 | 184 | _thread = pthread_l4_cap(thread); |
paul@124 | 185 | _started = true; |
paul@124 | 186 | |
paul@124 | 187 | return L4_EOK; |
paul@124 | 188 | } |
paul@124 | 189 | |
paul@124 | 190 | /* Wait for notification events on files. */ |
paul@124 | 191 | |
paul@124 | 192 | long FileNotifier::wait(file_t **file) |
paul@124 | 193 | { |
paul@124 | 194 | std::unique_lock<std::mutex> guard(_lock); |
paul@124 | 195 | |
paul@124 | 196 | while (1) |
paul@124 | 197 | { |
paul@124 | 198 | if (!_affected.empty()) |
paul@124 | 199 | { |
paul@124 | 200 | *file = _affected.front(); |
paul@124 | 201 | _affected.pop_front(); |
paul@125 | 202 | |
paul@125 | 203 | (*file)->notifications = _affected_flags[*file]; |
paul@125 | 204 | _affected_flags.erase(*file); |
paul@124 | 205 | return L4_EOK; |
paul@124 | 206 | } |
paul@124 | 207 | else |
paul@124 | 208 | _notified.wait(guard); |
paul@124 | 209 | } |
paul@124 | 210 | |
paul@124 | 211 | return L4_EOK; |
paul@124 | 212 | } |
paul@124 | 213 | |
paul@124 | 214 | // vim: tabstop=2 expandtab shiftwidth=2 |