Clarify documentation for DETACH-SOCKET
[hunchentoot.git] / lispworks.lisp
blobfb4d3c6fabbfe3daf5b0ddc6db1142ee60cb7419
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 #+(and :lispworks4.4 (or :win32 :linux))
44 (let ((id :system-cons-free-chain))
45 (unless (scm::patch-id-loaded-p id)
46 (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."
47 id (lisp-implementation-type)
48 #+:win32 "Windows"
49 #+:linux "Linux")))
51 (defvar *cleanup-interval* 100
52 "Should be NIL or a positive integer. The system calls
53 *CLEANUP-FUNCTION* whenever *CLEANUP-INTERVAL* new worker threads
54 \(counted globally across all acceptors) have been created unless the
55 value is NIL. The initial value is 100.
57 This variable is only available on LispWorks.")
59 (defvar *cleanup-function* 'cleanup-function
60 "A designator for a function without arguments which is called on a
61 regular basis if *CLEANUP-INTERVAL* is not NIL. The initial value is
62 the name of a function which invokes a garbage collection on 32-bit
63 versions of LispWorks.
65 This variable is only available on LispWorks.")
67 (defvar *worker-counter* 0
68 "Internal counter used to count worker threads. Needed for
69 *CLEANUP-FUNCTION*.")
71 (defun cleanup-function ()
72 "The default for *CLEANUP-FUNCTION*. Invokes a GC on 32-bit
73 LispWorks."
74 #-:lispworks-64bit
75 (hcl:mark-and-sweep 2))
77 (defun get-peer-address-and-port (socket)
78 "Returns the peer address and port of the socket SOCKET as two
79 values. The address is returned as a string in dotted IP address
80 notation."
81 (multiple-value-bind (peer-addr peer-port)
82 (comm:get-socket-peer-address socket)
83 (values (ignore-errors (comm:ip-address-string peer-addr)) peer-port)))
85 (defun get-local-address-and-port (socket)
86 "Returns the local address and port of the socket SOCKET as two
87 values. The address is returned as a string in dotted IP address
88 notation."
89 (multiple-value-bind (local-addr local-port)
90 (comm:get-socket-address socket)
91 (values (ignore-errors (comm:ip-address-string local-addr)) local-port)))
93 (eval-when (:compile-toplevel :load-toplevel)
94 (when (let ((sym (find-symbol "STREAM-READ-TIMEOUT" :stream)))
95 (and sym (fboundp sym)))
96 (pushnew :stream-has-timeouts *features*)))
98 (defun make-socket-stream (socket acceptor)
99 "Returns a stream for the socket SOCKET. The ACCEPTOR argument is
100 used to set the timeouts."
101 #-stream-has-timeouts
102 (when (acceptor-write-timeout acceptor)
103 (parameter-error "You need LispWorks 5 or higher for write timeouts."))
104 (make-instance 'comm:socket-stream
105 :socket socket
106 :direction :io
107 :read-timeout (acceptor-read-timeout acceptor)
108 #+stream-has-timeouts #+stream-has-timeouts
109 :write-timeout (acceptor-write-timeout acceptor)
110 :element-type 'octet))
112 (defun make-lock (name)
113 "Simple wrapper to allow LispWorks and Bordeaux Threads to coexist."
114 (mp:make-lock :name name))
116 (defmacro with-lock-held ((lock) &body body)
117 "Simple wrapper to allow LispWorks and Bordeaux Threads to coexist."
118 `(mp:with-lock (,lock) ,@body))
120 ;; some help for the IDE
121 (dspec:define-dspec-alias defvar-unbound (name)
122 `(defparameter ,name))
124 (dspec:define-dspec-alias def-http-return-code (name)
125 `(defconstant ,name))
127 (editor:setup-indent "defvar-unbound" 1 2 4)
129 (editor:setup-indent "def-http-return-code" 1 2 4)
131 (editor:setup-indent "handler-case*" 1 2 4)
133 (defun make-condition-variable (&key name)
134 (declare (ignore name))
135 (mp:make-condition-variable))
137 (defun condition-variable-signal (condition-variable)
138 (mp:condition-variable-signal condition-variable))
140 (defun condition-variable-wait (condition-variable lock)
141 (mp:condition-variable-wait condition-variable lock))