From 310c325f40d1ed1df451244980080811a58b8df1 Mon Sep 17 00:00:00 2001 From: "Ruslan (The BadAss SysMan) Laishev" <ruslan.laishev@demlabs.net> Date: Wed, 18 May 2022 22:51:40 +0300 Subject: [PATCH] [*] Tiny optimization of loops --- dap-sdk/net/core/dap_proc_thread.c | 34 ++++++++++++++++++------------ 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/dap-sdk/net/core/dap_proc_thread.c b/dap-sdk/net/core/dap_proc_thread.c index 3984150456..6a4151e277 100644 --- a/dap-sdk/net/core/dap_proc_thread.c +++ b/dap-sdk/net/core/dap_proc_thread.c @@ -61,7 +61,7 @@ typedef cpuset_t cpu_set_t; // Adopt BSD CPU setstructure to POSIX variant static size_t s_threads_count = 0; static dap_proc_thread_t * s_threads = NULL; -static dap_list_t * s_custom_threads = NULL; /* Customized proc threads out of the pool */ +static dap_slist_t s_custom_threads = $DAP_SLIST_INITALIZER; /* Customized proc threads out of the pool */ static pthread_rwlock_t s_custom_threads_rwlock = PTHREAD_RWLOCK_INITIALIZER; /* Lock to protect <s_custom_threads> */ @@ -106,20 +106,27 @@ int l_ret = 0; */ void dap_proc_thread_deinit() { - for (uint32_t i = 0; i < s_threads_count; i++){ +int l_rc; +size_t l_sz; +dap_proc_thread_t *l_proc_thread; + + for (uint32_t i = s_threads_count; i--; ){ dap_events_socket_event_signal(s_threads[i].event_exit, 1); pthread_join(s_threads[i].thread_id, NULL); } + // Cleaning custom proc threads pthread_rwlock_wrlock(&s_custom_threads_rwlock); - for ( dap_list_t * i = s_custom_threads; i; i=dap_list_next(i)){ - dap_proc_thread_t * l_proc_thread = (dap_proc_thread_t*) i->data; + + for ( uint32_t i = s_custom_threads.nr; i--; ) { + if ( s_dap_slist_get4head (&s_custom_threads, (void **) &l_proc_thread, &l_sz) ) + break; + dap_events_socket_event_signal(l_proc_thread->event_exit, 1); pthread_join(l_proc_thread->thread_id, NULL); DAP_DELETE(l_proc_thread); } - dap_list_free(s_custom_threads); - s_custom_threads = NULL; + pthread_rwlock_unlock(&s_custom_threads_rwlock); // Signal to cancel working threads and wait for finish @@ -170,20 +177,21 @@ unsigned l_id_min = 0, l_size_min = UINT32_MAX, l_queue_size; */ dap_proc_thread_t * dap_proc_thread_run_custom(void) { - dap_proc_thread_t * l_proc_thread = DAP_NEW_Z(dap_proc_thread_t); - if (l_proc_thread == NULL){ - log_it(L_CRITICAL,"Out of memory, can't create new proc thread, errno=%d", errno); - return NULL; - } - int l_ret; +dap_proc_thread_t * l_proc_thread = DAP_NEW_Z(dap_proc_thread_t); +int l_ret; + + if (l_proc_thread == NULL) + return log_it(L_CRITICAL,"Out of memory, can't create new proc thread, errno=%d", errno), NULL; + pthread_mutex_lock( &s_started_mutex ); + if ( (l_ret = pthread_create( &l_proc_thread->thread_id ,NULL, s_proc_thread_function, l_proc_thread )) ) { log_it(L_CRITICAL, "Create thread failed with code %d", l_ret); DAP_DEL_Z (l_proc_thread); }else{ pthread_cond_wait( &s_started_cond, &s_started_mutex); pthread_rwlock_wrlock(&s_custom_threads_rwlock); - s_custom_threads = dap_list_append(s_custom_threads, l_proc_thread ); + assert ( !s_dap_slist_add2tail (&s_custom_threads, l_proc_thread, sizeof(l_proc_thread)) ); pthread_rwlock_unlock(&s_custom_threads_rwlock); } -- GitLab