


 include/config.h |   13 +++++++++++--
 main.c           |   10 ++++++++++
 net.c            |   26 ++++++++++++++++++++++----
 3 files changed, 43 insertions(+), 6 deletions(-)

diff -puN net.c~UP-SMP-option net.c
--- cur/net.c~UP-SMP-option	2004-01-17 16:14:41.000000000 -0300
+++ cur-root/net.c	2004-01-17 16:21:11.000000000 -0300
@@ -50,21 +50,25 @@ static struct list *olocks[MAXCONN];
 
 
 /*
- * thread structures
+ * thread structures, some are only used in SMP
  */
 
+#if SMP
+
 /* thread busy indicators */
 static bool thread_busy[MAXTHREADS];
 
-/* fd busy indicator */
-static bool fd_busy[MAXCONN];
-
 /* temp space used to tell threads which fd to process */
 static unsigned int fd_to_process[MAXTHREADS];
 
 /* lock array to 'wake up' idle threads */
 static pthread_mutex_t thread_lock[MAXTHREADS];
 
+#endif
+
+/* fd busy indicator */
+static bool fd_busy[MAXCONN];
+
 
 /* 
  * private functions prototypes (publics are defined in net.h) 
@@ -87,6 +91,7 @@ int net_init(int nthreads) {
 	int i, rv;
 	struct sockaddr_in addr;
 
+#if SMP
 	/* initialize per-thread info */
 	for (i = 0; i < nthreads; i++) {
 		pthread_mutex_init(&thread_lock[i], NULL);
@@ -94,6 +99,7 @@ int net_init(int nthreads) {
 		thread_busy[i] = 0;
 		fd_to_process[i] = 0;
 	}
+#endif
 		
 	/* build the listening socket */
 	addr.sin_family = AF_INET;
@@ -169,6 +175,7 @@ void net_select_loop(int nthreads) {
 			if (!FD_ISSET(i, &readfds) || fd_busy[i])
 				continue;
 			
+#if SMP
 			/* loop looking for an idle thread; see
 			 * doc/multithread */
 			busycount = 0;
@@ -194,6 +201,11 @@ void net_select_loop(int nthreads) {
 			
 			/* if we didn't find any idle thread to assign the
 			 * job, just loop and go back to select */
+			
+#else /* UP */
+			net_proc_loop((void *) &i);
+			workdone = 1;
+#endif
 		}
 		
 		/* handle new connections */
@@ -244,12 +256,16 @@ void *net_proc_loop(void *tno) {
 	
 	tid = *((int *) tno);
 
+#if SMP
 	for (;;) {
 		/* this lock gets unlocked by net_select_loop when we have an
 		 * fd to process */
 		pthread_mutex_lock(&thread_lock[tid]);
 		
 		fd = fd_to_process[tid];
+#else /* UP */
+		fd = tid;
+#endif
 		
 		cmd = net_get_cmd(fd);
 		if (cmd == NULL)
@@ -266,12 +282,14 @@ void *net_proc_loop(void *tno) {
 		}
 		
 end_loop:
+#if SMP
 		/* mark the thread idle */
 		thread_busy[tid] = 0;
 
 		/* mark the fd idle */
 		fd_busy[fd] = 0;
 	}
+#endif
 	return NULL;
 }
 
diff -puN main.c~UP-SMP-option main.c
--- cur/main.c~UP-SMP-option	2004-01-17 16:14:41.000000000 -0300
+++ cur-root/main.c	2004-01-17 20:21:55.000000000 -0300
@@ -30,6 +30,7 @@ int main(int argc, char **argv)
 	if (argc == 1) {
 		nthreads = 1;
 	} else {
+#if SMP
 		nthreads = atoi(argv[1]);
 		if (nthreads < 1) {
 			printf("The number of threads must be at least 1\n");
@@ -43,7 +44,12 @@ int main(int argc, char **argv)
 				"allowed range (1 to %d).\n", MAXTHREADS);
 			exit(1);
 		}
+#else /* UP */
+		printf("The server was compiled to UP only and does not "
+				"support threads.\n");
+		exit(1);
 	}
+#endif
 
 
 	/* ignore SIGPIPE */
@@ -71,6 +77,7 @@ int main(int argc, char **argv)
 		exit(1);
 	}
 	
+#if SMP
 	/* create the threads */
 	pthread_t threads[nthreads];
 	for (i = 0; i < nthreads; i++) {
@@ -78,14 +85,17 @@ int main(int argc, char **argv)
 		 * is used to index several arrays inside net.c */
 		pthread_create(&threads[i], NULL, &net_proc_loop, (void *) &i);
 	}
+#endif
 
 	/* main select loop */
 	net_select_loop(nthreads);
 	
+#if SMP
 	/* wait for threads to complete */
 	for (i = 0; i < nthreads; i++) {
 		pthread_join(threads[i], NULL);
 	}
+#endif
 	
 	return 0;
 }
diff -puN include/config.h~UP-SMP-option include/config.h
--- cur/include/config.h~UP-SMP-option	2004-01-17 16:14:41.000000000 -0300
+++ cur-root/include/config.h	2004-01-17 16:16:12.000000000 -0300
@@ -14,8 +14,17 @@
 /* timeout for orphans, in seconds */
 #define ORPHAN_TIMEOUT 5
 
-/* max. number of threads */
-#define MAXTHREADS 16
+/* define to 1 if you need threading support, to 0 if you don't */
+#define SMP 0
+
+
+#if SMP
+	/* max. number of threads */
+	#define MAXTHREADS 16
+#else
+	#define MAXTHREADS 1
+#endif
+
 
 #endif
 

_
