1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 ***************************************************************************/
19 #include <NanoTimer.h>
24 #if defined(sparc) || defined(i686) || defined(x86_64)
27 pthread_mutexattr_t attr
;
28 pthread_mutexattr_init(&attr
);
29 pthread_mutexattr_setpshared(&attr
, PTHREAD_PROCESS_SHARED
);
30 pthread_mutex_init(&mutex_
, &attr
);
36 #if defined(sparc) || defined(i686) || defined(x86_64)
39 pthread_mutexattr_t attr
;
40 pthread_mutexattr_init(&attr
);
41 int ret
= pthread_mutexattr_setpshared(&attr
, PTHREAD_PROCESS_SHARED
);
42 printf("pthread_mutexattr_setpshared Returned %d\n", ret
);
43 pthread_mutex_init(&mutex_
, &attr
);
44 pthread_mutexattr_destroy(&attr
);
48 int Mutex::init(char *mname
)
50 if (strlen(mname
) > 19 ) return 0;
57 #if defined(sparc) || defined(i686) || defined(x86_64)
62 __asm("mov $1, %eax");
63 __asm("mov 20(%ebp), %edx");
64 __asm("xchg %eax, (%edx)");
65 __asm("test %eax %eax");
74 /*Was Working in linux
78 :"=q" (oldval), "=m" (lock)
83 #if defined(i686) || defined(x86_64)
87 if (*lock
== 1) return 1;
88 /* In assembly we use the so-called AT & T syntax where
89 the order of operands is inverted compared to the ordinary Intel
90 syntax. The 'l' after the mnemonics denotes a 32-bit operation.
91 The line after the code tells which values come out of the asm
92 code, and the second line tells the input to the asm code. */
94 /* This assembly compiles only with -O2 option, and not with -g option. Version1
96 "movl $1, %%eax; xchgl (%%ecx), %%eax"
97 : "=eax" (res), "=m" (*lw)
101 /* This assembly takes lot of time for test/performance/DMLTest. Version2
102 __asm__ __volatile__(
103 "movl %1, %0; xchgl %0, %2"
104 : "=r" (res), "=r" (lock)
109 // This assembly is Version3. Working fine for now
110 __asm__
__volatile__(
112 : "=r"(res
), "=m"(*lock
)
116 //fprintf(stderr,"after asm %d ret %d\n", *lock, res);
120 #elif defined (sparc)
122 __asm__
__volatile__("ldstub [%2], %0 \n"
123 "=r"(res
), "+m"(*lock
)
131 int Mutex::tryLock(int tryTimes
, int waitmsecs
)
139 struct timeval timeout
;
141 timeout
.tv_usec
= waitmsecs
;
142 if (tryTimes
== 0 && waitmsecs
== 0)
144 timeout
.tv_sec
= Conf::config
.getMutexSecs();
145 timeout
.tv_usec
= Conf::config
.getMutexUSecs();
146 tryTimes
= Conf::config
.getMutexRetries();
148 while (tries
< tryTimes
)
150 #if defined(sparc) || defined(i686) || defined(x86_64)
156 ret
= pthread_mutex_trylock(&mutex_
);
157 if (EBUSY
!= ret
) return 0;
160 os::select(0, 0, 0, 0, &timeout
);
163 printError(ErrLockTimeOut
, "Unable to get the mutex , tried %d times", tries
);
168 int Mutex::getLock(int procSlot
, bool procAccount
)
171 #if defined(sparc) || defined(i686) || defined(x86_64)
173 //add it to the has_ of the ThreadInfo
174 if (ret
==0 && procAccount
) ProcessManager::addMutex(this, procSlot
);
178 ret
= pthread_mutex_lock(&mutex_
);
180 if (ret
== 0) return 0;
185 int Mutex::releaseLock(int procSlot
, bool procAccount
)
188 #if defined(sparc) || defined(i686) || defined(x86_64)
190 if (*lw == 0) return 0;
191 __asm__ __volatile__("movl $0, %%eax; xchgl (%%ecx), %%eax" :
198 ret
= pthread_mutex_unlock(&mutex_
);
200 if (ret
== 0 && procAccount
)
202 ProcessManager::removeMutex(this, procSlot
);
211 #if defined(sparc) || defined(i686) || defined(x86_64)
213 return pthread_mutex_destroy(&mutex_
);
218 int Mutex::recoverMutex()
221 #if defined(sparc) || defined(i686) || defined(x86_64)
223 if (*lw == 0) return 0;
224 __asm__ __volatile__("movl $0, %%eax; xchgl (%%ecx), %%eax" :
231 ret
= pthread_mutex_unlock(&mutex_
);