-
Notifications
You must be signed in to change notification settings - Fork 3
threadpool: fix: min thread amount was ignored on POSIX #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -162,18 +162,19 @@ static void *threadpool_do_work(void *arg) { | |||||||||||||||
| pthread_exit(NULL); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| static void threadpool_create_thread_on_demand(threadpool_s *threadpool) { | ||||||||||||||||
| static bool threadpool_create_thread_on_demand(threadpool_s *threadpool) { | ||||||||||||||||
| // Create new thread and add it to the list of threads | ||||||||||||||||
| pthread_t handle = 0; | ||||||||||||||||
| if (pthread_create(&handle, NULL, threadpool_do_work, threadpool)) | ||||||||||||||||
| return; | ||||||||||||||||
| return false; | ||||||||||||||||
|
|
||||||||||||||||
| threadpool_thread_s *thread = (threadpool_thread_s *)calloc(1, sizeof(threadpool_thread_s)); | ||||||||||||||||
| thread->handle = handle; | ||||||||||||||||
| thread->next = threadpool->threads; | ||||||||||||||||
|
|
||||||||||||||||
| threadpool->threads = thread; | ||||||||||||||||
| threadpool->num_threads++; | ||||||||||||||||
| return true; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| bool threadpool_enqueue(void *ctx, void *user_data, threadpool_job_cb callback) { | ||||||||||||||||
|
|
@@ -189,6 +190,12 @@ bool threadpool_enqueue(void *ctx, void *user_data, threadpool_job_cb callback) | |||||||||||||||
| // Add job to the job queue | ||||||||||||||||
| threadpool_enqueue_job(threadpool, job); | ||||||||||||||||
|
|
||||||||||||||||
| // Create min amount of threads | ||||||||||||||||
| while (threadpool->num_threads < threadpool->min_threads) { | ||||||||||||||||
| if (!threadpool_create_thread_on_demand(threadpool)) | ||||||||||||||||
| break; | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+193
to
+197
|
||||||||||||||||
|
|
||||||||||||||||
| // Create new thread if all threads are busy | ||||||||||||||||
| if (threadpool->busy_threads == threadpool->num_threads && threadpool->num_threads < threadpool->max_threads) | ||||||||||||||||
| threadpool_create_thread_on_demand(threadpool); | ||||||||||||||||
|
Comment on lines
200
to
201
|
||||||||||||||||
| if (threadpool->busy_threads == threadpool->num_threads && threadpool->num_threads < threadpool->max_threads) | |
| threadpool_create_thread_on_demand(threadpool); | |
| if (threadpool->busy_threads == threadpool->num_threads && threadpool->num_threads < threadpool->max_threads) { | |
| if (!threadpool_create_thread_on_demand(threadpool)) { | |
| fprintf(stderr, "threadpool: warning: failed to create thread on demand; job processing may be delayed\n"); | |
| } | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function creates a pthread but doesn't check if the subsequent calloc fails. If calloc fails (thread is NULL), the code will dereference a NULL pointer on line 172. Additionally, if calloc fails after pthread_create succeeds, the created thread will be orphaned (memory leak) since the thread handle is not stored and cannot be joined later. Add a NULL check after calloc and if it fails, consider detaching the thread or implementing proper cleanup.