|
16 | 16 |
|
17 | 17 | #include "UDPSocket.h" |
18 | 18 | #include "Timer.h" |
| 19 | +#include "mbed_assert.h" |
19 | 20 |
|
20 | 21 | UDPSocket::UDPSocket() |
21 | | - : _pending(0), _read_sem(0), _write_sem(0) |
| 22 | + : _pending(0), _read_sem(0), _write_sem(0), |
| 23 | + _read_in_progress(false), _write_in_progress(false) |
22 | 24 | { |
23 | 25 | } |
24 | 26 |
|
25 | 27 | UDPSocket::UDPSocket(NetworkStack *iface) |
26 | | - : _pending(0), _read_sem(0), _write_sem(0) |
| 28 | + : _pending(0), _read_sem(0), _write_sem(0), |
| 29 | + _read_in_progress(false), _write_in_progress(false) |
27 | 30 | { |
28 | 31 | open(iface); |
29 | 32 | } |
30 | 33 |
|
31 | 34 | UDPSocket::UDPSocket(NetworkInterface *iface) |
32 | | - : _pending(0), _read_sem(0), _write_sem(0) |
| 35 | + : _pending(0), _read_sem(0), _write_sem(0), |
| 36 | + _read_in_progress(false), _write_in_progress(false) |
33 | 37 | { |
34 | 38 | open(iface->get_stack()); |
35 | 39 | } |
@@ -64,10 +68,12 @@ int UDPSocket::sendto(const char *host, uint16_t port, const void *data, unsigne |
64 | 68 |
|
65 | 69 | int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned size) |
66 | 70 | { |
67 | | - if (osOK != _write_lock.lock(_timeout)) { |
68 | | - return NSAPI_ERROR_WOULD_BLOCK; |
69 | | - } |
70 | 71 | _lock.lock(); |
| 72 | + // If this assert is hit then there are two threads |
| 73 | + // performing a send at the same time which is undefined |
| 74 | + // behavior |
| 75 | + MBED_ASSERT(!_write_in_progress); |
| 76 | + _write_in_progress = true; |
71 | 77 |
|
72 | 78 | int ret; |
73 | 79 | while (true) { |
@@ -98,17 +104,19 @@ int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned s |
98 | 104 | } |
99 | 105 | } |
100 | 106 |
|
| 107 | + _write_in_progress = false; |
101 | 108 | _lock.unlock(); |
102 | | - _write_lock.unlock(); |
103 | 109 | return ret; |
104 | 110 | } |
105 | 111 |
|
106 | 112 | int UDPSocket::recvfrom(SocketAddress *address, void *buffer, unsigned size) |
107 | 113 | { |
108 | | - if (osOK != _read_lock.lock(_timeout)) { |
109 | | - return NSAPI_ERROR_WOULD_BLOCK; |
110 | | - } |
111 | 114 | _lock.lock(); |
| 115 | + // If this assert is hit then there are two threads |
| 116 | + // performing a recv at the same time which is undefined |
| 117 | + // behavior |
| 118 | + MBED_ASSERT(!_read_in_progress); |
| 119 | + _read_in_progress = true; |
112 | 120 |
|
113 | 121 | int ret; |
114 | 122 | while (true) { |
@@ -139,8 +147,8 @@ int UDPSocket::recvfrom(SocketAddress *address, void *buffer, unsigned size) |
139 | 147 | } |
140 | 148 | } |
141 | 149 |
|
| 150 | + _read_in_progress = false; |
142 | 151 | _lock.unlock(); |
143 | | - _read_lock.unlock(); |
144 | 152 | return ret; |
145 | 153 | } |
146 | 154 |
|
|
0 commit comments