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>
23 int ProcessManager::noThreads
=0;
24 caddr_t
ProcessManager::sysAddr
=0;
25 caddr_t
ProcessManager::usrAddr
=0;
26 //It does not check for re registering as well as deregistering unregistered threads.
27 //as it is handled in the connection class open and close methods.
28 DbRetVal
ProcessManager::registerThread()
33 DbRetVal rv
= systemDatabase
->getProcessTableMutex();
36 printError(rv
,"Unable to get process table mutex");
42 //first thread to register
49 pthread_t thrid
= os::getthrid();
50 ProcInfo
* pInfo
= systemDatabase
->getProcInfo(0);
52 ProcInfo
* freeSlot
= NULL
;
54 bool freeSlotSelected
= false;
55 for (; i
< config
.getMaxProcs(); i
++)
57 if (pInfo
->pid_
== pid
) break;
58 if (!freeSlotSelected
&& 0 == pInfo
->pid_
)
62 freeSlotSelected
=true;
66 if (i
== config
.getMaxProcs())
68 //First thread to register.
69 ThreadInfo
*tInfo
=systemDatabase
->getThreadInfo(freeSlotPos
, 0);
70 tInfo
->thrid_
= thrid
;
73 freeSlot
->numThreads_
= 1;
76 ThreadInfo
*tInfo
= systemDatabase
->getThreadInfo(i
, 0);
78 for (; j
< config
.getMaxThreads(); j
++)
81 if (0 == tInfo
->thrid_
)
83 tInfo
->thrid_
= thrid
;
89 if ( j
== config
.getMaxThreads())
91 systemDatabase
->releaseProcessTableMutex();
92 printError(ErrNoResource
, "No free thread slot. Limit reached");
98 systemDatabase
->releaseProcessTableMutex();
102 DbRetVal
ProcessManager::deregisterThread()
107 DbRetVal rv
= systemDatabase
->getProcessTableMutex();
110 printError(rv
,"Unable to get process table mutex");
115 systemDatabase
->releaseProcessTableMutex();
116 printError(ErrSysInternal
, "Process not registered\n");
117 return ErrSysInternal
;
120 pthread_t thrid
= os::getthrid();
122 ProcInfo
* pInfo
= systemDatabase
->getProcInfo(0);
124 for (; i
< config
.getMaxProcs(); i
++)
126 if (pInfo
->pid_
== pid
) break;
129 if (i
== config
.getMaxProcs())
131 systemDatabase
->releaseProcessTableMutex();
132 printError(ErrSysFatal
, "Degistering process %d is not registered with csql", pid
);
133 return ErrNoResource
;
135 ThreadInfo
*tInfo
= systemDatabase
->getThreadInfo(i
, 0);
138 for (; i
< config
.getMaxThreads(); i
++)
140 if (thrid
== tInfo
->thrid_
)
150 if ( i
== config
.getMaxThreads())
152 systemDatabase
->releaseProcessTableMutex();
153 printError(ErrSysFatal
, "Degistering Pid %d Thread %d is not registered with csql", pid
, thrid
);
154 return ErrNoResource
;
156 pInfo
->numThreads_
--;
158 if (0 == pInfo
->numThreads_
) pInfo
->pid_
= 0;
160 systemDatabase
->releaseProcessTableMutex();