transaction.h

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021    Author: Eric Viara <viara@sysra.com>
00022 */
00023 
00024 
00025 #ifndef _EYEDBSM_TRANSACTION_H
00026 #define _EYEDBSM_TRANSACTION_H
00027 
00028 #include <time.h>
00029 #include "xm_alloc.h"
00030 #include <eyedblib/thread.h>
00031 #include <eyedbsm/eyedbsm.h>
00032 #include "eyedbsm/mutex.h"
00033 
00034 #define TRY_DATA
00035 #define KEEP_ORDER
00036 
00037 //#define TRS_INTER_MUT
00038 
00039 namespace eyedbsm {
00040   enum TransactionOP {
00041     /* object operations */
00042     OCREATE = 0,
00043     OREAD,
00044     OWRITE,
00045     ODELETE,
00046     OCREADEL,
00047     OCHSIZE,
00048     OERROR,
00049     OP_CNT, /* OP_CNT should be after PDELETE when all operations
00050                will be implemented */
00051     /* protection operations */
00052     PCREATE,
00053     PREAD,
00054     PWRITE,
00055     PDELETE,
00056     LOCKS  = 0x200,
00057     LOCKX  = 0x400,
00058     LOCKSX = 0x800,
00059     LOCKN  = 0x1000
00060   };
00061 
00062   enum LockModeIndex {
00063     R_S_W_S = 0, /* read shared; write shared */
00064     R_S_W_SX,    /* read shared; write shared/exclusive */
00065     R_S_W_X,     /* read shared; write exclusive */
00066     R_SX_W_SX,   /* read shared/exclusive; write shared/exclusive */
00067     R_SX_W_X,    /* read shared/exclusive; write shared/exclusive */
00068     R_X_W_X,     /* read exclusive; write exclusive */
00069     R_N_W_S,     /* read no lock; write shared */
00070     R_N_W_SX,    /* read no lock; write shared/exclusive */
00071     R_N_W_X,     /* read no lock; write exclusive */
00072     R_N_W_N,     /* read no lock; write no lock */
00073     DB_W,        /* database exclusive for writing */
00074     DB_RW,       /* database exclusive for reading and writing */
00075     DB_Wtrans,   /* database exclusive for writing trans */
00076     LockModeIndex_CNT
00077   };
00078 
00079   enum TransState {
00080     NilTransState,
00081     TransACTIVE,
00082     TransCOMMITING,
00083     TransABORTING,
00084     TransCOMMITED,
00085     TransABORTED
00086   };
00087 
00088   enum ProcState {
00089     NilProcState,
00090     ProcRunning,
00091     ProcWaiting
00092   };
00093 
00094   enum OPMode {
00095     OPImmediate = 1,
00096     OPGrowingPhase,
00097     OPShrinkingPhase,
00098     OPDefault = OPGrowingPhase
00099   };
00100 
00101 #define TRS_SECURE
00102 
00103   struct Transaction {
00104 #ifdef TRS_SECURE
00105     eyedblib::uint32 magic;
00106 #endif
00107     TransState trs_state;
00108     ProcState proc_state;
00109 
00110     eyedblib::int64 create_time;
00111     eyedblib::int64 access_time;
00112     time_t timestamp;
00113     unsigned int xid;
00114     unsigned int obj_cnt;
00115     unsigned int del_obj_cnt;
00116     XMOffset trobj_wait;    /* sleeping on object ... */
00117     LockMode lock_wait;  /* in lockX or lockS */
00118     XMOffset ht_off;        /* hash table offset */
00119 
00120     Boolean wrimmediate; /* true if write immediate transaction */
00121     char dl;                /* deadlock avoid recursion */
00122     Boolean prot_update; /* to update protections runtime list */
00123     XMOffset prev, next;    /* double links */
00124 #ifdef TRS_INTER_MUT
00125     eyedbsm::Mutex mut;
00126     eyedbsm::MutexP mp;
00127 #else
00128     eyedblib::Mutex mut; // warning: non process shared data
00129 #endif
00130   };
00131   /*@@@@ eyedblib::Mutex is a class*/
00132   //#endif
00133 
00134   enum State {
00135     Active = 1,
00136     Deleted,
00137     Zombie
00138   };
00139 
00140   static const NS INVALID_NS = ~0U;
00141 
00142   struct OidLoc {
00143     NS ns;
00144     short datid;
00145   };
00146 
00147   /*#define EYEDB_USE_DATA_VMEM*/
00148 
00149   struct TRObject {
00150 #ifdef TRS_SECURE
00151     eyedblib::uint32 magic;
00152 #endif
00153     TransactionOP op;    /* main transaction operation */
00154     unsigned int lockS;     /* cardinal of S locking: [0, N] */ 
00155     unsigned int lockX;     /* cardinal of X locking: [0, 1] */
00156     unsigned int lockSX;    /* cardinal of SX locking: [0, 1] */
00157     unsigned int lockP;     /* cardinal of P locking: [0, 1] */
00158     XMOffset po_off;        /* offset of the PObject */
00159     Oid oid;             /* part of object oid */
00160     OidLoc oidloc;
00161     State state;
00162 #ifdef EYEDB_USE_DATA_VMEM
00163     void *data; // warning: volatile data
00164 #else
00165     XMOffset data;
00166 #endif
00167     XMOffset prev, next;          /* simple link */
00168 #ifdef KEEP_ORDER
00169     XMOffset xprev, xnext;
00170 #endif
00171     Boolean prot_oid_set;
00172     Oid prot_oid;
00173 #ifdef TRS_INTER_MUT
00174     eyedbsm::Mutex mut;
00175     eyedbsm::MutexP mp;
00176 #else
00177     eyedblib::Mutex mut; // warning: non process shared data
00178 #endif
00179   };
00180 
00181   struct TransOwner {
00182     XMOffset trs_off;       /* transaction owner */
00183     LockMode trs_lock;   /* lock mode for transaction */
00184     XMOffset prev, next;    /* double links */
00185   };
00186 
00187   struct PObject {
00188 #ifdef TRS_SECURE
00189     eyedblib::uint32 magic;
00190 #endif
00191     Oid oid;             /* object oid */
00192     unsigned int lockS;     /* cardinal of S locking: [0, N] */
00193     unsigned int lockX;     /* cardinal of X locking: [0, 1] */
00194     unsigned int lockSX;    /* cardinal of SX locking: [0, 1] */
00195     unsigned int lockPxid;  /* xid of P locking */
00196     unsigned int tridx;     /* idx of transaction */
00197     XMOffset cond;          /* offset of condition */
00198 
00199     int refcnt;
00200     int trs_cnt;            /* count of transaction owner */
00201     TransOwner trs_own;  /* head list for transowner */
00202 
00203     int wait_cnt;           /* waiting threads count */
00204 
00205     State state;
00206     XMOffset prev, next;    /* double links */
00207   };
00208 
00209   struct TransactionContext {
00210     XMOffset trs_off;
00211     TransactionParams params;
00212     LockModeIndex lockmodeIndex;
00213     Boolean skip;
00214   };
00215 
00216   struct TransHeader {
00217     MutexP mut_access;
00218     XMOffset obj_ht;
00219     XMOffset first_trs;
00220     int tr_cnt;
00221   };
00222 
00223 #ifdef TRS_SECURE
00224   static const unsigned int TRS_MAGIC =  0x23ffed12;
00225   static const unsigned int POBJ_MAGIC = 0x6e199930;
00226   static const unsigned int TROBJ_MAGIC = 0x5110293e;
00227   static const unsigned int MT_MAGIC_BASE = (eyedblib::uint32)0x62efd812;
00228 
00229   /* this magic number must be changed each time mutex mapping changed */
00230   static const unsigned int MT_MAGIC = (MT_MAGIC_BASE + 1);
00231 
00232   static const unsigned int POBJ_MAGIC_DELETED  = 0xffe35601;
00233   static const unsigned int TROBJ_MAGIC_DELETED = 0x77ef1234;
00234 
00235   enum {
00236     MAXTRS = 16
00237   };
00238 
00239   extern TRObject *
00240   TRObjectAlloc(XMHandle *);
00241 
00242   extern void
00243   TRObjectFree(XMHandle *, TRObject *);
00244 
00245   extern PObject *
00246   PObjectAlloc(XMHandle *);
00247 
00248   extern void
00249   PObjectFree(XMHandle *, PObject *);
00250 
00251   extern void
00252   ESM_transactionObjectProtSet(TRObject *, const Oid *prot_oid);
00253 
00254   extern Status
00255   ESM_objectLock(DbHandle const *dbh, const Oid *oid, TransactionOP,
00256                  Boolean *, TRObject **),
00257     ESM_objectLockCheck(DbHandle const *dbh, const Oid *oid,
00258                         TransactionOP op, Boolean *popsync,
00259                         Boolean *mustExists, TRObject **ptro),
00260     ESM_objectGetLock(DbHandle const *dbh, const Oid *oid,
00261                       LockMode *rmode),
00262     ESM_objectDownLock(DbHandle const *dbh, Oid const *const oid),
00263     ESM_transactionBegin(DbHandle const *, const TransactionParams *),
00264 
00265     ESM_transactionCommit(DbHandle const *),
00266     ESM_transactionAbort(DbHandle const *),
00267 
00268     ESM_transactionParamsSet(DbHandle const *dbh,
00269                              const TransactionParams *params),
00270 
00271     ESM_transactionParamsGet(DbHandle const *dbh,
00272                              TransactionParams *params);
00273   extern char *
00274   ESM_trobjDataGet(DbHandle const *dbh, TRObject *tro, unsigned int size);
00275 
00276   extern char *
00277   ESM_trobjDataGetIfExist(DbHandle const *dbh, TRObject *tro);
00278 
00279   extern void
00280   ESM_trobjDataWrite(char *addr, const char *object, unsigned int start,
00281                      unsigned int length, OPMode, Boolean);
00282 
00283   extern Status
00284   ESM_trobjDataRead(char *object, const char *addr, const char *dbaddr,
00285                     unsigned int start,
00286                     unsigned int length, Boolean, Boolean);
00287 
00288   extern void
00289   ESM_transInit(DbDescription *, char *, unsigned int);
00290 
00291   extern Status
00292   ESM_transactionCreate(DbHandle const *, const TransactionParams *params,
00293                         XMOffset *);
00294 
00295   extern Status
00296   ESM_transactionDelete(DbHandle const *, XMOffset, TransState);
00297 
00298   extern Status
00299   deadLockCheck(XMHandle *, Transaction *, PObject *, LockMode);
00300 
00301   extern Status
00302   ESM_transactionLockSet(DbHandle const *dbh, ObjectLockMode mode,
00303                          ObjectLockMode *omode);
00304 
00305   extern Boolean
00306   ESM_protectionsMustUpdate(DbHandle const *);
00307 
00308   extern const Oid *
00309   ESM_getProtection(DbHandle const *, const Oid *oid,
00310                     const Oid *prot_oid);
00311 
00312   extern Status
00313   ESM_transactionsGarbage(DbHandle const *dbh, Boolean mustLock);
00314 
00315   extern Boolean ESM_isTransactionActive(Transaction *trs);
00316 
00317 #define SHM_HEADSIZE  sizeof(DbShmHeader)
00318   /*#define SHM_ALLOCSIZE (SHM_DEFSIZE - SHM_HEADSIZE)*/
00319 
00320 #endif
00321 
00322   extern int trs_init(void);
00323 
00324   extern Boolean trace_garb_trs;
00325 
00326 }
00327 #endif

Generated on Mon Dec 22 18:16:11 2008 for eyedb by  doxygen 1.5.3