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