Skip to content

Commit

Permalink
Merge pull request wolfSSL#6697 from julek-wolfssl/refactor-cond-again
Browse files Browse the repository at this point in the history
Refactor conditional code again
  • Loading branch information
JacobBarthelmeh authored Aug 10, 2023
2 parents 26fcdbf + c3fea8c commit 65401cf
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 116 deletions.
41 changes: 18 additions & 23 deletions examples/benchmark/tls_bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ typedef struct {
int read_bytes;
int read_idx;

wolfSSL_Mutex mutex;
COND_TYPE cond;

int done;
Expand Down Expand Up @@ -398,12 +397,12 @@ static double gettime_secs(int reset)
/* server send callback */
static int ServerMemSend(info_t* info, char* buf, int sz)
{
THREAD_CHECK_RET(wc_LockMutex(&info->to_client.mutex));
THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_client.cond));

#ifndef BENCH_USE_NONBLOCK
/* check for overflow */
if (info->to_client.write_idx + sz > MEM_BUFFER_SZ) {
THREAD_CHECK_RET(wc_UnLockMutex(&info->to_client.mutex));
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_client.cond));
fprintf(stderr, "ServerMemSend overflow\n");
return -1;
}
Expand All @@ -416,9 +415,9 @@ static int ServerMemSend(info_t* info, char* buf, int sz)
XMEMCPY(&info->to_client.buf[info->to_client.write_idx], buf, sz);
info->to_client.write_idx += sz;
info->to_client.write_bytes += sz;
THREAD_CHECK_RET(wc_UnLockMutex(&info->to_client.mutex));

THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_client.cond));
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_client.cond));

#ifdef BENCH_USE_NONBLOCK
if (sz == 0) {
Expand All @@ -431,14 +430,12 @@ static int ServerMemSend(info_t* info, char* buf, int sz)
/* server recv callback */
static int ServerMemRecv(info_t* info, char* buf, int sz)
{
THREAD_CHECK_RET(wc_LockMutex(&info->to_server.mutex));
THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_server.cond));

#ifndef BENCH_USE_NONBLOCK
while (info->to_server.write_idx - info->to_server.read_idx < sz &&
!info->to_client.done) {
THREAD_CHECK_RET(wc_UnLockMutex(&info->to_server.mutex));
THREAD_CHECK_RET(wolfSSL_CondWait(&info->to_server.cond));
THREAD_CHECK_RET(wc_LockMutex(&info->to_server.mutex));
}
#else
if (info->to_server.write_idx - info->to_server.read_idx < sz) {
Expand All @@ -456,7 +453,7 @@ static int ServerMemRecv(info_t* info, char* buf, int sz)
info->to_server.write_bytes = info->to_server.write_idx = 0;
}

THREAD_CHECK_RET(wc_UnLockMutex(&info->to_server.mutex));
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_server.cond));

if (info->to_client.done != 0) {
return -1;
Expand All @@ -473,14 +470,14 @@ static int ServerMemRecv(info_t* info, char* buf, int sz)
/* client send callback */
static int ClientMemSend(info_t* info, char* buf, int sz)
{
THREAD_CHECK_RET(wc_LockMutex(&info->to_server.mutex));
THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_server.cond));

#ifndef BENCH_USE_NONBLOCK
/* check for overflow */
if (info->to_server.write_idx + sz > MEM_BUFFER_SZ) {
fprintf(stderr, "ClientMemSend overflow %d %d %d\n",
info->to_server.write_idx, sz, MEM_BUFFER_SZ);
THREAD_CHECK_RET(wc_UnLockMutex(&info->to_server.mutex));
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_server.cond));
return -1;
}
#else
Expand All @@ -493,8 +490,8 @@ static int ClientMemSend(info_t* info, char* buf, int sz)
info->to_server.write_idx += sz;
info->to_server.write_bytes += sz;

THREAD_CHECK_RET(wc_UnLockMutex(&info->to_server.mutex));
THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_server.cond));
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_server.cond));

#ifdef BENCH_USE_NONBLOCK
if (sz == 0) {
Expand All @@ -507,14 +504,12 @@ static int ClientMemSend(info_t* info, char* buf, int sz)
/* client recv callback */
static int ClientMemRecv(info_t* info, char* buf, int sz)
{
THREAD_CHECK_RET(wc_LockMutex(&info->to_client.mutex));
THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_client.cond));

#ifndef BENCH_USE_NONBLOCK
while (info->to_client.write_idx - info->to_client.read_idx < sz &&
!info->to_server.done) {
THREAD_CHECK_RET(wc_UnLockMutex(&info->to_client.mutex));
THREAD_CHECK_RET(wolfSSL_CondWait(&info->to_client.cond));
THREAD_CHECK_RET(wc_LockMutex(&info->to_client.mutex));
}
#else
if (info->to_client.write_idx - info->to_client.read_idx < sz) {
Expand All @@ -532,7 +527,7 @@ static int ClientMemRecv(info_t* info, char* buf, int sz)
info->to_client.write_bytes = info->to_client.write_idx = 0;
}

THREAD_CHECK_RET(wc_UnLockMutex(&info->to_client.mutex));
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_client.cond));

if (info->to_server.done != 0) {
return -1;
Expand Down Expand Up @@ -1054,15 +1049,13 @@ static int bench_tls_client(info_t* info)
#if !defined(SINGLE_THREADED) && defined(WOLFSSL_DTLS)
/* synchronize with server */
if (info->doDTLS && !info->clientOrserverOnly) {
THREAD_CHECK_RET(wc_LockMutex(&info->dtls_mutex));
THREAD_CHECK_RET(wolfSSL_CondStart(&info->dtls_cond));
if (info->serverReady != 1) {
THREAD_CHECK_RET(wc_UnLockMutex(&info->dtls_mutex));
THREAD_CHECK_RET(wolfSSL_CondWait(&info->dtls_cond));
THREAD_CHECK_RET(wc_LockMutex(&info->dtls_mutex));
}
/* for next loop */
info->serverReady = 0;
THREAD_CHECK_RET(wc_UnLockMutex(&info->dtls_mutex));
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->dtls_cond));
}
#endif
/* perform connect */
Expand Down Expand Up @@ -1204,9 +1197,11 @@ static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN client_thread(void* args)

ret = bench_tls_client(info);

THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_server.cond));
info->to_client.done = 1;
info->client.ret = ret;
THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_server.cond));
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_server.cond));

WOLFSSL_RETURN_FROM_THREAD(NULL);
}
Expand Down Expand Up @@ -1292,10 +1287,10 @@ static int SocketWaitClient(info_t* info)
char msg[64];
#ifndef SINGLE_THREADED
if (!info->clientOrserverOnly) {
THREAD_CHECK_RET(wc_LockMutex(&info->dtls_mutex));
THREAD_CHECK_RET(wolfSSL_CondStart(&info->dtls_cond));
info->serverReady = 1;
THREAD_CHECK_RET(wc_UnLockMutex(&info->dtls_mutex));
THREAD_CHECK_RET(wolfSSL_CondSignal(&info->dtls_cond));
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->dtls_cond));
}
#endif
connd = (int)recvfrom(info->listenFd, (char *)msg, sizeof(msg),
Expand Down Expand Up @@ -1662,9 +1657,11 @@ static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN server_thread(void* args)
}
}

THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_client.cond));
info->to_server.done = 1;
info->server.ret = ret;
THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_client.cond));
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_client.cond));

WOLFSSL_RETURN_FROM_THREAD(NULL);
}
Expand Down Expand Up @@ -2127,8 +2124,6 @@ int bench_tls(void* args)
else {
#if !defined(SINGLE_THREADED) && defined(WOLFSSL_THREAD_NO_JOIN)
info->useLocalMem = argLocalMem;
THREAD_CHECK_RET(wc_InitMutex(&info->to_server.mutex));
THREAD_CHECK_RET(wc_InitMutex(&info->to_client.mutex));
#ifdef WOLFSSL_DTLS
THREAD_CHECK_RET(wc_InitMutex(&info->dtls_mutex));
THREAD_CHECK_RET(wolfSSL_CondInit(&info->dtls_cond));
Expand Down
65 changes: 53 additions & 12 deletions examples/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,55 @@ static int ClientRead(WOLFSSL* ssl, char* reply, int replyLen, int mustRead,
return err;
}

static int ClientWriteRead(WOLFSSL* ssl, const char* msg, int msgSz,
char* reply, int replyLen, int mustRead,
const char* str, int exitWithRet)
{
int ret = 0;

do {
ret = ClientWrite(ssl, msg, msgSz, str, exitWithRet);
if (ret != 0) {
if (!exitWithRet)
err_sys("ClientWrite failed");
else
break;
}
if (wolfSSL_dtls(ssl)) {
ret = tcp_select(wolfSSL_get_fd(ssl), DEFAULT_TIMEOUT_SEC);
if (ret == TEST_TIMEOUT) {
continue;
}
else if (ret == TEST_RECV_READY) {
/* Ready to read */
}
else {
LOG_ERROR("%s tcp_select error\n", str);
if (!exitWithRet)
err_sys("tcp_select failed");
else
ret = WOLFSSL_FATAL_ERROR;
break;
}
}
ret = ClientRead(ssl, reply, replyLen, mustRead, str, exitWithRet);
if (mustRead && ret != 0) {
if (!exitWithRet)
err_sys("ClientRead failed");
else
break;
}
break;
} while (1);

if (ret != 0) {
char buffer[WOLFSSL_MAX_ERROR_SZ];
LOG_ERROR("SSL_write%s msg error %d, %s\n", str, ret,
wolfSSL_ERR_error_string(ret, buffer));
}

return ret;
}

/* when adding new option, please follow the steps below: */
/* 1. add new option message in English section */
Expand Down Expand Up @@ -4195,15 +4244,8 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
wolfSSL_update_keys(ssl);
#endif

err = ClientWrite(ssl, msg, msgSz, "", exitWithRet);
if (exitWithRet && (err != 0)) {
((func_args*)args)->return_code = err;
wolfSSL_free(ssl); ssl = NULL;
wolfSSL_CTX_free(ctx); ctx = NULL;
goto exit;
}

err = ClientRead(ssl, reply, sizeof(reply)-1, 1, "", exitWithRet);
err = ClientWriteRead(ssl, msg, msgSz, reply, sizeof(reply)-1, 1, "",
exitWithRet);
if (exitWithRet && (err != 0)) {
((func_args*)args)->return_code = err;
wolfSSL_free(ssl); ssl = NULL;
Expand Down Expand Up @@ -4500,10 +4542,9 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
msgSz = (int)XSTRLEN(kResumeMsg);
XMEMCPY(msg, kResumeMsg, msgSz);
}
(void)ClientWrite(sslResume, msg, msgSz, " resume", 0);

(void)ClientRead(sslResume, reply, sizeof(reply)-1, sendGET,
"Server resume: ", 0);
(void)ClientWriteRead(sslResume, msg, msgSz, reply, sizeof(reply)-1,
sendGET, " resume", 0);

ret = wolfSSL_shutdown(sslResume);
if (wc_shutdown && ret == WOLFSSL_SHUTDOWN_NOT_DONE)
Expand Down
4 changes: 2 additions & 2 deletions examples/echoserver/echoserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ static void SignalReady(void* args, word16 port)
/* signal ready to tcp_accept */
func_args* server_args = (func_args*)args;
tcp_ready* ready = server_args->signal;
THREAD_CHECK_RET(wc_LockMutex(&ready->mutex));
THREAD_CHECK_RET(wolfSSL_CondStart(&ready->cond));
ready->ready = 1;
ready->port = port;
THREAD_CHECK_RET(wc_UnLockMutex(&ready->mutex));
THREAD_CHECK_RET(wolfSSL_CondSignal(&ready->cond));
THREAD_CHECK_RET(wolfSSL_CondEnd(&ready->cond));
#endif /* NO_MAIN_DRIVER && WOLFSSL_COND */
(void)args;
(void)port;
Expand Down
53 changes: 19 additions & 34 deletions src/crl.c
Original file line number Diff line number Diff line change
Expand Up @@ -928,24 +928,20 @@ int wolfSSL_X509_STORE_add_crl(WOLFSSL_X509_STORE *store, WOLFSSL_X509_CRL *newc
/* Signal Monitor thread is setup, save status to setup flag, 0 on success */
static int SignalSetup(WOLFSSL_CRL* crl, int status)
{
int ret;
int ret, condRet;

ret = wolfSSL_CondStart(&crl->cond);
if (ret != 0)
return ret;

/* signal to calling thread we're setup */
if (wc_LockMutex(&crl->crlLock) != 0) {
WOLFSSL_MSG("wc_LockMutex crlLock failed");
return BAD_MUTEX_E;
}
crl->setup = status;
if (wc_UnLockMutex(&crl->crlLock) != 0) {
WOLFSSL_MSG("wc_UnLockMutex crlLock failed");
return BAD_MUTEX_E;
}

ret = wolfSSL_CondSignal(&crl->cond);
condRet = wolfSSL_CondSignal(&crl->cond);
ret = wolfSSL_CondEnd(&crl->cond);
if (ret != 0)
return BAD_COND_E;
return ret;

return 0;
return condRet;
}


Expand Down Expand Up @@ -1248,18 +1244,9 @@ static THREAD_RETURN WOLFSSL_THREAD DoMonitor(void* arg)
}
}

#ifdef WOLFSSL_SMALL_STACK
buff = (char*)XMALLOC(8192, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (buff == NULL)
return NULL;
#endif

/* signal to calling thread we're setup */
if (SignalSetup(crl, 1) != 0) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif

if (wd > 0) {
if (inotify_rm_watch(notifyFd, wd) < 0)
WOLFSSL_MSG("inotify_rm_watch #1 failed in DoMonitor");
Expand All @@ -1269,6 +1256,12 @@ static THREAD_RETURN WOLFSSL_THREAD DoMonitor(void* arg)
return NULL;
}

#ifdef WOLFSSL_SMALL_STACK
buff = (char*)XMALLOC(8192, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (buff == NULL)
return NULL;
#endif

for (;;) {
fd_set readfds;
int result;
Expand Down Expand Up @@ -1489,21 +1482,13 @@ static int StartMonitorCRL(WOLFSSL_CRL* crl)
}

/* wait for setup to complete */
if (wc_LockMutex(&crl->crlLock) != 0) {
WOLFSSL_MSG("wc_LockMutex crlLock failed");
if (wolfSSL_CondStart(&crl->cond) != 0) {
WOLFSSL_MSG("wolfSSL_CondStart failed");
return BAD_MUTEX_E;
}
while (crl->setup == 0) {
int condRet;
if (wc_UnLockMutex(&crl->crlLock) != 0) {
WOLFSSL_MSG("wc_UnLockMutex crlLock failed");
return BAD_MUTEX_E;
}
condRet = wolfSSL_CondWait(&crl->cond);
if (wc_LockMutex(&crl->crlLock) != 0) {
WOLFSSL_MSG("wc_LockMutex crlLock failed");
return BAD_MUTEX_E;
}
if (condRet != 0) {
ret = BAD_COND_E;
break;
Expand All @@ -1516,8 +1501,8 @@ static int StartMonitorCRL(WOLFSSL_CRL* crl)
WOLFSSL_MSG("DoMonitor setup failure");
crl->tid = INVALID_THREAD_VAL; /* thread already done */
}
if (wc_UnLockMutex(&crl->crlLock) != 0) {
WOLFSSL_MSG("wc_UnLockMutex crlLock failed");
if (wolfSSL_CondEnd(&crl->cond) != 0) {
WOLFSSL_MSG("wolfSSL_CondEnd failed");
return BAD_MUTEX_E;
}

Expand Down
Loading

0 comments on commit 65401cf

Please sign in to comment.