1 /* 2 * Resource server functionality. 3 * 4 * Copyright (C) 2018, 2019, 2020, 2021 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 providing the server 72 capability. */ 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 config->handler_obj = resource->interface(); 91 config->finaliser_obj = resource; 92 config->expected_items = resource->expected_items(); 93 config->handler = resource->handler(); 94 } 95 96 /* Set a configuration to be threaded. */ 97 98 void resource_set_config_threaded(ipc_server_config_type *config, 99 l4_cap_idx_t thread, int new_thread) 100 { 101 config->finaliser = resource_thread_finaliser; 102 config->new_thread = new_thread; 103 config->thread = thread; 104 config->notifications = 1; 105 } 106 107 /* Activate a resource and start a server for it. */ 108 109 long resource_start_config(ipc_server_config_type *config, Resource *resource) 110 { 111 resource->activate(); 112 long err = ipc_server_start_config(config); 113 114 /* Discard any server resources if starting it failed. */ 115 116 if (err) 117 { 118 ipc_server_finalise_config(config); 119 ipc_server_discard_thread(config); 120 } 121 122 return err; 123 } 124 125 126 127 /* A finaliser for exposed resources. */ 128 129 void resource_thread_finaliser(ipc_server_config_type *config) 130 { 131 Resource *resource = reinterpret_cast<Resource *>(config->finaliser_obj); 132 133 /* Close and delete the resource. */ 134 135 resource->close(); 136 delete resource; 137 138 /* Release the capabilities. */ 139 140 ipc_server_finalise_config(config); 141 delete config; 142 } 143 144 // vim: tabstop=2 expandtab shiftwidth=2