• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University (SJTU)
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 #ifndef LIBIPC_IPC_LIB_H
13 #define LIBIPC_IPC_LIB_H
14 
15 #include <stdint.h>
16 #include <stdbool.h>
17 #include <stddef.h>
18 #include <time.h>
19 
20 /* declare that wait forever when receive msg */
21 #define OS_WAIT_FOREVER 0xFFFFFFFF
22 
23 /* declare that no wait when receiver msg */
24 #define OS_NO_WAIT 0
25 
26 #define NOTIFY_MAX_LEN 152
27 
28 #define CH_CNT_MAX         2
29 #define E_EX_TIMER_TIMEOUT 0xff
30 
31 #define TID_MASK   0xFFFFULL
32 #define PID_OFFSET 16U
33 #define PID_MASK   ((0x1 << PID_OFFSET) - 1)
34 
35 #define CID_OFFSET 12U
36 #if (CID_OFFSET >= PID_OFFSET)
37 #error "CID_OFFSET must be less than PID_OFFSET"
38 #endif
39 
40 #define CID_BIT_MASK ((1U << (PID_OFFSET - CID_OFFSET)) - 1)
41 #if (CID_BIT_MASK < CH_CNT_MAX)
42 #error "CID_BIT_MASK must be larger than CH_CNT_MAX"
43 #endif
44 
45 #define CID_MASK (CID_BIT_MASK << CID_OFFSET)
46 
47 #define pid_to_taskid(tid, pid) \
48     (((uint32_t)(tid) << PID_OFFSET) | ((uint32_t)(pid)&PID_MASK))
49 #define taskid_to_pid(task_id) ((task_id)&PID_MASK)
50 #define taskid_to_tid(task_id) (((task_id) >> PID_OFFSET) & TID_MASK)
51 #define cid_to_taskid(cid)     ((cid) & (~CID_MASK))
52 #define taskid_to_cid(task_id, ch_num) \
53     ((task_id) | ((uint32_t)(ch_num) << CID_OFFSET))
54 
55 typedef int cref_t;
56 
57 typedef uint32_t taskid_t;
58 
59 struct reg_items_st {
60     bool reg_pid;
61     bool reg_name;
62     bool reg_tamgr;
63 };
64 
65 struct src_msginfo {
66     uint32_t src_pid;
67     uint32_t src_tid;
68     uint16_t msg_type;
69 };
70 
71 enum message_msgtype {
72     MSG_TYPE_INVALID = 0,
73     MSG_TYPE_NOTIF = 1,
74     MSG_TYPE_CALL = 2,
75 };
76 
77 int32_t ipc_create_channel(const char *name, int32_t ch_cnt, cref_t *pp_ch[],
78                            struct reg_items_st reg_items);
79 
80 int32_t ipc_remove_channel(uint32_t task_id, const char *name, int32_t ch_num,
81                            cref_t ch);
82 
83 /* Get channel cap from PathMgr based on name. */
84 int32_t ipc_get_ch_from_path(const char *name, cref_t *p_ch);
85 
86 /* Get channel cap based on task_id. */
87 int32_t ipc_get_ch_from_taskid(uint32_t task_id, int32_t ch_num, cref_t *p_ch);
88 
89 /* Get task_id of the server based on the registered TAMgr information. */
90 uint32_t ipc_hunt_by_name(const char *name, uint32_t *task_id);
91 
92 /* Obtain the associated channel of the current thread tls. */
93 int32_t ipc_get_my_channel(int32_t ch_num, cref_t *p_ch);
94 
95 /* Use with ipc_get_ch_from_path to release resources on the client. */
96 uint32_t ipc_release_from_path(const char *name, cref_t rref);
97 
98 /* Use with ipc_get_ch_from_taskid to release resources on the client and clear
99  * the cache. */
100 uint32_t ipc_release_from_taskid(uint32_t task_id, int32_t ch_num);
101 
102 /* Use with ipc_hunt_by_name to clear the cache. */
103 uint32_t ipc_release_by_name(const char *name);
104 
105 int32_t ipc_msg_call(cref_t channel, void *send_buf, size_t send_len,
106                      void *reply_buf, size_t reply_len, int32_t timeout);
107 
108 int32_t ipc_msg_notification(cref_t ch, void *send_buf, size_t send_len);
109 
110 int32_t ipc_msg_reply(cref_t msg_hdl, void *reply_buf, size_t len);
111 
112 int32_t ipc_msg_receive(cref_t channel, void *recv_buf, size_t recv_len,
113                         cref_t msg_hdl, struct src_msginfo *info,
114                         int32_t timeout);
115 
116 cref_t ipc_msg_create_hdl(void);
117 
118 int32_t ipc_msg_delete_hdl(cref_t msg_hdl);
119 
120 int32_t ipc_save_my_msghdl(cref_t msg_hdl);
121 
122 cref_t ipc_get_my_msghdl(void);
123 
124 uint32_t get_self_taskid(void);
125 
126 bool check_ref_valid(cref_t cref);
127 
128 int tamgr_register(const char *name);
129 
130 #endif
131