1
/*___INFO__MARK_BEGIN__*/
2
/*************************************************************************
4
* The Contents of this file are made available subject to the terms of
5
* the Sun Industry Standards Source License Version 1.2
7
* Sun Microsystems Inc., March, 2001
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
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.
24
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
26
* Copyright: 2003 by Sun Microsystems, Inc.
28
* All Rights Reserved.
30
************************************************************************/
31
/*___INFO__MARK_END__*/
38
/* do not compile in monitoring code */
39
#ifndef NO_SGE_COMPILE_DEBUG
40
#define NO_SGE_COMPILE_DEBUG
45
#include "msg_lcklib.h"
48
/* enable or disable lock printing*/
49
/* #define PRINT_LOCK */
51
/****** sge_mtutil/sge_mutex_lock() ********************************************
53
* sge_mutex_lock() -- Mutex locking wrapper with rmon monitoring
56
* void sge_mutex_lock(const char *mutex_name, const char *func,
57
* int line, pthread_mutex_t *mutex)
60
* Locks the passed mutex. Before and after locking rmon DLOCKPRINTF()
61
* is used to facilitate tracking of deadlocks that are caused by
65
* const char *mutex_name - The name of the mutex.
66
* const char *func - The function where sge_mutex_lock()
68
* int line - The line number where sge_mutex_lock()
70
* pthread_mutex_t *mutex - The mutex.
73
* MT-NOTE: sge_mutex_lock() is MT-safe
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.
80
* sge_mtutil/sge_mutex_unlock()
81
*******************************************************************************/
83
void sge_mutex_lock(const char *mutex_name, const char *func, int line, pthread_mutex_t *mutex)
87
DENTER(BASIS_LAYER, "sge_mutex_lock");
89
DLOCKPRINTF(("%s() line %d: about to lock mutex \"%s\" : %u\n", func, line, mutex_name, sge_get_gmt()));
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);
99
if (( res = pthread_mutex_lock(mutex)) != 0) {
100
CRITICAL((SGE_EVENT, MSG_LCK_MUTEXLOCKFAILED_SSS, func, mutex_name, strerror(res)));
104
DLOCKPRINTF(("%s() line %d: locked mutex \"%s\" : %u\n", func, line, mutex_name, sge_get_gmt()));
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);
115
} /* sge_mutex_lock() */
117
void sge_mutex_lock(const char *mutex_name, const char *func, int line, pthread_mutex_t *mutex)
121
DENTER(BASIS_LAYER, "sge_mutex_lock");
123
if (( res = pthread_mutex_lock(mutex)) != 0) {
124
CRITICAL((SGE_EVENT, MSG_LCK_MUTEXLOCKFAILED_SSS, func, mutex_name, strerror(res)));
129
} /* sge_mutex_lock() */
133
/****** sge_mtutil/sge_mutex_unlock() ********************************************
135
* sge_mutex_unlock() -- Mutex unlocking wrapper with rmon monitoring
138
* void sge_mutex_unlock(const char *mutex_name, const char *func,
139
* int line, pthread_mutex_t *mutex)
142
* Unlocks the passed mutex. Before and after unlocking rmon DLOCKPRINTF()
143
* is used to facilitate tracking of deadlocks that are caused by
147
* const char *mutex_name - The name of the mutex.
148
* const char *func - The function where sge_unmutex_unlock()
150
* int line - The line number where sge_unmutex_lock()
152
* pthread_mutex_t *mutex - The mutex.
155
* MT-NOTE: sge_mutex_unlock() is MT-safe
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.
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)
169
DENTER(BASIS_LAYER, "sge_mutex_unlock");
171
if (( res = pthread_mutex_unlock(mutex)) != 0)
173
CRITICAL((SGE_EVENT, MSG_LCK_MUTEXUNLOCKFAILED_SSS, func, mutex_name, strerror(res)));
180
gettimeofday(&now, NULL);
181
printf("%ld unlock %lu:%lu %s\n", (long int) pthread_self(),now.tv_sec, now.tv_usec, mutex_name);
185
DLOCKPRINTF(("%s() line %d: unlocked mutex \"%s\"\n", func, line, mutex_name));
188
} /* sge_mutex_unlock() */
190
void sge_mutex_unlock(const char *mutex_name, const char *func, int line, pthread_mutex_t *mutex)
194
DENTER(BASIS_LAYER, "sge_mutex_unlock");
196
if (( res = pthread_mutex_unlock(mutex)) != 0)
198
CRITICAL((SGE_EVENT, MSG_LCK_MUTEXUNLOCKFAILED_SSS, func, mutex_name, strerror(res)));
203
} /* sge_mutex_unlock() */
208
/****** sge_mtutil/sge_relative_timespec() **************************************
210
* sge_relative_timespec() -- set timespec to now plus timeout
213
* static void sge_relative_timespec(signed long timeout, struct
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
224
* signed long timeout - A relative timeout interval or 0
227
* struct timespec *ts - An abstime timespec value
230
* MT-NOTE: sge_relative_timespec() is MT safe
231
*******************************************************************************/
232
void sge_relative_timespec(signed long timeout, struct timespec *ts)
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;
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 */