1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libresource/lib/src/resource_server.cc Wed Mar 22 00:26:17 2023 +0100
1.3 @@ -0,0 +1,189 @@
1.4 +/*
1.5 + * Resource server functionality.
1.6 + *
1.7 + * Copyright (C) 2018, 2019, 2020, 2021, 2022 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/re/env.h>
1.26 +#include <l4/sys/types.h>
1.27 +
1.28 +#include <pthread-l4.h>
1.29 +#include <pthread.h>
1.30 +
1.31 +#include "resource_server.h"
1.32 +
1.33 +
1.34 +
1.35 +/* Convenience server methods. */
1.36 +
1.37 +/* Bind to a named IPC gate capability. */
1.38 +
1.39 +long ResourceServer::bind(const char *name)
1.40 +{
1.41 + return ipc_server_bind(name, (l4_umword_t) _config, &_config->server);
1.42 +}
1.43 +
1.44 +/* Start in the same thread indicating whether deletion notifications and
1.45 + finalisation are to be used. */
1.46 +
1.47 +long ResourceServer::start(bool finalisation)
1.48 +{
1.49 + resource_init_config(_config, _resource);
1.50 + _config->thread = pthread_l4_cap(pthread_self());
1.51 +
1.52 + /* NOTE: On MIPS32, at least, in a payload started by libexec, the main thread
1.53 + is not necessarily returned correctly. */
1.54 +
1.55 + if (l4_is_invalid_cap(_config->thread))
1.56 + _config->thread = l4re_env()->main_thread;
1.57 +
1.58 + if (finalisation)
1.59 + {
1.60 + _config->finaliser = resource_same_thread_finaliser;
1.61 + _config->notifications = 1;
1.62 + }
1.63 +
1.64 + return resource_start_config(_config, _resource);
1.65 +}
1.66 +
1.67 +/* Start serving a resource in an existing thread. */
1.68 +
1.69 +long ResourceServer::start_in_thread(l4_cap_idx_t thread, bool finalisation)
1.70 +{
1.71 + resource_init_config(_config, _resource);
1.72 + resource_set_config_threaded(_config, thread, 1, finalisation);
1.73 +
1.74 + return resource_start_config(_config, _resource);
1.75 +}
1.76 +
1.77 +/* Start serving a resource in a new thread. */
1.78 +
1.79 +long ResourceServer::start_thread(bool finalisation)
1.80 +{
1.81 + pthread_t thread;
1.82 + pthread_attr_t attr;
1.83 + long err;
1.84 +
1.85 + pthread_attr_init(&attr);
1.86 + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1.87 +
1.88 + resource_init_config(_config, _resource);
1.89 +
1.90 + err = pthread_create(&thread, &attr, ipc_server_start_mainloop, _config);
1.91 + if (err)
1.92 + return err;
1.93 +
1.94 + resource_set_config_threaded(_config, pthread_l4_cap(thread), 1, finalisation);
1.95 +
1.96 + return resource_start_config(_config, _resource);
1.97 +}
1.98 +
1.99 +/* A convenience method starting a thread and returning the server capability
1.100 + employed via the given parameter. */
1.101 +
1.102 +long ResourceServer::start_thread(l4_cap_idx_t *server, bool finalisation)
1.103 +{
1.104 + long err = start_thread(finalisation);
1.105 +
1.106 + if (!err)
1.107 + *server = _config->server;
1.108 +
1.109 + return err;
1.110 +}
1.111 +
1.112 +
1.113 +
1.114 +/* Initialise a server configuration for a resource. */
1.115 +
1.116 +void resource_init_config(ipc_server_config_type *config, Resource *resource)
1.117 +{
1.118 + ipc_server_default_config_type default_config = resource->config();
1.119 +
1.120 + config->handler_obj = resource->interface();
1.121 + config->finaliser_obj = resource;
1.122 + config->expected_items = default_config.expected_items;
1.123 + config->handler = default_config.handler;
1.124 +}
1.125 +
1.126 +/* Set a configuration to be threaded. */
1.127 +
1.128 +void resource_set_config_threaded(ipc_server_config_type *config,
1.129 + l4_cap_idx_t thread, int new_thread,
1.130 + int finalisation)
1.131 +{
1.132 + config->finaliser = resource_thread_finaliser;
1.133 + config->config_thread = new_thread;
1.134 + config->thread = thread;
1.135 + config->notifications = finalisation;
1.136 +}
1.137 +
1.138 +/* Activate a resource and start a server for it. */
1.139 +
1.140 +long resource_start_config(ipc_server_config_type *config, Resource *resource)
1.141 +{
1.142 + resource->activate();
1.143 + long err = ipc_server_start_config(config);
1.144 +
1.145 + /* Discard any server resources if starting it failed. */
1.146 +
1.147 + if (err)
1.148 + {
1.149 + ipc_server_finalise_config(config);
1.150 + ipc_server_discard_thread(config);
1.151 + }
1.152 +
1.153 + return err;
1.154 +}
1.155 +
1.156 +
1.157 +
1.158 +/* A finaliser for exposed resources in the same thread. */
1.159 +
1.160 +void resource_same_thread_finaliser(ipc_server_config_type *config)
1.161 +{
1.162 + Resource *resource = reinterpret_cast<Resource *>(config->finaliser_obj);
1.163 +
1.164 + /* Close but do not delete the resource since it is assumed that it is being
1.165 + managed by the thread. */
1.166 +
1.167 + resource->close();
1.168 +
1.169 + /* Release the capabilities. */
1.170 +
1.171 + ipc_server_finalise_config(config);
1.172 + delete config;
1.173 +}
1.174 +
1.175 +/* A finaliser for exposed resources in a different thread. */
1.176 +
1.177 +void resource_thread_finaliser(ipc_server_config_type *config)
1.178 +{
1.179 + Resource *resource = reinterpret_cast<Resource *>(config->finaliser_obj);
1.180 +
1.181 + /* Close and delete the resource. */
1.182 +
1.183 + resource->close();
1.184 + delete resource;
1.185 +
1.186 + /* Release the capabilities. */
1.187 +
1.188 + ipc_server_finalise_config(config);
1.189 + delete config;
1.190 +}
1.191 +
1.192 +// vim: tabstop=2 expandtab shiftwidth=2