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 | #pragma once |
paul@124 | 23 | |
paul@124 | 24 | #include <condition_variable> |
paul@124 | 25 | #include <list> |
paul@125 | 26 | #include <map> |
paul@124 | 27 | #include <mutex> |
paul@124 | 28 | |
paul@124 | 29 | #include <fsclient/file.h> |
paul@124 | 30 | #include <systypes/base.h> |
paul@124 | 31 | |
paul@124 | 32 | |
paul@124 | 33 | |
paul@177 | 34 | /* File-specific notification details. */ |
paul@177 | 35 | |
paul@177 | 36 | class FileNotificationState |
paul@177 | 37 | { |
paul@177 | 38 | public: |
paul@177 | 39 | /* Synchronisation primitives for state access and notification. */ |
paul@177 | 40 | |
paul@177 | 41 | std::mutex lock; |
paul@177 | 42 | std::condition_variable condition; |
paul@177 | 43 | |
paul@177 | 44 | /* Pending notifications for monitored files. */ |
paul@177 | 45 | |
paul@177 | 46 | notify_flags_t pending = 0; |
paul@177 | 47 | |
paul@177 | 48 | /* Endpoints associated with monitored files. */ |
paul@177 | 49 | |
paul@177 | 50 | l4_cap_idx_t endpoint = L4_INVALID_CAP; |
paul@177 | 51 | |
paul@177 | 52 | bool is_null() { return l4_is_invalid_cap(endpoint); } |
paul@177 | 53 | }; |
paul@177 | 54 | |
paul@177 | 55 | |
paul@177 | 56 | |
paul@125 | 57 | /* Collection types. */ |
paul@125 | 58 | |
paul@177 | 59 | typedef std::map<file_t *, FileNotificationState> FileNotificationStates; |
paul@177 | 60 | typedef std::map<file_t *, std::mutex> FileStateLocks; |
paul@125 | 61 | |
paul@125 | 62 | |
paul@125 | 63 | |
paul@124 | 64 | /* An object for monitoring file event notifications. */ |
paul@124 | 65 | |
paul@124 | 66 | class FileNotifier |
paul@124 | 67 | { |
paul@124 | 68 | protected: |
paul@177 | 69 | /* General state access locking. */ |
paul@177 | 70 | |
paul@177 | 71 | std::mutex _state_lock; |
paul@177 | 72 | |
paul@177 | 73 | /* File-specific state locking. */ |
paul@177 | 74 | |
paul@177 | 75 | FileStateLocks _file_locks; |
paul@177 | 76 | |
paul@177 | 77 | /* Notification state. */ |
paul@177 | 78 | |
paul@177 | 79 | FileNotificationStates _state; |
paul@177 | 80 | |
paul@177 | 81 | /* Notifier thread details. */ |
paul@177 | 82 | |
paul@124 | 83 | l4_cap_idx_t _thread = L4_INVALID_CAP; |
paul@124 | 84 | bool _started = false; |
paul@124 | 85 | |
paul@177 | 86 | /* Convenience method to access file state. */ |
paul@177 | 87 | |
paul@177 | 88 | virtual FileNotificationState &file_state(file_t *file, bool create); |
paul@124 | 89 | |
paul@124 | 90 | /* Helper methods. */ |
paul@124 | 91 | |
paul@177 | 92 | virtual void _notify(file_t *file, notify_flags_t flags) = 0; |
paul@177 | 93 | |
paul@177 | 94 | virtual bool _transfer(FileNotificationState &state, file_t *file); |
paul@177 | 95 | |
paul@177 | 96 | virtual void _unsubscribe(FileNotificationState &state, file_t *file); |
paul@124 | 97 | |
paul@124 | 98 | public: |
paul@180 | 99 | virtual ~FileNotifier(); |
paul@180 | 100 | |
paul@124 | 101 | /* Local operations. */ |
paul@124 | 102 | |
paul@129 | 103 | virtual long start(); |
paul@129 | 104 | |
paul@124 | 105 | virtual long subscribe(file_t *file, notify_flags_t flags); |
paul@124 | 106 | |
paul@124 | 107 | virtual long unsubscribe(file_t *file); |
paul@124 | 108 | |
paul@129 | 109 | /* Event handling support. */ |
paul@124 | 110 | |
paul@129 | 111 | virtual void mainloop(); |
paul@124 | 112 | }; |
paul@124 | 113 | |
paul@124 | 114 | |
paul@124 | 115 | |
paul@177 | 116 | /* An object monitoring notifications for a collection of different files. */ |
paul@177 | 117 | |
paul@177 | 118 | class GeneralFileNotifier : public FileNotifier |
paul@177 | 119 | { |
paul@177 | 120 | protected: |
paul@177 | 121 | /* Locking to protect pending notification members and to coordinate access |
paul@177 | 122 | to notifications. */ |
paul@177 | 123 | |
paul@177 | 124 | std::mutex _general_lock; |
paul@177 | 125 | |
paul@177 | 126 | /* General lock synchronisation. */ |
paul@177 | 127 | |
paul@177 | 128 | std::condition_variable _general_condition; |
paul@177 | 129 | |
paul@177 | 130 | /* Files affected by notifications. */ |
paul@177 | 131 | |
paul@177 | 132 | std::list<file_t *> _affected; |
paul@177 | 133 | |
paul@177 | 134 | /* Helper methods. */ |
paul@177 | 135 | |
paul@177 | 136 | virtual void _notify(file_t *file, notify_flags_t flags); |
paul@177 | 137 | |
paul@177 | 138 | virtual bool _retrieve(file_t **file); |
paul@177 | 139 | |
paul@177 | 140 | virtual bool _retrieve_for_file(file_t *file); |
paul@177 | 141 | |
paul@177 | 142 | public: |
paul@177 | 143 | virtual long wait(file_t **file); |
paul@177 | 144 | }; |
paul@177 | 145 | |
paul@177 | 146 | |
paul@177 | 147 | |
paul@177 | 148 | /* An object monitoring notifications for specific files. */ |
paul@177 | 149 | |
paul@177 | 150 | class SpecificFileNotifier : public FileNotifier |
paul@177 | 151 | { |
paul@177 | 152 | protected: |
paul@177 | 153 | /* Helper methods. */ |
paul@177 | 154 | |
paul@177 | 155 | virtual void _notify(file_t *file, notify_flags_t flags); |
paul@177 | 156 | |
paul@177 | 157 | public: |
paul@177 | 158 | virtual long wait_file(file_t *file); |
paul@177 | 159 | }; |
paul@177 | 160 | |
paul@177 | 161 | |
paul@177 | 162 | |
paul@124 | 163 | /* Helper functions. */ |
paul@124 | 164 | |
paul@180 | 165 | SpecificFileNotifier *notifier_get_task_notifier(); |
paul@177 | 166 | |
paul@180 | 167 | GeneralFileNotifier *notifier_get_local_notifier(); |
paul@124 | 168 | |
paul@124 | 169 | // vim: tabstop=4 expandtab shiftwidth=4 |