Changeset 7d7f5e3 in mainline


Ignore:
Timestamp:
2023-09-17T09:56:59Z (8 months ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
153dd3b
Parents:
4c6fd56
Message:

loc_server_register() should be callable more than once (implementation)

We create a new session for each loc_server_register() / loc_srv_t
object. Alternatively we could multiplex all to a single connection
and then demultiplex them in the server, but this seemed simpler
at the moment.

We add a test case to libc test suite.

Location:
uspace/lib/c
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/loc.c

    r4c6fd56 r7d7f5e3  
    3939#include <stdbool.h>
    4040
    41 static FIBRIL_MUTEX_INITIALIZE(loc_supp_block_mutex);
    4241static FIBRIL_MUTEX_INITIALIZE(loc_cons_block_mutex);
    43 
    44 static FIBRIL_MUTEX_INITIALIZE(loc_supplier_mutex);
    4542static FIBRIL_MUTEX_INITIALIZE(loc_consumer_mutex);
    4643
     
    5047static void *cat_change_arg = NULL;
    5148
    52 static async_sess_t *loc_supp_block_sess = NULL;
    5349static async_sess_t *loc_cons_block_sess = NULL;
    54 
    55 static async_sess_t *loc_supplier_sess = NULL;
    5650static async_sess_t *loc_consumer_sess = NULL;
    5751
     
    108102        if (!loc_callback_created) {
    109103                async_exch_t *exch =
    110                     loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
     104                    loc_exchange_begin_blocking();
    111105
    112106                ipc_call_t answer;
     
    135129/** Start an async exchange on the loc session (blocking).
    136130 *
    137  * @param iface Location service interface to choose
    138  *
    139131 * @return New exchange.
    140132 *
    141133 */
    142 async_exch_t *loc_exchange_begin_blocking(iface_t iface)
    143 {
    144         switch (iface) {
    145         case INTERFACE_LOC_SUPPLIER:
    146                 fibril_mutex_lock(&loc_supp_block_mutex);
    147 
    148                 while (loc_supp_block_sess == NULL) {
    149                         clone_session(&loc_supplier_mutex, loc_supplier_sess,
    150                             &loc_supp_block_sess);
    151 
    152                         if (loc_supp_block_sess == NULL)
    153                                 loc_supp_block_sess =
    154                                     service_connect_blocking(SERVICE_LOC,
    155                                     INTERFACE_LOC_SUPPLIER, 0, NULL);
    156                 }
    157 
    158                 fibril_mutex_unlock(&loc_supp_block_mutex);
    159 
    160                 clone_session(&loc_supplier_mutex, loc_supp_block_sess,
    161                     &loc_supplier_sess);
    162 
    163                 return async_exchange_begin(loc_supp_block_sess);
    164         case INTERFACE_LOC_CONSUMER:
    165                 fibril_mutex_lock(&loc_cons_block_mutex);
    166 
    167                 while (loc_cons_block_sess == NULL) {
    168                         clone_session(&loc_consumer_mutex, loc_consumer_sess,
    169                             &loc_cons_block_sess);
    170 
    171                         if (loc_cons_block_sess == NULL)
    172                                 loc_cons_block_sess =
    173                                     service_connect_blocking(SERVICE_LOC,
    174                                     INTERFACE_LOC_CONSUMER, 0, NULL);
    175                 }
    176 
    177                 fibril_mutex_unlock(&loc_cons_block_mutex);
    178 
    179                 clone_session(&loc_consumer_mutex, loc_cons_block_sess,
    180                     &loc_consumer_sess);
    181 
    182                 return async_exchange_begin(loc_cons_block_sess);
    183         default:
     134async_exch_t *loc_exchange_begin_blocking(void)
     135{
     136        fibril_mutex_lock(&loc_cons_block_mutex);
     137
     138        while (loc_cons_block_sess == NULL) {
     139                clone_session(&loc_consumer_mutex, loc_consumer_sess,
     140                    &loc_cons_block_sess);
     141
     142                if (loc_cons_block_sess == NULL)
     143                        loc_cons_block_sess =
     144                            service_connect_blocking(SERVICE_LOC,
     145                            INTERFACE_LOC_CONSUMER, 0, NULL);
     146        }
     147
     148        fibril_mutex_unlock(&loc_cons_block_mutex);
     149
     150        clone_session(&loc_consumer_mutex, loc_cons_block_sess,
     151            &loc_consumer_sess);
     152
     153        return async_exchange_begin(loc_cons_block_sess);
     154}
     155
     156/** Start an async exchange on the loc session.
     157 *
     158 * @return New exchange.
     159 *
     160 */
     161async_exch_t *loc_exchange_begin(void)
     162{
     163        fibril_mutex_lock(&loc_consumer_mutex);
     164
     165        if (loc_consumer_sess == NULL)
     166                loc_consumer_sess =
     167                    service_connect(SERVICE_LOC,
     168                    INTERFACE_LOC_CONSUMER, 0, NULL);
     169
     170        fibril_mutex_unlock(&loc_consumer_mutex);
     171
     172        if (loc_consumer_sess == NULL)
    184173                return NULL;
    185         }
    186 }
    187 
    188 /** Start an async exchange on the loc session.
    189  *
    190  * @param iface Location service interface to choose
    191  *
    192  * @return New exchange.
    193  *
    194  */
    195 async_exch_t *loc_exchange_begin(iface_t iface)
    196 {
    197         switch (iface) {
    198         case INTERFACE_LOC_SUPPLIER:
    199                 fibril_mutex_lock(&loc_supplier_mutex);
    200 
    201                 if (loc_supplier_sess == NULL)
    202                         loc_supplier_sess =
    203                             service_connect(SERVICE_LOC,
    204                             INTERFACE_LOC_SUPPLIER, 0, NULL);
    205 
    206                 fibril_mutex_unlock(&loc_supplier_mutex);
    207 
    208                 if (loc_supplier_sess == NULL)
    209                         return NULL;
    210 
    211                 return async_exchange_begin(loc_supplier_sess);
    212         case INTERFACE_LOC_CONSUMER:
    213                 fibril_mutex_lock(&loc_consumer_mutex);
    214 
    215                 if (loc_consumer_sess == NULL)
    216                         loc_consumer_sess =
    217                             service_connect(SERVICE_LOC,
    218                             INTERFACE_LOC_CONSUMER, 0, NULL);
    219 
    220                 fibril_mutex_unlock(&loc_consumer_mutex);
    221 
    222                 if (loc_consumer_sess == NULL)
    223                         return NULL;
    224 
    225                 return async_exchange_begin(loc_consumer_sess);
    226         default:
    227                 return NULL;
    228         }
     174
     175        return async_exchange_begin(loc_consumer_sess);
    229176}
    230177
     
    240187
    241188/** Register new server with loc.
    242  *
    243  * XXX Proper impementation - currently cannot actually call
    244  * this function more than once.
    245189 *
    246190 * @param name Server name
     
    257201                return ENOMEM;
    258202
    259         exch = loc_exchange_begin_blocking(INTERFACE_LOC_SUPPLIER);
     203        srv->sess = service_connect_blocking(SERVICE_LOC,
     204            INTERFACE_LOC_SUPPLIER, 0, NULL);
     205        if (srv->sess == NULL) {
     206                free(srv);
     207                return ENOMEM;
     208        }
     209
     210        exch = async_exchange_begin(srv->sess);
    260211
    261212        ipc_call_t answer;
     
    265216        if (retval != EOK) {
    266217                async_forget(req);
    267                 loc_exchange_end(exch);
     218                async_exchange_end(exch);
     219                async_hangup(srv->sess);
    268220                free(srv);
    269221                return retval;
     
    278230         */
    279231        async_wait_for(req, &retval);
    280         loc_exchange_end(exch);
    281 
    282         if (retval != EOK) {
     232        async_exchange_end(exch);
     233
     234        if (retval != EOK) {
     235                async_hangup(srv->sess);
    283236                free(srv);
    284237                return retval;
     
    293246 * Unregister server and free server object.
    294247 *
    295  * XXX Proper implementation
    296  *
    297248 * @param srv Server object
    298249 */
    299250void loc_server_unregister(loc_srv_t *srv)
    300251{
     252        async_hangup(srv->sess);
    301253        free(srv);
    302254}
     
    312264    service_id_t *sid)
    313265{
    314         async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_SUPPLIER);
    315 
    316         (void)srv;
    317 
     266        async_exch_t *exch = async_exchange_begin(srv->sess);
    318267        ipc_call_t answer;
    319268        aid_t req = async_send_0(exch, LOC_SERVICE_REGISTER, &answer);
     
    322271        if (retval != EOK) {
    323272                async_forget(req);
    324                 loc_exchange_end(exch);
     273                async_exchange_end(exch);
    325274                return retval;
    326275        }
     
    332281         */
    333282        async_wait_for(req, &retval);
    334         loc_exchange_end(exch);
     283        async_exchange_end(exch);
    335284
    336285        if (retval != EOK) {
     
    357306        errno_t retval;
    358307
    359         (void)srv;
    360 
    361         exch = loc_exchange_begin_blocking(INTERFACE_LOC_SUPPLIER);
     308        exch = async_exchange_begin(srv->sess);
    362309        retval = async_req_1_0(exch, LOC_SERVICE_UNREGISTER, sid);
    363         loc_exchange_end(exch);
     310        async_exchange_end(exch);
    364311
    365312        return (errno_t)retval;
     
    372319
    373320        if (flags & IPC_FLAG_BLOCKING)
    374                 exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
     321                exch = loc_exchange_begin_blocking();
    375322        else {
    376                 exch = loc_exchange_begin(INTERFACE_LOC_CONSUMER);
     323                exch = loc_exchange_begin();
    377324                if (exch == NULL)
    378325                        return errno;
     
    425372
    426373        *name = NULL;
    427         exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
     374        exch = loc_exchange_begin_blocking();
    428375
    429376        ipc_call_t answer;
     
    505452
    506453        if (flags & IPC_FLAG_BLOCKING)
    507                 exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
     454                exch = loc_exchange_begin_blocking();
    508455        else {
    509                 exch = loc_exchange_begin(INTERFACE_LOC_CONSUMER);
     456                exch = loc_exchange_begin();
    510457                if (exch == NULL)
    511458                        return errno;
     
    554501
    555502        if (flags & IPC_FLAG_BLOCKING)
    556                 exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
     503                exch = loc_exchange_begin_blocking();
    557504        else {
    558                 exch = loc_exchange_begin(INTERFACE_LOC_CONSUMER);
     505                exch = loc_exchange_begin();
    559506                if (exch == NULL)
    560507                        return errno;
     
    590537loc_object_type_t loc_id_probe(service_id_t handle)
    591538{
    592         async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
     539        async_exch_t *exch = loc_exchange_begin_blocking();
    593540
    594541        sysarg_t type;
     
    621568int loc_null_create(void)
    622569{
    623         async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
     570        async_exch_t *exch = loc_exchange_begin_blocking();
    624571
    625572        sysarg_t null_id;
     
    636583void loc_null_destroy(int null_id)
    637584{
    638         async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
     585        async_exch_t *exch = loc_exchange_begin_blocking();
    639586        async_req_1_0(exch, LOC_NULL_DESTROY, (sysarg_t) null_id);
    640587        loc_exchange_end(exch);
     
    665612        errno_t retval;
    666613
    667         exch = loc_exchange_begin_blocking(INTERFACE_LOC_SUPPLIER);
     614        exch = async_exchange_begin(srv->sess);
    668615        retval = async_req_2_0(exch, LOC_SERVICE_ADD_TO_CAT, svc_id, cat_id);
    669         loc_exchange_end(exch);
     616        async_exchange_end(exch);
    670617
    671618        return retval;
     
    686633size_t loc_count_namespaces(void)
    687634{
    688         async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
     635        async_exch_t *exch = loc_exchange_begin_blocking();
    689636        size_t size = loc_count_namespaces_internal(exch);
    690637        loc_exchange_end(exch);
     
    695642size_t loc_count_services(service_id_t ns_handle)
    696643{
    697         async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
     644        async_exch_t *exch = loc_exchange_begin_blocking();
    698645        size_t size = loc_count_services_internal(exch, ns_handle);
    699646        loc_exchange_end(exch);
     
    706653        /* Loop until read is succesful */
    707654        while (true) {
    708                 async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
     655                async_exch_t *exch = loc_exchange_begin_blocking();
    709656                size_t count = loc_count_namespaces_internal(exch);
    710657                loc_exchange_end(exch);
     
    717664                        return 0;
    718665
    719                 exch = loc_exchange_begin(INTERFACE_LOC_CONSUMER);
     666                exch = loc_exchange_begin();
    720667
    721668                ipc_call_t answer;
     
    755702        /* Loop until read is succesful */
    756703        while (true) {
    757                 async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
     704                async_exch_t *exch = loc_exchange_begin_blocking();
    758705                size_t count = loc_count_services_internal(exch, ns_handle);
    759706                loc_exchange_end(exch);
     
    766713                        return 0;
    767714
    768                 exch = loc_exchange_begin(INTERFACE_LOC_CONSUMER);
     715                exch = loc_exchange_begin();
    769716
    770717                ipc_call_t answer;
     
    803750    sysarg_t *id_buf, size_t buf_size, size_t *act_size)
    804751{
    805         async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
     752        async_exch_t *exch = loc_exchange_begin_blocking();
    806753
    807754        ipc_call_t answer;
  • uspace/lib/c/include/loc.h

    r4c6fd56 r7d7f5e3  
    4343typedef void (*loc_cat_change_cb_t)(void *);
    4444
    45 extern async_exch_t *loc_exchange_begin_blocking(iface_t);
    46 extern async_exch_t *loc_exchange_begin(iface_t);
     45extern async_exch_t *loc_exchange_begin_blocking(void);
     46extern async_exch_t *loc_exchange_begin(void);
    4747extern void loc_exchange_end(async_exch_t *);
    4848
  • uspace/lib/c/include/types/loc.h

    r4c6fd56 r7d7f5e3  
    3636#define _LIBC_TYPES_LOC_H_
    3737
     38#include <async.h>
     39
    3840/** Server register with location service */
    3941typedef struct {
    40         int dummy;
     42        async_sess_t *sess;
    4143} loc_srv_t;
    4244
  • uspace/lib/c/meson.build

    r4c6fd56 r7d7f5e3  
    11#
     2# Copyright (c) 2023 Jiri Svoboda
    23# Copyright (c) 2005 Martin Decky
    34# Copyright (c) 2007 Jakub Jermar
     
    186187        'test/inttypes.c',
    187188        'test/io/table.c',
     189        'test/loc.c',
    188190        'test/main.c',
    189191        'test/mem.c',
  • uspace/lib/c/test/main.c

    r4c6fd56 r7d7f5e3  
    11/*
     2 * Copyright (c) 2023 Jiri Svoboda
    23 * Copyright (c) 2014 Vojtech Horky
    34 * All rights reserved.
     
    4243PCUT_IMPORT(imath);
    4344PCUT_IMPORT(inttypes);
     45PCUT_IMPORT(loc);
    4446PCUT_IMPORT(mem);
    4547PCUT_IMPORT(odict);
Note: See TracChangeset for help on using the changeset viewer.