1.1 --- a/Makefile Fri Apr 02 01:27:50 2021 +0200
1.2 +++ b/Makefile Fri Apr 02 22:38:14 2021 +0200
1.3 @@ -53,12 +53,12 @@
1.4
1.5 PLAIN_SRC_CC_common_server = \
1.6 generic/accessor.cc generic/pager.cc \
1.7 + generic/resource_server.cc \
1.8 mapping/access_map.cc mapping/flexpage.cc mapping/ipc.cc \
1.9 memory/memory_incremental.cc mapping/page_mapper.cc \
1.10 memory/memory_preallocated.cc memory/region.cc \
1.11 pages/page_queue.cc pages/page_queue_partitioned.cc \
1.12 - pages/page_queue_shared.cc pages/pages.cc \
1.13 - resource_server.cc
1.14 + pages/page_queue_shared.cc pages/pages.cc
1.15
1.16 PLAIN_SRC_CC_common_file_server = \
1.17 files/file_pager.cc files/file_paging.cc \
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/generic/resource_server.cc Fri Apr 02 22:38:14 2021 +0200
2.3 @@ -0,0 +1,131 @@
2.4 +/*
2.5 + * Resource server functionality.
2.6 + *
2.7 + * Copyright (C) 2018, 2019, 2020, 2021 Paul Boddie <paul@boddie.org.uk>
2.8 + *
2.9 + * This program is free software; you can redistribute it and/or
2.10 + * modify it under the terms of the GNU General Public License as
2.11 + * published by the Free Software Foundation; either version 2 of
2.12 + * the License, or (at your option) any later version.
2.13 + *
2.14 + * This program is distributed in the hope that it will be useful,
2.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2.17 + * GNU General Public License for more details.
2.18 + *
2.19 + * You should have received a copy of the GNU General Public License
2.20 + * along with this program; if not, write to the Free Software
2.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
2.22 + * Boston, MA 02110-1301, USA
2.23 + */
2.24 +
2.25 +#include <l4/sys/types.h>
2.26 +
2.27 +#include <pthread-l4.h>
2.28 +#include <pthread.h>
2.29 +
2.30 +#include "resource_server.h"
2.31 +
2.32 +
2.33 +
2.34 +/* Convenience server methods. */
2.35 +
2.36 +/* Bind to a named IPC gate capability. */
2.37 +
2.38 +long ResourceServer::bind(const char *name)
2.39 +{
2.40 + return ipc_server_bind(name, (l4_umword_t) _resource, &_config->server);
2.41 +}
2.42 +
2.43 +/* Start in the same thread with no deletion notifications or finalisation. */
2.44 +
2.45 +long ResourceServer::start()
2.46 +{
2.47 + resource_init_config(_config, _resource);
2.48 + _config->thread = pthread_l4_cap(pthread_self());
2.49 + return resource_start_config(_config, _resource);
2.50 +}
2.51 +
2.52 +/* Start a new thread with deletion notifications and finalisation. */
2.53 +
2.54 +long ResourceServer::start_thread()
2.55 +{
2.56 + pthread_t thread;
2.57 + pthread_attr_t attr;
2.58 + long err;
2.59 +
2.60 + pthread_attr_init(&attr);
2.61 + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2.62 +
2.63 + resource_init_config(_config, _resource);
2.64 +
2.65 + err = pthread_create(&thread, &attr, ipc_server_start_mainloop, _config);
2.66 + if (err)
2.67 + return err;
2.68 +
2.69 + resource_set_config_threaded(_config, pthread_l4_cap(thread), 1);
2.70 +
2.71 + return resource_start_config(_config, _resource);
2.72 +}
2.73 +
2.74 +
2.75 +
2.76 +/* Initialise a server configuration for a resource. */
2.77 +
2.78 +void resource_init_config(ipc_server_config_type *config, Resource *resource)
2.79 +{
2.80 + config->handler_obj = resource->interface();
2.81 + config->finaliser_obj = resource;
2.82 + config->expected_items = resource->expected_items();
2.83 + config->handler = resource->handler();
2.84 +}
2.85 +
2.86 +/* Set a configuration to be threaded. */
2.87 +
2.88 +void resource_set_config_threaded(ipc_server_config_type *config,
2.89 + l4_cap_idx_t thread, int new_thread)
2.90 +{
2.91 + config->finaliser = resource_thread_finaliser;
2.92 + config->new_thread = new_thread;
2.93 + config->thread = thread;
2.94 + config->notifications = 1;
2.95 +}
2.96 +
2.97 +/* Activate a resource and start a server for it. */
2.98 +
2.99 +long resource_start_config(ipc_server_config_type *config, Resource *resource)
2.100 +{
2.101 + resource->activate();
2.102 + long err = ipc_server_start_config(config);
2.103 +
2.104 + /* Discard any server resources if starting it failed. */
2.105 +
2.106 + if (err)
2.107 + {
2.108 + ipc_server_finalise_config(config);
2.109 + ipc_server_discard_thread(config);
2.110 + }
2.111 +
2.112 + return err;
2.113 +}
2.114 +
2.115 +
2.116 +
2.117 +/* A finaliser for exposed resources. */
2.118 +
2.119 +void resource_thread_finaliser(ipc_server_config_type *config)
2.120 +{
2.121 + Resource *resource = reinterpret_cast<Resource *>(config->finaliser_obj);
2.122 +
2.123 + /* Close and delete the resource. */
2.124 +
2.125 + resource->close();
2.126 + delete resource;
2.127 +
2.128 + /* Release the capabilities. */
2.129 +
2.130 + ipc_server_finalise_config(config);
2.131 + delete config;
2.132 +}
2.133 +
2.134 +// vim: tabstop=2 expandtab shiftwidth=2
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/generic/resource_server.h Fri Apr 02 22:38:14 2021 +0200
3.3 @@ -0,0 +1,76 @@
3.4 +/*
3.5 + * Common resource server functions.
3.6 + *
3.7 + * Copyright (C) 2018, 2019, 2020 Paul Boddie <paul@boddie.org.uk>
3.8 + *
3.9 + * This program is free software; you can redistribute it and/or
3.10 + * modify it under the terms of the GNU General Public License as
3.11 + * published by the Free Software Foundation; either version 2 of
3.12 + * the License, or (at your option) any later version.
3.13 + *
3.14 + * This program is distributed in the hope that it will be useful,
3.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.17 + * GNU General Public License for more details.
3.18 + *
3.19 + * You should have received a copy of the GNU General Public License
3.20 + * along with this program; if not, write to the Free Software
3.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
3.22 + * Boston, MA 02110-1301, USA
3.23 + */
3.24 +
3.25 +#pragma once
3.26 +
3.27 +#include "resource.h"
3.28 +#include <ipc/server.h>
3.29 +
3.30 +
3.31 +
3.32 +/* Convenience abstraction for blocking servers. */
3.33 +
3.34 +class ResourceServer
3.35 +{
3.36 +protected:
3.37 + Resource *_resource;
3.38 + ipc_server_config_type *_config;
3.39 +
3.40 +public:
3.41 + explicit ResourceServer(Resource *resource)
3.42 + : _resource(resource)
3.43 + {
3.44 + _config = new ipc_server_config_type;
3.45 + ipc_server_init_config(_config);
3.46 + }
3.47 +
3.48 + /* Access to configuration. */
3.49 +
3.50 + ipc_server_config_type *config()
3.51 + { return _config; }
3.52 +
3.53 + /* Server IPC gate allocation. */
3.54 +
3.55 + long bind(const char *name);
3.56 +
3.57 + /* Server initiation. */
3.58 +
3.59 + long start();
3.60 +
3.61 + long start_thread();
3.62 +};
3.63 +
3.64 +
3.65 +
3.66 +/* Server initialisation. */
3.67 +
3.68 +void resource_init_config(ipc_server_config_type *config, Resource *resource);
3.69 +
3.70 +void resource_set_config_threaded(ipc_server_config_type *config,
3.71 + l4_cap_idx_t thread, int new_thread);
3.72 +
3.73 +/* Server initiation. */
3.74 +
3.75 +long resource_start_config(ipc_server_config_type *config, Resource *resource);
3.76 +
3.77 +/* Server finalisation. */
3.78 +
3.79 +void resource_thread_finaliser(ipc_server_config_type *config);
4.1 --- a/resource_server.cc Fri Apr 02 01:27:50 2021 +0200
4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
4.3 @@ -1,131 +0,0 @@
4.4 -/*
4.5 - * Resource server functionality.
4.6 - *
4.7 - * Copyright (C) 2018, 2019, 2020, 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 <l4/sys/types.h>
4.26 -
4.27 -#include <pthread-l4.h>
4.28 -#include <pthread.h>
4.29 -
4.30 -#include "resource_server.h"
4.31 -
4.32 -
4.33 -
4.34 -/* Convenience server methods. */
4.35 -
4.36 -/* Bind to a named IPC gate capability. */
4.37 -
4.38 -long ResourceServer::bind(const char *name)
4.39 -{
4.40 - return ipc_server_bind(name, (l4_umword_t) _resource, &_config->server);
4.41 -}
4.42 -
4.43 -/* Start in the same thread with no deletion notifications or finalisation. */
4.44 -
4.45 -long ResourceServer::start()
4.46 -{
4.47 - resource_init_config(_config, _resource);
4.48 - _config->thread = pthread_l4_cap(pthread_self());
4.49 - return resource_start_config(_config, _resource);
4.50 -}
4.51 -
4.52 -/* Start a new thread with deletion notifications and finalisation. */
4.53 -
4.54 -long ResourceServer::start_thread()
4.55 -{
4.56 - pthread_t thread;
4.57 - pthread_attr_t attr;
4.58 - long err;
4.59 -
4.60 - pthread_attr_init(&attr);
4.61 - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
4.62 -
4.63 - resource_init_config(_config, _resource);
4.64 -
4.65 - err = pthread_create(&thread, &attr, ipc_server_start_mainloop, _config);
4.66 - if (err)
4.67 - return err;
4.68 -
4.69 - resource_set_config_threaded(_config, pthread_l4_cap(thread), 1);
4.70 -
4.71 - return resource_start_config(_config, _resource);
4.72 -}
4.73 -
4.74 -
4.75 -
4.76 -/* Initialise a server configuration for a resource. */
4.77 -
4.78 -void resource_init_config(ipc_server_config_type *config, Resource *resource)
4.79 -{
4.80 - config->handler_obj = resource->interface();
4.81 - config->finaliser_obj = resource;
4.82 - config->expected_items = resource->expected_items();
4.83 - config->handler = resource->handler();
4.84 -}
4.85 -
4.86 -/* Set a configuration to be threaded. */
4.87 -
4.88 -void resource_set_config_threaded(ipc_server_config_type *config,
4.89 - l4_cap_idx_t thread, int new_thread)
4.90 -{
4.91 - config->finaliser = resource_thread_finaliser;
4.92 - config->new_thread = new_thread;
4.93 - config->thread = thread;
4.94 - config->notifications = 1;
4.95 -}
4.96 -
4.97 -/* Activate a resource and start a server for it. */
4.98 -
4.99 -long resource_start_config(ipc_server_config_type *config, Resource *resource)
4.100 -{
4.101 - resource->activate();
4.102 - long err = ipc_server_start_config(config);
4.103 -
4.104 - /* Discard any server resources if starting it failed. */
4.105 -
4.106 - if (err)
4.107 - {
4.108 - ipc_server_finalise_config(config);
4.109 - ipc_server_discard_thread(config);
4.110 - }
4.111 -
4.112 - return err;
4.113 -}
4.114 -
4.115 -
4.116 -
4.117 -/* A finaliser for exposed resources. */
4.118 -
4.119 -void resource_thread_finaliser(ipc_server_config_type *config)
4.120 -{
4.121 - Resource *resource = reinterpret_cast<Resource *>(config->finaliser_obj);
4.122 -
4.123 - /* Close and delete the resource. */
4.124 -
4.125 - resource->close();
4.126 - delete resource;
4.127 -
4.128 - /* Release the capabilities. */
4.129 -
4.130 - ipc_server_finalise_config(config);
4.131 - delete config;
4.132 -}
4.133 -
4.134 -// vim: tabstop=2 expandtab shiftwidth=2
5.1 --- a/resource_server.h Fri Apr 02 01:27:50 2021 +0200
5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
5.3 @@ -1,76 +0,0 @@
5.4 -/*
5.5 - * Common resource server functions.
5.6 - *
5.7 - * Copyright (C) 2018, 2019, 2020 Paul Boddie <paul@boddie.org.uk>
5.8 - *
5.9 - * This program is free software; you can redistribute it and/or
5.10 - * modify it under the terms of the GNU General Public License as
5.11 - * published by the Free Software Foundation; either version 2 of
5.12 - * the License, or (at your option) any later version.
5.13 - *
5.14 - * This program is distributed in the hope that it will be useful,
5.15 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
5.16 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5.17 - * GNU General Public License for more details.
5.18 - *
5.19 - * You should have received a copy of the GNU General Public License
5.20 - * along with this program; if not, write to the Free Software
5.21 - * Foundation, Inc., 51 Franklin Street, Fifth Floor,
5.22 - * Boston, MA 02110-1301, USA
5.23 - */
5.24 -
5.25 -#pragma once
5.26 -
5.27 -#include "resource.h"
5.28 -#include <ipc/server.h>
5.29 -
5.30 -
5.31 -
5.32 -/* Convenience abstraction for blocking servers. */
5.33 -
5.34 -class ResourceServer
5.35 -{
5.36 -protected:
5.37 - Resource *_resource;
5.38 - ipc_server_config_type *_config;
5.39 -
5.40 -public:
5.41 - explicit ResourceServer(Resource *resource)
5.42 - : _resource(resource)
5.43 - {
5.44 - _config = new ipc_server_config_type;
5.45 - ipc_server_init_config(_config);
5.46 - }
5.47 -
5.48 - /* Access to configuration. */
5.49 -
5.50 - ipc_server_config_type *config()
5.51 - { return _config; }
5.52 -
5.53 - /* Server IPC gate allocation. */
5.54 -
5.55 - long bind(const char *name);
5.56 -
5.57 - /* Server initiation. */
5.58 -
5.59 - long start();
5.60 -
5.61 - long start_thread();
5.62 -};
5.63 -
5.64 -
5.65 -
5.66 -/* Server initialisation. */
5.67 -
5.68 -void resource_init_config(ipc_server_config_type *config, Resource *resource);
5.69 -
5.70 -void resource_set_config_threaded(ipc_server_config_type *config,
5.71 - l4_cap_idx_t thread, int new_thread);
5.72 -
5.73 -/* Server initiation. */
5.74 -
5.75 -long resource_start_config(ipc_server_config_type *config, Resource *resource);
5.76 -
5.77 -/* Server finalisation. */
5.78 -
5.79 -void resource_thread_finaliser(ipc_server_config_type *config);