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) _resource, &_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 72 73 /* Initialise a server configuration for a resource. */ 74 75 void resource_init_config(ipc_server_config_type *config, Resource *resource) 76 { 77 config->handler_obj = resource->interface(); 78 config->finaliser_obj = resource; 79 config->expected_items = resource->expected_items(); 80 config->handler = resource->handler(); 81 } 82 83 /* Set a configuration to be threaded. */ 84 85 void resource_set_config_threaded(ipc_server_config_type *config, 86 l4_cap_idx_t thread, int new_thread) 87 { 88 config->finaliser = resource_thread_finaliser; 89 config->new_thread = new_thread; 90 config->thread = thread; 91 config->notifications = 1; 92 } 93 94 /* Activate a resource and start a server for it. */ 95 96 long resource_start_config(ipc_server_config_type *config, Resource *resource) 97 { 98 resource->activate(); 99 long err = ipc_server_start_config(config); 100 101 /* Discard any server resources if starting it failed. */ 102 103 if (err) 104 { 105 ipc_server_finalise_config(config); 106 ipc_server_discard_thread(config); 107 } 108 109 return err; 110 } 111 112 113 114 /* A finaliser for exposed resources. */ 115 116 void resource_thread_finaliser(ipc_server_config_type *config) 117 { 118 Resource *resource = reinterpret_cast<Resource *>(config->finaliser_obj); 119 120 /* Close and delete the resource. */ 121 122 resource->close(); 123 delete resource; 124 125 /* Release the capabilities. */ 126 127 ipc_server_finalise_config(config); 128 delete config; 129 } 130 131 // vim: tabstop=2 expandtab shiftwidth=2