code reorg
[csql.git] / include / Lock.h
blobb1ceabb8fcc7293e4028d10fe943a6d6567372fb
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #ifndef LOCK_H
17 #define LOCK_H
18 #include<os.h>
19 #include<Index.h>
20 class Chunk;
21 class Database;
22 class Transaction;
24 class LockInfo
26 public:
27 InUse noOfReaders_;
28 //-1 indicates write lock is taken
30 InUse waitReaders_;
31 InUse waitWriters_;
32 LockInfo() { noOfReaders_ = 0; waitReaders_ = 0; waitWriters_ = 0; }
36 class LockHashNode
38 public:
39 void *ptrToTuple_;
40 LockInfo lInfo_;
41 LockHashNode *next_;
42 void print()
44 printf("<Lock Node>\n");
45 printf(" <Tuple> %x </Tuple>\n", ptrToTuple_);
46 printf(" <NoOfReaders> %d </NoOfReaders>\n", lInfo_.noOfReaders_);
47 printf(" <WaitReaders> %x </WaitReaders>\n", lInfo_.waitReaders_);
48 printf(" <WaitWriters> %x </WaitWriters>\n", lInfo_.waitWriters_);
49 printf("</Lock Node>\n");
53 class LockTable
55 Bucket *lockBuckets;
56 Database *systemDatabase_;
58 //Each thread contains its own DatabaseManager object which inturn contains
59 //LockManager and which inturn contains LockTable. So this is thread
60 //specific and contains cached current bucket for which lock needs to be
61 //obtained or released
62 Bucket *curBucket;
64 public:
65 LockHashNode* allocLockNode(LockInfo &info, void *tuple, DbRetVal *rv);
66 DbRetVal deallocLockNode(LockHashNode *head, Bucket *bucket);
67 void deallocLockNode(LockHashNode *head);
68 DbRetVal addNewLockNode(void *tuple, Transaction **trans,
69 LockInfo &info);
70 DbRetVal releaseLock(LockHashNode *node);
71 LockHashNode* getLockNode(void *tuple, DbRetVal &rv, bool takeLock=true);
73 LockTable();
74 ~LockTable();
75 void setDb(Database *sysDb);
76 Bucket* getLockBucket(void *tuple);
78 DbRetVal getBucketMutex();
79 void releaseBucketMutex();
80 void printUsageStatistics();
81 void printDebugInfo();
82 void printMutexInfo();
85 class TransHasNode
87 public:
88 LockHashNode *node_;
89 TransHasNode *next_;
90 void print() { node_->print(); }
93 class DatabaseManagerImpl;
94 //singleton
95 //DatabaseManager has this object and give reference to it to
96 //Table interface and so on.
97 class DllExport LockManager
99 public:
100 Database *systemDatabase_;
101 LockTable lockTable;
103 private:
104 LockHashNode* allocLockNode(LockInfo &info, void *tuple, DbRetVal *rv);
105 DbRetVal deallocLockNode(LockHashNode *head, Bucket *bucket);
106 void deallocLockNode(LockHashNode *head);
107 Bucket* getLockBucket(void *tuple);
108 DbRetVal createFirstNodeInBucket(void *tuple, Transaction **trans,
109 Bucket *bucket, LockInfo &info);
110 DbRetVal addNewNodeToBucket(void *tuple, Transaction **trans,
111 Bucket *bucket, LockInfo &info);
112 DbRetVal retrySharedLock(Transaction **trans,
113 LockHashNode *node);
114 DbRetVal retryExclusiveLock(Transaction **trans,
115 LockHashNode *node);
116 DbRetVal takeXLockNotInUse(Transaction **trans, LockHashNode *node);
117 bool takeXLockOneReader(Transaction **trans, LockHashNode *node);
118 bool takeXLockOneWriter(Transaction **trans, LockHashNode *node);
119 DbRetVal releaseLock(Bucket *bucket, LockHashNode *node);
121 public:
123 LockManager(Database *sysDb_);
124 DbRetVal getSharedLock(void *tuple, Transaction **trans);
125 DbRetVal getExclusiveLock(void * tuple, Transaction **trans);
126 DbRetVal releaseLock(void *tuple);
127 DbRetVal isExclusiveLocked(void *tuple, Transaction **trans, bool &status);
128 DbRetVal getBucketMutex(Bucket *bucket, int procslot);
129 void printUsageStatistics();
130 void printDebugInfo();
131 void printMutexInfo();
135 #endif