Skip to content

Commit

Permalink
Ensure minimum stack size for threads is 1MB
Browse files Browse the repository at this point in the history
  • Loading branch information
Jovasa committed Jul 30, 2024
1 parent ea09af5 commit 1eeccb3
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/threadqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,31 @@ threadqueue_queue_t * kvz_threadqueue_init(int thread_count)
threadqueue->first = NULL;
threadqueue->last = NULL;

#ifndef _WIN32
pthread_attr_t attr;
if (pthread_attr_init(&attr) != 0) {
fprintf(stderr, "pthread_attr_init failed!\n");
goto failed;
}
size_t default_stack_size;
if (pthread_attr_getstacksize(&attr, &default_stack_size) != 0) {
fprintf(stderr, "pthread_attr_getstacksize failed!\n");
goto failed;
}
if (default_stack_size < 1024 * 1024) {
if (pthread_attr_setstacksize(&attr, 1024 * 1024) != 0) {
fprintf(stderr, "pthread_attr_setstacksize failed!\n");
goto failed;
}
}
#else
pthread_attr_t* attr;
#endif

// Lock the queue before creating threads, to ensure they all have correct information.
PTHREAD_LOCK(&threadqueue->lock);
for (int i = 0; i < thread_count; i++) {
if (pthread_create(&threadqueue->threads[i], NULL, threadqueue_worker, threadqueue) != 0) {
if (pthread_create(&threadqueue->threads[i], &attr, threadqueue_worker, threadqueue) != 0) {
fprintf(stderr, "pthread_create failed!\n");
goto failed;
}
Expand All @@ -404,6 +425,11 @@ threadqueue_queue_t * kvz_threadqueue_init(int thread_count)
return threadqueue;

failed:
#ifndef _WIN32
if (pthread_attr_destroy(&attr) != 0) {
fprintf(stderr, "pthread_attr_destroy failed!\n");
}
#endif
kvz_threadqueue_free(threadqueue);
return NULL;
}
Expand Down

0 comments on commit 1eeccb3

Please sign in to comment.