Ignore:
File:
1 edited

Legend:

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

    r7d7f5e3 r2a09dcb  
    11/*
    2  * Copyright (c) 2023 Jiri Svoboda
    32 * Copyright (c) 2007 Josef Cejka
     3 * Copyright (c) 2011 Jiri Svoboda
    44 * All rights reserved.
    55 *
     
    3939#include <stdbool.h>
    4040
     41static FIBRIL_MUTEX_INITIALIZE(loc_supp_block_mutex);
    4142static FIBRIL_MUTEX_INITIALIZE(loc_cons_block_mutex);
     43
     44static FIBRIL_MUTEX_INITIALIZE(loc_supplier_mutex);
    4245static FIBRIL_MUTEX_INITIALIZE(loc_consumer_mutex);
    4346
     
    4750static void *cat_change_arg = NULL;
    4851
     52static async_sess_t *loc_supp_block_sess = NULL;
    4953static async_sess_t *loc_cons_block_sess = NULL;
     54
     55static async_sess_t *loc_supplier_sess = NULL;
    5056static async_sess_t *loc_consumer_sess = NULL;
    5157
     
    102108        if (!loc_callback_created) {
    103109                async_exch_t *exch =
    104                     loc_exchange_begin_blocking();
     110                    loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
    105111
    106112                ipc_call_t answer;
     
    129135/** Start an async exchange on the loc session (blocking).
    130136 *
     137 * @param iface Location service interface to choose
     138 *
    131139 * @return New exchange.
    132140 *
    133141 */
    134 async_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,
     142async_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:
     184                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 */
     195async_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,
    145218                            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  */
    161 async_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)
     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:
    173227                return NULL;
    174 
    175         return async_exchange_begin(loc_consumer_sess);
     228        }
    176229}
    177230
     
    186239}
    187240
    188 /** Register new server with loc.
    189  *
    190  * @param name Server name
    191  * @param rsrv Place to store new server object on success
    192  * @return EOK on succes or an error code
    193  */
    194 errno_t loc_server_register(const char *name, loc_srv_t **rsrv)
    195 {
    196         async_exch_t *exch;
    197         loc_srv_t *srv;
    198 
    199         srv = calloc(1, sizeof(loc_srv_t));
    200         if (srv == NULL)
    201                 return ENOMEM;
    202 
    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);
     241/** Register new server with loc. */
     242errno_t loc_server_register(const char *name)
     243{
     244        async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_SUPPLIER);
    211245
    212246        ipc_call_t answer;
     
    216250        if (retval != EOK) {
    217251                async_forget(req);
    218                 async_exchange_end(exch);
    219                 async_hangup(srv->sess);
    220                 free(srv);
     252                loc_exchange_end(exch);
    221253                return retval;
    222254        }
     
    230262         */
    231263        async_wait_for(req, &retval);
    232         async_exchange_end(exch);
    233 
    234         if (retval != EOK) {
    235                 async_hangup(srv->sess);
    236                 free(srv);
    237                 return retval;
    238         }
    239 
    240         *rsrv = srv;
     264        loc_exchange_end(exch);
     265
    241266        return retval;
    242267}
    243268
    244 /** Unregister server from loc.
    245  *
    246  * Unregister server and free server object.
    247  *
    248  * @param srv Server object
    249  */
    250 void loc_server_unregister(loc_srv_t *srv)
    251 {
    252         async_hangup(srv->sess);
    253         free(srv);
    254 }
    255 
    256269/** Register new service.
    257270 *
    258  * @param srv Server object
    259  * @param fqsn Fully qualified service name
    260  * @param sid  Service ID of new service
    261  *
    262  */
    263 errno_t loc_service_register(loc_srv_t *srv, const char *fqsn,
    264     service_id_t *sid)
    265 {
    266         async_exch_t *exch = async_exchange_begin(srv->sess);
     271 * @param      fqsn  Fully qualified service name
     272 * @param[out] sid   Service ID of new service
     273 *
     274 */
     275errno_t loc_service_register(const char *fqsn, service_id_t *sid)
     276{
     277        async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_SUPPLIER);
     278
    267279        ipc_call_t answer;
    268280        aid_t req = async_send_0(exch, LOC_SERVICE_REGISTER, &answer);
     
    271283        if (retval != EOK) {
    272284                async_forget(req);
    273                 async_exchange_end(exch);
     285                loc_exchange_end(exch);
    274286                return retval;
    275287        }
     
    281293         */
    282294        async_wait_for(req, &retval);
    283         async_exchange_end(exch);
     295        loc_exchange_end(exch);
    284296
    285297        if (retval != EOK) {
     
    298310/** Unregister service.
    299311 *
    300  * @param srv Server object
    301  * @param sid Service ID
    302  */
    303 errno_t loc_service_unregister(loc_srv_t *srv, service_id_t sid)
     312 * @param sid   Service ID
     313 */
     314errno_t loc_service_unregister(service_id_t sid)
    304315{
    305316        async_exch_t *exch;
    306317        errno_t retval;
    307318
    308         exch = async_exchange_begin(srv->sess);
     319        exch = loc_exchange_begin_blocking(INTERFACE_LOC_SUPPLIER);
    309320        retval = async_req_1_0(exch, LOC_SERVICE_UNREGISTER, sid);
    310         async_exchange_end(exch);
     321        loc_exchange_end(exch);
    311322
    312323        return (errno_t)retval;
     
    319330
    320331        if (flags & IPC_FLAG_BLOCKING)
    321                 exch = loc_exchange_begin_blocking();
     332                exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
    322333        else {
    323                 exch = loc_exchange_begin();
     334                exch = loc_exchange_begin(INTERFACE_LOC_CONSUMER);
    324335                if (exch == NULL)
    325336                        return errno;
     
    372383
    373384        *name = NULL;
    374         exch = loc_exchange_begin_blocking();
     385        exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
    375386
    376387        ipc_call_t answer;
     
    452463
    453464        if (flags & IPC_FLAG_BLOCKING)
    454                 exch = loc_exchange_begin_blocking();
     465                exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
    455466        else {
    456                 exch = loc_exchange_begin();
     467                exch = loc_exchange_begin(INTERFACE_LOC_CONSUMER);
    457468                if (exch == NULL)
    458469                        return errno;
     
    501512
    502513        if (flags & IPC_FLAG_BLOCKING)
    503                 exch = loc_exchange_begin_blocking();
     514                exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
    504515        else {
    505                 exch = loc_exchange_begin();
     516                exch = loc_exchange_begin(INTERFACE_LOC_CONSUMER);
    506517                if (exch == NULL)
    507518                        return errno;
     
    537548loc_object_type_t loc_id_probe(service_id_t handle)
    538549{
    539         async_exch_t *exch = loc_exchange_begin_blocking();
     550        async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
    540551
    541552        sysarg_t type;
     
    568579int loc_null_create(void)
    569580{
    570         async_exch_t *exch = loc_exchange_begin_blocking();
     581        async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
    571582
    572583        sysarg_t null_id;
     
    583594void loc_null_destroy(int null_id)
    584595{
    585         async_exch_t *exch = loc_exchange_begin_blocking();
     596        async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
    586597        async_req_1_0(exch, LOC_NULL_DESTROY, (sysarg_t) null_id);
    587598        loc_exchange_end(exch);
     
    600611/** Add service to category.
    601612 *
    602  * @param srv    Server object
    603  * @param svc_id Service ID
    604  * @param cat_id Category ID
    605  *
    606  * @return EOK on success or an error code
    607  */
    608 errno_t loc_service_add_to_cat(loc_srv_t *srv, service_id_t svc_id,
    609     service_id_t cat_id)
     613 * @param svc_id        Service ID
     614 * @param cat_id        Category ID
     615 * @return              EOK on success or an error code
     616 */
     617errno_t loc_service_add_to_cat(service_id_t svc_id, service_id_t cat_id)
    610618{
    611619        async_exch_t *exch;
    612620        errno_t retval;
    613621
    614         exch = async_exchange_begin(srv->sess);
     622        exch = loc_exchange_begin_blocking(INTERFACE_LOC_SUPPLIER);
    615623        retval = async_req_2_0(exch, LOC_SERVICE_ADD_TO_CAT, svc_id, cat_id);
    616         async_exchange_end(exch);
     624        loc_exchange_end(exch);
    617625
    618626        return retval;
     
    633641size_t loc_count_namespaces(void)
    634642{
    635         async_exch_t *exch = loc_exchange_begin_blocking();
     643        async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
    636644        size_t size = loc_count_namespaces_internal(exch);
    637645        loc_exchange_end(exch);
     
    642650size_t loc_count_services(service_id_t ns_handle)
    643651{
    644         async_exch_t *exch = loc_exchange_begin_blocking();
     652        async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
    645653        size_t size = loc_count_services_internal(exch, ns_handle);
    646654        loc_exchange_end(exch);
     
    653661        /* Loop until read is succesful */
    654662        while (true) {
    655                 async_exch_t *exch = loc_exchange_begin_blocking();
     663                async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
    656664                size_t count = loc_count_namespaces_internal(exch);
    657665                loc_exchange_end(exch);
     
    664672                        return 0;
    665673
    666                 exch = loc_exchange_begin();
     674                exch = loc_exchange_begin(INTERFACE_LOC_CONSUMER);
    667675
    668676                ipc_call_t answer;
     
    702710        /* Loop until read is succesful */
    703711        while (true) {
    704                 async_exch_t *exch = loc_exchange_begin_blocking();
     712                async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
    705713                size_t count = loc_count_services_internal(exch, ns_handle);
    706714                loc_exchange_end(exch);
     
    713721                        return 0;
    714722
    715                 exch = loc_exchange_begin();
     723                exch = loc_exchange_begin(INTERFACE_LOC_CONSUMER);
    716724
    717725                ipc_call_t answer;
     
    750758    sysarg_t *id_buf, size_t buf_size, size_t *act_size)
    751759{
    752         async_exch_t *exch = loc_exchange_begin_blocking();
     760        async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
    753761
    754762        ipc_call_t answer;
Note: See TracChangeset for help on using the changeset viewer.