Module: Lich::SQLiteBusyWait::SerializedDatabase

Defined in:
documented/common/sqlite_connection.rb

Overview

Extended (after Connection) onto a connection that several threads share. Every sqlite3 C method on the connection, and on each statement prepared on it (SerializedStatement), runs under one re-entrant Monitor, so only one thread at a time is inside SQLite on this connection. A thread that finds another one waiting for a lock waits for the Monitor, which releases the GVL, instead of blocking on SQLite's connection mutex with the GVL held. Ruby-level methods such as #execute or #transaction are not wrapped as a whole: as before, statements from different threads can interleave between calls.

The wrappers are defined by SQLiteBusyWait.prepare_sqlite3 from the sqlite3 gem that is loaded, so a C method added by a later gem version is serialized too.

Class Method Summary collapse

Class Method Details

.extended(conn) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes a shared connection with a re-entrant Monitor for thread-safe serialization.

Parameters:

  • conn (SQLite3::Database) —

    the shared connection being extended



218
219
220
# File 'documented/common/sqlite_connection.rb', line 218

def self.extended(conn)
  conn.instance_variable_set(:@lich_monitor, Monitor.new)
end