@@ -39,6 +39,7 @@ public final class ConnectionPool {
39
39
private var unavailableReadConnections = [ DirectConnection] ( )
40
40
private let lockQueue : dispatch_queue_t
41
41
private var writeConnection : DirectConnection !
42
+ private let connectionSemaphore = dispatch_semaphore_create ( 5 )
42
43
43
44
public var foreignKeys : Bool {
44
45
get {
@@ -91,6 +92,7 @@ public final class ConnectionPool {
91
92
self . pool. unavailableReadConnections. removeAtIndex ( index)
92
93
}
93
94
self . pool. availableReadConnections. append ( self . connection)
95
+ dispatch_semaphore_signal ( self . pool. connectionSemaphore)
94
96
}
95
97
}
96
98
@@ -151,44 +153,41 @@ public final class ConnectionPool {
151
153
152
154
var borrowed : BorrowedConnection !
153
155
154
- repeat {
156
+ dispatch_semaphore_wait ( connectionSemaphore, DISPATCH_TIME_FOREVER)
157
+ dispatch_sync ( lockQueue) {
155
158
156
- dispatch_sync ( lockQueue) {
157
-
158
- // Ensure database is open
159
- self . writable
159
+ // Ensure database is open
160
+ self . writable
161
+
162
+ let connection : DirectConnection
163
+
164
+ if let availableConnection = self . availableReadConnections. popLast ( ) {
165
+ connection = availableConnection
166
+ }
167
+ else {
160
168
161
- let connection : DirectConnection
169
+ let flags = SQLITE_OPEN_READONLY | SQLITE_OPEN_WAL | SQLITE_OPEN_NOMUTEX
162
170
163
- if let availableConnection = self . availableReadConnections. popLast ( ) {
164
- connection = availableConnection
165
- }
166
- else {
167
-
168
- let flags = SQLITE_OPEN_READONLY | SQLITE_OPEN_WAL | SQLITE_OPEN_NOMUTEX
169
-
170
- connection = try ! DirectConnection ( self . location, flags: flags, dispatcher: ImmediateDispatcher ( ) , vfsName: vfsName)
171
- connection. busyTimeout = 2
171
+ connection = try ! DirectConnection ( self . location, flags: flags, dispatcher: ImmediateDispatcher ( ) , vfsName: vfsName)
172
+ connection. busyTimeout = 2
172
173
173
- for (type, setupProcessor) in self . internalSetup {
174
- if type == . WriteAheadLogging {
175
- continue
176
- }
177
- try ! setupProcessor ( connection)
178
- }
179
-
180
- for setupProcessor in self . setup {
181
- try ! setupProcessor ( connection)
174
+ for (type, setupProcessor) in self . internalSetup {
175
+ if type == . WriteAheadLogging {
176
+ continue
182
177
}
183
-
178
+ try ! setupProcessor ( connection )
184
179
}
185
180
186
- self . unavailableReadConnections. append ( connection)
181
+ for setupProcessor in self . setup {
182
+ try ! setupProcessor ( connection)
183
+ }
187
184
188
- borrowed = BorrowedConnection ( pool: self , connection: connection)
189
185
}
190
186
191
- } while borrowed == nil
187
+ self . unavailableReadConnections. append ( connection)
188
+
189
+ borrowed = BorrowedConnection ( pool: self , connection: connection)
190
+ }
192
191
193
192
return borrowed
194
193
}
0 commit comments