00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef _CRAY_MTLOCK_H
00043 #define _CRAY_MTLOCK_H
00044
00045
00046
00047
00048
00049 #if !defined(__mips) && !defined(_LITTLE_ENDIAN)
00050 #include <cray/libuni.h>
00051 #endif
00052
00053 #if defined(__mips)
00054 #include <mutex.h>
00055 #elif defined(_SOLARIS)
00056 #include <synch.h>
00057 #elif defined(KEY)
00058 #include <pthread.h>
00059 #elif defined(_LITTLE_ENDIAN) && !defined(__sv2)
00060
00061 #endif
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 #if defined(__mips) || (defined(_LITTLE_ENDIAN) && defined(__sv2))
00073 typedef unsigned long plock_t;
00074 #elif defined(_SOLARIS)
00075 typedef mutex_t plock_t;
00076 #elif defined(KEY)
00077 typedef pthread_mutex_t plock_t;
00078 #elif defined(_LITTLE_ENDIAN) && !defined(__sv2)
00079
00080 typedef unsigned long plock_t;
00081 #else
00082 typedef volatile long plock_t;
00083 #endif
00084 typedef long mpplock_t;
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 #define DECL_LOCK(name) plock_t name;
00098 #define DECL_MPP_LOCK(name) mpplock_t name;
00099 #define EXTERN_LOCK(name) extern plock_t name;
00100 #define EXTERN_MPP_LOCK(name) extern mpplock_t name;
00101
00102
00103
00104
00105
00106 #if defined(_CRAY1) || defined(_LIB_UMK)
00107 #define MULTI_ON ($MULTION == 0)
00108 #elif defined(__mips)
00109 #define MULTI_ON 1
00110 #elif defined(_SOLARIS)
00111 #define MULTI_ON 1
00112 #else
00113 #define MULTI_ON 0
00114 #endif
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 #if defined(_CRAY1) || defined(_LIB_UMK)
00125 #define MEM_LOCK(lock) { if (MULTI_ON) _inline_mtlock(lock); }
00126 #define MEM_UNLOCK(lock) { if (MULTI_ON) _inline_mtunlock(lock); }
00127
00128 #elif defined(__mips) || (defined(_LITTLE_ENDIAN) && defined(__sv2))
00129 #define MEM_LOCK(lock) { while (__lock_test_and_set((lock),1L)); }
00130 #define MEM_UNLOCK(lock) { *(lock) = 0; }
00131
00132 #elif defined(_SOLARIS)
00133 #define MEM_LOCK(lock) mutex_lock(lock);
00134 #define MEM_UNLOCK(lock) mutex_unlock(lock);
00135
00136 #elif defined(KEY)
00137
00138
00139
00140
00141 #pragma weak pthread_mutex_init
00142 #pragma weak pthread_mutex_lock
00143 #pragma weak pthread_mutex_unlock
00144 #define MEM_LOCK_INIT PTHREAD_MUTEX_INITIALIZER
00145 #define MEM_LOCK(lock) { if (pthread_mutex_lock) pthread_mutex_lock(lock); }
00146 #define MEM_UNLOCK(lock) { if (pthread_mutex_unlock) pthread_mutex_unlock(lock); }
00147
00148 #elif defined(_LITTLE_ENDIAN) && !defined(__sv2)
00149
00150
00151
00152 #define MEM_LOCK(lock)
00153 #define MEM_UNLOCK(lock)
00154
00155 #else
00156 #define MEM_LOCK(lock)
00157 #define MEM_UNLOCK(lock)
00158
00159 #endif
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 #if defined(_CRAY1) || defined(_LIB_UMK)
00171 #define LMEM_LOCK(lock) { _inline_mtlock(lock); }
00172 #define LMEM_UNLOCK(lock) { _inline_mtunlock(lock); }
00173
00174 #else
00175
00176 #define LMEM_LOCK(lock) MEM_LOCK(lock)
00177 #define LMEM_UNLOCK(lock) MEM_UNLOCK(lock)
00178
00179 #endif
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 #ifdef _CRAYMPP
00191 #define MPP_LOCK(lock) { if (_num_pes() > 1) _shmem_set_lock(lock); }
00192 #define MPP_UNLOCK(lock) { if (_num_pes() > 1) _shmem_clear_lock(lock); }
00193 #else
00194 #define MPP_LOCK(lock)
00195 #define MPP_UNLOCK(lock)
00196 #endif
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 #ifdef MULTIP
00208 #define LIB_LOCK(index) MEM_LOCK(&__lib_locks[index])
00209 #define LIB_UNLOCK(index) MEM_UNLOCK(&__lib_locks[index])
00210 #else
00211 #define LIB_LOCK(index)
00212 #define LIB_UNLOCK(index)
00213 #endif
00214
00215
00216
00217
00218
00219 #define ENVIRON_LOCK 0
00220 #define TRACEBACK_LOCK 1
00221 #define MESSAGE_LOCK 2
00222 #define LIB_LOCK_MAX 64
00223
00224
00225
00226
00227
00228 #ifdef MULTIP
00229 extern plock_t __lib_locks[];
00230 #pragma common __lib_locks
00231 #endif
00232
00233 #if defined(_CRAY1) || defined(_LIB_UMK)
00234
00235 #pragma _CRI soft $MULTION
00236 extern $MULTION(void);
00237
00238 #pragma _CRI soft _lockon
00239 extern int _lockon(plock_t *);
00240
00241 #pragma _CRI soft _lockoff
00242 extern int _lockoff(plock_t *);
00243
00244 #endif
00245
00246
00247
00248
00249
00250 #define SIGNBIT (1<<63)
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 #if defined(_CRAY1) || defined(_LIB_UMK)
00271
00272 extern void _semts(int);
00273 extern void _semclr(int);
00274 extern void _cmr(void);
00275
00276 #define TSKLK 2
00277
00278 #pragma _CRI inline _inline_mtlock
00279 static void
00280 _inline_mtlock(plock_t *lck)
00281 {
00282 _semts(TSKLK);
00283
00284
00285
00286
00287 if (*lck != 0) {
00288 _semclr(TSKLK);
00289 _lockon(lck);
00290 }
00291 else {
00292 *lck = SIGNBIT;
00293
00294 #if defined(_CRAY1) && defined(_ADDR64)
00295
00296
00297
00298
00299 _Pragma("_CRI suppress");
00300 #endif
00301
00302
00303
00304
00305 _cmr();
00306 _semclr(TSKLK);
00307 }
00308 }
00309
00310 #pragma _CRI inline _inline_mtunlock
00311 static void
00312 _inline_mtunlock(plock_t *lck)
00313 {
00314 _semts(TSKLK);
00315
00316
00317
00318
00319 if ((*lck & ~SIGNBIT) != 0) {
00320
00321
00322
00323
00324 _semclr(TSKLK);
00325 _lockoff(lck);
00326 }
00327 else {
00328
00329
00330
00331
00332
00333
00334
00335
00336 *lck = 0;
00337 _cmr();
00338 _semclr(TSKLK);
00339 }
00340 }
00341
00342 #endif
00343
00344 #undef SIGNBIT
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 #ifdef _CRAYT3D
00361 #define SHARED_CC_BUG_WORKAROUND(name) void name(void) {}
00362 #else
00363 #define SHARED_CC_BUG_WORKAROUND(name)
00364 #endif
00365
00366 #endif