1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/generic/resource_server.cc Fri Apr 02 22:38:14 2021 +0200
1.3 @@ -0,0 +1,131 @@
1.4 +/*
1.5 + * Resource server functionality.
1.6 + *
1.7 + * Copyright (C) 2018, 2019, 2020, 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 +#include <l4/sys/types.h>
1.26 +
1.27 +#include <pthread-l4.h>
1.28 +#include <pthread.h>
1.29 +
1.30 +#include "resource_server.h"
1.31 +
1.32 +
1.33 +
1.34 +/* Convenience server methods. */
1.35 +
1.36 +/* Bind to a named IPC gate capability. */
1.37 +
1.38 +long ResourceServer::bind(const char *name)
1.39 +{
1.40 + return ipc_server_bind(name, (l4_umword_t) _resource, &_config->server);
1.41 +}
1.42 +
1.43 +/* Start in the same thread with no deletion notifications or finalisation. */
1.44 +
1.45 +long ResourceServer::start()
1.46 +{
1.47 + resource_init_config(_config, _resource);
1.48 + _config->thread = pthread_l4_cap(pthread_self());
1.49 + return resource_start_config(_config, _resource);
1.50 +}
1.51 +
1.52 +/* Start a new thread with deletion notifications and finalisation. */
1.53 +
1.54 +long ResourceServer::start_thread()
1.55 +{
1.56 + pthread_t thread;
1.57 + pthread_attr_t attr;
1.58 + long err;
1.59 +
1.60 + pthread_attr_init(&attr);
1.61 + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1.62 +
1.63 + resource_init_config(_config, _resource);
1.64 +
1.65 + err = pthread_create(&thread, &attr, ipc_server_start_mainloop, _config);
1.66 + if (err)
1.67 + return err;
1.68 +
1.69 + resource_set_config_threaded(_config, pthread_l4_cap(thread), 1);
1.70 +
1.71 + return resource_start_config(_config, _resource);
1.72 +}
1.73 +
1.74 +
1.75 +
1.76 +/* Initialise a server configuration for a resource. */
1.77 +
1.78 +void resource_init_config(ipc_server_config_type *config, Resource *resource)
1.79 +{
1.80 + config->handler_obj = resource->interface();
1.81 + config->finaliser_obj = resource;
1.82 + config->expected_items = resource->expected_items();
1.83 + config->handler = resource->handler();
1.84 +}
1.85 +
1.86 +/* Set a configuration to be threaded. */
1.87 +
1.88 +void resource_set_config_threaded(ipc_server_config_type *config,
1.89 + l4_cap_idx_t thread, int new_thread)
1.90 +{
1.91 + config->finaliser = resource_thread_finaliser;
1.92 + config->new_thread = new_thread;
1.93 + config->thread = thread;
1.94 + config->notifications = 1;
1.95 +}
1.96 +
1.97 +/* Activate a resource and start a server for it. */
1.98 +
1.99 +long resource_start_config(ipc_server_config_type *config, Resource *resource)
1.100 +{
1.101 + resource->activate();
1.102 + long err = ipc_server_start_config(config);
1.103 +
1.104 + /* Discard any server resources if starting it failed. */
1.105 +
1.106 + if (err)
1.107 + {
1.108 + ipc_server_finalise_config(config);
1.109 + ipc_server_discard_thread(config);
1.110 + }
1.111 +
1.112 + return err;
1.113 +}
1.114 +
1.115 +
1.116 +
1.117 +/* A finaliser for exposed resources. */
1.118 +
1.119 +void resource_thread_finaliser(ipc_server_config_type *config)
1.120 +{
1.121 + Resource *resource = reinterpret_cast<Resource *>(config->finaliser_obj);
1.122 +
1.123 + /* Close and delete the resource. */
1.124 +
1.125 + resource->close();
1.126 + delete resource;
1.127 +
1.128 + /* Release the capabilities. */
1.129 +
1.130 + ipc_server_finalise_config(config);
1.131 + delete config;
1.132 +}
1.133 +
1.134 +// vim: tabstop=2 expandtab shiftwidth=2