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 //It does not check for re registering as well as deregistering unregistered threads.
25 //as it is handled in the connection class open and close methods.
26 DbRetVal
ProcessManager::registerThread()
28 DbRetVal rv
= systemDatabase
->getProcessTableMutex();
31 printError(rv
,"Unable to get process table mutex");
37 //first thread to register
44 pthread_t thrid
= os::getthrid();
45 printf("global pid is %d\n", appPid
);
46 ProcInfo
* pInfo
= systemDatabase
->getProcInfo(0);
48 ProcInfo
* freeSlot
= NULL
;
50 bool freeSlotSelected
= false;
51 for (; i
< config
.getMaxProcs(); i
++)
53 //printf("Reg: Address %x, pid %d lpid %d:\n", pInfo, pInfo->pid_, pid);
54 if (pInfo
->pid_
== pid
) break;
55 if (!freeSlotSelected
&& 0 == pInfo
->pid_
)
59 freeSlotSelected
=true;
63 if (i
== config
.getMaxProcs())
65 //First thread to register.
66 printf("Register: First thread to register pid %d\n", freeSlotPos
);
67 ThreadInfo
*tInfo
=systemDatabase
->getThreadInfo(freeSlotPos
, 0);
68 tInfo
->thrid_
= thrid
;
71 //printf("Address:: %x\n", freeSlot);
72 freeSlot
->numThreads_
= 1;
75 ThreadInfo
*tInfo
= systemDatabase
->getThreadInfo(i
, 0);
77 for (; j
< config
.getMaxThreads(); j
++)
79 printf("Reg: Address %x, tid %d :\n", tInfo
, tInfo
->thrid_
);
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");
95 printf("Register:pid slot taken is %d and thrslot %d\n", i
, j
);
99 systemDatabase
->releaseProcessTableMutex();
102 DbRetVal
ProcessManager::deregisterThread()
104 DbRetVal rv
= systemDatabase
->getProcessTableMutex();
107 printError(rv
,"Unable to get process table mutex");
112 systemDatabase
->releaseProcessTableMutex();
113 printError(ErrSysInternal
, "Process not registered\n");
114 return ErrSysInternal
;
117 pthread_t thrid
= os::getthrid();
119 ProcInfo
* pInfo
= systemDatabase
->getProcInfo(0);
121 for (; i
< config
.getMaxProcs(); i
++)
123 //printf("Address %x, pid %d :", pInfo, pInfo->pid_);
124 if (pInfo
->pid_
== pid
) break;
127 if (i
== config
.getMaxProcs())
129 systemDatabase
->releaseProcessTableMutex();
130 printError(ErrSysFatal
, "Degistering process %d is not registered with csql", pid
);
131 return ErrNoResource
;
133 ThreadInfo
*tInfo
= systemDatabase
->getThreadInfo(i
, 0);
134 printf("Deregister: pid slot taken is %d ", i
);
137 for (; i
< config
.getMaxThreads(); i
++)
139 if (thrid
== tInfo
->thrid_
)
149 if ( i
== config
.getMaxThreads())
151 systemDatabase
->releaseProcessTableMutex();
152 printError(ErrSysFatal
, "Degistering Pid %d Thread %d is not registered with csql", pid
, thrid
);
153 return ErrNoResource
;
155 pInfo
->numThreads_
--;
156 printf("and thrslot %d nothreads %d\n", i
,pInfo
->numThreads_
);
158 if (0 == pInfo
->numThreads_
) pInfo
->pid_
= 0;
160 systemDatabase
->releaseProcessTableMutex();