Fix for
[csql.git] / src / server / Process.cxx
blob35ed2f4ad2bfcd20ac163281832acee0c30cf2bb
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 #include <Process.h>
17 #include <Debug.h>
18 #include <Database.h>
19 #include <Config.h>
20 #include <ErrorType.h>
21 #include <Globals.h>
22 #include <Mutex.h>
24 int ProcessManager::noThreads=0;
25 Mutex ProcessManager::mutex;
26 caddr_t ProcessManager::sysAddr=0;
27 caddr_t ProcessManager::usrAddr=0;
28 Database* ProcessManager::systemDatabase=NULL;
31 void ThreadInfo::init()
33 pid_ = 0;
34 thrid_ =0;
35 trans_ = NULL;
36 want_ = NULL;
37 for (int i =0; i <MAX_MUTEX_PER_THREAD; i++) has_[i] = NULL;
40 //It does not check for re registering as well as deregistering unregistered threads.
41 //as it is handled in the connection class open and close methods.
42 DbRetVal ProcessManager::registerThread()
44 mutex.getLock(false);
45 noThreads++;
46 mutex.releaseLock(false);
47 DbRetVal rv = systemDatabase->getProcessTableMutex(false);
48 if (OK != rv)
50 printError(rv,"Unable to get process table mutex");
51 return rv;
53 pid_t pid;
54 pid = os::getpid();
55 pthread_t thrid = os::getthrid();
58 ThreadInfo* pInfo = systemDatabase->getThreadInfo(0);
59 int i=0;
60 ThreadInfo* freeSlot = NULL;
61 int freeSlotPos =0;
62 bool freeSlotSelected = false;
63 for (; i < Conf::config.getMaxProcs(); i++)
65 if (pInfo->pid_ == 0 ) break;
66 pInfo++;
68 if ( i == Conf::config.getMaxProcs())
70 systemDatabase->releaseProcessTableMutex();
71 printError(ErrNoResource, "No free thread slot. Limit reached");
72 return ErrNoResource;
74 //printf("Process slot used %d %x\n", i, pInfo);
75 //TODO::make the above debug message
76 //TODO:print it to the trace file
77 pInfo->init();
78 pInfo->pid_ = pid;
79 pInfo->thrid_ = thrid;
80 systemDatabase->releaseProcessTableMutex(false);
81 return OK;
83 DbRetVal ProcessManager::deregisterThread()
85 mutex.getLock(false);
86 noThreads--;
87 mutex.releaseLock(false);
88 DbRetVal rv = systemDatabase->getProcessTableMutex(false);
89 if (OK != rv)
91 printError(rv,"Unable to get process table mutex");
92 return rv;
94 pid_t pid = os::getpid();
95 pthread_t thrid = os::getthrid();
97 ThreadInfo* pInfo = systemDatabase->getThreadInfo(0);
98 int i=0;
99 for (; i < Conf::config.getMaxProcs(); i++)
101 if (pInfo->pid_ == pid && pInfo->thrid_ == thrid) break;
102 pInfo++;
105 systemDatabase->releaseProcessTableMutex(false);
106 if (i == Conf::config.getMaxProcs())
108 printError(ErrSysFatal, "Degistering process %d is not registered with csql", pid);
109 return ErrNoResource;
111 if (pInfo->trans_ && pInfo->trans_->status_ == TransRunning)
113 printError(ErrWarning, "Transaction is still running\n");
115 if (pInfo->want_ != NULL)
117 printError(ErrSysFatal, "Probable data corruption.wants_ is not null\n");
118 return ErrSysFatal;
120 for (int muti = 0 ;i < MAX_MUTEX_PER_THREAD; i++)
122 if (pInfo->has_[muti] != NULL)
124 printError(ErrSysFatal, "Probable data corruption.some mutexes are not freed\n");
125 pInfo->has_[muti]->releaseLock();
126 return ErrSysFatal;
129 //printf("Slot freed %d %x %d %lu\n", i, pInfo, pid, thrid);
130 pInfo->init();
131 return OK;
134 DbRetVal ProcessManager::addMutex(Mutex *mut)
136 pid_t pid = os::getpid();
137 pthread_t thrid = os::getthrid();
138 if (systemDatabase == NULL)
140 return OK;
142 ThreadInfo* pInfo = systemDatabase->getThreadInfo(0);
143 int i=0;
144 for (; i < Conf::config.getMaxProcs(); i++)
146 if (pInfo->pid_ == pid && pInfo->thrid_ == thrid) break;
147 pInfo++;
149 if (i == Conf::config.getMaxProcs())
151 printError(ErrSysFatal, "Logical Error pid %d thrid %lu not found in procTable while adding mutex %s", pid, thrid, mut->name);
152 return ErrSysFatal;
154 for (int i = 0 ;i < MAX_MUTEX_PER_THREAD; i++)
156 if (pInfo->has_[i] == NULL)
158 pInfo->has_[i] = mut;
159 return OK;
162 printError(ErrSysInternal, "All slots are full. Reached per thread mutex limit.");
163 return ErrSysInternal;
166 DbRetVal ProcessManager::removeMutex(Mutex *mut)
168 pid_t pid = os::getpid();
169 pthread_t thrid = os::getthrid();
170 if (systemDatabase == NULL)
172 return OK;
175 ThreadInfo* pInfo = systemDatabase->getThreadInfo(0);
176 int i=0;
177 for (; i < Conf::config.getMaxProcs(); i++)
179 if (pInfo->pid_ == pid && pInfo->thrid_ == thrid) break;
180 pInfo++;
182 if (i == Conf::config.getMaxProcs())
184 printError(ErrSysFatal, "Logical Error pid %d thrid %lu not found in procTable", pid, thrid);
185 return ErrSysFatal;
187 for (int i = 0 ;i < MAX_MUTEX_PER_THREAD; i++)
189 if (pInfo->has_[i] == mut)
191 pInfo->has_[i] = NULL;
192 return OK;
195 printError(ErrSysInternal, "Mutex could not be found in the list %s", mut->name);
196 return ErrSysInternal;
199 DbRetVal ProcessManager::setTransaction(Transaction *trans)
201 pid_t pid = os::getpid();
202 pthread_t thrid = os::getthrid();
203 if (systemDatabase == NULL)
205 return OK;
208 ThreadInfo* pInfo = systemDatabase->getThreadInfo(0);
209 int i=0;
210 for (; i < Conf::config.getMaxProcs(); i++)
212 if (pInfo->pid_ == pid && pInfo->thrid_ == thrid) break;
213 pInfo++;
215 if (i == Conf::config.getMaxProcs())
217 printError(ErrSysFatal, "Logical Error pid %d thrid %lu not found in procTable", pid, thrid);
218 return ErrSysFatal;
220 pInfo->trans_ = trans;
221 return OK;