1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
20 #include <ErrorType.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()
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()
46 mutex
.releaseLock(false);
47 DbRetVal rv
= systemDatabase
->getProcessTableMutex(false);
50 printError(rv
,"Unable to get process table mutex");
55 pthread_t thrid
= os::getthrid();
58 ThreadInfo
* pInfo
= systemDatabase
->getThreadInfo(0);
60 ThreadInfo
* freeSlot
= NULL
;
62 bool freeSlotSelected
= false;
63 for (; i
< Conf::config
.getMaxProcs(); i
++)
65 if (pInfo
->pid_
== 0 ) break;
68 if ( i
== Conf::config
.getMaxProcs())
70 systemDatabase
->releaseProcessTableMutex();
71 printError(ErrNoResource
, "No free thread slot. Limit reached");
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
79 pInfo
->thrid_
= thrid
;
80 systemDatabase
->releaseProcessTableMutex(false);
83 DbRetVal
ProcessManager::deregisterThread()
87 mutex
.releaseLock(false);
88 DbRetVal rv
= systemDatabase
->getProcessTableMutex(false);
91 printError(rv
,"Unable to get process table mutex");
94 pid_t pid
= os::getpid();
95 pthread_t thrid
= os::getthrid();
97 ThreadInfo
* pInfo
= systemDatabase
->getThreadInfo(0);
99 for (; i
< Conf::config
.getMaxProcs(); i
++)
101 if (pInfo
->pid_
== pid
&& pInfo
->thrid_
== thrid
) break;
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");
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();
129 //printf("Slot freed %d %x %d %lu\n", i, pInfo, pid, thrid);
134 DbRetVal
ProcessManager::addMutex(Mutex
*mut
)
136 pid_t pid
= os::getpid();
137 pthread_t thrid
= os::getthrid();
138 if (systemDatabase
== NULL
)
142 ThreadInfo
* pInfo
= systemDatabase
->getThreadInfo(0);
144 for (; i
< Conf::config
.getMaxProcs(); i
++)
146 if (pInfo
->pid_
== pid
&& pInfo
->thrid_
== thrid
) break;
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
);
154 for (int i
= 0 ;i
< MAX_MUTEX_PER_THREAD
; i
++)
156 if (pInfo
->has_
[i
] == NULL
)
158 pInfo
->has_
[i
] = mut
;
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
)
175 ThreadInfo
* pInfo
= systemDatabase
->getThreadInfo(0);
177 for (; i
< Conf::config
.getMaxProcs(); i
++)
179 if (pInfo
->pid_
== pid
&& pInfo
->thrid_
== thrid
) break;
182 if (i
== Conf::config
.getMaxProcs())
184 printError(ErrSysFatal
, "Logical Error pid %d thrid %lu not found in procTable", pid
, thrid
);
187 for (int i
= 0 ;i
< MAX_MUTEX_PER_THREAD
; i
++)
189 if (pInfo
->has_
[i
] == mut
)
191 pInfo
->has_
[i
] = NULL
;
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
)
208 ThreadInfo
* pInfo
= systemDatabase
->getThreadInfo(0);
210 for (; i
< Conf::config
.getMaxProcs(); i
++)
212 if (pInfo
->pid_
== pid
&& pInfo
->thrid_
== thrid
) break;
215 if (i
== Conf::config
.getMaxProcs())
217 printError(ErrSysFatal
, "Logical Error pid %d thrid %lu not found in procTable", pid
, thrid
);
220 pInfo
->trans_
= trans
;