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 ***************************************************************************/
20 typedef unsigned char Lock
;
21 #elif defined (i686) || defined(x86_64)
22 typedef unsigned int Lock
;
27 #if defined(sparc) || defined(i686) || defined(x86_64)
30 pthread_mutex_t mutex_
;
37 int tryLock(int tries
=0, int waitmsecs
=0);
38 int getLock(int procSlot
, bool procAccount
=true);
39 int releaseLock(int procSlot
, bool procAccount
=true);
42 static int CAS(int *ptr
, int oldVal
, int newVal
)
45 __asm__
__volatile__ (
49 : "=q" (ret
), "=m" (*ptr
)
50 : "r" (newVal
), "m" (*ptr
), "a" (oldVal
)
53 //above assembly returns 0 in case of failure
56 struct timeval timeout
;
59 os::select(0,0,0,0, &timeout
);
60 __asm__
__volatile__ (
64 : "=q" (ret
), "=m" (*ptr
)
65 : "r" (newVal
), "m" (*ptr
), "a" (oldVal
)
67 //if (ret) return 0; else {printf("DEBUG::CAS Fails %d-\n", ret); return 1; }
68 if (ret
) return 0; else return 1;
71 /*static int CASB(char *ptr, char oldVal, char newVal)
74 __asm__ __volatile__ ("lock; cmpxchgb %b1, %2"
76 : "m" (*(ptr), "q" (newVal), "0" (oldVal)
78 printf("Value of ret is %d\n", ret);
79 //above assembly returns 0 in case of failure
81 struct timeval timeout;
84 os::select(0,0,0,0, &timeout);
85 __asm__ __volatile__ ("lock; cmpxchgb %b1, %2"
87 : "m" (*(ptr), "q" (newVal), "0" (oldVal)
89 if(!ret) { printf("DEBUG::CAS Fails\n"); return 1; } else return 0;