changeset 1522:4d0b80dd4c37

Introduce URWEB_STACK_SIZE environment variable (based on a patch by Hao Deng)
author Adam Chlipala <adam@chlipala.net>
date Tue, 02 Aug 2011 14:31:37 -0400
parents 001638622c4f
children 52fbd8534ef3
files doc/manual.tex include/request.h src/c/fastcgi.c src/c/http.c src/c/request.c tests/overflow.ur tests/overflow.urp tests/overflow.urs
diffstat 8 files changed, 58 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/doc/manual.tex	Tue Aug 02 13:48:26 2011 -0400
+++ b/doc/manual.tex	Tue Aug 02 14:31:37 2011 -0400
@@ -321,6 +321,15 @@
 
 A word of warning: as for demo generation, tutorial generation calls Emacs to syntax-highlight Ur code.
 
+\subsection{Run-Time Options}
+
+Compiled applications consult a few environment variables to modify their behavior:
+
+\begin{itemize}
+  \item \cd{URWEB\_NUM\_THREADS}: alternative to the \cd{-t} command-line argument (currently used only by FastCGI)
+  \item \cd{URWEB\_STACK\_SIZE}: size of per-thread stacks, in bytes
+\end{itemize}
+
 
 \section{Ur Syntax}
 
--- a/include/request.h	Tue Aug 02 13:48:26 2011 -0400
+++ b/include/request.h	Tue Aug 02 14:31:37 2011 -0400
@@ -32,4 +32,6 @@
 
 void *client_pruner(void *data);
 
+int pthread_create_big(pthread_t *outThread, void *foo, void *threadFunc, void *arg);
+
 #endif
--- a/src/c/fastcgi.c	Tue Aug 02 13:48:26 2011 -0400
+++ b/src/c/fastcgi.c	Tue Aug 02 14:31:37 2011 -0400
@@ -569,7 +569,7 @@
   {
     pthread_t thread;
 
-    if (pthread_create(&thread, NULL, client_pruner, &ls)) {
+    if (pthread_create_big(&thread, NULL, client_pruner, &ls)) {
       fprintf(stderr, "Error creating pruner thread\n");
       return 1;
     }
@@ -578,7 +578,7 @@
   for (i = 0; i < nthreads; ++i) {
     pthread_t thread;    
     names[i] = i;
-    if (pthread_create(&thread, NULL, worker, &names[i])) {
+    if (pthread_create_big(&thread, NULL, worker, &names[i])) {
       fprintf(stderr, "Error creating worker thread #%d\n", i);
       return 1;
     }
--- a/src/c/http.c	Tue Aug 02 13:48:26 2011 -0400
+++ b/src/c/http.c	Tue Aug 02 14:31:37 2011 -0400
@@ -311,7 +311,7 @@
   {
     pthread_t thread;
 
-    if (pthread_create(&thread, NULL, client_pruner, &ls)) {
+    if (pthread_create_big(&thread, NULL, client_pruner, &ls)) {
       fprintf(stderr, "Error creating pruner thread\n");
       return 1;
     }
@@ -320,7 +320,7 @@
   for (i = 0; i < nthreads; ++i) {
     pthread_t thread;    
     names[i] = i;
-    if (pthread_create(&thread, NULL, worker, &names[i])) {
+    if (pthread_create_big(&thread, NULL, worker, &names[i])) {
       fprintf(stderr, "Error creating worker thread #%d\n", i);
       return 1;
     }
--- a/src/c/request.c	Tue Aug 02 13:48:26 2011 -0400
+++ b/src/c/request.c	Tue Aug 02 14:31:37 2011 -0400
@@ -123,12 +123,40 @@
   };
 }
 
+static unsigned long long stackSize;
+
+int pthread_create_big(pthread_t *outThread, void *foo, void *threadFunc, void *arg)
+{
+  int err;
+  pthread_attr_t stackSizeAttribute;
+
+  err = pthread_attr_init(&stackSizeAttribute);
+  if (err) return err; 
+
+  if (stackSize > 0) {
+    err = pthread_attr_setstacksize(&stackSizeAttribute, stackSize);
+    if (err) return err;
+  }
+
+  return pthread_create(outThread, &stackSizeAttribute, threadFunc, arg);
+}
+
 void uw_request_init(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) {
   uw_context ctx;
   failure_kind fk;
   uw_periodic *ps;
   loggers *ls = malloc(sizeof(loggers));
   int id;
+  char *stackSize_s;
+
+  if ((stackSize_s = getenv("URWEB_STACK_SIZE")) != NULL && stackSize_s[0] != 0) {
+    stackSize = atoll(stackSize_s);
+
+    if (stackSize <= 0) {
+      fprintf(stderr, "Invalid stack size \"%s\"\n", stackSize_s);
+      exit(1);
+    }
+  }
 
   ls->app = app;
   ls->logger_data = logger_data;
@@ -141,7 +169,7 @@
   {
     pthread_t thread;
     
-    if (uw_time_max && pthread_create(&thread, NULL, ticker, NULL)) {
+    if (uw_time_max && pthread_create_big(&thread, NULL, ticker, NULL)) {
       fprintf(stderr, "Error creating ticker thread\n");
       exit(1);
     }
@@ -174,7 +202,7 @@
     arg->ls = ls;
     arg->pdic = *ps;
     
-    if (pthread_create(&thread, NULL, periodic_loop, arg)) {
+    if (pthread_create_big(&thread, NULL, periodic_loop, arg)) {
       fprintf(stderr, "Error creating periodic thread\n");
       exit(1);
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/overflow.ur	Tue Aug 02 14:31:37 2011 -0400
@@ -0,0 +1,8 @@
+fun makeList n = if n = 0 then [] else 1 :: makeList (n - 1)
+
+fun doit {N = n} = return <xml><body>{[List.length (makeList (readError n))]}</body></xml>
+
+fun main () =
+    return <xml><body>
+      <form> <textbox{#N}/> <submit action={doit}/> </form>
+    </body></xml>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/overflow.urp	Tue Aug 02 14:31:37 2011 -0400
@@ -0,0 +1,4 @@
+rewrite all Overflow/*
+
+$/list
+overflow
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/overflow.urs	Tue Aug 02 14:31:37 2011 -0400
@@ -0,0 +1,1 @@
+val main : unit -> transaction page