Merge pull request #4580 from deepikabhavnani/wifi_34

Use EventFlags instead of Semaphores
pull/4776/head
Martin Kojtal 2017-09-04 16:54:55 +01:00 committed by GitHub
commit abafc6e7df
4 changed files with 37 additions and 35 deletions

View File

@ -18,8 +18,11 @@
#include "Timer.h" #include "Timer.h"
#include "mbed_assert.h" #include "mbed_assert.h"
#define READ_FLAG 0x1u
#define WRITE_FLAG 0x2u
TCPSocket::TCPSocket() TCPSocket::TCPSocket()
: _pending(0), _read_sem(0), _write_sem(0), : _pending(0), _event_flag(),
_read_in_progress(false), _write_in_progress(false) _read_in_progress(false), _write_in_progress(false)
{ {
} }
@ -60,16 +63,15 @@ nsapi_error_t TCPSocket::connect(const SocketAddress &address)
} else { } else {
blocking_connect_in_progress = true; blocking_connect_in_progress = true;
int32_t count; uint32_t flag;
// Release lock before blocking so other threads // Release lock before blocking so other threads
// accessing this object aren't blocked // accessing this object aren't blocked
_lock.unlock(); _lock.unlock();
count = _write_sem.wait(_timeout); flag = _event_flag.wait_any(WRITE_FLAG, _timeout);
_lock.lock(); _lock.lock();
if (flag & osFlagsError) {
if (count < 1) { // Timeout break
// Semaphore wait timed out so break out and return
break; break;
} }
} }
@ -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)) { if ((_timeout == 0) || (ret != NSAPI_ERROR_WOULD_BLOCK)) {
break; break;
} else { } else {
int32_t count; uint32_t flag;
// Release lock before blocking so other threads // Release lock before blocking so other threads
// accessing this object aren't blocked // accessing this object aren't blocked
_lock.unlock(); _lock.unlock();
count = _write_sem.wait(_timeout); flag = _event_flag.wait_any(WRITE_FLAG, _timeout);
_lock.lock(); _lock.lock();
if (count < 1) { if (flag & osFlagsError) {
// Semaphore wait timed out so break out and return // Timeout break
ret = NSAPI_ERROR_WOULD_BLOCK; ret = NSAPI_ERROR_WOULD_BLOCK;
break; break;
} }
@ -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)) { if ((_timeout == 0) || (ret != NSAPI_ERROR_WOULD_BLOCK)) {
break; break;
} else { } else {
int32_t count; uint32_t flag;
// Release lock before blocking so other threads // Release lock before blocking so other threads
// accessing this object aren't blocked // accessing this object aren't blocked
_lock.unlock(); _lock.unlock();
count = _read_sem.wait(_timeout); flag = _event_flag.wait_any(READ_FLAG, _timeout);
_lock.lock(); _lock.lock();
if (count < 1) { if (flag & osFlagsError) {
// Semaphore wait timed out so break out and return // Timeout break
ret = NSAPI_ERROR_WOULD_BLOCK; ret = NSAPI_ERROR_WOULD_BLOCK;
break; break;
} }
@ -188,8 +190,7 @@ nsapi_size_or_error_t TCPSocket::recv(void *data, nsapi_size_t size)
void TCPSocket::event() void TCPSocket::event()
{ {
_write_sem.release(); _event_flag.set(READ_FLAG|WRITE_FLAG);
_read_sem.release();
_pending += 1; _pending += 1;
if (_callback && _pending == 1) { if (_callback && _pending == 1) {

View File

@ -23,7 +23,7 @@
#include "netsocket/Socket.h" #include "netsocket/Socket.h"
#include "netsocket/NetworkStack.h" #include "netsocket/NetworkStack.h"
#include "netsocket/NetworkInterface.h" #include "netsocket/NetworkInterface.h"
#include "rtos/Semaphore.h" #include "rtos/EventFlags.h"
/** TCP socket connection /** TCP socket connection
@ -45,7 +45,7 @@ public:
*/ */
template <typename S> template <typename S>
TCPSocket(S *stack) TCPSocket(S *stack)
: _pending(0), _read_sem(0), _write_sem(0), : _pending(0), _event_flag(0),
_read_in_progress(false), _write_in_progress(false) _read_in_progress(false), _write_in_progress(false)
{ {
open(stack); open(stack);
@ -117,8 +117,7 @@ protected:
virtual void event(); virtual void event();
volatile unsigned _pending; volatile unsigned _pending;
rtos::Semaphore _read_sem; rtos::EventFlags _event_flag;
rtos::Semaphore _write_sem;
bool _read_in_progress; bool _read_in_progress;
bool _write_in_progress; bool _write_in_progress;
}; };

View File

@ -18,8 +18,12 @@
#include "Timer.h" #include "Timer.h"
#include "mbed_assert.h" #include "mbed_assert.h"
#define TCP_EVENT "UDP_Events"
#define READ_FLAG 0x1u
#define WRITE_FLAG 0x2u
UDPSocket::UDPSocket() UDPSocket::UDPSocket()
: _pending(0), _read_sem(0), _write_sem(0) : _pending(0), _event_flag()
{ {
} }
@ -64,16 +68,16 @@ nsapi_size_or_error_t UDPSocket::sendto(const SocketAddress &address, const void
ret = sent; ret = sent;
break; break;
} else { } else {
int32_t count; uint32_t flag;
// Release lock before blocking so other threads // Release lock before blocking so other threads
// accessing this object aren't blocked // accessing this object aren't blocked
_lock.unlock(); _lock.unlock();
count = _write_sem.wait(_timeout); flag = _event_flag.wait_any(WRITE_FLAG, _timeout);
_lock.lock(); _lock.lock();
if (count < 1) { if (flag & osFlagsError) {
// Semaphore wait timed out so break out and return // Timeout break
ret = NSAPI_ERROR_WOULD_BLOCK; ret = NSAPI_ERROR_WOULD_BLOCK;
break; break;
} }
@ -101,16 +105,16 @@ nsapi_size_or_error_t UDPSocket::recvfrom(SocketAddress *address, void *buffer,
ret = recv; ret = recv;
break; break;
} else { } else {
int32_t count; uint32_t flag;
// Release lock before blocking so other threads // Release lock before blocking so other threads
// accessing this object aren't blocked // accessing this object aren't blocked
_lock.unlock(); _lock.unlock();
count = _read_sem.wait(_timeout); flag = _event_flag.wait_any(READ_FLAG, _timeout);
_lock.lock(); _lock.lock();
if (count < 1) { if (flag & osFlagsError) {
// Semaphore wait timed out so break out and return // Timeout break
ret = NSAPI_ERROR_WOULD_BLOCK; ret = NSAPI_ERROR_WOULD_BLOCK;
break; break;
} }
@ -123,8 +127,7 @@ nsapi_size_or_error_t UDPSocket::recvfrom(SocketAddress *address, void *buffer,
void UDPSocket::event() void UDPSocket::event()
{ {
_write_sem.release(); _event_flag.set(READ_FLAG|WRITE_FLAG);
_read_sem.release();
_pending += 1; _pending += 1;
if (_callback && _pending == 1) { if (_callback && _pending == 1) {

View File

@ -23,7 +23,7 @@
#include "netsocket/Socket.h" #include "netsocket/Socket.h"
#include "netsocket/NetworkStack.h" #include "netsocket/NetworkStack.h"
#include "netsocket/NetworkInterface.h" #include "netsocket/NetworkInterface.h"
#include "rtos/Semaphore.h" #include "rtos/EventFlags.h"
/** UDP socket /** UDP socket
@ -45,7 +45,7 @@ public:
*/ */
template <typename S> template <typename S>
UDPSocket(S *stack) UDPSocket(S *stack)
: _pending(0), _read_sem(0), _write_sem(0) : _pending(0), _event_flag(0)
{ {
open(stack); open(stack);
} }
@ -117,8 +117,7 @@ protected:
virtual void event(); virtual void event();
volatile unsigned _pending; volatile unsigned _pending;
rtos::Semaphore _read_sem; rtos::EventFlags _event_flag;
rtos::Semaphore _write_sem;
}; };