Skip to content

Commit beb5aac

Browse files
author
Deepika
committed
RTOS: Add EventFlags class
EventFlags class is a wrapper for Event Flag functionality introduced in RTOS2/RTX5.
1 parent b0ad73e commit beb5aac

File tree

4 files changed

+37
-35
lines changed

4 files changed

+37
-35
lines changed

features/netsocket/TCPSocket.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
#include "Timer.h"
1919
#include "mbed_assert.h"
2020

21+
#define READ_FLAG 0x1u
22+
#define WRITE_FLAG 0x2u
23+
2124
TCPSocket::TCPSocket()
22-
: _pending(0), _read_sem(0), _write_sem(0),
25+
: _pending(0), _event_flag(),
2326
_read_in_progress(false), _write_in_progress(false)
2427
{
2528
}
@@ -60,16 +63,15 @@ nsapi_error_t TCPSocket::connect(const SocketAddress &address)
6063
} else {
6164
blocking_connect_in_progress = true;
6265

63-
int32_t count;
66+
uint32_t flag;
6467

6568
// Release lock before blocking so other threads
6669
// accessing this object aren't blocked
6770
_lock.unlock();
68-
count = _write_sem.wait(_timeout);
71+
flag = _event_flag.wait_any(WRITE_FLAG, _timeout);
6972
_lock.lock();
70-
71-
if (count < 1) {
72-
// Semaphore wait timed out so break out and return
73+
if (flag & osFlagsError) {
74+
// Timeout break
7375
break;
7476
}
7577
}
@@ -122,16 +124,16 @@ nsapi_size_or_error_t TCPSocket::send(const void *data, nsapi_size_t size)
122124
if ((_timeout == 0) || (ret != NSAPI_ERROR_WOULD_BLOCK)) {
123125
break;
124126
} else {
125-
int32_t count;
127+
uint32_t flag;
126128

127129
// Release lock before blocking so other threads
128130
// accessing this object aren't blocked
129131
_lock.unlock();
130-
count = _write_sem.wait(_timeout);
132+
flag = _event_flag.wait_any(WRITE_FLAG, _timeout);
131133
_lock.lock();
132134

133-
if (count < 1) {
134-
// Semaphore wait timed out so break out and return
135+
if (flag & osFlagsError) {
136+
// Timeout break
135137
ret = NSAPI_ERROR_WOULD_BLOCK;
136138
break;
137139
}
@@ -165,16 +167,16 @@ nsapi_size_or_error_t TCPSocket::recv(void *data, nsapi_size_t size)
165167
if ((_timeout == 0) || (ret != NSAPI_ERROR_WOULD_BLOCK)) {
166168
break;
167169
} else {
168-
int32_t count;
170+
uint32_t flag;
169171

170172
// Release lock before blocking so other threads
171173
// accessing this object aren't blocked
172174
_lock.unlock();
173-
count = _read_sem.wait(_timeout);
175+
flag = _event_flag.wait_any(READ_FLAG, _timeout);
174176
_lock.lock();
175177

176-
if (count < 1) {
177-
// Semaphore wait timed out so break out and return
178+
if (flag & osFlagsError) {
179+
// Timeout break
178180
ret = NSAPI_ERROR_WOULD_BLOCK;
179181
break;
180182
}
@@ -188,8 +190,7 @@ nsapi_size_or_error_t TCPSocket::recv(void *data, nsapi_size_t size)
188190

189191
void TCPSocket::event()
190192
{
191-
_write_sem.release();
192-
_read_sem.release();
193+
_event_flag.set(READ_FLAG|WRITE_FLAG);
193194

194195
_pending += 1;
195196
if (_callback && _pending == 1) {

features/netsocket/TCPSocket.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "netsocket/Socket.h"
2424
#include "netsocket/NetworkStack.h"
2525
#include "netsocket/NetworkInterface.h"
26-
#include "rtos/Semaphore.h"
26+
#include "rtos/EventFlags.h"
2727

2828

2929
/** TCP socket connection
@@ -45,7 +45,7 @@ class TCPSocket : public Socket {
4545
*/
4646
template <typename S>
4747
TCPSocket(S *stack)
48-
: _pending(0), _read_sem(0), _write_sem(0),
48+
: _pending(0), _event_flag(0),
4949
_read_in_progress(false), _write_in_progress(false)
5050
{
5151
open(stack);
@@ -117,8 +117,7 @@ class TCPSocket : public Socket {
117117
virtual void event();
118118

119119
volatile unsigned _pending;
120-
rtos::Semaphore _read_sem;
121-
rtos::Semaphore _write_sem;
120+
rtos::EventFlags _event_flag;
122121
bool _read_in_progress;
123122
bool _write_in_progress;
124123
};

features/netsocket/UDPSocket.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
#include "Timer.h"
1919
#include "mbed_assert.h"
2020

21+
#define TCP_EVENT "UDP_Events"
22+
#define READ_FLAG 0x1u
23+
#define WRITE_FLAG 0x2u
24+
2125
UDPSocket::UDPSocket()
22-
: _pending(0), _read_sem(0), _write_sem(0)
26+
: _pending(0), _event_flag()
2327
{
2428
}
2529

@@ -64,16 +68,16 @@ nsapi_size_or_error_t UDPSocket::sendto(const SocketAddress &address, const void
6468
ret = sent;
6569
break;
6670
} else {
67-
int32_t count;
71+
uint32_t flag;
6872

6973
// Release lock before blocking so other threads
7074
// accessing this object aren't blocked
7175
_lock.unlock();
72-
count = _write_sem.wait(_timeout);
76+
flag = _event_flag.wait_any(WRITE_FLAG, _timeout);
7377
_lock.lock();
7478

75-
if (count < 1) {
76-
// Semaphore wait timed out so break out and return
79+
if (flag & osFlagsError) {
80+
// Timeout break
7781
ret = NSAPI_ERROR_WOULD_BLOCK;
7882
break;
7983
}
@@ -101,16 +105,16 @@ nsapi_size_or_error_t UDPSocket::recvfrom(SocketAddress *address, void *buffer,
101105
ret = recv;
102106
break;
103107
} else {
104-
int32_t count;
108+
uint32_t flag;
105109

106110
// Release lock before blocking so other threads
107111
// accessing this object aren't blocked
108112
_lock.unlock();
109-
count = _read_sem.wait(_timeout);
113+
flag = _event_flag.wait_any(READ_FLAG, _timeout);
110114
_lock.lock();
111115

112-
if (count < 1) {
113-
// Semaphore wait timed out so break out and return
116+
if (flag & osFlagsError) {
117+
// Timeout break
114118
ret = NSAPI_ERROR_WOULD_BLOCK;
115119
break;
116120
}
@@ -123,8 +127,7 @@ nsapi_size_or_error_t UDPSocket::recvfrom(SocketAddress *address, void *buffer,
123127

124128
void UDPSocket::event()
125129
{
126-
_write_sem.release();
127-
_read_sem.release();
130+
_event_flag.set(READ_FLAG|WRITE_FLAG);
128131

129132
_pending += 1;
130133
if (_callback && _pending == 1) {

features/netsocket/UDPSocket.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "netsocket/Socket.h"
2424
#include "netsocket/NetworkStack.h"
2525
#include "netsocket/NetworkInterface.h"
26-
#include "rtos/Semaphore.h"
26+
#include "rtos/EventFlags.h"
2727

2828

2929
/** UDP socket
@@ -45,7 +45,7 @@ class UDPSocket : public Socket {
4545
*/
4646
template <typename S>
4747
UDPSocket(S *stack)
48-
: _pending(0), _read_sem(0), _write_sem(0)
48+
: _pending(0), _event_flag(0)
4949
{
5050
open(stack);
5151
}
@@ -117,8 +117,7 @@ class UDPSocket : public Socket {
117117
virtual void event();
118118

119119
volatile unsigned _pending;
120-
rtos::Semaphore _read_sem;
121-
rtos::Semaphore _write_sem;
120+
rtos::EventFlags _event_flag;
122121
};
123122

124123

0 commit comments

Comments
 (0)