Commit cce19c4dc7e421ab39a29065ee44d56bbaac1615

Thomas de Grivel 2017-06-19T04:59:28

Well it compiles without warning, but...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
diff --git a/package.lisp b/package.lisp
index b064139..4e82d73 100644
--- a/package.lisp
+++ b/package.lisp
@@ -15,11 +15,15 @@
 
 (defpackage :thot
   (:use
+   :babel-stream
    :bordeaux-threads
    :cffi
    :cffi-errno
    :cl-debug
+   :cl-stream
+   :fd-stream
    :common-lisp)
+  #.(cl-stream:shadowing-import-from)
   (:export
    #:request
    #:*request*
@@ -41,4 +45,4 @@
    #:end-headers
    #:content
    #:start
-   #:stop))
+   #:start-threaded))
diff --git a/thot-epoll.lisp b/thot-epoll.lisp
new file mode 100644
index 0000000..21aa89a
--- /dev/null
+++ b/thot-epoll.lisp
@@ -0,0 +1,102 @@
+
+(in-package :thot)
+
+;;  Generic epoll agent class
+
+(defclass agent ()
+  ((fd :initarg :fd
+       :reader agent-fd
+       :type (unsigned-byte 31))))
+
+(defgeneric agent-epoll-events (agent))
+(defgeneric agent-error (agent))
+(defgeneric agent-in (agent))
+(defgeneric agent-out (agent))
+
+;;  Adding an agent
+
+(defvar *epoll-fd*)
+
+(defvar *epoll-agents*
+  (make-hash-table))
+
+(defmacro get-agent (fd)
+  `(gethash ,fd *epoll-agents*))
+
+(defun set-nonblocking (fd)
+  (let ((flags (fcntl:getfl fd)))
+    (fcntl:setfl fd (logior +o-nonblock+ flags))))
+
+(defun epoll-add (agent)
+  (let ((fd (agent-fd agent)))
+    (set-nonblocking fd)
+    (setf (get-agent fd) agent)
+    (epoll:add *epoll-fd* fd
+	       (agent-epoll-events agent)
+	       :data-fd fd)))
+
+;;  Worker agent
+
+(defclass worker (agent)
+  ((stream :reader worker-stream
+	   :type stream)
+   (addr :initarg :addr
+	    :reader worker-addr)
+   (request :initform nil
+	    :accessor worker-request
+	    :type (or null request))
+   (reply :initform nil
+	  :accessor worker-reply)))
+
+(defmethod agent-epoll-events ((agent worker))
+  (logior epoll:+in+ epoll:+out+ epoll:+err+))
+
+(defmethod agent-error ((agent worker))
+  (error "worker"))
+
+(defmethod agent-in ((agent worker))
+  )
+
+(defmethod agent-out ((agent worker))
+  )
+
+;;  Acceptor agent
+
+(defclass acceptor (agent) ())
+
+(defmethod agent-epoll-events ((agent acceptor))
+  (logior epoll:+in+ epoll:+err+))
+
+(define-condition accept-error (error)
+  ((acceptor :initarg acceptor
+	     :reader accept-error-acceptor
+	     :type acceptor)))
+
+(defmethod agent-error ((agent acceptor))
+  (error 'accept-error :acceptor agent))
+
+(defmethod agent-in ((agent acceptor))
+  (multiple-value-bind (clientfd clientaddr)
+      (cffi-sockets:accept (agent-fd agent))
+    (let ((worker (make-instance 'worker :fd clientfd :addr clientaddr)))
+      (epoll-add worker))))
+
+;;  Thread event loop
+
+(defun event-loop-epoll (acceptfd)
+  (epoll:with (*epoll-fd*)
+    (let ((acceptor (make-instance 'acceptor :fd acceptfd)))
+      (epoll-add acceptor))
+    (epoll:wait (events fd *epoll-fd*)
+      (let ((agent (get-agent fd)))
+	(cond ((not (= 0 (logand epoll:+err+ events)))
+	       (agent-error agent))
+	      ((not (= 0 (logand epoll:+in+ events)))
+	       (agent-in agent))
+	      ((not (= 0 (logand epoll:+out+ events)))
+	       (agent-out agent)))))))
+
+;;  
+(defun acceptor-loop-epoll (fd)
+  (declare (type (unsigned-byte 31) fd))
+  )
diff --git a/thot-single.lisp b/thot-single.lisp
new file mode 100644
index 0000000..1c28085
--- /dev/null
+++ b/thot-single.lisp
@@ -0,0 +1,13 @@
+
+(in-package :thot)
+
+(defun acceptor-loop (fd)
+  (declare (type (unsigned-byte 31) fd))
+  (loop
+     (when *stop*
+       (return))
+     (cffi-socket:with-accept (clientfd) fd
+       (with-stream (stream (babel-io-stream (fd-io-stream clientfd)))
+         (request-loop stream)))))
+
+(setq *acceptor-loop* 'acceptor-loop)
diff --git a/thot-threaded.lisp b/thot-threaded.lisp
new file mode 100644
index 0000000..7ddc2ee
--- /dev/null
+++ b/thot-threaded.lisp
@@ -0,0 +1,54 @@
+
+(in-package :thot)
+
+(defvar *listen-fd*)
+
+(defun worker-thread ()
+  ;(format t "~&WORKER THREAD~%")
+  (loop
+     (when *stop*
+       (return))
+     (cffi-socket:with-accept (clientfd) *listen-fd*
+       (with-stream (stream (babel-io-stream (fd-io-stream clientfd)))
+         (request-loop stream)))))
+
+(defparameter *init-threads* 8)
+
+(defvar *worker-threads*)
+(defvar *worker-sockfds*)
+
+(defun init-worker-threads (n)
+  (loop
+     (when (<= n (length *worker-threads*))
+       (return))
+     (let ((thread (bordeaux-threads:make-thread 'worker-thread
+						 :name "worker")))
+       (push thread *worker-threads*))))
+
+(defun join-worker-threads ()
+  (setq *stop* t)
+  (bordeaux-set:set-each (lambda (sockfd)
+			   (cffi-socket:shutdown sockfd t t))
+			 *worker-sockfds*)
+  (loop
+     (when (endp *worker-threads*)
+       (return))
+     (let ((thread (pop *worker-threads*)))
+       (bordeaux-threads:join-thread thread))))
+
+(defmacro with-worker-threads (count &body body)
+  `(let ((*worker-threads* ())
+	 (*worker-sockfds* (make-instance 'bordeaux-set:set)))
+     (init-worker-threads ,count)
+     (unwind-protect (progn ,@body)
+       (join-worker-threads))))
+
+(defun acceptor-loop-threaded (fd)
+  (declare (type (unsigned-byte 31) fd))
+  (setq *stop* nil
+        *listen-fd* fd)
+  (with-worker-threads *init-threads*
+    (worker-thread)))
+
+(when bordeaux-threads:*supports-threads-p*
+  (setq *acceptor-loop* 'acceptor-loop-threaded))
diff --git a/thot.asd b/thot.asd
index a91c913..c828758 100644
--- a/thot.asd
+++ b/thot.asd
@@ -8,10 +8,16 @@
 
 (defsystem "thot"
   :depends-on ("babel"
+	       "bordeaux-queue"
+	       "bordeaux-set"
 	       "bordeaux-threads"
-	       "cffi-posix"
-	       "cffi-sockets-flexi"
-	       "cl-debug")
+               "babel-stream"
+               "cffi-socket"
+	       "cl-debug"
+               "cl-stream"
+               "fd-stream")
   :components
   ((:file "package")
-   (:file "thot" :depends-on ("package"))))
+   (:file "thot" :depends-on ("package"))
+   (:file "thot-single" :depends-on ("thot"))
+   (:file "thot-threaded" :depends-on ("thot-single"))))
diff --git a/thot.lisp b/thot.lisp
index 126b1f6..6658e3c 100644
--- a/thot.lisp
+++ b/thot.lisp
@@ -1,93 +1,50 @@
 
 (in-package :thot)
 
-(setf (debug-p :thot) t)
+(setf (debug-p :thot) nil)
 
 (eval-when (:compile-toplevel :load-toplevel :execute)
   (unless (boundp '+crlf+)
     (defconstant +crlf+
       (coerce '(#\Return #\Newline) 'string))))
 
-(defun read-line-crlf (stream)
-  (with-output-to-string (out)
-    (let ((cr))
-      (loop
-	 (let ((c (read-char stream)))
-	   (when cr
-	     (when (char= #\Newline c)
-	       (return))
-	     (write-char cr out)
-	     (setf cr nil))
-	   (if (char= #\Return c)
-	       (setf cr c)
-	       (write-char c out)))))))
-
-(defun read-until (end-char stream)
-  "Reads stream into a string until END-CHAR is read.
-END-CHAR is not returned as part of the string."
-  (with-output-to-string (out)
-    (loop
-       (let ((c (read-char stream)))
-	 (when (char= end-char c)
-	   (return))
-	 (write-char c out)))))
+;;  Request
 
 (defclass request ()
-  ((socket :initarg :socket
-	   :reader request-socket%
+  ((stream :initarg :stream
+	   :reader request-stream%
 	   :type stream)
    (method :initarg :method
-	   :reader request-method%
+	   :accessor request-method%
 	   :type symbol)
-   (url :initarg :url
-	:reader request-url%
-	:type string)
-   (uri :initform nil
-	:accessor request-uri%
-	:type string)
-   (query :initform nil
-	  :accessor request-query%
-	  :type string)
+   (target :initarg :target
+           :accessor request-target%
+           :type string)
    (http-version :initarg :http-version
-		 :reader request-http-version%
-		 :type string)
+                 :accessor request-http-version%
+                 :type string)
    (headers :initform (make-hash-table :test 'equalp :size 32)
 	    :reader request-headers%
 	    :type hash-table)
-   (data :initform nil
-	 :accessor request-data%
-	 :type string)))
+   (uri :accessor request-uri%
+	:type string)
+   (query :initform nil
+	  :accessor request-query%
+	  :type string)))
 
 (defvar *request*)
 
-(defun request-socket (&optional (request *request*))
+(defun request-stream (&optional (request *request*))
   (declare (type request request))
-  (request-socket% request))
+  (request-stream% request))
 
 (defun request-method (&optional (request *request*))
   (declare (type request request))
   (request-method% request))
 
-(defun request-url (&optional (request *request*))
+(defun request-target (&optional (request *request*))
   (declare (type request request))
-  (request-url% request))
-
-(defun split-uri-query (&optional (request *request*))
-  (let* ((url (thot:request-url request))
-	 (url-? (position #\? url))
-	 (uri (if url-?
-		  (subseq url 0 url-?)
-		  url))
-	 (query (if url-?
-		    (subseq url url-?)
-		    nil)))
-    (setf (request-uri% request) uri
-	  (request-query% request) query)))
-  
-(defun request-uri (&optional (request *request*))
-  (unless (request-uri% request)
-    (split-uri-query request)
-    (request-uri% request)))
+  (request-target% request))
 
 (defun request-http-version (&optional (request *request*))
   (declare (type request request))
@@ -97,50 +54,129 @@ END-CHAR is not returned as part of the string."
   (declare (type request request))
   (request-headers% request))
 
-(defun request-header (name &optional (request *request*))
-  (gethash name (request-headers request)))
+(defun split-request-uri-and-query (request)
+  (declare (type request request))
+  (let* ((target (request-target% request))
+	 (target-? (position #\? target))
+	 (uri (if target-?
+		  (subseq target 0 target-?)
+		  target))
+	 (query (if target-?
+		    (subseq target target-?)
+		    nil)))
+    (setf (request-uri% request) uri
+	  (request-query% request) query)))
+
+(defun request-uri (&optional (request *request*))
+  (declare (type request request))
+  (unless (slot-boundp request 'uri)
+    (split-request-uri-and-query request))
+  (slot-value request 'uri))
+
+(defun request-query (&optional (request *request*))
+  (declare (type request request))
+  (unless (slot-boundp request 'query)
+    (split-request-uri-and-query request))
+  (slot-value request 'query))
+
+(defun request-header (header-name &optional (request *request*))
+  (declare (type request request))
+  (gethash header-name (request-headers% request)))
+
+(defsetf request-header (header-name &optional (request '*request*)) (value)
+  `(setf (gethash ,header-name (request-headers ,request)) ,value))
 
 (defun request-content-length (&optional (request *request*))
   (parse-integer (request-header "Content-Length" request)))
 
-(defun request-data (&optional (request *request*))
-  (let* ((length (request-content-length request))
-	 (data (make-array length :element-type '(unsigned-byte 8)))
-	 (socket (request-socket request))
-	 (encoding (flexi-streams:flexi-stream-external-format socket)))
-    (read-sequence data socket)
-    (babel:octets-to-string data :encoding encoding)))
-    
-(defun read-spaces (socket)
-  (loop
-     (unless (char= #\Space (peek-char nil socket))
-       (return))
-     (read-char socket)))
-
-(defun read-header (socket headers)
-  (unless (and (char= #\Return (peek-char nil socket))
-	       (read-char socket)
-	       (char= #\Newline (read-char socket)))
-    (let ((name (read-until #\: socket)))
-      (read-spaces socket)
-      (let ((value (read-line-crlf socket)))
-	(when (debug-p (or :thot :http))
-	  (format t "~&thot: ~A: ~A~%" name value))
-	(setf (gethash name headers) value)))))
-
-(defun read-request (socket)
-  (let* ((method (find-symbol (read-until #\Space socket) :http-method))
-	 (url (read-until #\Space socket))
-	 (http-version (read-line-crlf socket))
-	 (request (make-instance 'request :socket socket :method method
-				 :url url :http-version http-version))
-	 (headers (request-headers request)))
-    (when (debug-p (or :thot :http))
-      (format t "~&thot: ~A ~A ~A~%" method url http-version))
-    (loop
-       (unless (read-header socket headers)
-	 (return)))
-    request))
+;;  HTTP parser
+
+(defmacro with-readers-for (stream definitions &body body)
+  (declare (type symbol stream))
+  (flet ((reader (definition)
+           (destructuring-bind (name (element) &rest body) definition
+             (declare (type symbol name element))
+             (let ((state (gensym "STATE-")))
+               `(,name ()
+                  (multiple-value-bind (,element ,state) (read ,stream)
+                    (case ,state
+                      ((nil) ,@body)
+                      ((:eof) :eof)
+                      ((:non-blocking) #',name)
+                      (otherwise (error 'stream-input-error
+                                        :stream ,stream)))))))))
+    `(labels ,(mapcar #'reader definitions)
+       ,@body)))
+
+(defun request-reader (stream cont)
+  (let ((*request* (make-instance 'request))
+        (buffer (string-output-stream))
+        (name "")
+        (value ""))
+    (flet ((get-buffer ()
+             (prog1 (string-output-stream-string buffer)
+               (sequence-output-stream-reset buffer))))
+      (with-readers-for stream
+          ((method (char)
+             (cond ((char= #\Space char)
+                    (setf (request-method% *request*) (get-buffer))
+                    (target))
+                   (t (write buffer char)
+                      (method))))
+           (target (char)
+             (cond ((char= #\Space char)
+                    (setf (request-target% *request*) (get-buffer))
+                    (version))
+                   (t (write buffer char)
+                      (target))))
+           (version (char)
+             (cond ((char= #\Return char)
+                    (setf (request-http-version% *request*) (get-buffer))
+                    (version-lf))
+                   (t (write buffer char)
+                      (version))))
+           (version-lf (char)
+             (cond ((char= #\Newline char)
+                    (when (debug-p (or :thot :http))
+                      (format t "~&thot: ~A ~A ~A~%"
+                              (request-method)
+                              (request-target)
+                              (request-http-version)))
+                    (next-header))
+                   (t (error "Missing request line LF"))))
+           (next-header (char)
+             (cond ((char= #\Return char) (end-of-headers))
+                   (t (write buffer char)
+                      (header-name))))
+           (header-name (char)
+             (cond ((char= #\: char)
+                    (setq name (get-buffer))
+                    (header-spaces))
+                   (t (write buffer char)
+                      (header-name))))
+           (header-spaces (char)
+             (cond ((char= #\Space char) (header-spaces))
+                   (t (write buffer char)
+                      (header-value))))
+           (header-value (char)
+             (cond ((char= #\Return char)
+                    (setq value (get-buffer))
+                    (header-lf))
+                   (t (write buffer char)
+                      (header-value))))
+           (header-lf (char)
+             (cond ((char= #\Newline char)
+                    (when (debug-p (or :thot :http))
+                      (format t "~&thot: ~A: ~A~%" name value))
+                    (setf (request-header name *request*) value)
+                    (next-header))
+                   (t (error "Missing header LF"))))
+           (end-of-headers (char)
+             (cond ((char= #\Newline char) (funcall cont))
+                   (t (error "Missing end of headers LF")))))
+        #'method))))
+
+;;  Reply
 
 (defclass reply ()
   ((status :initform nil
@@ -148,7 +184,8 @@ END-CHAR is not returned as part of the string."
    (headers :initform nil
 	    :accessor reply-headers%)
    (headers-sent :initform nil
-		 :accessor reply-headers-sent%)))
+		 :accessor reply-headers-sent%
+                 :type boolean)))
 
 (defvar *reply*)
 
@@ -188,7 +225,11 @@ END-CHAR is not returned as part of the string."
     (when status
       (error 'status-already-sent status line)))
   (setf (reply-status) line)
-  (format (request-socket) "~A ~A~A" (request-http-version) line +crlf+))
+  (let ((stream (request-stream)))
+    (write-sequence stream (request-http-version))
+    (write stream #\Space)
+    (write-sequence stream line)
+    (write-sequence stream +crlf+)))
 
 (defun header (line)
   (unless (reply-status)
@@ -201,7 +242,9 @@ END-CHAR is not returned as part of the string."
 	     (setf (rest headers) (list line))
 	     (return))
 	   (pop headers))))
-  (format (request-socket) "~A~A" line +crlf+))
+  (let ((stream (request-stream)))
+    (write-sequence stream line)
+    (write-sequence stream +crlf+)))
 
 (defun end-headers ()
   (unless (reply-headers-sent)
@@ -210,7 +253,7 @@ END-CHAR is not returned as part of the string."
 
 (defun content (string)
   (end-headers)
-  (write-string string (request-socket)))
+  (write-sequence (request-stream) string))
 
 (defun 404-not-found ()
   (status "404 Not found")
@@ -218,7 +261,7 @@ END-CHAR is not returned as part of the string."
   (content (format nil "404 Not found
   
 The requested url ~S was not found on this server."
-		   (request-url *request*))))
+		   (request-target))))
 
 (defun 404-not-found-handler ()
   '404-not-found)
@@ -226,9 +269,8 @@ The requested url ~S was not found on this server."
 (defvar *url-handlers*
   '(404-not-found-handler))
 
-(defun handle-request (request)
+(defun handle-request ()
   (let ((handlers *url-handlers*)
-	(*request* request)
 	(*reply* (make-instance 'reply)))
     (loop
        (when (endp handlers)
@@ -239,103 +281,28 @@ The requested url ~S was not found on this server."
 	   (when (debug-p (or :thot :http))
 	     (format t "~&~S -> ~S~%" handler-func handler))
 	   (funcall handler)
-	   (return))))))
+	   (return))))
+    (if (string-equal "keep-alive" (request-header 'connection))
+        :keep-alive
+        nil)))
 
-(defun request-loop (socket)
-  (loop
-     (let ((request (read-request socket)))
-       (unless request
+(defvar *stop* nil)
+
+(defun request-loop (stream)
+  (ignore-errors
+    (loop
+       (when *stop*
 	 (return))
-       (handle-request request)
-       (unless (string-equal "keep-alive" (request-header 'connection request))
-	 (return)))))
+       (unless (eq :keep-alive
+                   (funcall (request-reader stream #'handle-request)))
+         (return)))))
 
-(defun acceptor-loop (fd)
-  (declare (type (unsigned-byte 31) fd))
-  (loop
-     (cffi-sockets:with-accept (clientfd fd)
-       (cffi-sockets-flexi:with-socket-stream (socket clientfd)
-	 (request-loop socket)))))
+(defvar *acceptor-loop*)
 
 (defun start (&key (address "0.0.0.0") (port 8000))
-  (cffi-sockets:with-socket (fd cffi-sockets:+af-inet+
-				cffi-sockets:+sock-stream+
-				0)
-    (cffi-sockets:bind-inet fd address port)
-    (cffi-sockets:listen-sock fd 128)
-    (acceptor-loop fd)))
-
-(defvar *pipe-in* -1)
-(defvar *pipe-out* -1)
-
-(defun read-fd ()
-  (with-foreign-object (fd :int)
-    (let ((r (cffi-posix:c-read *pipe-in* fd (foreign-type-size :int))))
-      (when (< r 0)
-	(error-errno "read"))
-      (when (< 0 r)
-	(mem-aref fd :int)))))
-
-(defun write-fd (fd)
-  (with-foreign-object (fd% :int)
-    (setf (mem-aref fd% :int) fd)
-    (let ((w (cffi-posix:c-write *pipe-out* fd% (foreign-type-size :int))))
-      (when (< w 0)
-	(error-errno "write"))
-      (when (< 0 w)
-	fd))))
-
-(defun worker-thread ()
-  ;(format t "~&WORKER THREAD~%")
-  (loop
-     (let ((sockfd (read-fd)))
-       (unless sockfd
-	 (return))
-       (cffi-sockets-flexi:with-socket-stream (socket sockfd)
-	 (request-loop socket)))))
-
-(defparameter *init-threads* 8)
-
-(defvar *threads*)
-
-(defun init-threads (n)
-  (loop
-     (when (<= n (length *threads*))
-       (return))
-     (let* ((*thread-id* (decf n))
-	    (thread (bordeaux-threads:make-thread 'worker-thread)))
-       (push thread *threads*))))
-
-(defun join-threads ()
-  (loop
-     (when (endp *threads*)
-       (return))
-     (let ((thread (pop *threads*)))
-       (bordeaux-threads:join-thread thread))))
-
-(defmacro with-threads (n &body body)
-  `(let ((*threads* ()))
-     (init-threads ,n)
-     (unwind-protect (progn ,@body)
-       (join-threads))))
-
-(defun acceptor-loop-threaded (fd)
-  (declare (type (unsigned-byte 31) fd))
-  (loop
-     (let ((clientfd (cffi-sockets:accept fd)))
-       (write-fd clientfd))))
-
-(defun start-threaded (&key (address "0.0.0.0") (port 8000))
-  (cffi-sockets:with-socket (fd cffi-sockets:+af-inet+
-				cffi-sockets:+sock-stream+
-				0)
-    (cffi-sockets:bind-inet fd address port)
-    (cffi-sockets:listen-sock fd 128)
-    (cffi-posix:with-pipe (in out)
-      (setq *pipe-in* in *pipe-out* out)
-      (with-threads *init-threads*
-	(acceptor-loop-threaded fd)))))
-
-(untrace read-fd write-fd worker-thread init-threads join-threads acceptor-loop-threaded)
-(untrace cffi-sockets:socket cffi-sockets:accept)
-(untrace cffi-posix:pipe cffi-posix:close)
+  (cffi-socket:with-socket (fd cffi-socket:+af-inet+
+                               cffi-socket:+sock-stream+
+                               0)
+    (cffi-socket:bind-inet fd address port)
+    (cffi-socket:listen fd 128)
+    (funcall *acceptor-loop* fd)))