Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame^] | 1 | #pragma once |
| 2 | #include <stdint.h> |
| 3 | #include <stdbool.h> |
| 4 | #include <pthread.h> |
| 5 | #include <osmocom/core/linuxlist.h> |
| 6 | |
| 7 | struct bank_slot { |
| 8 | uint16_t bank_id; |
| 9 | uint16_t slot_nr; |
| 10 | }; |
| 11 | |
| 12 | static inline bool bank_slot_equals(const struct bank_slot *a, const struct bank_slot *b) |
| 13 | { |
| 14 | if (a->bank_id == b->bank_id && a->slot_nr == b->slot_nr) |
| 15 | return true; |
| 16 | else |
| 17 | return false; |
| 18 | } |
| 19 | |
| 20 | struct client_slot { |
| 21 | uint16_t client_id; |
| 22 | uint16_t slot_nr; |
| 23 | }; |
| 24 | |
| 25 | static inline bool client_slot_equals(const struct client_slot *a, const struct client_slot *b) |
| 26 | { |
| 27 | if (a->client_id == b->client_id && a->slot_nr == b->slot_nr) |
| 28 | return true; |
| 29 | else |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | /* slot mappings are created / removed by the server */ |
| 34 | struct slot_mapping { |
| 35 | /* global lits of bankd slot mappings */ |
| 36 | struct llist_head list; |
| 37 | /* slot on bank side */ |
| 38 | struct bank_slot bank; |
| 39 | /* slot on client side */ |
| 40 | struct client_slot client; |
| 41 | }; |
| 42 | |
| 43 | /* collection of slot mappings */ |
| 44 | struct slotmaps { |
| 45 | struct llist_head mappings; |
| 46 | pthread_rwlock_t rwlock; |
| 47 | }; |
| 48 | |
| 49 | /* thread-safe lookup of map by client:slot */ |
| 50 | struct slot_mapping *slotmap_by_client(struct slotmaps *maps, const struct client_slot *client); |
| 51 | |
| 52 | /* thread-safe lookup of map by bank:slot */ |
| 53 | struct slot_mapping *slotmap_by_bank(struct slotmaps *maps, const struct bank_slot *bank); |
| 54 | |
| 55 | /* thread-safe creating of a new bank<->client map */ |
| 56 | int slotmap_add(struct slotmaps *maps, const struct bank_slot *bank, const struct client_slot *client); |
| 57 | |
| 58 | /* thread-safe removal of a bank<->client map */ |
| 59 | void slotmap_del(struct slotmaps *maps, struct slot_mapping *map); |
| 60 | |
| 61 | /* initialize the entire map collection */ |
| 62 | struct slotmaps *slotmap_init(void *ctx); |