1 /* 2 * Resource server functionality. 3 * 4 * Copyright (C) 2018, 2019, 2020, 2021, 2022 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/sys/types.h> 23 24 #include <pthread-l4.h> 25 #include <pthread.h> 26 27 #include "resource_server.h" 28 29 30 31 /* Convenience server methods. */ 32 33 /* Bind to a named IPC gate capability. */ 34 35 long ResourceServer::bind(const char *name) 36 { 37 return ipc_server_bind(name, (l4_umword_t) _config, &_config->server); 38 } 39 40 /* Start in the same thread with no deletion notifications or finalisation. */ 41 42 long ResourceServer::start() 43 { 44 resource_init_config(_config, _resource); 45 _config->thread = pthread_l4_cap(pthread_self()); 46 return resource_start_config(_config, _resource); 47 } 48 49 /* Start a new thread with deletion notifications and finalisation. */ 50 51 long ResourceServer::start_thread() 52 { 53 pthread_t thread; 54 pthread_attr_t attr; 55 long err; 56 57 pthread_attr_init(&attr); 58 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 59 60 resource_init_config(_config, _resource); 61 62 err = pthread_create(&thread, &attr, ipc_server_start_mainloop, _config); 63 if (err) 64 return err; 65 66 resource_set_config_threaded(_config, pthread_l4_cap(thread), 1); 67 68 return resource_start_config(_config, _resource); 69 } 70 71 /* A convenience method starting a thread and returning the server capability 72 employed via the given parameter. */ 73 74 long ResourceServer::start_thread(l4_cap_idx_t *server) 75 { 76 long err = start_thread(); 77 78 if (!err) 79 *server = _config->server; 80 81 return err; 82 } 83 84 85 86 /* Initialise a server configuration for a resource. */ 87 88 void resource_init_config(ipc_server_config_type *config, Resource *resource) 89 { 90 ipc_server_default_config_type default_config = resource->config(); 91 92 config->handler_obj = resource->interface(); 93 config->finaliser_obj = resource; 94 config->expected_items = default_config.expected_items; 95 config->handler = default_config.handler; 96 } 97 98 /* Set a configuration to be threaded. */ 99 100 void resource_set_config_threaded(ipc_server_config_type *config, 101 l4_cap_idx_t thread, int new_thread) 102 { 103 config->finaliser = resource_thread_finaliser; 104 config->config_thread = new_thread; 105 config->thread = thread; 106 config->notifications = 1; 107 } 108 109 /* Activate a resource and start a server for it. */ 110 111 long resource_start_config(ipc_server_config_type *config, Resource *resource) 112 { 113 resource->activate(); 114 long err = ipc_server_start_config(config); 115 116 /* Discard any server resources if starting it failed. */ 117 118 if (err) 119 { 120 ipc_server_finalise_config(config); 121 ipc_server_discard_thread(config); 122 } 123 124 return err; 125 } 126 127 128 129 /* A finaliser for exposed resources. */ 130 131 void resource_thread_finaliser(ipc_server_config_type *config) 132 { 133 Resource *resource = reinterpret_cast<Resource *>(config->finaliser_obj); 134 135 /* Close and delete the resource. */ 136 137 resource->close(); 138 delete resource; 139 140 /* Release the capabilities. */ 141 142 ipc_server_finalise_config(config); 143 delete config; 144 } 145 146 // vim: tabstop=2 expandtab shiftwidth=2