Skip to content

Commit

Permalink
Merge pull request #8 from selvamKrish/nopoll_yocto
Browse files Browse the repository at this point in the history
Fix issue #7 PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP is non-portable
  • Loading branch information
schmidtw authored Oct 12, 2017
2 parents 8033547 + bee1a23 commit c5d468d
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/nopoll_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
#if defined(NOPOLL_OS_UNIX)
# include <netinet/tcp.h>
#endif
static pthread_once_t mutex_once = PTHREAD_ONCE_INIT;
static pthread_mutex_t send_mutex;


/**
Expand Down Expand Up @@ -577,6 +579,17 @@ int nopoll_conn_tls_receive (noPollConn * conn, char * buffer, int buffer_size)

return res;
}
/**
* @internal function to init recursive mutex only once
*/
static void nopoll_init_recursive_mut_once(void)
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex_once, &attr);
pthread_mutexattr_destroy(&attr);
}

/**
* @internal Default connection send until handshake is complete.
Expand All @@ -586,9 +599,11 @@ int nopoll_conn_tls_send (noPollConn * conn, char * buffer, int buffer_size)
int res;
nopoll_bool needs_retry;
int ret = 0;
static pthread_mutex_t mut = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;

ret = pthread_mutex_lock (&mut);
ret = pthread_once(&mutex_once, nopoll_init_recursive_mut_once);
if ( ret != 0)
nopoll_log (conn->ctx, NOPOLL_LEVEL_CRITICAL,"pthread_once failed ( ret = %d) ( errno = %d)",ret, errno);

ret = pthread_mutex_lock (&send_mutex);
if(ret != 0)
{
nopoll_log (conn->ctx, NOPOLL_LEVEL_CRITICAL, "mutex failed to lock ( ret = %d) ( errno = %d)",ret, errno);
Expand All @@ -607,7 +622,7 @@ int nopoll_conn_tls_send (noPollConn * conn, char * buffer, int buffer_size)
WSASetLastError(NOPOLL_EWOULDBLOCK);
#endif
}
ret = pthread_mutex_unlock (&mut);
ret = pthread_mutex_unlock (&send_mutex);
if(ret != 0)
{
nopoll_log (conn->ctx, NOPOLL_LEVEL_CRITICAL, "mutex failed to unlock ( ret = %d) ( errno = %d)",ret, errno);
Expand Down

0 comments on commit c5d468d

Please sign in to comment.