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>
22 #if defined(sparc) || defined(i686)
25 pthread_mutexattr_t attr
;
26 pthread_mutexattr_init(&attr
);
27 pthread_mutexattr_setpshared(&attr
, PTHREAD_PROCESS_SHARED
);
28 pthread_mutex_init(&mutex_
, &attr
);
34 #if defined(sparc) || defined(i686)
37 pthread_mutexattr_t attr
;
38 pthread_mutexattr_init(&attr
);
39 int ret
= pthread_mutexattr_setpshared(&attr
, PTHREAD_PROCESS_SHARED
);
40 printf("pthread_mutexattr_setpshared Returned %d\n", ret
);
41 pthread_mutex_init(&mutex_
, &attr
);
42 pthread_mutexattr_destroy(&attr
);
47 #if defined(sparc) || defined(i686)
52 __asm("mov $1, %eax");
53 __asm("mov 20(%ebp), %edx");
54 __asm("xchg %eax, (%edx)");
55 __asm("test %eax %eax");
64 /*Was Working in linux
68 :"=q" (oldval), "=m" (lock)
77 if (*lock
== 1) return 1;
78 /* In assembly we use the so-called AT & T syntax where
79 the order of operands is inverted compared to the ordinary Intel
80 syntax. The 'l' after the mnemonics denotes a 32-bit operation.
81 The line after the code tells which values come out of the asm
82 code, and the second line tells the input to the asm code. */
83 //printf("before asm %d\n", *lock);
84 __asm__
__volatile__("movl $1, %%eax; xchgl (%%ecx), %%eax" :
85 "=eax" (res
), "=m" (*lw
) :
87 //printf("after asm %d ret %d\n", *lock, res);
92 __asm__
__volatile__("ldstub [%2], %0 \n"
93 "=r"(res
), "+m"(*lock
)
105 struct timeval timeout
;
108 //TODO::Mutex timeout should come from csql.conf
112 #if defined(sparc) || defined(i686)
113 if (TSL(&lock
) == 0) return 0;
115 ret
= pthread_mutex_trylock(&mutex_
);
116 if (EBUSY
!= ret
) return 0;
119 os::select(0, 0, 0, 0, &timeout
);
122 printError(ErrLockTimeOut
, "Unable to get the mutex");
130 #if defined(sparc) || defined(i686)
133 ret
= pthread_mutex_lock(&mutex_
);
135 if (ret
== 0) return 0;
140 int Mutex::releaseLock()
143 #if defined(sparc) || defined(i686)
145 if (*lw == 0) return 0;
146 __asm__ __volatile__("movl $0, %%eax; xchgl (%%ecx), %%eax" :
153 ret
= pthread_mutex_unlock(&mutex_
);
155 if (ret
== 0) return 0;
162 #if defined(sparc) || defined(i686)
164 return pthread_mutex_destroy(&mutex_
);