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__*/
40
#include "lck/test_sge_lock_main.h"
41
#include "lck/sge_lock.h"
42
#include "lck/sge_mtutil.h"
43
#include "uti/sge_time.h"
47
/*-------------------------*/
48
/* part for the mutex test */
49
/*-------------------------*/
51
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
52
static pthread_once_t log_once = PTHREAD_ONCE_INIT;
54
/*--------------------------------*/
55
/* part for the thread local test */
56
/*--------------------------------*/
57
static pthread_key_t state_key;
64
static void *thread_function(void *anArg);
66
static void state_destroy(void* state)
71
static void state_init(state_t* state)
77
/*---------------------------*/
78
/* sync part between threads */
79
/*---------------------------*/
82
pthread_mutex_t mutex;
85
pthread_cond_t cond_var;
88
static int threads = MAX_THREADS;
90
static sge_control_t Control = {PTHREAD_MUTEX_INITIALIZER, MAX_THREADS,0.0, PTHREAD_COND_INITIALIZER};
92
static void has_finished(const char* str, double time)
94
DENTER(TOP_LAYER, "has_finished");
96
sge_mutex_lock("has_finished", SGE_FUNC, __LINE__, &Control.mutex);
100
if (Control.working == 0) {
101
Control.working = threads;
102
Control.time /= threads;
103
printf("%s : %fs\n", str, Control.time);
105
pthread_cond_broadcast(&Control.cond_var);
110
ts.tv_sec = (long) (sge_get_gmt() + 180);
112
pthread_cond_timedwait(&Control.cond_var,
113
&Control.mutex, &ts);
116
sge_mutex_unlock("has_finished", SGE_FUNC, __LINE__, &Control.mutex);
121
/*---------------------------*/
122
/* part of the general setup */
123
/*---------------------------*/
125
void set_thread_count(int count)
128
Control.working = count;
131
int get_thrd_demand(void)
133
int p = MAX_THREADS; /* min num of threads */
135
pthread_key_create(&state_key, &state_destroy);
140
static void log_once_init(void)
145
void *(*get_thrd_func(void))(void *anArg)
147
return thread_function;
150
void *get_thrd_func_arg(void)
155
/****** test_sge_lock_multiple/thread_function() *********************************
157
* thread_function() -- Thread function to execute
160
* static void* thread_function(void *anArg)
163
* Acquire multiple locks and sleep. Release the locks. After each 'sge_lock()'
164
* and 'sge_unlock()' sleep to increase the probability of interlocked execution.
165
* Note that we deliberately test the boundaries of 'sge_locktype_t'.
168
* void *anArg - thread function arguments
171
* static void* - none
174
* test_sge_lock_multiple/get_thrd_func()
175
*******************************************************************************/
176
static void *thread_function(void *anArg)
178
struct timeval before;
179
struct timeval after;
186
DENTER(TOP_LAYER, "thread_function");
188
has_finished("start",0.0);
190
gettimeofday(&before, NULL);
191
for (i = 0; i < max; i++) {
195
gettimeofday(&after, NULL);
197
time_new = after.tv_usec - before.tv_usec;
198
time_new = after.tv_sec - before.tv_sec + (time_new/1000000);
200
has_finished("variable access", time_new);
202
gettimeofday(&before, NULL);
203
for (i = 0; i < max; i++) {
204
GET_SPECIFIC(state_t, state, state_init, state_key, "test_sge_lock_multiple");
205
state->value2 = state->value +1;
206
state->value = state->value2 +1;
208
gettimeofday(&after, NULL);
210
time_new = after.tv_usec - before.tv_usec;
211
time_new = after.tv_sec - before.tv_sec + (time_new/1000000);
213
has_finished("thread local ", time_new);
215
gettimeofday(&before, NULL);
216
for (i = 0; i < max; i++) {
217
pthread_once(&log_once, log_once_init);
219
GET_SPECIFIC(state_t, state, state_init, state_key, "test_sge_lock_multiple");
220
state->value2 = state->value +1;
221
state->value = state->value2 +1;
224
gettimeofday(&after, NULL);
226
time_new = after.tv_usec - before.tv_usec;
227
time_new = after.tv_sec - before.tv_sec + (time_new/1000000);
229
has_finished("thread local once ", time_new);
231
gettimeofday(&before, NULL);
232
for (i = 0; i < max; i++) {
233
sge_mutex_lock("mutex", SGE_FUNC, __LINE__, &mutex);
236
sge_mutex_unlock("mutex", SGE_FUNC, __LINE__, &mutex);
238
gettimeofday(&after, NULL);
240
time_new = after.tv_usec - before.tv_usec;
241
time_new = after.tv_sec - before.tv_sec + (time_new/1000000);
243
has_finished("mutex ", time_new);
245
gettimeofday(&before, NULL);
246
for (i = 0; i < max; i++) {
247
SGE_LOCK(LOCK_GLOBAL, LOCK_READ);
250
SGE_UNLOCK(LOCK_GLOBAL, LOCK_READ);
252
gettimeofday(&after, NULL);
254
time_new = after.tv_usec - before.tv_usec;
255
time_new = after.tv_sec - before.tv_sec + (time_new/1000000);
257
has_finished("read lock ", time_new);
259
gettimeofday(&before, NULL);
260
for (i = 0; i < max; i++) {
261
SGE_LOCK(LOCK_GLOBAL, LOCK_WRITE);
264
SGE_UNLOCK(LOCK_GLOBAL, LOCK_WRITE);
266
gettimeofday(&after, NULL);
268
time_new = after.tv_usec - before.tv_usec;
269
time_new = after.tv_sec - before.tv_sec + (time_new/1000000);
271
has_finished("write lock ", time_new);
276
} /* thread_function */