r3133: more multiprocessing support
[clsql.git] / sql / pool.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          pool.cl
6 ;;;; Purpose:       Support function for connection pool
7 ;;;; Programmers:   Kevin M. Rosenberg, Marc Battyani
8 ;;;; Date Started:  Apr 2002
9 ;;;;
10 ;;;; $Id: pool.lisp,v 1.3 2002/10/21 14:11:09 kevin Exp $
11 ;;;;
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; CLSQL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :clsql-sys)
21
22 (defun make-process-lock (name) 
23   #+allegro (mp:make-process-lock :name name)
24   #+scl (thread:make-lock name)
25   #+lispworks (mp:make-lock :name name)
26   #-(or allegro scl lispworks) (declare (ignore name))
27   #-(or allegro scl lispworks) nil)
28
29 (defmacro with-process-lock ((lock desc) &body body)
30   #+scl `(thread:with-lock-held (,lock ,desc) ,@body)
31   #+(or allegro lispworks)
32   (declare (ignore desc))
33   #+(or allegro lispworks)
34   (let ((l (gensym)))
35     `(let ((,l ,lock))
36        #+allegro (mp:with-process-lock (,l) ,@body)
37        #+lispworks (mp:with-lock (,l) ,@body)))
38   #-(or scl allegro lispworks) (declare (ignore lock desc))
39   #-(or scl allegro lispworks) `(progn ,@body))
40
41 (defvar *db-pool* (make-hash-table :test #'equal))
42 (defvar *db-pool-lock* (make-process-lock "DB Pool lock"))
43
44 (defclass conn-pool ()
45   ((connection-spec :accessor connection-spec :initarg :connection-spec)
46    (database-type :accessor database-type :initarg :database-type)
47    (free-connections :accessor free-connections
48                      :initform (make-array 5 :fill-pointer 0 :adjustable t))
49    (all-connections :accessor all-connections
50                     :initform (make-array 5 :fill-pointer 0 :adjustable t))
51    (lock :accessor conn-pool-lock
52          :initform (make-lock "Connection pool"))))
53
54 (defun acquire-from-conn-pool (pool)
55   (or (with-process-lock ((conn-pool-lock pool) "Acquire from pool")
56         (and (plusp (length (free-connections pool)))
57              (vector-pop (free-connections pool))))
58       (let ((conn (connect (connection-spec pool)
59                            :database-type (database-type pool)
60                            :if-exists :new)))
61         (with-process-lock ((conn-pool-lock pool) "Acquire from pool")
62           (vector-push-extend conn (all-connections pool))
63           (setf (conn-pool conn) pool))
64         conn)))
65
66 (defun release-to-conn-pool (conn)
67   (let ((pool (conn-pool conn)))
68     (with-process-lock ((conn-pool-lock pool) "Release to pool")
69       (vector-push-extend conn (free-connections pool)))))
70
71 (defun clear-conn-pool (pool)
72   (with-process-lock ((conn-pool-lock pool) "Clear pool")
73     (loop for conn across (all-connections pool)
74           do (setf (conn-pool conn) nil)
75           (disconnect :database conn))
76     (setf (fill-pointer (free-connections pool)) 0)
77     (setf (fill-pointer (all-connections pool)) 0))
78   nil)
79
80 (defun find-or-create-connection-pool (connection-spec database-type)
81   "Find connection pool in hash table, creates a new connection pool if not found"
82   (with-process-lock (*db-pool-lock* "Find connection")
83     (let* ((key (list connection-spec database-type))
84            (conn-pool (gethash key *db-pool*)))
85       (unless conn-pool
86         (setq conn-pool (make-instance 'conn-pool
87                                        :connection-spec connection-spec
88                                        :database-type database-type))
89         (setf (gethash key *db-pool*) conn-pool))
90       conn-pool)))
91
92 (defun acquire-from-pool (connection-spec database-type &optional pool)
93   (unless (typep pool 'conn-pool)
94     (setf pool (find-or-create-connection-pool connection-spec database-type)))
95   (acquire-from-conn-pool pool))
96
97 (defun release-to-pool (database)
98   (release-to-conn-pool database))
99
100 (defun disconnect-pooled (&optional clear)
101   "Disconnects all connections in the pool"
102   (with-process-lock (*db-pool-lock* "Find connection")
103     (maphash
104      #'(lambda (key conn-pool)
105          (declare (ignore key))
106          (clear-conn-pool conn-pool))
107      *db-pool*)
108     (when clear (clrhash *db-pool*)))
109   t)
110