summaryrefslogtreecommitdiffstats
path: root/stacks/lwip_stack/src/include/nsfw_msg_api.h
blob: b7e0d1cbba4dd65187638ae4270cbd965aa65327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
*
* Copyright (c) 2018 Huawei Technologies Co.,Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef MSG_API_H
#define MSG_API_H
#include "nsfw_msg.h"
#include "nsfw_mem_api.h"
#include "nstack_log.h"
#include "nsfw_rti.h"
#include "common_mem_api.h"
#include "nsfw_recycle_api.h"
#include "common_pal_bitwide_adjust.h"
#include "pid_common.h"

#ifdef __cplusplus
/* *INDENT-OFF* */
extern "C" {
/* *INDENT-ON* */
#endif

#define SET_MSG_ERR(m, error) ((m)->param.err = (error))
#define GET_MSG_ERR(m) ((m)->param.err)

/* for sync message from sbr we should signal sem */
#define SYNC_MSG_ACK(m) sys_sem_s_signal(&((m)->param.op_completed))

/* for async message from sbr we should free the message */
#define ASYNC_MSG_FREE(m) msg_free(m)

#define MSG_ENTRY(_ptr, _type, _member) container_of((void *)_ptr, _type, _member)

#ifndef NSTACK_STATIC_CHECK
/*****************************************************************************
*   Prototype    : msg_malloc
*   Description  : malloc msg
*   Input        : mring_handle mhandle
*   Output       : None
*   Return Value : static inline data_com_msg*
*   Calls        :
*   Called By    :
*****************************************************************************/
static inline data_com_msg *msg_malloc(mring_handle mhandle)
{
    if (!mhandle)
    {
        NSFW_LOGERR("mhandle is null");
        return NULL;
    }

    data_com_msg *m = NULL;
    if (nsfw_mem_ring_dequeue(mhandle, (void **) &m) != 1)
    {
        return NULL;
    }

    m->param.recycle_pid = get_sys_pid();
    res_alloc(&m->param.res_chk);
    return m;
}

/*****************************************************************************
*   Prototype    : msg_free
*   Description  : free msg
*   Input        : data_com_msg* m
*                  mring_handle mhandle
*   Output       : None
*   Return Value : static inline void
*   Calls        :
*   Called By    :
*****************************************************************************/
static inline void msg_free(data_com_msg * m)
{
    if (!m)
    {
        NSFW_LOGERR("m is NULL");
        return;
    }

    mring_handle mhandle = ADDR_SHTOL(m->param.msg_from);
    if (!mhandle)
    {
        return;
    }

    if (res_free(&m->param.res_chk))
    {
        NSFW_LOGERR("m refree!]m=%p", m);
        return;
    }

    m->param.recycle_pid = 0;

    if (nsfw_mem_ring_enqueue(mhandle, (void *) m) != 1)
    {
        NSFW_LOGERR("nsfw_mem_ring_enqueue failed,this can not happen");
    }
}

/*****************************************************************************
*   Prototype    : msg_post
*   Description  : post msg
*   Input        : data_com_msg* m
*                  mring_handle mhandle
*   Output       : None
*   Return Value : static inline int
*   Calls        :
*   Called By    :
*****************************************************************************/
static inline int msg_post(data_com_msg * m, mring_handle mhandle)
{
    int ret;
    if (!m || !mhandle)
    {
        NSFW_LOGERR("param is not ok]m=%p,mhandle=%p", m, mhandle);
        return -1;
    }

    while (1)
    {
        ret = nsfw_mem_ring_enqueue(mhandle, (void *) m);
        switch (ret)
        {
            case 1:
                if (MSG_SYN_POST == m->param.op_type)
                {
                    sys_arch_sem_s_wait(&m->param.op_completed, 0);
                }

                return 0;
            case 0:
                continue;
            default:
                nsfw_rti_stat_macro(NSFW_STAT_PRIMARY_ENQ_FAIL, m);
                return -1;
        }
    }
}

#define MSG_POST_FAILED 50
/*****************************************************************************
*   Prototype    : msg_post_with_lock_rel
*   Description  : post msg to tcpip thread in mgrcom thread
*   Input        : data_com_msg* m
*                  mring_handle mhandle
*   Output       : None
*   Return Value : static inline int
*   Calls        :
*   Called By    :
*****************************************************************************/
static inline int
msg_post_with_lock_rel(data_com_msg * m, mring_handle mhandle)
{
    int ret;
    int try_count = 0;
    if (!m || !mhandle)
    {
        NSFW_LOGERR("param is not ok]m=%p,mhandle=%p", m, mhandle);
        return -1;
    }

    while (1)
    {
        ret = nsfw_mem_ring_enqueue(mhandle, (void *) m);
        switch (ret)
        {
            case 1:
                if (MSG_SYN_POST == m->param.op_type)
                {
                    sys_arch_sem_s_wait(&m->param.op_completed, 0);
                }

                return 0;
            case 0:
                try_count++;
                if (try_count > MSG_POST_FAILED)
                {
                    try_count = 0;
                    nsfw_recycle_rechk_lock();
                }
                sys_sleep_ns(0, 1000000);
                continue;
            default:
                nsfw_rti_stat_macro(NSFW_STAT_PRIMARY_ENQ_FAIL, m);
                return -1;
        }
    }
}

/*****************************************************************************
*   Prototype    : msg_try_post
*   Description  : try post msg
*   Input        : data_com_msg* m
*                  mring_handle mhandle
*   Output       : None
*   Return Value : static inline int
*   Calls        :
*   Called By    :
*****************************************************************************/
static inline int msg_try_post(data_com_msg * m, mring_handle mhandle)
{
    if (!m || !mhandle)
    {
        NSFW_LOGERR("param is not ok]m=%p,mhandle=%p", m, mhandle);
        return -1;
    }

    int ret = nsfw_mem_ring_enqueue(mhandle, (void *) m);
    if (1 == ret)
    {
        if (MSG_SYN_POST == m->param.op_type)
        {
            sys_arch_sem_s_wait(&m->param.op_completed, 0);
        }

        return 0;
    }

    return -1;
}

/*****************************************************************************
*   Prototype    : msg_fetch
*   Description  : fetch msg
*   Input        : mring_handle mhandle
*                  data_com_msg** m
*                  u32 num
*   Output       : None
*   Return Value : static inline int
*   Calls        :
*   Called By    :
*****************************************************************************/
static inline int msg_fetch(mring_handle mhandle, data_com_msg ** m, u32 num)
{
    if (!m || !mhandle)
    {
        NSFW_LOGERR("param is not ok]m=%p,mhandle=%p", m, mhandle);
        return -1;
    }

    int ret;
    while (1)
    {
        ret = nsfw_mem_ring_dequeuev(mhandle, (void *) m, num);
        if (ret > 0)
        {
            break;
        }
    }

    return ret;
}

/*****************************************************************************
*   Prototype    : msg_try_fetch
*   Description  : try fetch msg
*   Input        : mring_handle mhandle
*                  data_com_msg** m
*                  u32 num
*   Output       : None
*   Return Value : static inline int
*   Calls        :
*   Called By    :
*****************************************************************************/
static inline int
msg_try_fetch(mring_handle mhandle, data_com_msg ** m, u32 num)
{
    if (!m || !mhandle)
    {
        NSFW_LOGERR("param is not ok]m=%p,mhandle=%p", m, mhandle);
        return -1;
    }

    return nsfw_mem_ring_dequeuev(mhandle, (void *) m, num);
}

#else
data_com_msg *msg_malloc(mring_handle mhandle);
void msg_free(data_com_msg * m);
int msg_post(data_com_msg * m, mring_handle mhandle);
int msg_try_post(data_com_msg * m, mring_handle mhandle);
int msg_fetch(mring_handle mhandle, data_com_msg ** m, u32 num);
int msg_try_fetch(mring_handle mhandle, data_com_msg ** m, u32 num);
int msg_post_with_lock_rel(data_com_msg * m, mring_handle mhandle);
#endif

#ifdef __cplusplus
/* *INDENT-OFF* */
}
/* *INDENT-ON* */
#endif

#endif