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