aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/daemon.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd/daemon.c')
-rw-r--r--src/microhttpd/daemon.c120
1 files changed, 24 insertions, 96 deletions
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
index fd8754ce..f493d19d 100644
--- a/src/microhttpd/daemon.c
+++ b/src/microhttpd/daemon.c
@@ -25,6 +25,7 @@
25 * @author Christian Grothoff 25 * @author Christian Grothoff
26 */ 26 */
27#include "platform.h" 27#include "platform.h"
28#include "mhd_threads.h"
28#include "internal.h" 29#include "internal.h"
29#include "response.h" 30#include "response.h"
30#include "connection.h" 31#include "connection.h"
@@ -61,7 +62,6 @@
61#define WIN32_LEAN_AND_MEAN 1 62#define WIN32_LEAN_AND_MEAN 1
62#endif /* !WIN32_LEAN_AND_MEAN */ 63#endif /* !WIN32_LEAN_AND_MEAN */
63#include <windows.h> 64#include <windows.h>
64#include <process.h>
65#endif 65#endif
66 66
67/** 67/**
@@ -1280,81 +1280,6 @@ send_param_adapter (struct MHD_Connection *connection,
1280 1280
1281 1281
1282/** 1282/**
1283 * Signature of main function for a thread.
1284 *
1285 * @param cls closure argument for the function
1286 * @return termination code from the thread
1287 */
1288typedef MHD_THRD_RTRN_TYPE_
1289(MHD_THRD_CALL_SPEC_ *ThreadStartRoutine)(void *cls);
1290
1291
1292/**
1293 * Create a thread and set the attributes according to our options.
1294 *
1295 * @param thread handle to initialize
1296 * @param daemon daemon with options
1297 * @param start_routine main function of thread
1298 * @param arg argument for start_routine
1299 * @return 0 on success
1300 */
1301static int
1302create_thread (MHD_thread_handle_ *thread,
1303 const struct MHD_Daemon *daemon,
1304 ThreadStartRoutine start_routine,
1305 void *arg)
1306{
1307#if defined(MHD_USE_POSIX_THREADS)
1308 pthread_attr_t attr;
1309 pthread_attr_t *pattr;
1310 int ret;
1311
1312 if (0 != daemon->thread_stack_size)
1313 {
1314 if (0 != (ret = pthread_attr_init (&attr)))
1315 goto ERR;
1316 if (0 != (ret = pthread_attr_setstacksize (&attr, daemon->thread_stack_size)))
1317 {
1318 pthread_attr_destroy (&attr);
1319 goto ERR;
1320 }
1321 pattr = &attr;
1322 }
1323 else
1324 {
1325 pattr = NULL;
1326 }
1327 ret = pthread_create (thread, pattr,
1328 start_routine, arg);
1329#ifdef HAVE_PTHREAD_SETNAME_NP
1330 if (0 == ret)
1331 (void) pthread_setname_np (*thread, "libmicrohttpd");
1332#endif /* HAVE_PTHREAD_SETNAME_NP */
1333 if (0 != daemon->thread_stack_size)
1334 pthread_attr_destroy (&attr);
1335 return ret;
1336 ERR:
1337#ifdef HAVE_MESSAGES
1338 MHD_DLOG (daemon,
1339 "Failed to set thread stack size\n");
1340#endif
1341 errno = EINVAL;
1342 return ret;
1343#elif defined(MHD_USE_W32_THREADS)
1344 unsigned threadID;
1345 *thread = (HANDLE)_beginthreadex(NULL, (unsigned)daemon->thread_stack_size, start_routine,
1346 arg, 0, &threadID);
1347 if (NULL == (*thread))
1348 return errno;
1349
1350 W32_SetThreadName(threadID, "libmicrohttpd");
1351
1352 return 0;
1353#endif
1354}
1355
1356
1357/**
1358 * Add another client connection to the set of connections 1283 * Add another client connection to the set of connections
1359 * managed by MHD. This API is usually not needed (since 1284 * managed by MHD. This API is usually not needed (since
1360 * MHD will accept inbound connections on the server socket). 1285 * MHD will accept inbound connections on the server socket).
@@ -1388,7 +1313,6 @@ internal_add_connection (struct MHD_Daemon *daemon,
1388 int external_add) 1313 int external_add)
1389{ 1314{
1390 struct MHD_Connection *connection; 1315 struct MHD_Connection *connection;
1391 int res_thread_create;
1392 unsigned int i; 1316 unsigned int i;
1393 int eno; 1317 int eno;
1394 struct MHD_Daemon *worker; 1318 struct MHD_Daemon *worker;
@@ -1635,17 +1559,17 @@ internal_add_connection (struct MHD_Daemon *daemon,
1635 /* attempt to create handler thread */ 1559 /* attempt to create handler thread */
1636 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 1560 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
1637 { 1561 {
1638 res_thread_create = create_thread (&connection->pid, 1562 if (!MHD_create_named_thread_(&connection->pid,
1639 daemon, 1563 "MHD-connection",
1640 &MHD_handle_connection, 1564 daemon->thread_stack_size,
1641 connection); 1565 &MHD_handle_connection,
1642 if (0 != res_thread_create) 1566 connection))
1643 { 1567 {
1644 eno = errno; 1568 eno = errno;
1645#ifdef HAVE_MESSAGES 1569#ifdef HAVE_MESSAGES
1646 MHD_DLOG (daemon, 1570 MHD_DLOG (daemon,
1647 "Failed to create a thread: %s\n", 1571 "Failed to create a thread: %s\n",
1648 MHD_strerror_ (res_thread_create)); 1572 MHD_strerror_ (eno));
1649#endif 1573#endif
1650 goto cleanup; 1574 goto cleanup;
1651 } 1575 }
@@ -1723,9 +1647,7 @@ internal_add_connection (struct MHD_Daemon *daemon,
1723 MHD_pool_destroy (connection->pool); 1647 MHD_pool_destroy (connection->pool);
1724 free (connection->addr); 1648 free (connection->addr);
1725 free (connection); 1649 free (connection);
1726#if EINVAL
1727 errno = eno; 1650 errno = eno;
1728#endif
1729 return MHD_NO; 1651 return MHD_NO;
1730} 1652}
1731 1653
@@ -2157,7 +2079,7 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon)
2157 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && 2079 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
2158 (MHD_NO == pos->thread_joined) ) 2080 (MHD_NO == pos->thread_joined) )
2159 { 2081 {
2160 if (0 != MHD_join_thread_ (pos->pid)) 2082 if (!MHD_join_thread_ (pos->pid))
2161 { 2083 {
2162 MHD_PANIC ("Failed to join a thread\n"); 2084 MHD_PANIC ("Failed to join a thread\n");
2163 } 2085 }
@@ -3765,7 +3687,6 @@ MHD_start_daemon_va (unsigned int flags,
3765 const struct sockaddr *servaddr = NULL; 3687 const struct sockaddr *servaddr = NULL;
3766 socklen_t addrlen; 3688 socklen_t addrlen;
3767 unsigned int i; 3689 unsigned int i;
3768 int res_thread_create;
3769 int use_pipe; 3690 int use_pipe;
3770 3691
3771#ifndef HAVE_INET6 3692#ifndef HAVE_INET6
@@ -4307,13 +4228,17 @@ MHD_start_daemon_va (unsigned int flags,
4307 ( (0 != (flags & MHD_USE_SELECT_INTERNALLY)) && 4228 ( (0 != (flags & MHD_USE_SELECT_INTERNALLY)) &&
4308 (0 == daemon->worker_pool_size)) ) && 4229 (0 == daemon->worker_pool_size)) ) &&
4309 (0 == (daemon->options & MHD_USE_NO_LISTEN_SOCKET)) && 4230 (0 == (daemon->options & MHD_USE_NO_LISTEN_SOCKET)) &&
4310 (0 != (res_thread_create = 4231 (!MHD_create_named_thread_ (&daemon->pid,
4311 create_thread (&daemon->pid, daemon, &MHD_select_thread, daemon)))) 4232 (flags & MHD_USE_THREAD_PER_CONNECTION) ?
4233 "MHD-listen" : "MHD-single",
4234 daemon->thread_stack_size,
4235 &MHD_select_thread,
4236 daemon) ) )
4312 { 4237 {
4313#ifdef HAVE_MESSAGES 4238#ifdef HAVE_MESSAGES
4314 MHD_DLOG (daemon, 4239 MHD_DLOG (daemon,
4315 "Failed to create listen thread: %s\n", 4240 "Failed to create listen thread: %s\n",
4316 MHD_strerror_ (res_thread_create)); 4241 MHD_strerror_ (errno));
4317#endif 4242#endif
4318 (void) MHD_mutex_destroy_ (&daemon->cleanup_connection_mutex); 4243 (void) MHD_mutex_destroy_ (&daemon->cleanup_connection_mutex);
4319 (void) MHD_mutex_destroy_ (&daemon->per_ip_connection_mutex); 4244 (void) MHD_mutex_destroy_ (&daemon->per_ip_connection_mutex);
@@ -4416,13 +4341,16 @@ MHD_start_daemon_va (unsigned int flags,
4416 } 4341 }
4417 4342
4418 /* Spawn the worker thread */ 4343 /* Spawn the worker thread */
4419 if (0 != (res_thread_create = 4344 if (!MHD_create_named_thread_(&d->pid,
4420 create_thread (&d->pid, daemon, &MHD_select_thread, d))) 4345 "MHD-worker",
4346 daemon->thread_stack_size,
4347 &MHD_select_thread,
4348 d))
4421 { 4349 {
4422#ifdef HAVE_MESSAGES 4350#ifdef HAVE_MESSAGES
4423 MHD_DLOG (daemon, 4351 MHD_DLOG (daemon,
4424 "Failed to create pool thread: %s\n", 4352 "Failed to create pool thread: %s\n",
4425 MHD_strerror_ (res_thread_create)); 4353 MHD_strerror_ (errno));
4426#endif 4354#endif
4427 /* Free memory for this worker; cleanup below handles 4355 /* Free memory for this worker; cleanup below handles
4428 * all previously-created workers. */ 4356 * all previously-created workers. */
@@ -4564,7 +4492,7 @@ close_all_connections (struct MHD_Daemon *daemon)
4564 { 4492 {
4565 if (MHD_YES != pos->thread_joined) 4493 if (MHD_YES != pos->thread_joined)
4566 { 4494 {
4567 if (0 != MHD_join_thread_ (pos->pid)) 4495 if (!MHD_join_thread_ (pos->pid))
4568 MHD_PANIC ("Failed to join a thread\n"); 4496 MHD_PANIC ("Failed to join a thread\n");
4569 pos->thread_joined = MHD_YES; 4497 pos->thread_joined = MHD_YES;
4570 /* The thread may have concurrently modified the DLL, 4498 /* The thread may have concurrently modified the DLL,
@@ -4690,7 +4618,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon)
4690 if (1 != MHD_pipe_write_ (daemon->worker_pool[i].wpipe[1], "e", 1)) 4618 if (1 != MHD_pipe_write_ (daemon->worker_pool[i].wpipe[1], "e", 1))
4691 MHD_PANIC ("failed to signal shutdown via pipe"); 4619 MHD_PANIC ("failed to signal shutdown via pipe");
4692 } 4620 }
4693 if (0 != MHD_join_thread_ (daemon->worker_pool[i].pid)) 4621 if (!MHD_join_thread_ (daemon->worker_pool[i].pid))
4694 MHD_PANIC ("Failed to join a thread\n"); 4622 MHD_PANIC ("Failed to join a thread\n");
4695 close_all_connections (&daemon->worker_pool[i]); 4623 close_all_connections (&daemon->worker_pool[i]);
4696 (void) MHD_mutex_destroy_ (&daemon->worker_pool[i].cleanup_connection_mutex); 4624 (void) MHD_mutex_destroy_ (&daemon->worker_pool[i].cleanup_connection_mutex);
@@ -4720,7 +4648,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon)
4720 ( (0 != (daemon->options & MHD_USE_SELECT_INTERNALLY)) && 4648 ( (0 != (daemon->options & MHD_USE_SELECT_INTERNALLY)) &&
4721 (0 == daemon->worker_pool_size) ) ) 4649 (0 == daemon->worker_pool_size) ) )
4722 { 4650 {
4723 if (0 != MHD_join_thread_ (daemon->pid)) 4651 if (!MHD_join_thread_ (daemon->pid))
4724 { 4652 {
4725 MHD_PANIC ("Failed to join a thread\n"); 4653 MHD_PANIC ("Failed to join a thread\n");
4726 } 4654 }