paul@124 | 1 | /* |
paul@124 | 2 | * File event notification support. |
paul@124 | 3 | * |
paul@285 | 4 | * Copyright (C) 2021, 2022 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@177 | 36 | /* Null notification state. */ |
paul@177 | 37 | |
paul@177 | 38 | static FileNotificationState _null_state; |
paul@177 | 39 | |
paul@177 | 40 | |
paul@177 | 41 | |
paul@177 | 42 | /* Lock protecting per-task notifier access. */ |
paul@177 | 43 | |
paul@177 | 44 | static std::mutex _lock; |
paul@177 | 45 | |
paul@177 | 46 | /* Per-task storage for specific waiting operations. */ |
paul@177 | 47 | |
paul@177 | 48 | static SpecificFileNotifier *_notifier = NULL; |
paul@177 | 49 | |
paul@177 | 50 | |
paul@124 | 51 | |
paul@177 | 52 | /* Return the per-task notifier for file-specific waiting operations. */ |
paul@177 | 53 | |
paul@180 | 54 | SpecificFileNotifier *notifier_get_task_notifier() |
paul@177 | 55 | { |
paul@177 | 56 | std::lock_guard<std::mutex> guard(_lock); |
paul@177 | 57 | |
paul@177 | 58 | /* Start any new notifier. */ |
paul@124 | 59 | |
paul@177 | 60 | if (_notifier == NULL) |
paul@177 | 61 | { |
paul@177 | 62 | _notifier = new SpecificFileNotifier; |
paul@177 | 63 | _notifier->start(); |
paul@177 | 64 | } |
paul@177 | 65 | |
paul@177 | 66 | return _notifier; |
paul@177 | 67 | } |
paul@177 | 68 | |
paul@180 | 69 | /* Return a local notifier for general file waiting operations. */ |
paul@124 | 70 | |
paul@180 | 71 | GeneralFileNotifier *notifier_get_local_notifier() |
paul@180 | 72 | { |
paul@180 | 73 | GeneralFileNotifier *notifier = new GeneralFileNotifier; |
paul@124 | 74 | |
paul@180 | 75 | notifier->start(); |
paul@180 | 76 | return notifier; |
paul@124 | 77 | } |
paul@124 | 78 | |
paul@124 | 79 | |
paul@124 | 80 | |
paul@177 | 81 | /* Invoke the mainloop in a thread. */ |
paul@124 | 82 | |
paul@177 | 83 | static void *notifier_mainloop(void *data) |
paul@124 | 84 | { |
paul@177 | 85 | FileNotifier *notifier = reinterpret_cast<FileNotifier *>(data); |
paul@129 | 86 | |
paul@177 | 87 | notifier->mainloop(); |
paul@177 | 88 | return 0; |
paul@124 | 89 | } |
paul@124 | 90 | |
paul@180 | 91 | |
paul@180 | 92 | |
paul@180 | 93 | /* Virtual destructor required for base class instance reference deletion. */ |
paul@180 | 94 | |
paul@180 | 95 | FileNotifier::~FileNotifier() |
paul@180 | 96 | { |
paul@180 | 97 | } |
paul@180 | 98 | |
paul@124 | 99 | /* Listen for notifications. */ |
paul@124 | 100 | |
paul@124 | 101 | void FileNotifier::mainloop() |
paul@124 | 102 | { |
paul@124 | 103 | ipc_message_t msg; |
paul@124 | 104 | l4_umword_t label; |
paul@124 | 105 | |
paul@124 | 106 | while (1) |
paul@124 | 107 | { |
paul@124 | 108 | ipc_message_wait(&msg, &label); |
paul@124 | 109 | |
paul@124 | 110 | /* Clear lower label bits. */ |
paul@124 | 111 | |
paul@124 | 112 | label = label & ~3UL; |
paul@124 | 113 | |
paul@124 | 114 | /* Ignore erroneous messages. */ |
paul@124 | 115 | |
paul@124 | 116 | if (l4_ipc_error(msg.tag, l4_utcb())) |
paul@124 | 117 | continue; |
paul@124 | 118 | |
paul@126 | 119 | /* Interpret gate labels as file objects. */ |
paul@126 | 120 | |
paul@126 | 121 | file_t *file = (file_t *) label; |
paul@126 | 122 | |
paul@126 | 123 | /* Obtain message details. */ |
paul@126 | 124 | |
paul@126 | 125 | ipc_message_open(&msg); |
paul@126 | 126 | |
paul@126 | 127 | notify_flags_t flags = ipc_message_get_word(&msg, 0); |
paul@126 | 128 | |
paul@124 | 129 | /* Reply to notifications. */ |
paul@124 | 130 | |
paul@124 | 131 | ipc_message_reply(&msg); |
paul@126 | 132 | ipc_message_discard(&msg); |
paul@124 | 133 | |
paul@124 | 134 | /* Register the notification. */ |
paul@124 | 135 | |
paul@124 | 136 | _notify(file, flags); |
paul@124 | 137 | } |
paul@124 | 138 | |
paul@124 | 139 | ipc_message_free(&msg); |
paul@124 | 140 | } |
paul@124 | 141 | |
paul@124 | 142 | /* Start listening for notifications. */ |
paul@124 | 143 | |
paul@124 | 144 | long FileNotifier::start() |
paul@124 | 145 | { |
paul@124 | 146 | if (_started) |
paul@124 | 147 | return L4_EOK; |
paul@124 | 148 | |
paul@124 | 149 | pthread_t thread; |
paul@124 | 150 | pthread_attr_t attr; |
paul@124 | 151 | long err; |
paul@124 | 152 | |
paul@124 | 153 | pthread_attr_init(&attr); |
paul@124 | 154 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
paul@124 | 155 | |
paul@124 | 156 | err = pthread_create(&thread, &attr, notifier_mainloop, this); |
paul@124 | 157 | if (err) |
paul@124 | 158 | return err; |
paul@124 | 159 | |
paul@124 | 160 | _thread = pthread_l4_cap(thread); |
paul@124 | 161 | _started = true; |
paul@124 | 162 | |
paul@124 | 163 | return L4_EOK; |
paul@124 | 164 | } |
paul@124 | 165 | |
paul@177 | 166 | |
paul@177 | 167 | |
paul@177 | 168 | /* Return a notification state object for the given file or a null object if no |
paul@177 | 169 | record existed for the file. */ |
paul@177 | 170 | |
paul@177 | 171 | FileNotificationState &FileNotifier::file_state(file_t *file, bool create) |
paul@177 | 172 | { |
paul@177 | 173 | FileNotificationStates::iterator it = _state.find(file); |
paul@177 | 174 | |
paul@177 | 175 | if (it == _state.end()) |
paul@177 | 176 | { |
paul@177 | 177 | if (create) |
paul@177 | 178 | return _state[file]; |
paul@177 | 179 | else |
paul@177 | 180 | return _null_state; |
paul@177 | 181 | } |
paul@177 | 182 | |
paul@177 | 183 | return it->second; |
paul@177 | 184 | } |
paul@177 | 185 | |
paul@177 | 186 | /* Subscribe to notification events on a file. */ |
paul@177 | 187 | |
paul@177 | 188 | long FileNotifier::subscribe(file_t *file, notify_flags_t flags) |
paul@177 | 189 | { |
paul@177 | 190 | /* Acquire the lock for state lookup. */ |
paul@177 | 191 | |
paul@177 | 192 | std::unique_lock<std::mutex> state_guard(_state_lock); |
paul@177 | 193 | |
paul@177 | 194 | FileNotificationState &state = file_state(file, true); |
paul@288 | 195 | long err; |
paul@177 | 196 | |
paul@177 | 197 | /* Create a notification endpoint, if necessary. */ |
paul@177 | 198 | |
paul@177 | 199 | if (state.is_null()) |
paul@177 | 200 | { |
paul@288 | 201 | err = ipc_server_new_for_thread(&state.endpoint, file, _thread); |
paul@177 | 202 | |
paul@177 | 203 | if (err) |
paul@177 | 204 | return err; |
paul@177 | 205 | } |
paul@177 | 206 | |
paul@290 | 207 | /* Subscribe, sending the notification endpoint. */ |
paul@288 | 208 | |
paul@177 | 209 | client_Notification notify(file->ref); |
paul@177 | 210 | |
paul@290 | 211 | err = notify.subscribe(state.endpoint, flags); |
paul@288 | 212 | |
paul@288 | 213 | if (err) |
paul@288 | 214 | { |
paul@288 | 215 | ipc_cap_free_um(state.endpoint); |
paul@288 | 216 | _state.erase(file); |
paul@288 | 217 | return err; |
paul@288 | 218 | } |
paul@288 | 219 | |
paul@288 | 220 | return L4_EOK; |
paul@177 | 221 | } |
paul@177 | 222 | |
paul@177 | 223 | /* Unsubscribe from notification events on a file. */ |
paul@177 | 224 | |
paul@177 | 225 | long FileNotifier::unsubscribe(file_t *file) |
paul@177 | 226 | { |
paul@177 | 227 | /* Acquire the lock for state lookup. */ |
paul@177 | 228 | |
paul@177 | 229 | std::unique_lock<std::mutex> state_guard(_state_lock); |
paul@177 | 230 | |
paul@177 | 231 | FileNotificationState &state = file_state(file, false); |
paul@177 | 232 | |
paul@177 | 233 | if (state.is_null()) |
paul@177 | 234 | return -L4_EINVAL; |
paul@177 | 235 | |
paul@290 | 236 | /* Unsubscribe via the notification interface. */ |
paul@288 | 237 | |
paul@290 | 238 | client_Notification notify(file->ref); |
paul@288 | 239 | |
paul@290 | 240 | notify.unsubscribe(); |
paul@288 | 241 | |
paul@288 | 242 | ipc_cap_free_um(state.endpoint); |
paul@288 | 243 | |
paul@285 | 244 | _state.erase(file); |
paul@177 | 245 | |
paul@177 | 246 | /* Remove the lock for updating file state. */ |
paul@177 | 247 | |
paul@177 | 248 | _file_locks.erase(file); |
paul@177 | 249 | |
paul@177 | 250 | return L4_EOK; |
paul@177 | 251 | } |
paul@177 | 252 | |
paul@177 | 253 | |
paul@177 | 254 | |
paul@177 | 255 | /* Handle a notification event for a file. Ideally, this would be invoked by the |
paul@177 | 256 | generic server dispatch mechanism, with the gate label being interpreted and |
paul@177 | 257 | provided as the first parameter. */ |
paul@177 | 258 | |
paul@177 | 259 | void GeneralFileNotifier::_notify(file_t *file, notify_flags_t flags) |
paul@177 | 260 | { |
paul@177 | 261 | /* Enter critical section for the notifier (affecting all files). */ |
paul@177 | 262 | |
paul@177 | 263 | std::unique_lock<std::mutex> general_guard(_general_lock); |
paul@177 | 264 | |
paul@177 | 265 | /* Acquire the lock for state lookup. */ |
paul@177 | 266 | |
paul@177 | 267 | std::unique_lock<std::mutex> state_guard(_state_lock); |
paul@177 | 268 | |
paul@177 | 269 | FileNotificationState &state = file_state(file, false); |
paul@177 | 270 | |
paul@177 | 271 | if (state.is_null()) |
paul@177 | 272 | return; |
paul@177 | 273 | |
paul@177 | 274 | /* Acquire the lock for the file state itself. */ |
paul@177 | 275 | |
paul@177 | 276 | std::unique_lock<std::mutex> file_guard(state.lock); |
paul@177 | 277 | |
paul@177 | 278 | /* Record flags and return previous flags. */ |
paul@177 | 279 | |
paul@177 | 280 | notify_flags_t recorded = state.pending; |
paul@177 | 281 | |
paul@177 | 282 | state.pending |= flags; |
paul@177 | 283 | |
paul@177 | 284 | /* Add a file queue entry for any files without previous notifications. */ |
paul@177 | 285 | |
paul@177 | 286 | if (!recorded) |
paul@177 | 287 | _affected.push_back(file); |
paul@177 | 288 | |
paul@177 | 289 | /* Notify any waiting caller. */ |
paul@177 | 290 | |
paul@177 | 291 | _general_condition.notify_one(); |
paul@177 | 292 | } |
paul@177 | 293 | |
paul@177 | 294 | void SpecificFileNotifier::_notify(file_t *file, notify_flags_t flags) |
paul@177 | 295 | { |
paul@177 | 296 | /* Acquire the lock for state lookup. */ |
paul@177 | 297 | |
paul@177 | 298 | std::unique_lock<std::mutex> state_guard(_state_lock); |
paul@177 | 299 | |
paul@177 | 300 | FileNotificationState &state = file_state(file, false); |
paul@177 | 301 | |
paul@177 | 302 | if (state.is_null()) |
paul@177 | 303 | return; |
paul@177 | 304 | |
paul@177 | 305 | /* Acquire the lock for the file state itself. */ |
paul@177 | 306 | |
paul@177 | 307 | std::unique_lock<std::mutex> file_guard(state.lock); |
paul@177 | 308 | |
paul@177 | 309 | state.pending |= flags; |
paul@177 | 310 | |
paul@177 | 311 | /* Notify any waiting caller. */ |
paul@177 | 312 | |
paul@177 | 313 | state.condition.notify_one(); |
paul@177 | 314 | } |
paul@177 | 315 | |
paul@177 | 316 | |
paul@177 | 317 | |
paul@177 | 318 | /* Transfer pending notifications to the given file. This must be called with a |
paul@177 | 319 | lock acquired on the file notification state. */ |
paul@177 | 320 | |
paul@177 | 321 | bool FileNotifier::_transfer(FileNotificationState &state, file_t *file) |
paul@177 | 322 | { |
paul@177 | 323 | notify_flags_t recorded = state.pending; |
paul@177 | 324 | |
paul@177 | 325 | if (recorded) |
paul@177 | 326 | { |
paul@177 | 327 | file->notifications = recorded; |
paul@177 | 328 | state.pending = 0; |
paul@177 | 329 | return true; |
paul@177 | 330 | } |
paul@177 | 331 | |
paul@177 | 332 | return false; |
paul@177 | 333 | } |
paul@177 | 334 | |
paul@177 | 335 | |
paul@177 | 336 | |
paul@177 | 337 | /* Obtain file state and transfer notifications. */ |
paul@177 | 338 | |
paul@177 | 339 | bool GeneralFileNotifier::_retrieve_for_file(file_t *file) |
paul@177 | 340 | { |
paul@177 | 341 | /* Acquire the lock for state lookup. */ |
paul@177 | 342 | |
paul@177 | 343 | std::unique_lock<std::mutex> state_guard(_state_lock); |
paul@177 | 344 | |
paul@177 | 345 | FileNotificationState &state = file_state(file, false); |
paul@177 | 346 | |
paul@177 | 347 | if (state.is_null()) |
paul@177 | 348 | return false; |
paul@177 | 349 | |
paul@177 | 350 | /* Acquire the lock for the file state itself, then release the state lock. */ |
paul@177 | 351 | |
paul@177 | 352 | std::unique_lock<std::mutex> file_guard(state.lock); |
paul@177 | 353 | |
paul@177 | 354 | state_guard.unlock(); |
paul@177 | 355 | |
paul@177 | 356 | /* Call generic method to transfer notifications, if possible. */ |
paul@177 | 357 | |
paul@177 | 358 | return _transfer(state, file); |
paul@177 | 359 | } |
paul@177 | 360 | |
paul@177 | 361 | /* Obtain queued files until one is found that still has events recorded for it. |
paul@177 | 362 | This must be called with the notifier's general lock acquired. */ |
paul@177 | 363 | |
paul@177 | 364 | bool GeneralFileNotifier::_retrieve(file_t **file) |
paul@177 | 365 | { |
paul@177 | 366 | while (!_affected.empty()) |
paul@177 | 367 | { |
paul@177 | 368 | *file = _affected.front(); |
paul@177 | 369 | _affected.pop_front(); |
paul@177 | 370 | |
paul@177 | 371 | if (_retrieve_for_file(*file)) |
paul@177 | 372 | return true; |
paul@177 | 373 | } |
paul@177 | 374 | |
paul@177 | 375 | return false; |
paul@177 | 376 | } |
paul@177 | 377 | |
paul@177 | 378 | |
paul@177 | 379 | |
paul@124 | 380 | /* Wait for notification events on files. */ |
paul@124 | 381 | |
paul@177 | 382 | long GeneralFileNotifier::wait(file_t **file) |
paul@124 | 383 | { |
paul@177 | 384 | std::unique_lock<std::mutex> general_guard(_general_lock); |
paul@124 | 385 | |
paul@124 | 386 | while (1) |
paul@124 | 387 | { |
paul@177 | 388 | /* With pending notifications, update the first file and exit. */ |
paul@129 | 389 | |
paul@177 | 390 | if (_retrieve(file)) |
paul@177 | 391 | break; |
paul@129 | 392 | |
paul@177 | 393 | /* Otherwise, wait for notifications. */ |
paul@129 | 394 | |
paul@177 | 395 | _general_condition.wait(general_guard); |
paul@129 | 396 | } |
paul@129 | 397 | |
paul@129 | 398 | return L4_EOK; |
paul@129 | 399 | } |
paul@129 | 400 | |
paul@129 | 401 | /* Wait for notifications from a single file. */ |
paul@129 | 402 | |
paul@177 | 403 | long SpecificFileNotifier::wait_file(file_t *file) |
paul@129 | 404 | { |
paul@177 | 405 | /* Acquire the lock for reading file state. */ |
paul@177 | 406 | |
paul@177 | 407 | std::unique_lock<std::mutex> state_guard(_state_lock); |
paul@177 | 408 | |
paul@177 | 409 | FileNotificationState &state = file_state(file, false); |
paul@177 | 410 | |
paul@177 | 411 | if (state.is_null()) |
paul@177 | 412 | return -L4_EINVAL; |
paul@177 | 413 | |
paul@177 | 414 | /* Acquire the lock for the file state itself, then release the state lock. */ |
paul@177 | 415 | |
paul@177 | 416 | std::unique_lock<std::mutex> file_guard(state.lock); |
paul@177 | 417 | |
paul@177 | 418 | state_guard.unlock(); |
paul@129 | 419 | |
paul@129 | 420 | while (1) |
paul@129 | 421 | { |
paul@177 | 422 | /* With pending notifications, update the file and exit. */ |
paul@129 | 423 | |
paul@177 | 424 | if (_transfer(state, file)) |
paul@177 | 425 | break; |
paul@129 | 426 | |
paul@177 | 427 | /* Otherwise, wait for notifications. */ |
paul@129 | 428 | |
paul@177 | 429 | state.condition.wait(file_guard); |
paul@124 | 430 | } |
paul@124 | 431 | |
paul@124 | 432 | return L4_EOK; |
paul@124 | 433 | } |
paul@124 | 434 | |
paul@124 | 435 | // vim: tabstop=2 expandtab shiftwidth=2 |