paul@195 | 1 | /* |
paul@195 | 2 | * General functionality supporting file notifications. |
paul@195 | 3 | * |
paul@195 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@195 | 5 | * |
paul@195 | 6 | * This program is free software; you can redistribute it and/or |
paul@195 | 7 | * modify it under the terms of the GNU General Public License as |
paul@195 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@195 | 9 | * the License, or (at your option) any later version. |
paul@195 | 10 | * |
paul@195 | 11 | * This program is distributed in the hope that it will be useful, |
paul@195 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@195 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@195 | 14 | * GNU General Public License for more details. |
paul@195 | 15 | * |
paul@195 | 16 | * You should have received a copy of the GNU General Public License |
paul@195 | 17 | * along with this program; if not, write to the Free Software |
paul@195 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@195 | 19 | * Boston, MA 02110-1301, USA |
paul@195 | 20 | */ |
paul@195 | 21 | |
paul@195 | 22 | #include "file_notifier_registry.h" |
paul@195 | 23 | |
paul@195 | 24 | |
paul@195 | 25 | |
paul@195 | 26 | FileNotifierRegistry::FileNotifierRegistry() |
paul@195 | 27 | { |
paul@195 | 28 | } |
paul@195 | 29 | |
paul@195 | 30 | |
paul@195 | 31 | |
paul@195 | 32 | /* Obtain a file-specific notification manager. */ |
paul@195 | 33 | |
paul@195 | 34 | FileNotification *FileNotifierRegistry::_get(fileid_t fileid) |
paul@195 | 35 | { |
paul@195 | 36 | FileNotifiers::iterator entry = _notifiers.find(fileid); |
paul@195 | 37 | |
paul@195 | 38 | if (entry != _notifiers.end()) |
paul@195 | 39 | return _notifiers[fileid]; |
paul@195 | 40 | else |
paul@195 | 41 | return NULL; |
paul@195 | 42 | } |
paul@195 | 43 | |
paul@195 | 44 | /* Register a notification manager for the given 'fileid'. */ |
paul@195 | 45 | |
paul@195 | 46 | void FileNotifierRegistry::attach(fileid_t fileid) |
paul@195 | 47 | { |
paul@195 | 48 | std::lock_guard<std::mutex> guard(_lock); |
paul@195 | 49 | |
paul@195 | 50 | FileNotification *notifier = _get(fileid); |
paul@195 | 51 | |
paul@195 | 52 | if (notifier == NULL) |
paul@195 | 53 | { |
paul@195 | 54 | notifier = new FileNotification; |
paul@195 | 55 | _notifiers[fileid] = notifier; |
paul@195 | 56 | } |
paul@195 | 57 | |
paul@195 | 58 | notifier->attach(); |
paul@195 | 59 | } |
paul@195 | 60 | |
paul@195 | 61 | /* Detach from and potentially remove a notification manager for the given |
paul@195 | 62 | 'fileid'. */ |
paul@195 | 63 | |
paul@195 | 64 | void FileNotifierRegistry::detach(fileid_t fileid) |
paul@195 | 65 | { |
paul@195 | 66 | std::lock_guard<std::mutex> guard(_lock); |
paul@195 | 67 | |
paul@195 | 68 | FileNotification *notifier = _get(fileid); |
paul@195 | 69 | |
paul@195 | 70 | if ((notifier != NULL) && (!notifier->detach())) |
paul@195 | 71 | { |
paul@195 | 72 | _notifiers.erase(fileid); |
paul@195 | 73 | delete notifier; |
paul@195 | 74 | } |
paul@195 | 75 | } |
paul@195 | 76 | |
paul@195 | 77 | /* Obtain a file-specific notification manager. */ |
paul@195 | 78 | |
paul@195 | 79 | FileNotification *FileNotifierRegistry::get(fileid_t fileid) |
paul@195 | 80 | { |
paul@195 | 81 | std::lock_guard<std::mutex> guard(_lock); |
paul@195 | 82 | |
paul@195 | 83 | return _get(fileid); |
paul@195 | 84 | } |
paul@195 | 85 | |
paul@195 | 86 | // vim: tabstop=4 expandtab shiftwidth=4 |