This file is collection of snippets from emails, mailing list postings, etc. 
More will be added as time permits and as new question arise.
================================================================================
URL:          http://sourceforge.net/projects/openrepo
Contact info: openrepo@shift-right.com


What is this "Open Repository?"
--------------------------------------------------------------------------------
The Open Repository is the Source Forge project, where Shift-Right Technologies
make available their open-source C/C++ source code libraries.  The goal of the
Repository is to provide examples of dependency engineering, software reuse, and
to support the Open Source community.  The Open Repository contains a lot of
"stuff" (as the few brave souls who have downloaded it and peered inside will
testify).  As for its connection with Metakit, the repository contains a
threading and ITC interfaces and on top of these interfaces is a multi-threaded
C++ wrapper for Metakit. 


What is this "Jcl Library?"
--------------------------------------------------------------------------------
The Jcl Library is a "convenience" name given to the major C++ section of the
Open Repository.  Jcl is name of the C++ namespace use for this section (it is
also one of the name of one of the top level source directories).  The
mutli-threading Metakit wrappers are contained in the Jcl namesapace.  What does
the acronym Jcl stand for? "John's Class Library", original huh ;-).


Can I retro-fit an existing Metakit application to use your multi-threading 
wrappers?
--------------------------------------------------------------------------------
Using the wrapper on a new application from the ground up would not be a
problem.  To retrofit an existing multi-threaded application could be difficult.
For a retro-fit would have to due one of the following: 1) completely isolate
all of the database access code to a sub-system that uses the Open Repository's
Threading and ITC framework.  2) Replace the application current threading and
ITC framework with the one from the Open Repository.  Option 1 is more
practical, but still a pain.


Can I use the latest version of Metakit with the Jcl MT Wrappers?
--------------------------------------------------------------------------------
The Metakit source used by the Jcl wrappers is 99.99% unchanged, though a slight
modification is/was required in equi4/metakit/src /header.h file to configure
the header include of config.h for the Jcl/BENV build environment.  The version
of Metakit distributed with the Open Repository contains this change.   To use a
newer version with the example code, you will need to make the change above in
the new source. However, when using the Jcl wrapper code with a different build
environment, this change is probably not required.  Just be sure all of the code
sees the same config.h file. 


Do all database accesses (modify/update) get serialized through a single thread?
--------------------------------------------------------------------------------
No. For a given database, all accesses need to be serialized through the thread
that created the database.  This means if you have multiple databases/files,
each database/file can be accessed by it own thread.  For example I have
databases d1, d2, and d3.  I open/create/access each database in a different
thread - create/access d1 in thread t1, d2 in t2, d3 in t3. You can have N
databases running in N threads, or N databases running in M threads.


What about locking?
--------------------------------------------------------------------------------
There are two levels of 'locking'.  One is at the individual database level.
The Client/Server model used by the Jcl MT wrappers always guaranties that all
accesses (read and write) for a given database are serialized through the single
thread in which the database was originally created and/or opened.  The second
level is at the application level when you have more than one database file.
Your application defines the dependence and/or consistence relationships of the
data stored in across multiple database files. The Jcl MT wrappers provide a
locking interface to guaranty the integrity of the application data when it
spans multiple physical database files.  Currently there is a
single-read/single-writer and multiple-reader/single-writer implementation of
the interface.  Note: The Jcl locking mechanism does NOT provide a true "global"
commit functionality. The locks provide a logical global commit, but not a
physical one.  For example, when you "commit" a transaction than spans multiple
databases, the transaction code iterates through the list of databases you
updated and calls commit() on each one.  If there is a system or application
error that interrupts this iterative commit process, there is no way to detect
on reboot/restart that not all the db's completed the commit() process. 


What does the example program do?
--------------------------------------------------------------------------------
The example application is setup to allow up to 5 users to simultaneously access
a collection of Metakit database files.  The users interface to the application
via a telnet/socket interface.  Once a user has connected to the application
server (i.e. 'telnet localhost 5002'), he can issue simple commands to list,
create, and deleted database files; list, create, and delete records from
individual database file(s).  The format of the views and/or tables is
predetermined by the example application.  The application server is setup to
allow multiple read accesses to occur simultaneously, but database access is
restricted to a single user when a write transaction is in progress.


A little help with the Jcl threading and ITC please?
--------------------------------------------------------------------------------
The Jcl threading model is a basic OO approach to modeling threads.  The Jcl
Inter-thread-Communication (ITC) on the other hand is not so basic :-).  The Jcl
ITC models has the following features/limitations: 

1. No dynamic memory allocation is required and/or use.  The clients are
responsible for providing all required memory. 

2. No limit to the number of messages that can be sent/queued at any one time. 

3. Creating, sending, and dispatching of message is 100% type safe, there is no
type casting required. 

4. All messages transactions are request-and-response.  There are no broadcasts
or unacknowledged messages. 

5. All this makes for some interesting (read as: complex) OO code. 

6. Using the Jcl ITC is actually very cookbook'ish.  In fact most developers
don't really understand how it works, but are very proficient in
sending/receiving messages.  Where is the cook/recipe book you ask?  Well, that
would require someone (read as: me) to write it :-).  I have written shell
scripts and java applets to generate code template files for ITC message
classes, but no written documentation yet.

Another important piece to the ITC puzzle is the "Atomic API" interface.  This
is a very useful paradigm for ITC, but trying to reverse engineering (or even
engineering it) is difficult.  Every few months I have to review the code to
understand how it works.  The good news is that you don't have to understand
how/why it works, only why and how to use it.  

The Atomic API is built on top of the core Jcl ITC interfaces.  The Atomic API
provides a mechanism where an ITC client can issue several commands to an ITC
server such that all of the commands are "viewed" as single atomic operation
with respect to the server and other clients.  The Metakit wrapper makes
extensive use the Atomic API mechanism.  All database operations are carried out
using this approach.  

Atomic API overview:
1. You begin by having the server define an interface - a pure virtual class
with N methods.  These methods are the individual actions that a client can
called when doing an atomic-operation.

2. Write server code to implement the interface from step 1.  These also require
the server to inherent/implement the necessary ITC code.

3. For each client calling the server's Atomic API interface, the client must
inherent and the implement the template <class AccessApi>:: execute(AccessApi&
api) method.  The execute() method is a callback method that is used by the
server.  Once called, the client can make N calls to the "AccessApi" (which is
the interface defined in step).  The scope of the atomic-operation is the for
the during of the call to the execute() method.


4. Here is the call sequencing.  
- The client makes an ITC call to the server, passing a reference to the
callback instance defined in step 3.  The ITC can be an asynchronous or
synchronous ITC call (typically it is synchronous using the executeAtomic()
call).

- The server receives the ITC message.  The server then calls back the
referenced client and does *not* accept any more messages until the callback
function has returned.  The client code now makes N calls to the server's API in
the CONTEXT of the server's thread.   The client is responsible for insure the
data integrity of its data since the callback code runs in a one thread context
(the server's) and the rest of the client's code runs in another thread context
(the client's).  For this reason, it is recommended that the client makes a
synchronous ITC when using the Atomic API because it eliminates the issue of the
data contention issue (since the client thread is always blocked (i.e. not
running) when the server thread is executing the client's callback method. 

- Once the callback method has completed, the server returns the ITC message.
The client upon receipt of the server's response message either unblocks, if
synchronous semantics were used, or receives a response message call, if
asynchronous semantics were used.



Snippets from the Metakit mailing list about concurrency
--------------------------------------------------------------------------------
> (http://www.equi4.com/metakit/wiki.cgi/13):
>
> "... MetaKit cam be used in multi-threaded applications and in DLLs, 
> as long as no datafile or view is used in more than one thread. This 
> the case right now for Windows, it would be very easy to add this for 
> use in Unix (one mutex)."
>
> And
> "... Wouldn't locking solve multi-user access?
>
> No. As a matter of fact, locking was added to the Tcl interface
> (Mk4tcl)
> recently, to enforce the above rules. That's the easy part. The main
> issue which must be solved is cache coherence. Right now, there's some
> information which gets loaded on file open, and then cached. This
> caching increases as the file is being modified. To solve the problem,
> either some form of IPC, or some sort of change propagation is needed."
>
> My first question is: are the above statements still true for the 
> newer versions of Metakit v2.4.x?

Yes.

> Next, are the following statements true?
> 1. Give the above statements, then simply providing mutex protection 
> (and/or critical sections) around my code that access my metakit 
> databases (both reads and writes actions) is not sufficient because of 
> "cache coherence" issues.

Multi-thread != multi-user (multi-process/multi-machine).

With threading, all threads see the same consistent address space 
(assuming you open MK's datafile once).

Cache coherence just means that as soon as MK datafiles are opened more 
than once, then some cached data between won't be synced.  That is only 
the case with multiple processes, not with multiple threads inside a 
single process.

> 2. My only option for a multithread wrapper is do "...introduce a 
> client/server mechanism" to isolate all operations on a given metakit 
> database to the thread that the database was originally opened/created 
> in.

Yes.  Or wrap all accesses (read and write) to pass through a single 
mutex.

> Next question.  From a previous newsgroup posting, the comment has 
> been made "... The only global data structure is a property symbol 
> table, which has been properly mutex-protected (on Win32, Unix should 
> not be hard to do as well.." So for my non-win32 builds, for complete 
> concurrency protection I must also provide mutex protection of the 
> property symbol table.  Is this correct?  Are there any other platform 
> specific "gotchas" with respect to concurrency?

To summarize - there are two contexts:

1) Global data, which on Win32 is properly protected when q4_MULTI is 
enabled, and which should be extended for Unix - this is a matter of 
replicating the Win32 logic (all in src/view.cpp) with pthreads calls.

2) Storages, views, rows - these are not thread safe (but also have no 
global data usage at all.  You either need to open and use a storage in 
one thread at all times, or implement a scheme over MK itself which 
lets you make sure you never make *any* MK calls based on the same 
storage or view or row from more than one thread.

The "any MK calls" is the trouble-maker.  There may be a way to support 
N readers, but right now that is not safe because readers affect some 
cached data.

The current state is that you must treat all calls to storage/view/row 
objects as non-re-entrant.  You may however have each thread do things 
on its own storage/view/row - concurrently.  I.e. you can scan/modify 
things in one datafile in one thread, and do the same for another 
datafiel in anotehr thread.

The new commit-extend mode was implemented to find a way out of this.  
You can open the same datafile N times R/, each from a thread, and 1 
more time R/W even (in extend mode).  The readers will not see the 
changes.  The writer merely writes at the end of the file.  At some 
point you will need to sync and comnit in non-extend mode.  And you 
will need to compact the file (commit extend can grow a datafile very 
quickly).  The commit-extend mode also works multi-process and 
multi-machine, i.e. you can have N readers anywhere, and a single 
writer.  The trickiness is syncing them up again.

It's not perfect, but it works today.  On Mac OSX, this is done all the 
time in its central AddressBook database.  It is not as clumsy as it 
may seem - this design allows total lock-free parallelism, which is in 
fact a performance advantage.

> Last question.  In the source code (header.h) there is a preprocessor 
> option "q4_MULTI".  What does this option do currently (v2.4.x)?  What 
> is the planned functionality in the future?

It enables Win32 mutexes for the global data, i.e. property symbol 
table.  It should do the same once Unix (pthreads) locking is added.

-jcw


