From 52a579f8d16f106a83b4a5cab16bf32ba5b280dc Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 19 Dec 2010 12:47:23 +0000 Subject: option to set stack size --- ChangeLog | 3 +++ doc/microhttpd.texi | 9 ++++++++ src/daemon/daemon.c | 58 ++++++++++++++++++++++++++++++++++++++++++++---- src/daemon/internal.h | 5 +++++ src/include/microhttpd.h | 10 ++++++++- 5 files changed, 80 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1150386e..067208eb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +Sun Dec 19 13:46:52 CET 2010 + Added option to specify size of stacks for threads created by MHD. -CG + Tue Nov 23 09:41:00 CET 2010 Releasing libmicrohttpd 0.9.3. -CG diff --git a/doc/microhttpd.texi b/doc/microhttpd.texi index 9f509814..883018f1 100644 --- a/doc/microhttpd.texi +++ b/doc/microhttpd.texi @@ -562,6 +562,15 @@ updated. Note that the unescape function must not lengthen @code{s} (the result must be shorter than the input and still be 0-terminated). @code{cls} will be set to the second argument following MHD_OPTION_UNESCAPE_CALLBACK. + + +@item MHD_OPTION_THREAD_STACK_SIZE +@cindex stack, thread, pthread +Maximum stack size for threads created by MHD. This option must be +followed by a @code{size_t}). Not specifying this option or using +a value of zero means using the system default (which is likely to +differ based on your platform). + @end table @end deftp diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index f16491c7..558954d6 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c @@ -752,6 +752,52 @@ socket_set_nonblocking (int fd) #endif +/** + * Create a thread and set the attributes according to our options. + * + * @param thread handle to initialize + * @param daemon daemon with options + * @param start_routine main function of thread + * @param arg argument for start_routine + * @return 0 on success + */ +static int +create_thread (pthread_t * thread, + const struct MHD_Daemon *daemon, + void *(*start_routine)(void*), + void *arg) +{ + pthread_attr_t attr; + pthread_attr_t *pattr; + int ret; + + if (daemon->thread_stack_size != 0) + { + if ( (0 != (ret = pthread_attr_init (&attr))) || + (0 != (ret = pthread_attr_setstacksize (&attr, daemon->thread_stack_size))) ) + { +#if HAVE_MESSAGES + MHD_DLOG (daemon, + "Failed to set thread stack size\n"); +#endif + errno = EINVAL; + return ret; + } + pattr = &attr; + } + else + { + pattr = NULL; + } + ret = pthread_create (thread, pattr, + start_routine, arg); + if (pattr != NULL) + pthread_attr_destroy (&attr); + return ret; +} + + + /** * Accept an incoming connection and create the MHD_Connection object for * it. This function also enforces policy by way of checking with the @@ -943,8 +989,8 @@ MHD_accept_connection (struct MHD_Daemon *daemon) /* attempt to create handler thread */ if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) { - res_thread_create = pthread_create (&connection->pid, NULL, - &MHD_handle_connection, connection); + res_thread_create = create_thread (&connection->pid, daemon, + &MHD_handle_connection, connection); if (res_thread_create != 0) { #if HAVE_MESSAGES @@ -1464,6 +1510,9 @@ parse_options_va (struct MHD_Daemon *daemon, va_arg (ap, void *); #endif break; + case MHD_OPTION_THREAD_STACK_SIZE: + daemon->thread_stack_size = va_arg (ap, size_t); + break; case MHD_OPTION_ARRAY: oa = va_arg (ap, struct MHD_OptionItem*); i = 0; @@ -1473,6 +1522,7 @@ parse_options_va (struct MHD_Daemon *daemon, { /* all options taking 'size_t' */ case MHD_OPTION_CONNECTION_MEMORY_LIMIT: + case MHD_OPTION_THREAD_STACK_SIZE: if (MHD_YES != parse_options (daemon, servaddr, opt, @@ -1904,7 +1954,7 @@ MHD_start_daemon_va (unsigned int options, ( (0 != (options & MHD_USE_SELECT_INTERNALLY)) && (0 == retVal->worker_pool_size)) ) && (0 != (res_thread_create = - pthread_create (&retVal->pid, NULL, &MHD_select_thread, retVal)))) + create_thread (&retVal->pid, retVal, &MHD_select_thread, retVal)))) { #if HAVE_MESSAGES MHD_DLOG (retVal, @@ -1982,7 +2032,7 @@ MHD_start_daemon_va (unsigned int options, ++d->max_connections; /* Spawn the worker thread */ - if (0 != (res_thread_create = pthread_create (&d->pid, NULL, &MHD_select_thread, d))) + if (0 != (res_thread_create = create_thread (&d->pid, retVal, &MHD_select_thread, d))) { #if HAVE_MESSAGES MHD_DLOG (retVal, diff --git a/src/daemon/internal.h b/src/daemon/internal.h index cdf93f51..be476f4b 100644 --- a/src/daemon/internal.h +++ b/src/daemon/internal.h @@ -824,6 +824,11 @@ struct MHD_Daemon */ size_t pool_size; + /** + * Size of threads created by MHD. + */ + size_t thread_stack_size; + /** * Number of worker daemons */ diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h index de8468b0..a30f87a9 100644 --- a/src/include/microhttpd.h +++ b/src/include/microhttpd.h @@ -558,7 +558,15 @@ enum MHD_OPTION * the nonce counter. This option should be followed by a "unsigend int" * argument. */ - MHD_OPTION_NONCE_NC_SIZE = 18 + MHD_OPTION_NONCE_NC_SIZE = 18, + + /** + * Desired size of the stack for threads created by MHD. Followed + * by an argument of type 'size_t'. Use 0 for system 'default'. + */ + MHD_OPTION_THREAD_STACK_SIZE = 19 + + }; -- cgit v1.2.3