forked from flapjack321/lib-lwip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailbox.c
173 lines (151 loc) · 5.08 KB
/
mailbox.c
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
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* Copyright (c) 2019, NEC Laboratories Europe GmbH, NEC Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <uk/mbox.h>
#include <uk/arch/time.h>
#include <lwip/sys.h>
#include <lwipopts.h>
#include <uk/essentials.h>
/* Creates an empty mailbox. */
err_t sys_mbox_new(sys_mbox_t *mbox, int size)
{
if (size <= 0) {
LWIP_DEBUGF(SOCKETS_DEBUG,
("Mailbox created with default size.\n"));
size = TCPIP_MBOX_SIZE;
}
UK_ASSERT(mbox);
mbox->a = uk_alloc_get_default();
UK_ASSERT(mbox->a);
mbox->mbox = uk_mbox_create(mbox->a, size);
if (!mbox->mbox)
return ERR_MEM;
mbox->valid = 1;
return ERR_OK;
}
int sys_mbox_valid(sys_mbox_t *mbox)
{
if (!mbox)
return 0;
return (mbox->valid == 1);
}
void sys_mbox_set_invalid(sys_mbox_t *mbox)
{
UK_ASSERT(mbox);
mbox->valid = 0;
}
/**
* Deallocates a mailbox. If there are messages still present in the
* mailbox when the mailbox is deallocated, it is an indication of a
* programming error in lwIP and the developer should be notified.
*/
void sys_mbox_free(sys_mbox_t *mbox)
{
UK_ASSERT(sys_mbox_valid(mbox));
uk_mbox_free(mbox->a, mbox->mbox);
sys_mbox_set_invalid(mbox);
}
/* Posts "msg" to the mailbox, NULL msg's are not supported */
void sys_mbox_post(sys_mbox_t *mbox, void *msg)
{
UK_ASSERT(sys_mbox_valid(mbox));
if (!msg) {
uk_pr_debug("Ignored posting NULL message");
return;
}
uk_mbox_post(mbox->mbox, msg);
}
/* Try to post "msg" to the mailbox. */
err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
{
UK_ASSERT(sys_mbox_valid(mbox));
if (uk_mbox_post_try(mbox->mbox, msg) < 0)
return ERR_MEM;
return ERR_OK;
}
/* Try to post the "msg" to the mailbox from ISR context. */
err_t sys_mbox_trypost_fromisr(sys_mbox_t *mbox, void *msg)
{
UK_ASSERT(sys_mbox_valid(mbox));
if (uk_mbox_post_try(mbox->mbox, msg) < 0)
return ERR_MEM;
return ERR_OK;
}
/**
* Blocks the thread until a message arrives in the mailbox, but does
* not block the thread longer than "timeout" milliseconds (similar to
* the sys_arch_sem_wait() function). The "msg" argument is a result
* parameter that is set by the function (i.e., by doing "*msg =
* ptr"). The "msg" parameter maybe NULL to indicate that the message
* should be dropped.
*
* The return values are the same as for the sys_arch_sem_wait() function:
* Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a
* timeout.
*/
u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
{
__nsec nsret;
UK_ASSERT(sys_mbox_valid(mbox));
if (timeout == 0) {
nsret = ukplat_monotonic_clock();
uk_mbox_recv(mbox->mbox, msg);
nsret = ukplat_monotonic_clock() - nsret;
} else {
nsret = uk_mbox_recv_to(mbox->mbox, msg,
ukarch_time_msec_to_nsec((__nsec)
timeout));
if (unlikely(nsret == __NSEC_MAX))
return SYS_ARCH_TIMEOUT;
}
return (u32_t) ukarch_time_nsec_to_msec(nsret);
}
/**
* This is similar to sys_arch_mbox_fetch, however if a message is not
* present in the mailbox, it immediately returns with the code
* SYS_MBOX_EMPTY. On success 0 is returned.
*
* To allow for efficient implementations, this can be defined as a
* function-like macro in sys_arch.h instead of a normal function. For
* example, a naive implementation could be:
* #define sys_arch_mbox_tryfetch(mbox,msg) \
* sys_arch_mbox_fetch(mbox,msg,1)
* although this would introduce unnecessary delays.
*/
u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
{
void *rmsg;
UK_ASSERT(sys_mbox_valid(mbox));
if (uk_mbox_recv_try(mbox->mbox, &rmsg) < 0)
return SYS_MBOX_EMPTY;
if (msg)
*msg = rmsg;
return 0;
}