1 /* 2 * File event notification support. 3 * 4 * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #pragma once 23 24 #include <condition_variable> 25 #include <list> 26 #include <map> 27 #include <mutex> 28 29 #include <fsclient/file.h> 30 #include <ipc/server.h> 31 #include <systypes/base.h> 32 33 34 35 /* Collection types. */ 36 37 typedef std::map<file_t *, notify_flags_t> FileNotifications; 38 39 40 41 /* An object for monitoring file event notifications. */ 42 43 class FileNotifier 44 { 45 protected: 46 std::list<file_t *> _affected; 47 FileNotifications _affected_flags; 48 l4_cap_idx_t _thread = L4_INVALID_CAP; 49 bool _started = false; 50 51 std::mutex _lock; 52 std::condition_variable _notified; 53 54 /* Helper methods. */ 55 56 virtual void _notify(file_t *file, notify_flags_t flags); 57 58 public: 59 /* Server details. */ 60 61 int expected_items(); 62 63 ipc_server_handler_type handler(); 64 65 void *interface() 66 { return static_cast<FileNotifier *>(this); } 67 68 /* Local operations. */ 69 70 virtual long subscribe(file_t *file, notify_flags_t flags); 71 72 virtual long unsubscribe(file_t *file); 73 74 virtual void mainloop(); 75 76 virtual long start(); 77 78 virtual long wait(file_t **file); 79 }; 80 81 82 83 /* Helper functions. */ 84 85 FileNotifier *get_notifier(); 86 87 // vim: tabstop=4 expandtab shiftwidth=4