paul@124 | 1 | /* |
paul@479 | 2 | * Generic object event notification support. |
paul@124 | 3 | * |
paul@479 | 4 | * Copyright (C) 2021, 2022, 2023 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@481 | 33 | #include "notifier_interface.h" |
paul@124 | 34 | |
paul@124 | 35 | |
paul@124 | 36 | |
paul@177 | 37 | /* Null notification state. */ |
paul@177 | 38 | |
paul@479 | 39 | static ObjectNotificationState _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@479 | 49 | static SpecificObjectNotifier *_notifier = NULL; |
paul@177 | 50 | |
paul@177 | 51 | |
paul@124 | 52 | |
paul@479 | 53 | /* Return the per-task notifier for object-specific waiting operations. */ |
paul@177 | 54 | |
paul@479 | 55 | SpecificObjectNotifier *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@479 | 63 | _notifier = new SpecificObjectNotifier; |
paul@177 | 64 | _notifier->start(); |
paul@177 | 65 | } |
paul@177 | 66 | |
paul@177 | 67 | return _notifier; |
paul@177 | 68 | } |
paul@177 | 69 | |
paul@479 | 70 | /* Return a local notifier for general object waiting operations. */ |
paul@124 | 71 | |
paul@479 | 72 | GeneralObjectNotifier *notifier_get_local_notifier() |
paul@180 | 73 | { |
paul@479 | 74 | GeneralObjectNotifier *notifier = new GeneralObjectNotifier; |
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@479 | 86 | ObjectNotifier *notifier = reinterpret_cast<ObjectNotifier *>(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@479 | 96 | ObjectNotifier::~ObjectNotifier() |
paul@180 | 97 | { |
paul@180 | 98 | } |
paul@180 | 99 | |
paul@124 | 100 | /* Listen for notifications. */ |
paul@124 | 101 | |
paul@479 | 102 | void ObjectNotifier::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@479 | 120 | /* Interpret gate labels as notifiable objects. */ |
paul@126 | 121 | |
paul@479 | 122 | notifiable_t *object = (notifiable_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@481 | 128 | struct in_words_Notifier_notify *in_words = (struct in_words_Notifier_notify *) ipc_message_get_word_address(&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@481 | 137 | _notify(object, in_words->flags, in_words->values); |
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@479 | 145 | long ObjectNotifier::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@479 | 169 | /* Return notification state for the given object or null state if no record |
paul@479 | 170 | existed for the object. */ |
paul@177 | 171 | |
paul@479 | 172 | ObjectNotificationState &ObjectNotifier::object_state(notifiable_t *object, bool create) |
paul@177 | 173 | { |
paul@479 | 174 | ObjectNotificationStates::iterator it = _state.find(object); |
paul@177 | 175 | |
paul@177 | 176 | if (it == _state.end()) |
paul@177 | 177 | { |
paul@177 | 178 | if (create) |
paul@479 | 179 | return _state[object]; |
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@479 | 187 | /* Subscribe to notification events on an object. */ |
paul@177 | 188 | |
paul@479 | 189 | long ObjectNotifier::subscribe(notifiable_t *object, notify_flags_t flags) |
paul@177 | 190 | { |
paul@483 | 191 | l4_cap_idx_t endpoint; |
paul@483 | 192 | long err = get_endpoint(object, &endpoint, true); |
paul@177 | 193 | |
paul@483 | 194 | if (err) |
paul@483 | 195 | return err; |
paul@177 | 196 | |
paul@483 | 197 | /* Subscribe, sending the notification endpoint via the principal reference |
paul@483 | 198 | for the object. */ |
paul@288 | 199 | |
paul@479 | 200 | client_Notification notify(object->base->ref); |
paul@177 | 201 | |
paul@483 | 202 | return notify.subscribe(endpoint, flags); |
paul@177 | 203 | } |
paul@177 | 204 | |
paul@479 | 205 | /* Unsubscribe from notification events on an object. */ |
paul@177 | 206 | |
paul@479 | 207 | long ObjectNotifier::unsubscribe(notifiable_t *object) |
paul@177 | 208 | { |
paul@483 | 209 | l4_cap_idx_t endpoint; |
paul@483 | 210 | long err = get_endpoint(object, &endpoint, false); |
paul@177 | 211 | |
paul@483 | 212 | if (err) |
paul@483 | 213 | return err; |
paul@177 | 214 | |
paul@290 | 215 | /* Unsubscribe via the notification interface. */ |
paul@288 | 216 | |
paul@479 | 217 | client_Notification notify(object->base->ref); |
paul@288 | 218 | |
paul@488 | 219 | notify.unsubscribe(endpoint); |
paul@288 | 220 | |
paul@483 | 221 | return remove_endpoint(object, endpoint); |
paul@483 | 222 | } |
paul@483 | 223 | |
paul@483 | 224 | /* Obtain a notification endpoint for an object. */ |
paul@483 | 225 | |
paul@483 | 226 | long ObjectNotifier::get_endpoint(notifiable_t *object, l4_cap_idx_t *endpoint, bool create) |
paul@483 | 227 | { |
paul@483 | 228 | /* Acquire the lock for state lookup. */ |
paul@483 | 229 | |
paul@483 | 230 | std::unique_lock<std::mutex> state_guard(_state_lock); |
paul@483 | 231 | |
paul@483 | 232 | ObjectNotificationState &state = object_state(object, create); |
paul@483 | 233 | |
paul@483 | 234 | /* Create a notification endpoint, if necessary. */ |
paul@483 | 235 | |
paul@483 | 236 | if (state.is_null()) |
paul@483 | 237 | { |
paul@483 | 238 | if (create) |
paul@483 | 239 | { |
paul@483 | 240 | long err = ipc_server_new_for_thread(&state.endpoint, object, _thread); |
paul@483 | 241 | |
paul@483 | 242 | if (err) |
paul@483 | 243 | return err; |
paul@483 | 244 | } |
paul@483 | 245 | else |
paul@483 | 246 | return -L4_ENOENT; |
paul@483 | 247 | } |
paul@483 | 248 | |
paul@483 | 249 | *endpoint = state.endpoint; |
paul@483 | 250 | return L4_EOK; |
paul@483 | 251 | } |
paul@483 | 252 | |
paul@483 | 253 | /* Remove a notification endpoint for an object. */ |
paul@483 | 254 | |
paul@483 | 255 | long ObjectNotifier::remove_endpoint(notifiable_t *object, l4_cap_idx_t endpoint) |
paul@483 | 256 | { |
paul@483 | 257 | if (l4_is_invalid_cap(endpoint)) |
paul@483 | 258 | return -L4_EINVAL; |
paul@483 | 259 | |
paul@483 | 260 | ipc_cap_free_um(endpoint); |
paul@288 | 261 | |
paul@479 | 262 | _state.erase(object); |
paul@177 | 263 | |
paul@479 | 264 | /* Remove the lock for updating object state. */ |
paul@177 | 265 | |
paul@479 | 266 | _object_locks.erase(object); |
paul@177 | 267 | |
paul@177 | 268 | return L4_EOK; |
paul@177 | 269 | } |
paul@177 | 270 | |
paul@177 | 271 | |
paul@177 | 272 | |
paul@479 | 273 | /* Handle a notification event for an object. Ideally, this would be invoked by |
paul@479 | 274 | the generic server dispatch mechanism, with the gate label being interpreted |
paul@479 | 275 | and provided as the first parameter. */ |
paul@177 | 276 | |
paul@481 | 277 | void GeneralObjectNotifier::_notify(notifiable_t *object, notify_flags_t flags, |
paul@481 | 278 | notify_values_t values) |
paul@177 | 279 | { |
paul@479 | 280 | /* Enter critical section for the notifier (affecting all objects). */ |
paul@177 | 281 | |
paul@177 | 282 | std::unique_lock<std::mutex> general_guard(_general_lock); |
paul@177 | 283 | |
paul@177 | 284 | /* Acquire the lock for state lookup. */ |
paul@177 | 285 | |
paul@177 | 286 | std::unique_lock<std::mutex> state_guard(_state_lock); |
paul@177 | 287 | |
paul@479 | 288 | ObjectNotificationState &state = object_state(object, false); |
paul@177 | 289 | |
paul@177 | 290 | if (state.is_null()) |
paul@177 | 291 | return; |
paul@177 | 292 | |
paul@479 | 293 | /* Acquire the lock for the object state itself. */ |
paul@177 | 294 | |
paul@479 | 295 | std::unique_lock<std::mutex> object_guard(state.lock); |
paul@177 | 296 | |
paul@481 | 297 | /* Record flags and note previous flags. */ |
paul@177 | 298 | |
paul@481 | 299 | notify_flags_t recorded = state.pending_notifications; |
paul@177 | 300 | |
paul@481 | 301 | state.pending_notifications |= flags; |
paul@481 | 302 | state.pending_values = values; |
paul@177 | 303 | |
paul@479 | 304 | /* Add an object queue entry for any objects without previous notifications. */ |
paul@177 | 305 | |
paul@177 | 306 | if (!recorded) |
paul@479 | 307 | _affected.push_back(object); |
paul@177 | 308 | |
paul@177 | 309 | /* Notify any waiting caller. */ |
paul@177 | 310 | |
paul@177 | 311 | _general_condition.notify_one(); |
paul@177 | 312 | } |
paul@177 | 313 | |
paul@481 | 314 | void SpecificObjectNotifier::_notify(notifiable_t *object, notify_flags_t flags, |
paul@481 | 315 | notify_values_t values) |
paul@177 | 316 | { |
paul@177 | 317 | /* Acquire the lock for state lookup. */ |
paul@177 | 318 | |
paul@177 | 319 | std::unique_lock<std::mutex> state_guard(_state_lock); |
paul@177 | 320 | |
paul@479 | 321 | ObjectNotificationState &state = object_state(object, false); |
paul@177 | 322 | |
paul@177 | 323 | if (state.is_null()) |
paul@177 | 324 | return; |
paul@177 | 325 | |
paul@479 | 326 | /* Acquire the lock for the object state itself. */ |
paul@177 | 327 | |
paul@479 | 328 | std::unique_lock<std::mutex> object_guard(state.lock); |
paul@177 | 329 | |
paul@481 | 330 | state.pending_notifications |= flags; |
paul@481 | 331 | state.pending_values = values; |
paul@177 | 332 | |
paul@177 | 333 | /* Notify any waiting caller. */ |
paul@177 | 334 | |
paul@177 | 335 | state.condition.notify_one(); |
paul@177 | 336 | } |
paul@177 | 337 | |
paul@177 | 338 | |
paul@177 | 339 | |
paul@479 | 340 | /* Transfer pending notifications to the given object. This must be called with |
paul@479 | 341 | a lock acquired on the object notification state. */ |
paul@177 | 342 | |
paul@479 | 343 | bool ObjectNotifier::_transfer(ObjectNotificationState &state, notifiable_t *object) |
paul@177 | 344 | { |
paul@481 | 345 | notify_flags_t recorded = state.pending_notifications; |
paul@177 | 346 | |
paul@177 | 347 | if (recorded) |
paul@177 | 348 | { |
paul@479 | 349 | object->notifications = recorded; |
paul@481 | 350 | object->values = state.pending_values; |
paul@481 | 351 | state.pending_notifications = 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@479 | 360 | /* Obtain object state and transfer notifications. */ |
paul@177 | 361 | |
paul@479 | 362 | bool GeneralObjectNotifier::_retrieve_for_object(notifiable_t *object) |
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@479 | 368 | ObjectNotificationState &state = object_state(object, false); |
paul@177 | 369 | |
paul@177 | 370 | if (state.is_null()) |
paul@177 | 371 | return false; |
paul@177 | 372 | |
paul@479 | 373 | /* Acquire the lock for the object state itself, then release the state lock. */ |
paul@177 | 374 | |
paul@479 | 375 | std::unique_lock<std::mutex> object_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@479 | 381 | return _transfer(state, object); |
paul@177 | 382 | } |
paul@177 | 383 | |
paul@479 | 384 | /* Obtain queued objects until one is found that still has events recorded for |
paul@479 | 385 | it. This must be called with the notifier's general lock acquired. */ |
paul@177 | 386 | |
paul@479 | 387 | bool GeneralObjectNotifier::_retrieve(notifiable_t **object) |
paul@177 | 388 | { |
paul@177 | 389 | while (!_affected.empty()) |
paul@177 | 390 | { |
paul@479 | 391 | *object = _affected.front(); |
paul@177 | 392 | _affected.pop_front(); |
paul@177 | 393 | |
paul@479 | 394 | if (_retrieve_for_object(*object)) |
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@479 | 403 | /* Wait for notification events on objects. */ |
paul@124 | 404 | |
paul@479 | 405 | long GeneralObjectNotifier::wait(notifiable_t **object) |
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@479 | 411 | /* With pending notifications, update the first object and exit. */ |
paul@129 | 412 | |
paul@479 | 413 | if (_retrieve(object)) |
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@479 | 424 | /* Wait for notifications from a single object. */ |
paul@129 | 425 | |
paul@479 | 426 | long SpecificObjectNotifier::wait_object(notifiable_t *object) |
paul@129 | 427 | { |
paul@479 | 428 | /* Acquire the lock for reading object state. */ |
paul@177 | 429 | |
paul@177 | 430 | std::unique_lock<std::mutex> state_guard(_state_lock); |
paul@177 | 431 | |
paul@479 | 432 | ObjectNotificationState &state = object_state(object, false); |
paul@177 | 433 | |
paul@177 | 434 | if (state.is_null()) |
paul@177 | 435 | return -L4_EINVAL; |
paul@177 | 436 | |
paul@479 | 437 | /* Acquire the lock for the object state itself, then release the state lock. */ |
paul@177 | 438 | |
paul@479 | 439 | std::unique_lock<std::mutex> object_guard(state.lock); |
paul@177 | 440 | |
paul@177 | 441 | state_guard.unlock(); |
paul@129 | 442 | |
paul@129 | 443 | while (1) |
paul@129 | 444 | { |
paul@479 | 445 | /* With pending notifications, update the object and exit. */ |
paul@129 | 446 | |
paul@479 | 447 | if (_transfer(state, object)) |
paul@177 | 448 | break; |
paul@129 | 449 | |
paul@177 | 450 | /* Otherwise, wait for notifications. */ |
paul@129 | 451 | |
paul@479 | 452 | state.condition.wait(object_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 |