Commit | Line | Data |
b682daf3 |
1 | /* |
5a7cc494 |
2 | * threaded.c -- A simple multi-threaded FastCGI application. |
3 | */ |
4 | |
5 | #ifndef lint |
74e53736 |
6 | static const char rcsid[] = "$Id: threaded.c,v 1.8 1999/08/15 19:32:20 roberts Exp $"; |
5a7cc494 |
7 | #endif /* not lint */ |
8 | |
b682daf3 |
9 | #include "fcgi_config.h" |
10 | |
11 | #include <pthread.h> |
d06e1107 |
12 | #include <sys/types.h> |
b682daf3 |
13 | |
14 | #ifdef HAVE_UNISTD_H |
5a7cc494 |
15 | #include <unistd.h> |
16 | #endif |
17 | |
b682daf3 |
18 | #include "fcgiapp.h" |
19 | |
5a7cc494 |
20 | |
21 | #define THREAD_COUNT 20 |
22 | |
de53af2a |
23 | static int counts[THREAD_COUNT]; |
5a7cc494 |
24 | |
25 | static void *doit(void *a) |
26 | { |
de53af2a |
27 | int rc, i, thread_id = (int)a; |
3fcee697 |
28 | pid_t pid = getpid(); |
5a7cc494 |
29 | FCGX_Request request; |
b682daf3 |
30 | char *server_name; |
5a7cc494 |
31 | |
0b7c9662 |
32 | FCGX_InitRequest(&request, 0, 0); |
5a7cc494 |
33 | |
de53af2a |
34 | for (;;) |
5a7cc494 |
35 | { |
de53af2a |
36 | static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER; |
37 | static pthread_mutex_t counts_mutex = PTHREAD_MUTEX_INITIALIZER; |
38 | |
3fcee697 |
39 | /* Some platforms require accept() serialization, some don't.. */ |
de53af2a |
40 | pthread_mutex_lock(&accept_mutex); |
0b7c9662 |
41 | rc = FCGX_Accept_r(&request); |
de53af2a |
42 | pthread_mutex_unlock(&accept_mutex); |
43 | |
44 | if (rc < 0) |
45 | break; |
46 | |
8a7135ba |
47 | server_name = FCGX_GetParam("SERVER_NAME", request.envp); |
5a7cc494 |
48 | |
8a7135ba |
49 | FCGX_FPrintF(request.out, |
b682daf3 |
50 | "Content-type: text/html\r\n" |
51 | "\r\n" |
52 | "<title>FastCGI Hello! (multi-threaded C, fcgiapp library)</title>" |
53 | "<h1>FastCGI Hello! (multi-threaded C, fcgiapp library)</h1>" |
3fcee697 |
54 | "Thread %d, Process %ld<p>" |
b682daf3 |
55 | "Request counts for %d threads running on host <i>%s</i><p><code>", |
3fcee697 |
56 | thread_id, pid, THREAD_COUNT, server_name ? server_name : "?"); |
57 | |
58 | sleep(2); |
5a7cc494 |
59 | |
de53af2a |
60 | pthread_mutex_lock(&counts_mutex); |
61 | ++counts[thread_id]; |
5a7cc494 |
62 | for (i = 0; i < THREAD_COUNT; i++) |
8a7135ba |
63 | FCGX_FPrintF(request.out, "%5d " , counts[i]); |
de53af2a |
64 | pthread_mutex_unlock(&counts_mutex); |
65 | |
66 | FCGX_Finish_r(&request); |
5a7cc494 |
67 | } |
68 | |
69 | return NULL; |
70 | } |
71 | |
72 | int main(void) |
73 | { |
74 | int i; |
75 | pthread_t id[THREAD_COUNT]; |
76 | |
77 | FCGX_Init(); |
78 | |
5a7cc494 |
79 | for (i = 1; i < THREAD_COUNT; i++) |
80 | pthread_create(&id[i], NULL, doit, (void*)i); |
b682daf3 |
81 | |
5a7cc494 |
82 | doit(0); |
83 | |
84 | exit(0); |
85 | } |
86 | |