Skip to content

Commit 65401cf

Browse files
Merge pull request #6697 from julek-wolfssl/refactor-cond-again
Refactor conditional code again
2 parents 26fcdbf + c3fea8c commit 65401cf

8 files changed

Lines changed: 238 additions & 116 deletions

File tree

examples/benchmark/tls_bench.c

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,6 @@ typedef struct {
307307
int read_bytes;
308308
int read_idx;
309309

310-
wolfSSL_Mutex mutex;
311310
COND_TYPE cond;
312311

313312
int done;
@@ -398,12 +397,12 @@ static double gettime_secs(int reset)
398397
/* server send callback */
399398
static int ServerMemSend(info_t* info, char* buf, int sz)
400399
{
401-
THREAD_CHECK_RET(wc_LockMutex(&info->to_client.mutex));
400+
THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_client.cond));
402401

403402
#ifndef BENCH_USE_NONBLOCK
404403
/* check for overflow */
405404
if (info->to_client.write_idx + sz > MEM_BUFFER_SZ) {
406-
THREAD_CHECK_RET(wc_UnLockMutex(&info->to_client.mutex));
405+
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_client.cond));
407406
fprintf(stderr, "ServerMemSend overflow\n");
408407
return -1;
409408
}
@@ -416,9 +415,9 @@ static int ServerMemSend(info_t* info, char* buf, int sz)
416415
XMEMCPY(&info->to_client.buf[info->to_client.write_idx], buf, sz);
417416
info->to_client.write_idx += sz;
418417
info->to_client.write_bytes += sz;
419-
THREAD_CHECK_RET(wc_UnLockMutex(&info->to_client.mutex));
420418

421419
THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_client.cond));
420+
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_client.cond));
422421

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

436435
#ifndef BENCH_USE_NONBLOCK
437436
while (info->to_server.write_idx - info->to_server.read_idx < sz &&
438437
!info->to_client.done) {
439-
THREAD_CHECK_RET(wc_UnLockMutex(&info->to_server.mutex));
440438
THREAD_CHECK_RET(wolfSSL_CondWait(&info->to_server.cond));
441-
THREAD_CHECK_RET(wc_LockMutex(&info->to_server.mutex));
442439
}
443440
#else
444441
if (info->to_server.write_idx - info->to_server.read_idx < sz) {
@@ -456,7 +453,7 @@ static int ServerMemRecv(info_t* info, char* buf, int sz)
456453
info->to_server.write_bytes = info->to_server.write_idx = 0;
457454
}
458455

459-
THREAD_CHECK_RET(wc_UnLockMutex(&info->to_server.mutex));
456+
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_server.cond));
460457

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

478475
#ifndef BENCH_USE_NONBLOCK
479476
/* check for overflow */
480477
if (info->to_server.write_idx + sz > MEM_BUFFER_SZ) {
481478
fprintf(stderr, "ClientMemSend overflow %d %d %d\n",
482479
info->to_server.write_idx, sz, MEM_BUFFER_SZ);
483-
THREAD_CHECK_RET(wc_UnLockMutex(&info->to_server.mutex));
480+
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_server.cond));
484481
return -1;
485482
}
486483
#else
@@ -493,8 +490,8 @@ static int ClientMemSend(info_t* info, char* buf, int sz)
493490
info->to_server.write_idx += sz;
494491
info->to_server.write_bytes += sz;
495492

496-
THREAD_CHECK_RET(wc_UnLockMutex(&info->to_server.mutex));
497493
THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_server.cond));
494+
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_server.cond));
498495

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

512509
#ifndef BENCH_USE_NONBLOCK
513510
while (info->to_client.write_idx - info->to_client.read_idx < sz &&
514511
!info->to_server.done) {
515-
THREAD_CHECK_RET(wc_UnLockMutex(&info->to_client.mutex));
516512
THREAD_CHECK_RET(wolfSSL_CondWait(&info->to_client.cond));
517-
THREAD_CHECK_RET(wc_LockMutex(&info->to_client.mutex));
518513
}
519514
#else
520515
if (info->to_client.write_idx - info->to_client.read_idx < sz) {
@@ -532,7 +527,7 @@ static int ClientMemRecv(info_t* info, char* buf, int sz)
532527
info->to_client.write_bytes = info->to_client.write_idx = 0;
533528
}
534529

535-
THREAD_CHECK_RET(wc_UnLockMutex(&info->to_client.mutex));
530+
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_client.cond));
536531

537532
if (info->to_server.done != 0) {
538533
return -1;
@@ -1054,15 +1049,13 @@ static int bench_tls_client(info_t* info)
10541049
#if !defined(SINGLE_THREADED) && defined(WOLFSSL_DTLS)
10551050
/* synchronize with server */
10561051
if (info->doDTLS && !info->clientOrserverOnly) {
1057-
THREAD_CHECK_RET(wc_LockMutex(&info->dtls_mutex));
1052+
THREAD_CHECK_RET(wolfSSL_CondStart(&info->dtls_cond));
10581053
if (info->serverReady != 1) {
1059-
THREAD_CHECK_RET(wc_UnLockMutex(&info->dtls_mutex));
10601054
THREAD_CHECK_RET(wolfSSL_CondWait(&info->dtls_cond));
1061-
THREAD_CHECK_RET(wc_LockMutex(&info->dtls_mutex));
10621055
}
10631056
/* for next loop */
10641057
info->serverReady = 0;
1065-
THREAD_CHECK_RET(wc_UnLockMutex(&info->dtls_mutex));
1058+
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->dtls_cond));
10661059
}
10671060
#endif
10681061
/* perform connect */
@@ -1204,9 +1197,11 @@ static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN client_thread(void* args)
12041197

12051198
ret = bench_tls_client(info);
12061199

1200+
THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_server.cond));
12071201
info->to_client.done = 1;
12081202
info->client.ret = ret;
12091203
THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_server.cond));
1204+
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_server.cond));
12101205

12111206
WOLFSSL_RETURN_FROM_THREAD(NULL);
12121207
}
@@ -1292,10 +1287,10 @@ static int SocketWaitClient(info_t* info)
12921287
char msg[64];
12931288
#ifndef SINGLE_THREADED
12941289
if (!info->clientOrserverOnly) {
1295-
THREAD_CHECK_RET(wc_LockMutex(&info->dtls_mutex));
1290+
THREAD_CHECK_RET(wolfSSL_CondStart(&info->dtls_cond));
12961291
info->serverReady = 1;
1297-
THREAD_CHECK_RET(wc_UnLockMutex(&info->dtls_mutex));
12981292
THREAD_CHECK_RET(wolfSSL_CondSignal(&info->dtls_cond));
1293+
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->dtls_cond));
12991294
}
13001295
#endif
13011296
connd = (int)recvfrom(info->listenFd, (char *)msg, sizeof(msg),
@@ -1662,9 +1657,11 @@ static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN server_thread(void* args)
16621657
}
16631658
}
16641659

1660+
THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_client.cond));
16651661
info->to_server.done = 1;
16661662
info->server.ret = ret;
16671663
THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_client.cond));
1664+
THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_client.cond));
16681665

16691666
WOLFSSL_RETURN_FROM_THREAD(NULL);
16701667
}
@@ -2127,8 +2124,6 @@ int bench_tls(void* args)
21272124
else {
21282125
#if !defined(SINGLE_THREADED) && defined(WOLFSSL_THREAD_NO_JOIN)
21292126
info->useLocalMem = argLocalMem;
2130-
THREAD_CHECK_RET(wc_InitMutex(&info->to_server.mutex));
2131-
THREAD_CHECK_RET(wc_InitMutex(&info->to_client.mutex));
21322127
#ifdef WOLFSSL_DTLS
21332128
THREAD_CHECK_RET(wc_InitMutex(&info->dtls_mutex));
21342129
THREAD_CHECK_RET(wolfSSL_CondInit(&info->dtls_cond));

examples/client/client.c

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,55 @@ static int ClientRead(WOLFSSL* ssl, char* reply, int replyLen, int mustRead,
10661066
return err;
10671067
}
10681068

1069+
static int ClientWriteRead(WOLFSSL* ssl, const char* msg, int msgSz,
1070+
char* reply, int replyLen, int mustRead,
1071+
const char* str, int exitWithRet)
1072+
{
1073+
int ret = 0;
1074+
1075+
do {
1076+
ret = ClientWrite(ssl, msg, msgSz, str, exitWithRet);
1077+
if (ret != 0) {
1078+
if (!exitWithRet)
1079+
err_sys("ClientWrite failed");
1080+
else
1081+
break;
1082+
}
1083+
if (wolfSSL_dtls(ssl)) {
1084+
ret = tcp_select(wolfSSL_get_fd(ssl), DEFAULT_TIMEOUT_SEC);
1085+
if (ret == TEST_TIMEOUT) {
1086+
continue;
1087+
}
1088+
else if (ret == TEST_RECV_READY) {
1089+
/* Ready to read */
1090+
}
1091+
else {
1092+
LOG_ERROR("%s tcp_select error\n", str);
1093+
if (!exitWithRet)
1094+
err_sys("tcp_select failed");
1095+
else
1096+
ret = WOLFSSL_FATAL_ERROR;
1097+
break;
1098+
}
1099+
}
1100+
ret = ClientRead(ssl, reply, replyLen, mustRead, str, exitWithRet);
1101+
if (mustRead && ret != 0) {
1102+
if (!exitWithRet)
1103+
err_sys("ClientRead failed");
1104+
else
1105+
break;
1106+
}
1107+
break;
1108+
} while (1);
1109+
1110+
if (ret != 0) {
1111+
char buffer[WOLFSSL_MAX_ERROR_SZ];
1112+
LOG_ERROR("SSL_write%s msg error %d, %s\n", str, ret,
1113+
wolfSSL_ERR_error_string(ret, buffer));
1114+
}
1115+
1116+
return ret;
1117+
}
10691118

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

4198-
err = ClientWrite(ssl, msg, msgSz, "", exitWithRet);
4199-
if (exitWithRet && (err != 0)) {
4200-
((func_args*)args)->return_code = err;
4201-
wolfSSL_free(ssl); ssl = NULL;
4202-
wolfSSL_CTX_free(ctx); ctx = NULL;
4203-
goto exit;
4204-
}
4205-
4206-
err = ClientRead(ssl, reply, sizeof(reply)-1, 1, "", exitWithRet);
4247+
err = ClientWriteRead(ssl, msg, msgSz, reply, sizeof(reply)-1, 1, "",
4248+
exitWithRet);
42074249
if (exitWithRet && (err != 0)) {
42084250
((func_args*)args)->return_code = err;
42094251
wolfSSL_free(ssl); ssl = NULL;
@@ -4500,10 +4542,9 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
45004542
msgSz = (int)XSTRLEN(kResumeMsg);
45014543
XMEMCPY(msg, kResumeMsg, msgSz);
45024544
}
4503-
(void)ClientWrite(sslResume, msg, msgSz, " resume", 0);
45044545

4505-
(void)ClientRead(sslResume, reply, sizeof(reply)-1, sendGET,
4506-
"Server resume: ", 0);
4546+
(void)ClientWriteRead(sslResume, msg, msgSz, reply, sizeof(reply)-1,
4547+
sendGET, " resume", 0);
45074548

45084549
ret = wolfSSL_shutdown(sslResume);
45094550
if (wc_shutdown && ret == WOLFSSL_SHUTDOWN_NOT_DONE)

examples/echoserver/echoserver.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ static void SignalReady(void* args, word16 port)
7171
/* signal ready to tcp_accept */
7272
func_args* server_args = (func_args*)args;
7373
tcp_ready* ready = server_args->signal;
74-
THREAD_CHECK_RET(wc_LockMutex(&ready->mutex));
74+
THREAD_CHECK_RET(wolfSSL_CondStart(&ready->cond));
7575
ready->ready = 1;
7676
ready->port = port;
77-
THREAD_CHECK_RET(wc_UnLockMutex(&ready->mutex));
7877
THREAD_CHECK_RET(wolfSSL_CondSignal(&ready->cond));
78+
THREAD_CHECK_RET(wolfSSL_CondEnd(&ready->cond));
7979
#endif /* NO_MAIN_DRIVER && WOLFSSL_COND */
8080
(void)args;
8181
(void)port;

src/crl.c

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -928,24 +928,20 @@ int wolfSSL_X509_STORE_add_crl(WOLFSSL_X509_STORE *store, WOLFSSL_X509_CRL *newc
928928
/* Signal Monitor thread is setup, save status to setup flag, 0 on success */
929929
static int SignalSetup(WOLFSSL_CRL* crl, int status)
930930
{
931-
int ret;
931+
int ret, condRet;
932+
933+
ret = wolfSSL_CondStart(&crl->cond);
934+
if (ret != 0)
935+
return ret;
932936

933-
/* signal to calling thread we're setup */
934-
if (wc_LockMutex(&crl->crlLock) != 0) {
935-
WOLFSSL_MSG("wc_LockMutex crlLock failed");
936-
return BAD_MUTEX_E;
937-
}
938937
crl->setup = status;
939-
if (wc_UnLockMutex(&crl->crlLock) != 0) {
940-
WOLFSSL_MSG("wc_UnLockMutex crlLock failed");
941-
return BAD_MUTEX_E;
942-
}
943938

944-
ret = wolfSSL_CondSignal(&crl->cond);
939+
condRet = wolfSSL_CondSignal(&crl->cond);
940+
ret = wolfSSL_CondEnd(&crl->cond);
945941
if (ret != 0)
946-
return BAD_COND_E;
942+
return ret;
947943

948-
return 0;
944+
return condRet;
949945
}
950946

951947

@@ -1248,18 +1244,9 @@ static THREAD_RETURN WOLFSSL_THREAD DoMonitor(void* arg)
12481244
}
12491245
}
12501246

1251-
#ifdef WOLFSSL_SMALL_STACK
1252-
buff = (char*)XMALLOC(8192, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1253-
if (buff == NULL)
1254-
return NULL;
1255-
#endif
12561247

12571248
/* signal to calling thread we're setup */
12581249
if (SignalSetup(crl, 1) != 0) {
1259-
#ifdef WOLFSSL_SMALL_STACK
1260-
XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1261-
#endif
1262-
12631250
if (wd > 0) {
12641251
if (inotify_rm_watch(notifyFd, wd) < 0)
12651252
WOLFSSL_MSG("inotify_rm_watch #1 failed in DoMonitor");
@@ -1269,6 +1256,12 @@ static THREAD_RETURN WOLFSSL_THREAD DoMonitor(void* arg)
12691256
return NULL;
12701257
}
12711258

1259+
#ifdef WOLFSSL_SMALL_STACK
1260+
buff = (char*)XMALLOC(8192, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1261+
if (buff == NULL)
1262+
return NULL;
1263+
#endif
1264+
12721265
for (;;) {
12731266
fd_set readfds;
12741267
int result;
@@ -1489,21 +1482,13 @@ static int StartMonitorCRL(WOLFSSL_CRL* crl)
14891482
}
14901483

14911484
/* wait for setup to complete */
1492-
if (wc_LockMutex(&crl->crlLock) != 0) {
1493-
WOLFSSL_MSG("wc_LockMutex crlLock failed");
1485+
if (wolfSSL_CondStart(&crl->cond) != 0) {
1486+
WOLFSSL_MSG("wolfSSL_CondStart failed");
14941487
return BAD_MUTEX_E;
14951488
}
14961489
while (crl->setup == 0) {
14971490
int condRet;
1498-
if (wc_UnLockMutex(&crl->crlLock) != 0) {
1499-
WOLFSSL_MSG("wc_UnLockMutex crlLock failed");
1500-
return BAD_MUTEX_E;
1501-
}
15021491
condRet = wolfSSL_CondWait(&crl->cond);
1503-
if (wc_LockMutex(&crl->crlLock) != 0) {
1504-
WOLFSSL_MSG("wc_LockMutex crlLock failed");
1505-
return BAD_MUTEX_E;
1506-
}
15071492
if (condRet != 0) {
15081493
ret = BAD_COND_E;
15091494
break;
@@ -1516,8 +1501,8 @@ static int StartMonitorCRL(WOLFSSL_CRL* crl)
15161501
WOLFSSL_MSG("DoMonitor setup failure");
15171502
crl->tid = INVALID_THREAD_VAL; /* thread already done */
15181503
}
1519-
if (wc_UnLockMutex(&crl->crlLock) != 0) {
1520-
WOLFSSL_MSG("wc_UnLockMutex crlLock failed");
1504+
if (wolfSSL_CondEnd(&crl->cond) != 0) {
1505+
WOLFSSL_MSG("wolfSSL_CondEnd failed");
15211506
return BAD_MUTEX_E;
15221507
}
15231508

0 commit comments

Comments
 (0)