prepare for release 1.2.12
[hunchentoot.git] / lispworks.lisp
blob4eec6aeaefee5c6421a84d8cc32d7266b78252b5
1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: HUNCHENTOOT; Base: 10 -*-
3 ;;; Copyright (c) 2004-2010, Dr. Edmund Weitz. All rights reserved.
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
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.
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.
29 (in-package :hunchentoot)
31 (eval-when (:compile-toplevel :load-toplevel :execute)
32 ;; make sure socket code is loaded
33 (require "comm"))
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 "/"))))))
43 (defmacro with-rebinding (bindings &body body)
44 "Renaming LW:REBINDING for better indentation."
45 `(lw:rebinding ,bindings ,@body))
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")))
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.
61 This variable is only available on LispWorks.")
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.
69 This variable is only available on LispWorks.")
71 (defvar *worker-counter* 0
72 "Internal counter used to count worker threads. Needed for
73 *CLEANUP-FUNCTION*.")
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))
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)))
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)))
97 (defun make-socket-stream (socket acceptor)
98 "Returns a stream for the socket SOCKET. The ACCEPTOR argument is
99 used to set the timeouts."
100 #-(or :lispworks5 :lispworks6)
101 (when (acceptor-write-timeout acceptor)
102 (parameter-error "You need LispWorks 5 or higher for write timeouts."))
103 (make-instance 'comm:socket-stream
104 :socket socket
105 :direction :io
106 :read-timeout (acceptor-read-timeout acceptor)
107 #+(or :lispworks5 :lispworks6) #+(or :lispworks5 :lispworks6)
108 :write-timeout (acceptor-write-timeout acceptor)
109 :element-type 'octet))
111 (defun make-lock (name)
112 "Simple wrapper to allow LispWorks and Bordeaux Threads to coexist."
113 (mp:make-lock :name name))
115 (defmacro with-lock-held ((lock) &body body)
116 "Simple wrapper to allow LispWorks and Bordeaux Threads to coexist."
117 `(mp:with-lock (,lock) ,@body))
119 ;; some help for the IDE
120 (dspec:define-dspec-alias defvar-unbound (name)
121 `(defparameter ,name))
123 (dspec:define-dspec-alias def-http-return-code (name)
124 `(defconstant ,name))
126 (editor:setup-indent "defvar-unbound" 1 2 4)
128 (editor:setup-indent "def-http-return-code" 1 2 4)
130 (editor:setup-indent "handler-case*" 1 2 4)
132 (defun make-condition-variable (&key name)
133 (declare (ignore name))
134 (mp:make-condition-variable))
136 (defun condition-variable-signal (condition-variable)
137 (mp:condition-variable-signal condition-variable))
139 (defun condition-variable-wait (condition-variable lock)
140 (mp:condition-variable-wait condition-variable lock))