File: JobList.cpp

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 56,756 kB
  • sloc: ansic: 432,689; java: 87,068; cpp: 31,958; sh: 29,429; jsp: 7,757; perl: 6,336; xml: 5,828; makefile: 4,701; csh: 3,934; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361
file content (260 lines) | stat: -rw-r--r-- 7,597 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 *
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 *
 *  Sun Microsystems Inc., March, 2001
 *
 *
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *   Copyright: 2001 by Sun Microsystems, Inc.
 *
 *   All Rights Reserved.
 *
 * Portions of this software are Copyright (c) 2011 Univa Corporation
 *
 ************************************************************************/
/*___INFO__MARK_END__*/

#include <winsock2.h>
#include "Job.h"
#include "JobList.h"
#include "Logging.h"

/****** C_JobList::C_JobList() ************************************************
*  NAME
*     C_JobList::C_JobList() -- constructor
*
*  SYNOPSIS
*     C_JobList::C_JobList()
*
*  FUNCTION
*     Initializes C_JobList object.
*
*  NOTES
*******************************************************************************/
C_JobList::C_JobList()
{
   m_hJobListMutex = CreateMutex(NULL, FALSE, "ListMutex");
   m_pFirst = NULL;
}

C_JobList::~C_JobList()
{
   C_Job *pNext = NULL;
   C_Job *pJob  = m_pFirst;

   while (pJob != NULL) {
      pNext = pJob->next;
      delete pJob;
      pJob = pNext; 
   }
   CloseHandle(m_hJobListMutex);
}

/****** C_JobList::AddJobToList() *********************************************
*  NAME
*     C_JobList::AddJobToList() -- adds new job to the global job list
*
*  SYNOPSIS
*     POSITION C_JobList::AddJobToList(C_Job *pJob)
*
*  FUNCTION
*     Adds the new job to the global job list. If the job (identified by
*     job_id, ja_task_id, pe_task_id) already exists in the job list,
*     function fails.
*
*  INPUTS
*     C_Job *pJob - the new job object that is to be added to the list
*
*  RESULT
*     BOOL - true if the new job object already existed in the list,
*            false if it is really new.
*
*  NOTES
*******************************************************************************/
BOOL C_JobList::AddJobToList(C_Job *pJob)
{
   C_Job    *pExistingJob = NULL;
   BOOL     bAlreadyInList = TRUE;

   // lock access to job list
   WaitForSingleObject(m_hJobListMutex, INFINITE);
   pExistingJob = FindJobInList(*pJob);
   if(pExistingJob == NULL) {
      bAlreadyInList = FALSE;
      AddTail(pJob);
   }

   // unlock access to job list
   ReleaseMutex(m_hJobListMutex);

   return bAlreadyInList;
}

void C_JobList::AddTail(C_Job *pJob)
{
   C_Job *pPrevElem = NULL;
   C_Job *pCurElem = m_pFirst;

   // search last element in list
   while (pCurElem != NULL) {
      pPrevElem = pCurElem;
      pCurElem = pCurElem->next;
   }

   // in any case, we insert the last elem, so let it point to NULL.
   pJob->next = NULL;
   if (pPrevElem != NULL) {
      pPrevElem->next = pJob;
   } else {
      m_pFirst = pJob;
   }
}

/****** C_JobList::RemoveJobFromList() *****************************************
*  NAME
*     C_JobList::RemoveJobFromList() -- removes job from the job list
*
*  SYNOPSIS
*     C_Job *C_JobList::RemoveJobFromList(C_Job &Job)
*
*  FUNCTION
*     Removes a job object from the job list. 
*
*  INPUTS
*     C_Job &pJob - a temporary job object that contains the job_id, ja_task_id
*                   and pe_task_id of the job object that is to be removed from
*                   the list.
*
*  RESULT
*     C_Job* - pointer to the job object that was removed from the list.
*              NULL if the job object was not found in the list.
*
*  NOTES
*******************************************************************************/
C_Job *C_JobList::RemoveJobFromList(C_Job &Job)
{
   C_Job    *pJob = NULL;
   C_Job    *pPrev = NULL;

   // lock access to job list
   WaitForSingleObject(m_hJobListMutex, INFINITE);

   // get job from job list
   pJob = m_pFirst;
   while (pJob != NULL) {
      if(pJob->m_job_id == Job.m_job_id
         && pJob->m_ja_task_id == Job.m_ja_task_id
         && ((pJob->m_pe_task_id==NULL && Job.m_pe_task_id==NULL)
            || strcmp(pJob->m_pe_task_id, Job.m_pe_task_id)==0)
         && (pJob->m_JobStatus == js_Finished 
            || pJob->m_JobStatus == js_Failed
            || pJob->m_JobStatus == js_Deleted)) {
         if (pPrev != NULL) {
            pPrev->next = pJob->next;
         } else {
            m_pFirst = pJob->next;
         }
         break;
      }
      pPrev = pJob;
      pJob = pJob->next;
   }
   // unlock access to job list
   ReleaseMutex(m_hJobListMutex);

   return pJob;
}

/****** C_JobList::FindJobInList() ********************************************
*  NAME
*     C_JobList::FindJobInList() -- searches job in job list
*
*  SYNOPSIS
*     C_Job *C_JobList::FindJobInList(C_Job &Job)
*
*  FUNCTION
*     Searches a job object in the list
*
*  INPUTS
*     C_Job &pJob - a temporary job object that contains the job_id, ja_task_id
*                   and pe_task_id of the job object that is to be searched in 
*                   the list.
*
*  RESULT
*     C_Job* - pointer to a copy of the job object that was found in the list.
*              Delete this copy if you finished using it!
*              NULL if the job object was not found in the list.
*
*  NOTES
*    MT-Note: Lock list before calling this function!
*******************************************************************************/
C_Job *C_JobList::FindJobInList(C_Job &Job)
{
   C_Job *pJob = m_pFirst;

   // get job from job list that is not already executed
   while (pJob != NULL) {
      if(pJob->m_job_id == Job.m_job_id
         && pJob->m_ja_task_id == Job.m_ja_task_id
         && ((pJob->m_pe_task_id==NULL && Job.m_pe_task_id==NULL)
            || strcmp(pJob->m_pe_task_id, Job.m_pe_task_id)==0)) {
         break;
      }
      pJob = pJob->next;
   }
   return pJob;
}

/****** C_JobList::GetFirstJobInReceivedState() ********************************
*  NAME
*     C_JobList::GetFirstJobInReceivedState() -- searches first job in 
*                                                js_Revceived state
*
*  SYNOPSIS
*     C_Job *C_JobList::GetFirstJobInReceivedState(C_Job &Job)
*
*  FUNCTION
*     Searches the first job in js_Received state in the list
*
*  RESULT
*     C_Job* - pointer to the first job object in js_Received state in the list.
*              NULL if no object in js_Received state was found in the list.
*
*  NOTES
*    MT-Note: Lock list before calling this function!
*******************************************************************************/
C_Job* C_JobList::GetFirstJobInReceivedState()
{
   C_Job    *pJob = m_pFirst;

   while (pJob != NULL) {
      if (pJob->m_JobStatus == js_Received) {
         break;
      }
      pJob = pJob->next;
   }
   return pJob;
}

BOOL C_JobList::IsEmpty()
{
   return m_pFirst == NULL;
}