forked from edicl/hunchentoot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lispworks.lisp
executable file
·141 lines (112 loc) · 5.54 KB
/
lispworks.lisp
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
;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: HUNCHENTOOT; Base: 10 -*-
;;; Copyright (c) 2004-2010, Dr. Edmund Weitz. All rights reserved.
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;; * Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;; * Redistributions in binary form must reproduce the above
;;; copyright notice, this list of conditions and the following
;;; disclaimer in the documentation and/or other materials
;;; provided with the distribution.
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(in-package :hunchentoot)
(eval-when (:compile-toplevel :load-toplevel :execute)
;; make sure socket code is loaded
(require "comm"))
(defun get-env-variable-as-directory (name)
"Retrieves the environment variable named NAME and interprets it as
the pathname of a directory which is returned."
(lw:when-let (string (lw:environment-variable name))
(when (plusp (length string))
(cond ((find (char string (1- (length string))) "\\/" :test #'char=) string)
(t (lw:string-append string "/"))))))
#+(and :lispworks4.4 (or :win32 :linux))
(let ((id :system-cons-free-chain))
(unless (scm::patch-id-loaded-p id)
(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."
id (lisp-implementation-type)
#+:win32 "Windows"
#+:linux "Linux")))
(defvar *cleanup-interval* 100
"Should be NIL or a positive integer. The system calls
*CLEANUP-FUNCTION* whenever *CLEANUP-INTERVAL* new worker threads
\(counted globally across all acceptors) have been created unless the
value is NIL. The initial value is 100.
This variable is only available on LispWorks.")
(defvar *cleanup-function* 'cleanup-function
"A designator for a function without arguments which is called on a
regular basis if *CLEANUP-INTERVAL* is not NIL. The initial value is
the name of a function which invokes a garbage collection on 32-bit
versions of LispWorks.
This variable is only available on LispWorks.")
(defvar *worker-counter* 0
"Internal counter used to count worker threads. Needed for
*CLEANUP-FUNCTION*.")
(defun cleanup-function ()
"The default for *CLEANUP-FUNCTION*. Invokes a GC on 32-bit
LispWorks."
#-:lispworks-64bit
(hcl:mark-and-sweep 2))
(defun get-peer-address-and-port (socket)
"Returns the peer address and port of the socket SOCKET as two
values. The address is returned as a string in dotted IP address
notation."
(multiple-value-bind (peer-addr peer-port)
(comm:get-socket-peer-address socket)
(values (ignore-errors (comm:ip-address-string peer-addr)) peer-port)))
(defun get-local-address-and-port (socket)
"Returns the local address and port of the socket SOCKET as two
values. The address is returned as a string in dotted IP address
notation."
(multiple-value-bind (local-addr local-port)
(comm:get-socket-address socket)
(values (ignore-errors (comm:ip-address-string local-addr)) local-port)))
(eval-when (:compile-toplevel :load-toplevel)
(when (let ((sym (find-symbol "STREAM-READ-TIMEOUT" :stream)))
(and sym (fboundp sym)))
(pushnew :stream-has-timeouts *features*)))
(defun make-socket-stream (socket acceptor)
"Returns a stream for the socket SOCKET. The ACCEPTOR argument is
used to set the timeouts."
#-stream-has-timeouts
(when (acceptor-write-timeout acceptor)
(parameter-error "You need LispWorks 5 or higher for write timeouts."))
(make-instance 'comm:socket-stream
:socket socket
:direction :io
:read-timeout (acceptor-read-timeout acceptor)
#+stream-has-timeouts #+stream-has-timeouts
:write-timeout (acceptor-write-timeout acceptor)
:element-type 'octet))
(defun make-lock (name)
"Simple wrapper to allow LispWorks and Bordeaux Threads to coexist."
(mp:make-lock :name name))
(defmacro with-lock-held ((lock) &body body)
"Simple wrapper to allow LispWorks and Bordeaux Threads to coexist."
`(mp:with-lock (,lock) ,@body))
;; some help for the IDE
(dspec:define-dspec-alias defvar-unbound (name)
`(defparameter ,name))
(dspec:define-dspec-alias def-http-return-code (name)
`(defconstant ,name))
(editor:setup-indent "defvar-unbound" 1 2 4)
(editor:setup-indent "def-http-return-code" 1 2 4)
(editor:setup-indent "handler-case*" 1 2 4)
(defun make-condition-variable (&key name)
(declare (ignore name))
(mp:make-condition-variable))
(defun condition-variable-signal (condition-variable)
(mp:condition-variable-signal condition-variable))
(defun condition-variable-wait (condition-variable lock)
(mp:condition-variable-wait condition-variable lock))