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