Debian Codegraph @

src/pool.cpp

raw symbols SCIP
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <[email protected]>
4 //
5 // Distributed under:
6 //
7 // the Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // or (at your opinion) under:
12 //
13 // The MIT License
14 // (See accompanying file MIT.txt or a copy at
15 // http://www.opensource.org/licenses/mit-license.php)
16 //
17 ///////////////////////////////////////////////////////////////////////////////
18 #define CPPDB_SOURCE
19 #include <cppdb/pool.h>
20 #include <cppdb/backend.h>
21 #include <cppdb/utils.h>
22 #include <cppdb/driver_manager.h>
23
24 #include <stdlib.h>
25
26 namespace cppdb {
27
28 struct pool::data {};
29
30 ref_ptr<pool> pool::create(connection_info const &ci)
31 {
32 ref_ptr<pool> p = new pool(ci);
33 return p;
34 }
35 ref_ptr<pool> pool::create(std::string const &cs)
36 {
37 connection_info ci(cs);
38 ref_ptr<pool> p = new pool(ci);
39 return p;
40 }
41
42 pool::pool(connection_info const &ci) :
43 limit_(0),
44 life_time_(0),
45 ci_(ci),
46 size_(0)
47 {
48 limit_ = ci_.get("@pool_size",16);
49 life_time_ = ci_.get("@pool_max_idle",600);
50 }
51
52 pool::~pool()
53 {
54 }
55
56 ref_ptr<backend::connection> pool::open()
57 {
58 if(limit_ == 0)
59 return driver_manager::instance().connect(ci_);
60
61 ref_ptr<backend::connection> p = get();
62
63 if(!p) {
64 p=driver_manager::instance().connect(ci_);
65 }
66 p->set_pool(this);
67 return p;
68 }
69
70 // this is thread safe member function
71 ref_ptr<backend::connection> pool::get()
72 {
73 if(limit_ == 0)
74 return 0;
75 ref_ptr<backend::connection> c;
76 pool_type garbage;
77 std::time_t now = time(0);
78 {
79 mutex::guard l(lock_);
80 // Nothing there should throw so it is safe
81 pool_type::iterator p = pool_.begin(),tmp;
82 while(p!=pool_.end()) {
83 if(p->last_used + life_time_ < now) {
84 tmp=p;
85 p++;
86 garbage.splice(garbage.begin(),pool_,tmp);
87 size_ --;
88 }
89 else {
90 // all is sorted by time
91 break;
92 }
93 }
94 if(!pool_.empty()) {
95 c = pool_.back().conn;
96 pool_.pop_back();
97 size_ --;
98 }
99 }
100 return c;
101 }
102
103 // this is thread safe member function
104 void pool::put(backend::connection *c_in)
105 {
106 std::auto_ptr<backend::connection> c(c_in);
107 if(limit_ == 0)
108 return;
109 pool_type garbage;
110 std::time_t now = time(0);
111 {
112 mutex::guard l(lock_);
113 // under lock do all very fast
114 if(c.get()) {
115 pool_.push_back(entry());
116 pool_.back().last_used = now;
117 pool_.back().conn = c.release();
118 size_ ++;
119 }
120
121 // Nothing there should throw so it is safe
122
123 pool_type::iterator p = pool_.begin(),tmp;
124 while(p!=pool_.end()) {
125 if(p->last_used + life_time_ < now) {
126 tmp=p;
127 p++;
128 garbage.splice(garbage.begin(),pool_,tmp);
129 size_ --;
130 }
131 else {
132 // all is sorted by time
133 break;
134 }
135 }
136 // can be at most 1 entry bigger then limit
137 if(size_ > limit_) {
138 garbage.splice(garbage.begin(),pool_,pool_.begin());
139 size_--;
140 }
141 }
142 }
143
144 void pool::gc()
145 {
146 put(0);
147 }
148
149 void pool::clear()
150 {
151 pool_type garbage;
152 {
153 mutex::guard l(lock_);
154 garbage.swap(pool_);
155 size_ = 0;
156 } // destroy outside mutex scope
157 }
158 }
159
160
161