Skip to content

Use EventFlags instead of Semaphores #4580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions features/netsocket/TCPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
#include "Timer.h"
#include "mbed_assert.h"

#define READ_FLAG 0x1u
#define WRITE_FLAG 0x2u

TCPSocket::TCPSocket()
: _pending(0), _read_sem(0), _write_sem(0),
: _pending(0), _event_flag(),
_read_in_progress(false), _write_in_progress(false)
{
}
Expand Down Expand Up @@ -60,16 +63,15 @@ nsapi_error_t TCPSocket::connect(const SocketAddress &address)
} else {
blocking_connect_in_progress = true;

int32_t count;
uint32_t flag;

// Release lock before blocking so other threads
// accessing this object aren't blocked
_lock.unlock();
count = _write_sem.wait(_timeout);
flag = _event_flag.wait_any(WRITE_FLAG, _timeout);
_lock.lock();

if (count < 1) {
// Semaphore wait timed out so break out and return
if (flag & osFlagsError) {
// Timeout break
break;
}
}
Expand Down Expand Up @@ -122,16 +124,16 @@ nsapi_size_or_error_t TCPSocket::send(const void *data, nsapi_size_t size)
if ((_timeout == 0) || (ret != NSAPI_ERROR_WOULD_BLOCK)) {
break;
} else {
int32_t count;
uint32_t flag;

// Release lock before blocking so other threads
// accessing this object aren't blocked
_lock.unlock();
count = _write_sem.wait(_timeout);
flag = _event_flag.wait_any(WRITE_FLAG, _timeout);
_lock.lock();

if (count < 1) {
// Semaphore wait timed out so break out and return
if (flag & osFlagsError) {
// Timeout break
ret = NSAPI_ERROR_WOULD_BLOCK;
break;
}
Expand Down Expand Up @@ -165,16 +167,16 @@ nsapi_size_or_error_t TCPSocket::recv(void *data, nsapi_size_t size)
if ((_timeout == 0) || (ret != NSAPI_ERROR_WOULD_BLOCK)) {
break;
} else {
int32_t count;
uint32_t flag;

// Release lock before blocking so other threads
// accessing this object aren't blocked
_lock.unlock();
count = _read_sem.wait(_timeout);
flag = _event_flag.wait_any(READ_FLAG, _timeout);
_lock.lock();

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

void TCPSocket::event()
{
_write_sem.release();
_read_sem.release();
_event_flag.set(READ_FLAG|WRITE_FLAG);

_pending += 1;
if (_callback && _pending == 1) {
Expand Down
7 changes: 3 additions & 4 deletions features/netsocket/TCPSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "netsocket/Socket.h"
#include "netsocket/NetworkStack.h"
#include "netsocket/NetworkInterface.h"
#include "rtos/Semaphore.h"
#include "rtos/EventFlags.h"


/** TCP socket connection
Expand All @@ -45,7 +45,7 @@ class TCPSocket : public Socket {
*/
template <typename S>
TCPSocket(S *stack)
: _pending(0), _read_sem(0), _write_sem(0),
: _pending(0), _event_flag(0),
_read_in_progress(false), _write_in_progress(false)
{
open(stack);
Expand Down Expand Up @@ -117,8 +117,7 @@ class TCPSocket : public Socket {
virtual void event();

volatile unsigned _pending;
rtos::Semaphore _read_sem;
rtos::Semaphore _write_sem;
rtos::EventFlags _event_flag;
bool _read_in_progress;
bool _write_in_progress;
};
Expand Down
25 changes: 14 additions & 11 deletions features/netsocket/UDPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
#include "Timer.h"
#include "mbed_assert.h"

#define TCP_EVENT "UDP_Events"
#define READ_FLAG 0x1u
#define WRITE_FLAG 0x2u

UDPSocket::UDPSocket()
: _pending(0), _read_sem(0), _write_sem(0)
: _pending(0), _event_flag()
{
}

Expand Down Expand Up @@ -64,16 +68,16 @@ nsapi_size_or_error_t UDPSocket::sendto(const SocketAddress &address, const void
ret = sent;
break;
} else {
int32_t count;
uint32_t flag;

// Release lock before blocking so other threads
// accessing this object aren't blocked
_lock.unlock();
count = _write_sem.wait(_timeout);
flag = _event_flag.wait_any(WRITE_FLAG, _timeout);
_lock.lock();

if (count < 1) {
// Semaphore wait timed out so break out and return
if (flag & osFlagsError) {
// Timeout break
ret = NSAPI_ERROR_WOULD_BLOCK;
break;
}
Expand Down Expand Up @@ -101,16 +105,16 @@ nsapi_size_or_error_t UDPSocket::recvfrom(SocketAddress *address, void *buffer,
ret = recv;
break;
} else {
int32_t count;
uint32_t flag;

// Release lock before blocking so other threads
// accessing this object aren't blocked
_lock.unlock();
count = _read_sem.wait(_timeout);
flag = _event_flag.wait_any(READ_FLAG, _timeout);
_lock.lock();

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

void UDPSocket::event()
{
_write_sem.release();
_read_sem.release();
_event_flag.set(READ_FLAG|WRITE_FLAG);

_pending += 1;
if (_callback && _pending == 1) {
Expand Down
7 changes: 3 additions & 4 deletions features/netsocket/UDPSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "netsocket/Socket.h"
#include "netsocket/NetworkStack.h"
#include "netsocket/NetworkInterface.h"
#include "rtos/Semaphore.h"
#include "rtos/EventFlags.h"


/** UDP socket
Expand All @@ -45,7 +45,7 @@ class UDPSocket : public Socket {
*/
template <typename S>
UDPSocket(S *stack)
: _pending(0), _read_sem(0), _write_sem(0)
: _pending(0), _event_flag(0)
{
open(stack);
}
Expand Down Expand Up @@ -117,8 +117,7 @@ class UDPSocket : public Socket {
virtual void event();

volatile unsigned _pending;
rtos::Semaphore _read_sem;
rtos::Semaphore _write_sem;
rtos::EventFlags _event_flag;
};


Expand Down