lispworks.lisp 1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: HUNCHENTOOT; Base: 10 -*-
2
3 ;;; Copyright (c) 2004-2010, Dr. Edmund Weitz. All rights reserved.
4
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
8
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
11
12 ;;; * Redistributions in binary form must reproduce the above
13 ;;; copyright notice, this list of conditions and the following
14 ;;; disclaimer in the documentation and/or other materials
15 ;;; provided with the distribution.
16
17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 (in-package :hunchentoot)
30
31 (eval-when (:compile-toplevel :load-toplevel :execute)
32 ;; make sure socket code is loaded
33 (require "comm"))
34
35 (defun get-env-variable-as-directory (name)
36 "Retrieves the environment variable named NAME and interprets it as
37 the pathname of a directory which is returned."
38 (lw:when-let (string (lw:environment-variable name))
39 (when (plusp (length string))
40 (cond ((find (char string (1- (length string))) "\\/" :test #'char=) string)
41 (t (lw:string-append string "/"))))))
42
43 (defmacro with-rebinding (bindings &body body)
44 "Renaming LW:REBINDING for better indentation."
45 `(lw:rebinding ,bindings ,@body))
46
47 #+(and :lispworks4.4 (or :win32 :linux))
48 (let ((id :system-cons-free-chain))
49 (unless (scm::patch-id-loaded-p id)
50 (error "You need a patch to improve the performance of this code. Request patch ~S for ~A for ~A from lisp-support@lispworks.com using the Report Bug command."
51 id (lisp-implementation-type)
52 #+:win32 "Windows"
53 #+:linux "Linux")))
54
55 (defvar *cleanup-interval* 100
56 "Should be NIL or a positive integer. The system calls
57 *CLEANUP-FUNCTION* whenever *CLEANUP-INTERVAL* new worker threads
58 \(counted globally across all acceptors) have been created unless the
59 value is NIL. The initial value is 100.
60
61 This variable is only available on LispWorks.")
62
63 (defvar *cleanup-function* 'cleanup-function
64 "A designator for a function without arguments which is called on a
65 regular basis if *CLEANUP-INTERVAL* is not NIL. The initial value is
66 the name of a function which invokes a garbage collection on 32-bit
67 versions of LispWorks.
68
69 This variable is only available on LispWorks.")
70
71 (defvar *worker-counter* 0
72 "Internal counter used to count worker threads. Needed for
73 *CLEANUP-FUNCTION*.")
74
75 (defun cleanup-function ()
76 "The default for *CLEANUP-FUNCTION*. Invokes a GC on 32-bit
77 LispWorks."
78 #-:lispworks-64bit
79 (hcl:mark-and-sweep 2))
80
81 (defun get-peer-address-and-port (socket)
82 "Returns the peer address and port of the socket SOCKET as two
83 values. The address is returned as a string in dotted IP address
84 notation."
85 (multiple-value-bind (peer-addr peer-port)
86 (comm:get-socket-peer-address socket)
87 (values (ignore-errors (comm:ip-address-string peer-addr)) peer-port)))
88
89 (defun get-local-address-and-port (socket)
90 "Returns the local address and port of the socket SOCKET as two
91 values. The address is returned as a string in dotted IP address
92 notation."
93 (multiple-value-bind (local-addr local-port)
94 (comm:get-socket-address socket)
95 (values (ignore-errors (comm:ip-address-string local-addr)) local-port)))
96
97 (eval-when (:compile-toplevel :load-toplevel)
98 (when (let ((sym (find-symbol "STREAM-READ-TIMEOUT" :stream)))
99 (and sym (fboundp sym)))
100 (pushnew :stream-has-timeouts *features*)))
101
102 (defun make-socket-stream (socket acceptor)
103 "Returns a stream for the socket SOCKET. The ACCEPTOR argument is
104 used to set the timeouts."
105 #-stream-has-timeouts
106 (when (acceptor-write-timeout acceptor)
107 (parameter-error "You need LispWorks 5 or higher for write timeouts."))
108 (make-instance 'comm:socket-stream
109 :socket socket
110 :direction :io
111 :read-timeout (acceptor-read-timeout acceptor)
112 #+stream-has-timeouts #+stream-has-timeouts
113 :write-timeout (acceptor-write-timeout acceptor)
114 :element-type 'octet))
115
116 (defun make-lock (name)
117 "Simple wrapper to allow LispWorks and Bordeaux Threads to coexist."
118 (mp:make-lock :name name))
119
120 (defmacro with-lock-held ((lock) &body body)
121 "Simple wrapper to allow LispWorks and Bordeaux Threads to coexist."
122 `(mp:with-lock (,lock) ,@body))
123
124 ;; some help for the IDE
125 (dspec:define-dspec-alias defvar-unbound (name)
126 `(defparameter ,name))
127
128 (dspec:define-dspec-alias def-http-return-code (name)
129 `(defconstant ,name))
130
131 (editor:setup-indent "defvar-unbound" 1 2 4)
132
133 (editor:setup-indent "def-http-return-code" 1 2 4)
134
135 (editor:setup-indent "handler-case*" 1 2 4)
136
137 (defun make-condition-variable (&key name)
138 (declare (ignore name))
139 (mp:make-condition-variable))
140
141 (defun condition-variable-signal (condition-variable)
142 (mp:condition-variable-signal condition-variable))
143
144 (defun condition-variable-wait (condition-variable lock)
145 (mp:condition-variable-wait condition-variable lock))