# HG changeset patch # User Paul Boddie # Date 1617395894 -7200 # Node ID f76f98fe5ee439346a71e14f0fe3245f3f97c0bb # Parent 5e0048b834f208c539ef48badcfc1020a81fce51 Moved resource server files to the generic subdirectory. diff -r 5e0048b834f2 -r f76f98fe5ee4 Makefile --- a/Makefile Fri Apr 02 01:27:50 2021 +0200 +++ b/Makefile Fri Apr 02 22:38:14 2021 +0200 @@ -53,12 +53,12 @@ PLAIN_SRC_CC_common_server = \ generic/accessor.cc generic/pager.cc \ + generic/resource_server.cc \ mapping/access_map.cc mapping/flexpage.cc mapping/ipc.cc \ memory/memory_incremental.cc mapping/page_mapper.cc \ memory/memory_preallocated.cc memory/region.cc \ pages/page_queue.cc pages/page_queue_partitioned.cc \ - pages/page_queue_shared.cc pages/pages.cc \ - resource_server.cc + pages/page_queue_shared.cc pages/pages.cc PLAIN_SRC_CC_common_file_server = \ files/file_pager.cc files/file_paging.cc \ diff -r 5e0048b834f2 -r f76f98fe5ee4 generic/resource_server.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/generic/resource_server.cc Fri Apr 02 22:38:14 2021 +0200 @@ -0,0 +1,131 @@ +/* + * Resource server functionality. + * + * Copyright (C) 2018, 2019, 2020, 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 + +#include +#include + +#include "resource_server.h" + + + +/* Convenience server methods. */ + +/* Bind to a named IPC gate capability. */ + +long ResourceServer::bind(const char *name) +{ + return ipc_server_bind(name, (l4_umword_t) _resource, &_config->server); +} + +/* Start in the same thread with no deletion notifications or finalisation. */ + +long ResourceServer::start() +{ + resource_init_config(_config, _resource); + _config->thread = pthread_l4_cap(pthread_self()); + return resource_start_config(_config, _resource); +} + +/* Start a new thread with deletion notifications and finalisation. */ + +long ResourceServer::start_thread() +{ + pthread_t thread; + pthread_attr_t attr; + long err; + + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + + resource_init_config(_config, _resource); + + err = pthread_create(&thread, &attr, ipc_server_start_mainloop, _config); + if (err) + return err; + + resource_set_config_threaded(_config, pthread_l4_cap(thread), 1); + + return resource_start_config(_config, _resource); +} + + + +/* Initialise a server configuration for a resource. */ + +void resource_init_config(ipc_server_config_type *config, Resource *resource) +{ + config->handler_obj = resource->interface(); + config->finaliser_obj = resource; + config->expected_items = resource->expected_items(); + config->handler = resource->handler(); +} + +/* Set a configuration to be threaded. */ + +void resource_set_config_threaded(ipc_server_config_type *config, + l4_cap_idx_t thread, int new_thread) +{ + config->finaliser = resource_thread_finaliser; + config->new_thread = new_thread; + config->thread = thread; + config->notifications = 1; +} + +/* Activate a resource and start a server for it. */ + +long resource_start_config(ipc_server_config_type *config, Resource *resource) +{ + resource->activate(); + long err = ipc_server_start_config(config); + + /* Discard any server resources if starting it failed. */ + + if (err) + { + ipc_server_finalise_config(config); + ipc_server_discard_thread(config); + } + + return err; +} + + + +/* A finaliser for exposed resources. */ + +void resource_thread_finaliser(ipc_server_config_type *config) +{ + Resource *resource = reinterpret_cast(config->finaliser_obj); + + /* Close and delete the resource. */ + + resource->close(); + delete resource; + + /* Release the capabilities. */ + + ipc_server_finalise_config(config); + delete config; +} + +// vim: tabstop=2 expandtab shiftwidth=2 diff -r 5e0048b834f2 -r f76f98fe5ee4 generic/resource_server.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/generic/resource_server.h Fri Apr 02 22:38:14 2021 +0200 @@ -0,0 +1,76 @@ +/* + * Common resource server functions. + * + * Copyright (C) 2018, 2019, 2020 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 "resource.h" +#include + + + +/* Convenience abstraction for blocking servers. */ + +class ResourceServer +{ +protected: + Resource *_resource; + ipc_server_config_type *_config; + +public: + explicit ResourceServer(Resource *resource) + : _resource(resource) + { + _config = new ipc_server_config_type; + ipc_server_init_config(_config); + } + + /* Access to configuration. */ + + ipc_server_config_type *config() + { return _config; } + + /* Server IPC gate allocation. */ + + long bind(const char *name); + + /* Server initiation. */ + + long start(); + + long start_thread(); +}; + + + +/* Server initialisation. */ + +void resource_init_config(ipc_server_config_type *config, Resource *resource); + +void resource_set_config_threaded(ipc_server_config_type *config, + l4_cap_idx_t thread, int new_thread); + +/* Server initiation. */ + +long resource_start_config(ipc_server_config_type *config, Resource *resource); + +/* Server finalisation. */ + +void resource_thread_finaliser(ipc_server_config_type *config); diff -r 5e0048b834f2 -r f76f98fe5ee4 resource_server.cc --- a/resource_server.cc Fri Apr 02 01:27:50 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,131 +0,0 @@ -/* - * Resource server functionality. - * - * Copyright (C) 2018, 2019, 2020, 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 - -#include -#include - -#include "resource_server.h" - - - -/* Convenience server methods. */ - -/* Bind to a named IPC gate capability. */ - -long ResourceServer::bind(const char *name) -{ - return ipc_server_bind(name, (l4_umword_t) _resource, &_config->server); -} - -/* Start in the same thread with no deletion notifications or finalisation. */ - -long ResourceServer::start() -{ - resource_init_config(_config, _resource); - _config->thread = pthread_l4_cap(pthread_self()); - return resource_start_config(_config, _resource); -} - -/* Start a new thread with deletion notifications and finalisation. */ - -long ResourceServer::start_thread() -{ - pthread_t thread; - pthread_attr_t attr; - long err; - - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - - resource_init_config(_config, _resource); - - err = pthread_create(&thread, &attr, ipc_server_start_mainloop, _config); - if (err) - return err; - - resource_set_config_threaded(_config, pthread_l4_cap(thread), 1); - - return resource_start_config(_config, _resource); -} - - - -/* Initialise a server configuration for a resource. */ - -void resource_init_config(ipc_server_config_type *config, Resource *resource) -{ - config->handler_obj = resource->interface(); - config->finaliser_obj = resource; - config->expected_items = resource->expected_items(); - config->handler = resource->handler(); -} - -/* Set a configuration to be threaded. */ - -void resource_set_config_threaded(ipc_server_config_type *config, - l4_cap_idx_t thread, int new_thread) -{ - config->finaliser = resource_thread_finaliser; - config->new_thread = new_thread; - config->thread = thread; - config->notifications = 1; -} - -/* Activate a resource and start a server for it. */ - -long resource_start_config(ipc_server_config_type *config, Resource *resource) -{ - resource->activate(); - long err = ipc_server_start_config(config); - - /* Discard any server resources if starting it failed. */ - - if (err) - { - ipc_server_finalise_config(config); - ipc_server_discard_thread(config); - } - - return err; -} - - - -/* A finaliser for exposed resources. */ - -void resource_thread_finaliser(ipc_server_config_type *config) -{ - Resource *resource = reinterpret_cast(config->finaliser_obj); - - /* Close and delete the resource. */ - - resource->close(); - delete resource; - - /* Release the capabilities. */ - - ipc_server_finalise_config(config); - delete config; -} - -// vim: tabstop=2 expandtab shiftwidth=2 diff -r 5e0048b834f2 -r f76f98fe5ee4 resource_server.h --- a/resource_server.h Fri Apr 02 01:27:50 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -/* - * Common resource server functions. - * - * Copyright (C) 2018, 2019, 2020 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 "resource.h" -#include - - - -/* Convenience abstraction for blocking servers. */ - -class ResourceServer -{ -protected: - Resource *_resource; - ipc_server_config_type *_config; - -public: - explicit ResourceServer(Resource *resource) - : _resource(resource) - { - _config = new ipc_server_config_type; - ipc_server_init_config(_config); - } - - /* Access to configuration. */ - - ipc_server_config_type *config() - { return _config; } - - /* Server IPC gate allocation. */ - - long bind(const char *name); - - /* Server initiation. */ - - long start(); - - long start_thread(); -}; - - - -/* Server initialisation. */ - -void resource_init_config(ipc_server_config_type *config, Resource *resource); - -void resource_set_config_threaded(ipc_server_config_type *config, - l4_cap_idx_t thread, int new_thread); - -/* Server initiation. */ - -long resource_start_config(ipc_server_config_type *config, Resource *resource); - -/* Server finalisation. */ - -void resource_thread_finaliser(ipc_server_config_type *config);