Feature: 1501518
[csql.git] / src / server / Process.cxx
blob5d884a351509f7f617027c19b701dfd7f70c10ed
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>
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()
30 #ifdef LINUX
31 noThreads++;
32 #else
33 DbRetVal rv = systemDatabase->getProcessTableMutex();
34 if (OK != rv)
36 printError(rv,"Unable to get process table mutex");
37 return rv;
39 pid_t pid;
40 if (appPid == 0)
42 //first thread to register
43 pid = os::getpid();
44 appPid = pid;
45 } else
47 pid = appPid;
49 pthread_t thrid = os::getthrid();
50 ProcInfo* pInfo = systemDatabase->getProcInfo(0);
51 int i=0;
52 ProcInfo* freeSlot = NULL;
53 int freeSlotPos =0;
54 bool freeSlotSelected = false;
55 for (; i < config.getMaxProcs(); i++)
57 if (pInfo->pid_ == pid ) break;
58 if (!freeSlotSelected && 0 == pInfo->pid_)
60 freeSlot = pInfo;
61 freeSlotPos = i;
62 freeSlotSelected=true;
64 pInfo++;
66 if (i == config.getMaxProcs())
68 //First thread to register.
69 ThreadInfo *tInfo =systemDatabase->getThreadInfo(freeSlotPos, 0);
70 tInfo->thrid_ = thrid;
71 tInfo->pid_ = pid;
72 freeSlot->pid_ = pid;
73 freeSlot->numThreads_ = 1;
74 } else
76 ThreadInfo *tInfo = systemDatabase->getThreadInfo(i, 0);
77 int j =0;
78 for (; j < config.getMaxThreads(); j++)
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;
96 pInfo->numThreads_++;
98 systemDatabase->releaseProcessTableMutex();
99 #endif
100 return OK;
102 DbRetVal ProcessManager::deregisterThread()
104 #ifdef LINUX
105 noThreads--;
106 #else
107 DbRetVal rv = systemDatabase->getProcessTableMutex();
108 if (OK != rv)
110 printError(rv,"Unable to get process table mutex");
111 return rv;
113 if (appPid == 0)
115 systemDatabase->releaseProcessTableMutex();
116 printError(ErrSysInternal, "Process not registered\n");
117 return ErrSysInternal;
119 pid_t pid = appPid;
120 pthread_t thrid = os::getthrid();
122 ProcInfo* pInfo = systemDatabase->getProcInfo(0);
123 int i=0;
124 for (; i < config.getMaxProcs(); i++)
126 if (pInfo->pid_ == pid ) break;
127 pInfo++;
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);
137 i=0;
138 for (; i < config.getMaxThreads(); i++)
140 if (thrid == tInfo->thrid_)
142 tInfo->thrid_ = 0;
143 tInfo->pid_ = 0;
144 tInfo->want_ = NULL;
145 tInfo->has_ = NULL;
146 break;
148 tInfo++;
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();
161 #endif
162 return OK;