# HG changeset patch # User Paul Boddie # Date 1631312170 -7200 # Node ID 494753f0112be086b3d4aed866a3f0accf65cc1b # Parent 955a946c32f3594c377e5125c74fe39375ca1de9 Moved generic accounting functionality into a base class. diff -r 955a946c32f3 -r 494753f0112b libfsserver/include/fsserver/accountable.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libfsserver/include/fsserver/accountable.h Sat Sep 11 00:16:10 2021 +0200 @@ -0,0 +1,44 @@ +/* + * Accountable object support. + * + * Copyright (C) 2021 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#pragma once + +#include + + + +/* An interface supporting usage accounting. */ + +class Accountable +{ +protected: + std::mutex _attached_lock; + unsigned int _attached = 0; + +public: + virtual ~Accountable(); + + virtual void attach(); + + virtual unsigned int detach(); +}; + +// vim: tabstop=4 expandtab shiftwidth=4 diff -r 955a946c32f3 -r 494753f0112b libfsserver/include/fsserver/page_mapper.h --- a/libfsserver/include/fsserver/page_mapper.h Fri Sep 10 23:16:41 2021 +0200 +++ b/libfsserver/include/fsserver/page_mapper.h Sat Sep 11 00:16:10 2021 +0200 @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -31,13 +32,12 @@ /* A file mapper, associating flexpages with file regions. */ -class PageMapper : public PageOwner +class PageMapper : public PageOwner, public Accountable { protected: AccessMap _map; Accessor *_accessor; Pages *_pages; - unsigned int _attached; /* Serialisation of accesses. */ @@ -52,9 +52,7 @@ public: explicit PageMapper(Accessor *accessor, Pages *pages); - /* Accounting methods. */ - - void attach(); + /* Specialised accounting methods. */ unsigned int detach(); diff -r 955a946c32f3 -r 494753f0112b libfsserver/lib/Makefile --- a/libfsserver/lib/Makefile Fri Sep 10 23:16:41 2021 +0200 +++ b/libfsserver/lib/Makefile Sat Sep 11 00:16:10 2021 +0200 @@ -60,6 +60,7 @@ files/test_file_accessor.cc \ files/test_file_opener.cc \ generic/accessor.cc \ + generic/accountable.cc \ generic/notification.cc \ generic/pager.cc \ generic/resource_server.cc \ diff -r 955a946c32f3 -r 494753f0112b libfsserver/lib/generic/accountable.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libfsserver/lib/generic/accountable.cc Sat Sep 11 00:16:10 2021 +0200 @@ -0,0 +1,51 @@ +/* + * Accountable object support. + * + * Copyright (C) 2021 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include "accountable.h" + + + +Accountable::~Accountable() +{ +} + +/* Attach to the object. */ + +void Accountable::attach() +{ + std::lock_guard guard(_attached_lock); + + _attached += 1; +} + +/* Detach from the object. */ + +unsigned int Accountable::detach() +{ + std::lock_guard guard(_attached_lock); + + if (_attached) + _attached -= 1; + + return _attached; +} + +// vim: tabstop=4 expandtab shiftwidth=4 diff -r 955a946c32f3 -r 494753f0112b libfsserver/lib/mapping/page_mapper.cc --- a/libfsserver/lib/mapping/page_mapper.cc Fri Sep 10 23:16:41 2021 +0200 +++ b/libfsserver/lib/mapping/page_mapper.cc Sat Sep 11 00:16:10 2021 +0200 @@ -25,40 +25,26 @@ PageMapper::PageMapper(Accessor *accessor, Pages *pages) -: _accessor(accessor), _pages(pages), _attached(0) +: _accessor(accessor), _pages(pages) { } /* Accounting methods. */ -/* Attach a pager, opening the accessor if required. */ - -void PageMapper::attach() -{ - std::lock_guard guard(_lock); - - _attached += 1; -} - /* Detach a pager, purging active pages and closing the accessor if no more pagers are attached. Return whether any pagers are still attached. */ unsigned int PageMapper::detach() { - std::lock_guard guard(_lock); - - if (_attached) - { - _attached -= 1; + unsigned int attached = Accountable::detach(); - if (!_attached) - { - _map.purge(this, _pages); - _accessor->close(); - } + if (!attached) + { + _map.purge(this, _pages); + _accessor->close(); } - return _attached; + return attached; } /* Interface for the pager. */