~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/lck/sge_mtutil.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 * 
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 * 
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 * 
 
9
 * 
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 * 
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 * 
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 * 
 
26
 *   Copyright: 2003 by Sun Microsystems, Inc.
 
27
 * 
 
28
 *   All Rights Reserved.
 
29
 * 
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
 
 
33
#include <stdlib.h>
 
34
#include <pthread.h>
 
35
#include <string.h>
 
36
#include <sys/time.h>
 
37
 
 
38
/* do not compile in monitoring code */
 
39
#ifndef NO_SGE_COMPILE_DEBUG
 
40
#define NO_SGE_COMPILE_DEBUG
 
41
#endif
 
42
 
 
43
#include "sgermon.h"
 
44
#include "sge_log.h"
 
45
#include "msg_lcklib.h"
 
46
#include "sge_time.h"
 
47
 
 
48
/* enable or disable lock printing*/
 
49
/* #define PRINT_LOCK */
 
50
 
 
51
/****** sge_mtutil/sge_mutex_lock() ********************************************
 
52
*  NAME
 
53
*     sge_mutex_lock() -- Mutex locking wrapper with rmon monitoring
 
54
*
 
55
*  SYNOPSIS
 
56
*     void sge_mutex_lock(const char *mutex_name, const char *func, 
 
57
*     int line, pthread_mutex_t *mutex) 
 
58
*
 
59
*  FUNCTION
 
60
*     Locks the passed mutex. Before and after locking rmon DLOCKPRINTF() 
 
61
*     is used to facilitate tracking of deadlocks that are caused by 
 
62
*     mutexes.
 
63
*
 
64
*  INPUTS
 
65
*     const char *mutex_name - The name of the mutex.
 
66
*     const char *func       - The function where sge_mutex_lock() 
 
67
*                              was called from
 
68
*     int line               - The line number where sge_mutex_lock() 
 
69
*                              was called from
 
70
*     pthread_mutex_t *mutex - The mutex.
 
71
*
 
72
*  NOTES
 
73
*     MT-NOTE: sge_mutex_lock() is MT-safe
 
74
*     MT-NOTE: 
 
75
*     MT-NOTE: This function is considered being MT-safe, even though is does
 
76
*     MT-NOTE: use 'strerror()'. The error message returned from 'strerror()'
 
77
*     MT-NOTE: is not stored and used imediately.
 
78
*
 
79
*  SEE ALSO
 
80
*     sge_mtutil/sge_mutex_unlock()
 
81
*******************************************************************************/
 
82
#ifdef SGE_LOCK_DEBUG
 
83
void sge_mutex_lock(const char *mutex_name, const char *func, int line, pthread_mutex_t *mutex)
 
84
{
 
85
   int res = -1;
 
86
 
 
87
   DENTER(BASIS_LAYER, "sge_mutex_lock");
 
88
 
 
89
   DLOCKPRINTF(("%s() line %d: about to lock mutex \"%s\" : %u\n", func, line, mutex_name, sge_get_gmt()));
 
90
 
 
91
#ifdef PRINT_LOCK
 
92
   {
 
93
      struct timeval now;
 
94
      gettimeofday(&now, NULL);
 
95
      printf("%ld lock %lu:%lus %s\n", (long int) pthread_self(),now.tv_sec, (long unsigned)now.tv_usec, mutex_name); 
 
96
   }   
 
97
#endif  
 
98
  
 
99
   if (( res = pthread_mutex_lock(mutex)) != 0) {
 
100
      CRITICAL((SGE_EVENT, MSG_LCK_MUTEXLOCKFAILED_SSS, func, mutex_name, strerror(res)));
 
101
      abort();
 
102
   }
 
103
 
 
104
   DLOCKPRINTF(("%s() line %d: locked mutex \"%s\" : %u\n", func, line, mutex_name, sge_get_gmt()));
 
105
 
 
106
#ifdef PRINT_LOCK
 
107
   {
 
108
      struct timeval now;
 
109
      gettimeofday(&now, NULL);
 
110
      printf("%ld got lock %lu:%lu %s\n", (long int) pthread_self(),now.tv_sec, now.tv_usec, mutex_name); 
 
111
   }   
 
112
#endif  
 
113
 
 
114
   DRETURN_VOID;
 
115
} /* sge_mutex_lock() */
 
116
#else
 
117
void sge_mutex_lock(const char *mutex_name, const char *func, int line, pthread_mutex_t *mutex)
 
118
{
 
119
   int res = -1;
 
120
 
 
121
   DENTER(BASIS_LAYER, "sge_mutex_lock");
 
122
   
 
123
   if (( res = pthread_mutex_lock(mutex)) != 0) {
 
124
      CRITICAL((SGE_EVENT, MSG_LCK_MUTEXLOCKFAILED_SSS, func, mutex_name, strerror(res)));
 
125
      abort();
 
126
   }
 
127
   
 
128
   DRETURN_VOID;
 
129
} /* sge_mutex_lock() */
 
130
 
 
131
#endif
 
132
 
 
133
/****** sge_mtutil/sge_mutex_unlock() ********************************************
 
134
*  NAME
 
135
*     sge_mutex_unlock() -- Mutex unlocking wrapper with rmon monitoring
 
136
*
 
137
*  SYNOPSIS
 
138
*     void sge_mutex_unlock(const char *mutex_name, const char *func, 
 
139
*     int line, pthread_mutex_t *mutex) 
 
140
*
 
141
*  FUNCTION
 
142
*     Unlocks the passed mutex. Before and after unlocking rmon DLOCKPRINTF() 
 
143
*     is used to facilitate tracking of deadlocks that are caused by 
 
144
*     mutexes.
 
145
*
 
146
*  INPUTS
 
147
*     const char *mutex_name - The name of the mutex.
 
148
*     const char *func       - The function where sge_unmutex_unlock() 
 
149
*                              was called from
 
150
*     int line               - The line number where sge_unmutex_lock() 
 
151
*                              was called from
 
152
*     pthread_mutex_t *mutex - The mutex.
 
153
*
 
154
*  NOTES
 
155
*     MT-NOTE: sge_mutex_unlock() is MT-safe
 
156
*     MT-NOTE: 
 
157
*     MT-NOTE: This function is considered being MT-safe, even though is does
 
158
*     MT-NOTE: use 'strerror()'. The error message returned from 'strerror()'
 
159
*     MT-NOTE: is not stored and used imediately.
 
160
*
 
161
*  SEE ALSO
 
162
*     sge_mtutil/sge_mutex_lock()
 
163
*******************************************************************************/
 
164
#ifdef SGE_LOCK_DEBUG
 
165
void sge_mutex_unlock(const char *mutex_name, const char *func, int line, pthread_mutex_t *mutex)
 
166
{
 
167
   int res = -1;
 
168
 
 
169
   DENTER(BASIS_LAYER, "sge_mutex_unlock");
 
170
 
 
171
   if (( res = pthread_mutex_unlock(mutex)) != 0)
 
172
   {
 
173
      CRITICAL((SGE_EVENT, MSG_LCK_MUTEXUNLOCKFAILED_SSS, func, mutex_name, strerror(res)));
 
174
      abort();
 
175
   }
 
176
 
 
177
#ifdef PRINT_LOCK
 
178
   {
 
179
      struct timeval now;
 
180
      gettimeofday(&now, NULL);
 
181
      printf("%ld unlock %lu:%lu %s\n", (long int) pthread_self(),now.tv_sec, now.tv_usec, mutex_name); 
 
182
   }   
 
183
#endif  
 
184
   
 
185
   DLOCKPRINTF(("%s() line %d: unlocked mutex \"%s\"\n", func, line, mutex_name));
 
186
   
 
187
   DRETURN_VOID;
 
188
} /* sge_mutex_unlock() */
 
189
#else
 
190
void sge_mutex_unlock(const char *mutex_name, const char *func, int line, pthread_mutex_t *mutex)
 
191
{
 
192
   int res = -1;
 
193
 
 
194
   DENTER(BASIS_LAYER, "sge_mutex_unlock");
 
195
 
 
196
   if (( res = pthread_mutex_unlock(mutex)) != 0)
 
197
   {
 
198
      CRITICAL((SGE_EVENT, MSG_LCK_MUTEXUNLOCKFAILED_SSS, func, mutex_name, strerror(res)));
 
199
      abort();
 
200
   }
 
201
  
 
202
   DRETURN_VOID;
 
203
} /* sge_mutex_unlock() */
 
204
#endif
 
205
 
 
206
 
 
207
 
 
208
/****** sge_mtutil/sge_relative_timespec() **************************************
 
209
*  NAME
 
210
*     sge_relative_timespec() -- set timespec to now plus timeout 
 
211
*
 
212
*  SYNOPSIS
 
213
*     static void sge_relative_timespec(signed long timeout, struct 
 
214
*     timespec *ts) 
 
215
*
 
216
*  FUNCTION
 
217
*     Based on the relative timeout passed an absolute timespec is 
 
218
*     returned. The timespec can e.g. be used for pthread_cond_timedwait().
 
219
*     Also a timout of 0 can be used. However if the timespec returned is then 
 
220
*     used with pthread_cond_timedwait() this requires the predicate is checked 
 
221
*     once at least.
 
222
*
 
223
*  INPUTS
 
224
*     signed long timeout - A relative timeout interval or 0
 
225
*
 
226
*  OUTPUTS
 
227
*     struct timespec *ts - An abstime timespec value
 
228
*
 
229
*  NOTES
 
230
*     MT-NOTE: sge_relative_timespec() is MT safe
 
231
*******************************************************************************/
 
232
void sge_relative_timespec(signed long timeout, struct timespec *ts)
 
233
{
 
234
   struct timeval now;
 
235
 
 
236
   /* in examples also clock_gettime(CLOCK_REALTIME, &ts) was used */
 
237
   gettimeofday(&now, NULL);
 
238
   ts->tv_sec = now.tv_sec;
 
239
   ts->tv_nsec = now.tv_usec * 1000;
 
240
 
 
241
   if (timeout != 0)
 
242
      ts->tv_sec += timeout;
 
243
   /* in case of DRMAA_TIMEOUT_NO_WAIT the current system time 
 
244
      can be used assumed the predicate is checked once at least */
 
245
 
 
246
   return;
 
247
}
 
248