Feature: 1501518
[csql.git] / src / server / Process.cxx
blob6c12556adf3170aed696325ca40b0304a2d80136
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>
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();
29 if (OK != rv)
31 printError(rv,"Unable to get process table mutex");
32 return rv;
34 pid_t pid;
35 if (appPid == 0)
37 //first thread to register
38 pid = os::getpid();
39 appPid = pid;
40 } else
42 pid = appPid;
44 pthread_t thrid = os::getthrid();
45 printf("global pid is %d\n", appPid);
46 ProcInfo* pInfo = systemDatabase->getProcInfo(0);
47 int i=0;
48 ProcInfo* freeSlot = NULL;
49 int freeSlotPos =0;
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_)
57 freeSlot = pInfo;
58 freeSlotPos = i;
59 freeSlotSelected=true;
61 pInfo++;
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;
69 tInfo->pid_ = pid;
70 freeSlot->pid_ = pid;
71 //printf("Address:: %x\n", freeSlot);
72 freeSlot->numThreads_ = 1;
73 } else
75 ThreadInfo *tInfo = systemDatabase->getThreadInfo(i, 0);
76 int j =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;
84 tInfo->pid_ = pid;
85 break;
87 tInfo++;
89 if ( j == config.getMaxThreads())
91 systemDatabase->releaseProcessTableMutex();
92 printError(ErrNoResource, "No free thread slot. Limit reached");
93 return ErrNoResource;
95 printf("Register:pid slot taken is %d and thrslot %d\n", i, j);
97 pInfo->numThreads_++;
99 systemDatabase->releaseProcessTableMutex();
100 return OK;
102 DbRetVal ProcessManager::deregisterThread()
104 DbRetVal rv = systemDatabase->getProcessTableMutex();
105 if (OK != rv)
107 printError(rv,"Unable to get process table mutex");
108 return rv;
110 if (appPid == 0)
112 systemDatabase->releaseProcessTableMutex();
113 printError(ErrSysInternal, "Process not registered\n");
114 return ErrSysInternal;
116 pid_t pid = appPid;
117 pthread_t thrid = os::getthrid();
119 ProcInfo* pInfo = systemDatabase->getProcInfo(0);
120 int i=0;
121 for (; i < config.getMaxProcs(); i++)
123 //printf("Address %x, pid %d :", pInfo, pInfo->pid_);
124 if (pInfo->pid_ == pid ) break;
125 pInfo++;
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);
136 i=0;
137 for (; i < config.getMaxThreads(); i++)
139 if (thrid == tInfo->thrid_)
141 tInfo->thrid_ = 0;
142 tInfo->pid_ = 0;
143 tInfo->want_ = NULL;
144 tInfo->has_ = NULL;
145 break;
147 tInfo++;
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();
161 return OK;