If it is singleton data the I suppose you could keep a static pointer to the 
data structure, a static "use" counter, and a static mutex.

Then, for each connection (xConnect), lock the mutex, if the static use counter 
is zero then build the data structure and increment the use counter, then set 
the new connection to use the shared data structure, then release the mutex.

For each xDisconnect, lock the mutex, release that set of virtual table 
resources, decrement the counter and if the counter is zero, release the static 
data, then release the mutex.

This assumes that the data needs "reloading" after nothing is using it.  If 
this is not the case you do exactly the same thing except to never rebuild the 
data structure after the first time, all you need (inside the mutex) on the 
xConnect it to build the static data structure if it does not exist, and on the 
xDisconnect simply release the virtual table resources (but never the shared 
static data structure).

This would mean, of course, that the data would be "static" across all 
connections in a process, but different processes would have their own static 
data.  If you need to release and rebuild the data structure this will happen 
only when no connection in the process is using connected to the data.

By diddling with the data attributes (basically making them all extern 
references to a specially constructed data segment) you could make the static 
data structure a singleton across multiple processes.  However this is not the 
default because static data segments are "process local" not "library local" ...

-- 
˙uʍop-ǝpısdn sı ɹoʇıuoɯ ɹnoʎ 'sıɥʇ pɐǝɹ uɐɔ noʎ ɟı


> -----Original Message-----
> From: sqlite-users [mailto:sqlite-users-boun...@mailinglists.sqlite.org]
> On Behalf Of Dimitris Bil
> Sent: Monday, 3 July, 2017 11:14
> To: sqlite-users@mailinglists.sqlite.org
> Subject: [sqlite] Concurrent reads for VTs with in-memory data structures
> 
> I have some virtual tables that keep in-memory data structures. Data
> loading is happening at table creation (xCreate and xConnect are the same)
> and after that, during querying, only read access is needed. Queries do
> not access any other tables. Is there a way to achieve concurrent
> execution without having to keep multiple copies of each data structure?
> 
> _______________________________________________
> sqlite-users mailing list
> sqlite-users@mailinglists.sqlite.org
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users



_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to