File: sge_gdi_packet_queue.c

package info (click to toggle)
gridengine 6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 51,532 kB
  • ctags: 51,172
  • sloc: ansic: 418,155; java: 37,080; sh: 22,593; jsp: 7,699; makefile: 5,292; csh: 4,244; xml: 2,901; cpp: 2,086; perl: 1,895; tcl: 1,188; lisp: 669; ruby: 642; yacc: 393; lex: 266
file content (369 lines) | stat: -rw-r--r-- 13,437 bytes parent folder | download
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*___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.
 * 
 ************************************************************************/
/*___INFO__MARK_END__*/

#include <pthread.h>

#include "basis_types.h"

#include "cull/cull.h"

#include "comm/commlib.h"

#include "rmon/sgermon.h"

#include "lck/sge_mtutil.h"

#include "uti/sge_log.h"
#include "uti/sge_thread_ctrl.h"
#include "uti/sge_time.h"

#include "sgeobj/sge_answer.h"

#include "gdi/sge_gdi_ctx.h"
#include "gdi/sge_gdi_packet.h"
#include "gdi/sge_gdi_packet_queue.h"

#define PACKET_QUEUE_MUTEX "packet_queue_mutex"

#define WORKER_WAIT_TIME_S 1

/****** gdi/request_internal/Master_Packet_Queue **************************
*  NAME
*     Master_Packet_Queue
*
*  SYNOPSIS
*     sge_gdi_packet_queue_class_t Master_Packet_Queue;
*
*  FUNCTION
*     Global packet queue used in GDI functions to exchange data 
*     and synchronize threads. 
*
*     Packet producers (listener thread, schedd thread, JVM thread) store 
*     new packets by calling sge_gdi_packet_queue_store_notify(). 
*
*     Packet consumers (worker threads) get incoming packets by calling 
*     sge_gdi_packet_queue_wait_for_new_packet().
*
*     Packet producers are responsible to safe a handle to packets they
*     created because they are responsible to free allocated momory 
*     after the packet is not needed anymore.
*
*     Before producers release allocated momory they have wait until
*     consumers do not access the packet structure anymore. This
*     synchronisation can be done via calls to 
*     sge_gdi_packet_wait_till_handled() and 
*     sge_gdi_packet_broadcast_that_handled().
*
*     sge_gdi_packet_queue_wakeup_all_waiting() function is used during
*     the shutdown process of qmaster to notify consumer threads
*     which are waiting in sge_gdi_packet_queue_wait_for_new_packet(). 
*
*  NOTES
*     MT-NOTE: Master_Packet_Queue is MT safe until it is only
*              accessed with the predefined functions
*
*  SEE ALSO
*     gdi/request_internal/sge_gdi_packet_queue_wait_for_new_packet()
*     gdi/request_internal/sge_gdi_packet_queue_wakeup_all_waiting()
*     gdi/request_internal/sge_gdi_packet_queue_store_notify()
*     gdi/request_internal/sge_gdi_packet_wait_till_handled()
*     gdi/request_internal/sge_gdi_packet_broadcast_that_handled()
*****************************************************************************/
sge_gdi_packet_queue_class_t Master_Packet_Queue = {
   PTHREAD_MUTEX_INITIALIZER,
   PTHREAD_COND_INITIALIZER,
   NULL,
   NULL,
   0,
   false
};

/****** gdi/request_internal/ge_gdi_packet_queue_get_length) *****************
*  NAME
*     sge_gdi_packet_queue_get_length() -- returns the current queue length 
*
*  SYNOPSIS
*     u_long32 sge_gdi_packet_queue_get_length(
*                                sge_gdi_packet_queue_class_t *packet_queue) 
*
*  FUNCTION
*    Returns the queue length of "packet_queue". 
*
*  INPUTS
*     sge_gdi_packet_queue_class_t *packet_queue - packet queue 
*
*  RESULT
*     u_long32 - queue length 
*
*  NOTES
*     MT-NOTE: ge_gdi_packet_queue_get_length() is MT safe 
*
*  SEE ALSO
*     gdi/request_internal/sge_gdi_packet_queue_wait_for_new_packet()
*     gdi/request_internal/sge_gdi_packet_queue_store_notify()
*******************************************************************************/
u_long32
sge_gdi_packet_queue_get_length(sge_gdi_packet_queue_class_t *packet_queue)
{
   u_long32 ret = 0;

   DENTER(TOP_LAYER, "sge_gdi_packet_queue_get_length");
   sge_mutex_lock(PACKET_QUEUE_MUTEX, SGE_FUNC, __LINE__, &(packet_queue->mutex));
   ret =  packet_queue->counter;
   sge_mutex_unlock(PACKET_QUEUE_MUTEX, SGE_FUNC, __LINE__, &(packet_queue->mutex));
   DRETURN(ret);
}

/****** gdi/request_internal/sge_gdi_packet_queue_wakeup_all_waiting() *********
*  NAME
*     sge_gdi_packet_queue_wakeup_all_waiting() -- waitup all waiting 
*
*  SYNOPSIS
*     void sge_gdi_packet_queue_wakeup_all_waiting(
*                                 sge_gdi_packet_queue_class_t *packet_queue) 
*
*  FUNCTION
*     Notifys all threads waiting in sge_gdi_packet_queue_wait_for_new_packet().
*     The threads will check if there is a new packet available or if 
*     the shutdown process of the process were the threads are part of has
*     begun. Threads were both not applys will continue waiting.
*
*  INPUTS
*     sge_gdi_packet_queue_class_t *packet_queue - packet queue 
*
*  RESULT
*     void - none
*
*  NOTES
*     MT-NOTE: sge_gdi_packet_queue_wakeup_all_waiting() is MT safe 
*
*  SEE ALSO
*     gdi/request_internal/sge_gdi_packet_queue_wait_for_new_packet() 
*******************************************************************************/
void
sge_gdi_packet_queue_wakeup_all_waiting(sge_gdi_packet_queue_class_t *packet_queue)
{
   DENTER(TOP_LAYER, "sge_gdi_packet_queue_wakeup_all_waiting");

   sge_mutex_lock(PACKET_QUEUE_MUTEX, SGE_FUNC, __LINE__, &(packet_queue->mutex));

   /*
    * Wakeup all threads waiting for a handled packet. 
    */
   pthread_cond_broadcast(&(packet_queue->cond));

   sge_mutex_unlock(PACKET_QUEUE_MUTEX, SGE_FUNC, __LINE__, &(packet_queue->mutex));
   DRETURN_VOID;
}

/****** gdi/request_internal/sge_gdi_packet_queue_store_notify() **********
*  NAME
*     sge_gdi_packet_queue_store_notify() -- store a new packet and notify 
*
*  SYNOPSIS
*     void sge_gdi_packet_queue_store_notify(
*                                   sge_gdi_packet_queue_class_t *packet_queue, 
*                                   sge_gdi_packet_class_t *packet,
*                                   monitoring_t *monitor) 
*
*  FUNCTION
*     A call to this function will append "packet" at the end of the 
*     "packet_queue" and notify all threads which have called 
*     sge_gdi_packet_queue_wait_for_new_packet(). 
*
*  INPUTS
*     sge_gdi_packet_queue_class_t *packet_queue - packet queue pointer 
*     sge_gdi_packet_class_t *packet             - handle to a new packet 
*     monitoring_t *monitor                      - monitor object pointer
*
*  RESULT
*     void - none
*
*  NOTES
*     MT-NOTE: sge_gdi_packet_queue_store_notify() is MT safe 
*
*  SEE ALSO
*     gdi/request_internal/sge_gdi_packet_queue_wait_for_new_packet() 
*******************************************************************************/
void
sge_gdi_packet_queue_store_notify(sge_gdi_packet_queue_class_t *packet_queue,
                                  sge_gdi_packet_class_t *packet, monitoring_t *monitor)
{
   cl_thread_settings_t *thread_config = NULL; 

   DENTER(TOP_LAYER, "sge_gdi_packet_queue_store_notify");

   thread_config = cl_thread_get_thread_config();

   sge_mutex_lock(PACKET_QUEUE_MUTEX, SGE_FUNC, __LINE__, &(packet_queue->mutex));

   /*    
    * Append the packet at the end of the queue
    */
   if (packet != NULL) {
      if (packet_queue->first_packet == NULL) {
         packet_queue->first_packet = packet;
      } else {
         packet_queue->last_packet->next = packet;
      }
      packet_queue->last_packet = packet;
      packet_queue->counter++;
   }

   DPRINTF((SFN" added new packet (packet_queue->counter = "sge_U32CFormat")\n",
            thread_config ? thread_config->thread_name : "-NA-", packet_queue->counter));

   /*
    * Send a signal through the packet_queue->cond. Worker threads are waiting
    * for that signal so that they can handle the packet. 
    */
   DPRINTF((SFN" notifys one worker\n", thread_config ? thread_config->thread_name : "-NA-"));
   if (packet_queue->waiting > 0) {
      pthread_cond_signal(&(packet_queue->cond));
   }

   sge_mutex_unlock(PACKET_QUEUE_MUTEX, SGE_FUNC, __LINE__, &(packet_queue->mutex));
}

/****** gdi/request_internal/sge_gdi_packet_queue_wait_for_new_packet() ********
*  NAME
*     sge_gdi_packet_queue_wait_for_new_packet() -- wait for a new packet 
*
*  SYNOPSIS
*     bool 
*     sge_gdi_packet_queue_wait_for_new_packet(
*                                   sge_gdi_packet_queue_class_t *packet_queue, 
*                                   sge_gdi_packet_class_t **packet) 
*
*  FUNCTION
*     This function tests if there is a new "packet" in the "packet_queue".
*     If this case it returns immidiately with a handle to the first packet and
*     the packet is dechained from the queue before. If there is no packet the
*     functions blocks for maximally WORKER_WAIT_TIME_S seconds and tests 
*     again if there is a packet available or if the shutdown process has begun.
*
*     find more information in sge_gdi_packet_queue_store_notify() and
*     sge_gdi_packet_queue_wakeup_all_waiting()
* 
*      
* 
*     the thread is not in the process of terminatiion then the thread will block 
*     until someone calls either sge_gdi_packet_queue_store_notify() or
*     sge_gdi_packet_queue_wakeup_all_waiting().
*
*  INPUTS
*     sge_gdi_packet_queue_class_t *packet_queue - packet queue pointer 
*     sge_gdi_packet_class_t **packet            - pointer to an unhandled packet
*                                                  or NULL 
*
*  RESULT
*     bool - error state
*        true  - success
*        false - error occured
*
*  NOTES
*     MT-NOTE: sge_gdi_packet_queue_wait_for_new_packet() is MT safe 
*
*  SEE ALSO
*     gdi/request_internal/sge_gdi_packet_queue_store_notify()
*     gdi/request_internal/sge_gdi_packet_queue_wakeup_all_waiting() 
*******************************************************************************/
bool
sge_gdi_packet_queue_wait_for_new_packet(sge_gdi_packet_queue_class_t *packet_queue,
                                         sge_gdi_packet_class_t **packet,
                                         monitoring_t *monitor)
{
   bool ret = true;
   DENTER(TOP_LAYER, "sge_gdi_packet_queue_wait_for_new_packet");

   if (packet != NULL) {
      cl_thread_settings_t *thread_config = cl_thread_get_thread_config();
      sge_mutex_lock(PACKET_QUEUE_MUTEX, SGE_FUNC, __LINE__, &(packet_queue->mutex));

      /* 
       * Wait until there is a packet in the queue or until we get the information 
       * that shutdown has begun. If none of the events occured then wait
       * that someone calls sge_gdi_packet_queue_wakeup_all_waiting() or
       * sge_gdi_packet_queue_store_notify()
       */
      if (packet_queue->first_packet == NULL) {
         packet_queue->waiting++;
         DPRINTF((SFN" is waiting for packet (packet_queue->waiting = "
                  sge_U32CFormat")\n", thread_config ? thread_config->thread_name : "-NA-", 
                  packet_queue->waiting));
         do {
            struct timespec ts;

            sge_relative_timespec(WORKER_WAIT_TIME_S, &ts);
            pthread_cond_timedwait(&(packet_queue->cond), &(packet_queue->mutex), &ts);
         } while (packet_queue->first_packet == NULL && sge_thread_has_shutdown_started() == false);
         packet_queue->waiting--;
      }

      /* 
       * If there is a packet then dechain it an return it to the caller 
       */
      if (packet_queue->first_packet != NULL) {
         *packet = packet_queue->first_packet;

         if (packet_queue->first_packet == packet_queue->last_packet) {
            packet_queue->last_packet = NULL;
            packet_queue->first_packet = NULL;
         } else {
            packet_queue->first_packet = (*packet)->next;
         }
         (*packet)->next = NULL;
         packet_queue->counter--;
         DPRINTF((SFN" takes packet from priority queue. ("
                  "packet_queue->counter = "sge_U32CFormat
                  "; packet_queue->waiting = "sge_U32CFormat")\n",
                  thread_config ? thread_config->thread_name : "-NA-", 
                  packet_queue->counter, 
                  packet_queue->waiting));
      } else {
         *packet = NULL;
         DPRINTF((SFN" wokeup but got no packet. ("
                  "packet_queue->counter = "sge_U32CFormat
                  "; packet_queue->waiting = "sge_U32CFormat")\n",
                  thread_config ? thread_config->thread_name : "-NA-", 
                  packet_queue->counter, 
                  packet_queue->waiting));
      }

      MONITOR_SET_QLEN(monitor, packet_queue->counter);

      sge_mutex_unlock(PACKET_QUEUE_MUTEX, SGE_FUNC, __LINE__, &(packet_queue->mutex));
   }

   DRETURN(ret);
}