Verified Commit 9254e295 authored by Vincent Coubard's avatar Vincent Coubard
Browse files

Socket: Use atomic operations instead of mutex



Signed-off-by: Vincent Coubard's avatarVincent Coubard <vincent.coubard@arm.com>
parent c6e5161f
...@@ -41,6 +41,8 @@ ...@@ -41,6 +41,8 @@
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include "bootstrap/mbed_atomic.h"
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
#define SS_STATUS_CONNECTED ( 1 ) #define SS_STATUS_CONNECTED ( 1 )
...@@ -85,7 +87,6 @@ typedef struct _ss_ctx_t ...@@ -85,7 +87,6 @@ typedef struct _ss_ctx_t
char ** ppcAlpnProtocols; char ** ppcAlpnProtocols;
uint32_t ulAlpnProtocolsCount; uint32_t ulAlpnProtocolsCount;
uint32_t ulRefcount; uint32_t ulRefcount;
osMutexId_t RefcountLock;
} ss_ctx_t; } ss_ctx_t;
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
...@@ -151,11 +152,6 @@ static void prvSocketsClose( ss_ctx_t * ctx ) ...@@ -151,11 +152,6 @@ static void prvSocketsClose( ss_ctx_t * ctx )
{ {
vPortFree( ctx->destination ); vPortFree( ctx->destination );
} }
if ( ctx->RefcountLock )
{
osMutexDelete( ctx->RefcountLock );
}
vPortFree( ctx ); vPortFree( ctx );
} }
...@@ -166,15 +162,9 @@ static void prvSocketsClose( ss_ctx_t * ctx ) ...@@ -166,15 +162,9 @@ static void prvSocketsClose( ss_ctx_t * ctx )
*/ */
static void prvDecrementRefCount( ss_ctx_t * ctx ) static void prvDecrementRefCount( ss_ctx_t * ctx )
{ {
osMutexAcquire( ctx->RefcountLock, osWaitForever ); if ( core_util_atomic_decr_u32( &ctx->ulRefcount, 1 ) ) {
ctx->ulRefcount -= 1;
if( ctx->ulRefcount == 0 )
{
prvSocketsClose( ctx ); prvSocketsClose( ctx );
// do not release lock as it's been removed
return;
} }
osMutexRelease( ctx->RefcountLock );
} }
/* /*
...@@ -182,9 +172,7 @@ static void prvDecrementRefCount( ss_ctx_t * ctx ) ...@@ -182,9 +172,7 @@ static void prvDecrementRefCount( ss_ctx_t * ctx )
*/ */
static void prvIncrementRefCount( ss_ctx_t * ctx ) static void prvIncrementRefCount( ss_ctx_t * ctx )
{ {
osMutexAcquire( ctx->RefcountLock, osWaitForever ); core_util_atomic_incr_u32( &ctx->ulRefcount, 1 );
ctx->ulRefcount += 1;
osMutexRelease( ctx->RefcountLock );
} }
/* /*
...@@ -364,7 +352,6 @@ Socket_t SOCKETS_Socket( int32_t lDomain, ...@@ -364,7 +352,6 @@ Socket_t SOCKETS_Socket( int32_t lDomain,
if( ctx ) if( ctx )
{ {
ctx->RefcountLock = osMutexNew( NULL );
memset( ctx, 0, sizeof( *ctx ) ); memset( ctx, 0, sizeof( *ctx ) );
ctx->ip_socket = iotSocketCreate( IOT_SOCKET_AF_INET, IOT_SOCKET_SOCK_STREAM, IOT_SOCKET_IPPROTO_TCP ); ctx->ip_socket = iotSocketCreate( IOT_SOCKET_AF_INET, IOT_SOCKET_SOCK_STREAM, IOT_SOCKET_IPPROTO_TCP );
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment