1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libfsserver/include/fsserver/accountable.h Sat Sep 11 00:16:10 2021 +0200
1.3 @@ -0,0 +1,44 @@
1.4 +/*
1.5 + * Accountable object support.
1.6 + *
1.7 + * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#pragma once
1.26 +
1.27 +#include <mutex>
1.28 +
1.29 +
1.30 +
1.31 +/* An interface supporting usage accounting. */
1.32 +
1.33 +class Accountable
1.34 +{
1.35 +protected:
1.36 + std::mutex _attached_lock;
1.37 + unsigned int _attached = 0;
1.38 +
1.39 +public:
1.40 + virtual ~Accountable();
1.41 +
1.42 + virtual void attach();
1.43 +
1.44 + virtual unsigned int detach();
1.45 +};
1.46 +
1.47 +// vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/libfsserver/include/fsserver/page_mapper.h Fri Sep 10 23:16:41 2021 +0200
2.2 +++ b/libfsserver/include/fsserver/page_mapper.h Sat Sep 11 00:16:10 2021 +0200
2.3 @@ -23,6 +23,7 @@
2.4
2.5 #include <fsserver/access_map.h>
2.6 #include <fsserver/accessor.h>
2.7 +#include <fsserver/accountable.h>
2.8 #include <fsserver/page_owner.h>
2.9
2.10 #include <mutex>
2.11 @@ -31,13 +32,12 @@
2.12
2.13 /* A file mapper, associating flexpages with file regions. */
2.14
2.15 -class PageMapper : public PageOwner
2.16 +class PageMapper : public PageOwner, public Accountable
2.17 {
2.18 protected:
2.19 AccessMap _map;
2.20 Accessor *_accessor;
2.21 Pages *_pages;
2.22 - unsigned int _attached;
2.23
2.24 /* Serialisation of accesses. */
2.25
2.26 @@ -52,9 +52,7 @@
2.27 public:
2.28 explicit PageMapper(Accessor *accessor, Pages *pages);
2.29
2.30 - /* Accounting methods. */
2.31 -
2.32 - void attach();
2.33 + /* Specialised accounting methods. */
2.34
2.35 unsigned int detach();
2.36
3.1 --- a/libfsserver/lib/Makefile Fri Sep 10 23:16:41 2021 +0200
3.2 +++ b/libfsserver/lib/Makefile Sat Sep 11 00:16:10 2021 +0200
3.3 @@ -60,6 +60,7 @@
3.4 files/test_file_accessor.cc \
3.5 files/test_file_opener.cc \
3.6 generic/accessor.cc \
3.7 + generic/accountable.cc \
3.8 generic/notification.cc \
3.9 generic/pager.cc \
3.10 generic/resource_server.cc \
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/libfsserver/lib/generic/accountable.cc Sat Sep 11 00:16:10 2021 +0200
4.3 @@ -0,0 +1,51 @@
4.4 +/*
4.5 + * Accountable object support.
4.6 + *
4.7 + * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk>
4.8 + *
4.9 + * This program is free software; you can redistribute it and/or
4.10 + * modify it under the terms of the GNU General Public License as
4.11 + * published by the Free Software Foundation; either version 2 of
4.12 + * the License, or (at your option) any later version.
4.13 + *
4.14 + * This program is distributed in the hope that it will be useful,
4.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4.17 + * GNU General Public License for more details.
4.18 + *
4.19 + * You should have received a copy of the GNU General Public License
4.20 + * along with this program; if not, write to the Free Software
4.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
4.22 + * Boston, MA 02110-1301, USA
4.23 + */
4.24 +
4.25 +#include "accountable.h"
4.26 +
4.27 +
4.28 +
4.29 +Accountable::~Accountable()
4.30 +{
4.31 +}
4.32 +
4.33 +/* Attach to the object. */
4.34 +
4.35 +void Accountable::attach()
4.36 +{
4.37 + std::lock_guard<std::mutex> guard(_attached_lock);
4.38 +
4.39 + _attached += 1;
4.40 +}
4.41 +
4.42 +/* Detach from the object. */
4.43 +
4.44 +unsigned int Accountable::detach()
4.45 +{
4.46 + std::lock_guard<std::mutex> guard(_attached_lock);
4.47 +
4.48 + if (_attached)
4.49 + _attached -= 1;
4.50 +
4.51 + return _attached;
4.52 +}
4.53 +
4.54 +// vim: tabstop=4 expandtab shiftwidth=4
5.1 --- a/libfsserver/lib/mapping/page_mapper.cc Fri Sep 10 23:16:41 2021 +0200
5.2 +++ b/libfsserver/lib/mapping/page_mapper.cc Sat Sep 11 00:16:10 2021 +0200
5.3 @@ -25,40 +25,26 @@
5.4
5.5
5.6 PageMapper::PageMapper(Accessor *accessor, Pages *pages)
5.7 -: _accessor(accessor), _pages(pages), _attached(0)
5.8 +: _accessor(accessor), _pages(pages)
5.9 {
5.10 }
5.11
5.12 /* Accounting methods. */
5.13
5.14 -/* Attach a pager, opening the accessor if required. */
5.15 -
5.16 -void PageMapper::attach()
5.17 -{
5.18 - std::lock_guard<std::mutex> guard(_lock);
5.19 -
5.20 - _attached += 1;
5.21 -}
5.22 -
5.23 /* Detach a pager, purging active pages and closing the accessor if no more
5.24 pagers are attached. Return whether any pagers are still attached. */
5.25
5.26 unsigned int PageMapper::detach()
5.27 {
5.28 - std::lock_guard<std::mutex> guard(_lock);
5.29 -
5.30 - if (_attached)
5.31 - {
5.32 - _attached -= 1;
5.33 + unsigned int attached = Accountable::detach();
5.34
5.35 - if (!_attached)
5.36 - {
5.37 - _map.purge(this, _pages);
5.38 - _accessor->close();
5.39 - }
5.40 + if (!attached)
5.41 + {
5.42 + _map.purge(this, _pages);
5.43 + _accessor->close();
5.44 }
5.45
5.46 - return _attached;
5.47 + return attached;
5.48 }
5.49
5.50 /* Interface for the pager. */