@@ -33,9 +33,13 @@ struct ProcThread_i {
3333 volatile bool terminating ;
3434 volatile int flags ;
3535
36+ bool thrd_init ;
3637 thrd_t thrd ;
3738
39+ bool cond_init ;
3840 cnd_t cond ;
41+
42+ bool mutex_init ;
3943 mtx_t mutex ;
4044
4145 ProcThreadCallback callback ;
@@ -54,18 +58,24 @@ ProcThread pt_create(ProcThreadCallback callback, void *userdata) {
5458 LOG_WARNING ("Failed to initialize cnd_t" );
5559 pt_free (thread );
5660 return NULL ;
61+ }else {
62+ thread -> cond_init = true;
5763 }
5864
5965 if (mtx_init (& thread -> mutex , mtx_plain ) != thrd_success ){
6066 LOG_WARNING ("Failed to initialize mutex" );
6167 pt_free (thread );
6268 return NULL ;
69+ }else {
70+ thread -> mutex_init = true;
6371 }
6472
6573 if (thrd_create (& thread -> thrd , run_pt , thread ) != thrd_success ) {
6674 LOG_WARNING ("Failed to start thread" );
6775 pt_free (thread );
6876 return NULL ;
77+ }else {
78+ thread -> thrd_init = true;
6979 }
7080
7181 return thread ;
@@ -108,15 +118,15 @@ void pt_terminate(ProcThread thread) {
108118void pt_free (ProcThread thread ) {
109119 if (thread == NULL ) return ;
110120
111- if (( thread -> thrd != NULL ) && ( thread -> mutex != NULL ) ){
121+ if (thread -> thrd_init && thread -> mutex_init && thread -> cond_init ){
112122 pt_terminate (thread );
113123 }
114124
115- if (thread -> mutex != NULL ){
125+ if (thread -> mutex_init ){
116126 mtx_destroy (& thread -> mutex );
117127 }
118128
119- if (thread -> cond != NULL ){
129+ if (thread -> cond_init ){
120130 cnd_destroy (& thread -> cond );
121131 }
122132
0 commit comments