Enterprise to opensource. 40 files including Makefil.am and .in from storage, sqllog...
[csql.git] / include / Mutex.h
blob8f4edb79a98f728568ad9fa2f22f835fe9f08acd
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
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. *
9 * *
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. *
14 * *
15 ***************************************************************************/
16 #ifndef MUTEX_H
17 #define MUTEX_H
18 #include<os.h>
19 #if defined(sparc)
20 typedef unsigned char Lock;
21 #elif defined (i686) || defined(x86_64)
22 typedef unsigned int Lock;
23 #endif
24 class Mutex
27 #if defined(sparc) || defined(i686) || defined(x86_64)
28 Lock lock;
29 #else
30 pthread_mutex_t mutex_;
31 #endif
32 public:
33 char name[20];
34 Mutex();
35 int init();
36 int init(char *name);
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);
40 int destroy();
41 int recoverMutex();
42 static int CAS(int *ptr, int oldVal, int newVal)
44 unsigned char ret;
45 __asm__ __volatile__ (
46 " lock\n"
47 " cmpxchgl %2,%1\n"
48 " sete %0\n"
49 : "=q" (ret), "=m" (*ptr)
50 : "r" (newVal), "m" (*ptr), "a" (oldVal)
51 : "memory");
53 //above assembly returns 0 in case of failure
54 if (ret) return 0;
56 struct timeval timeout;
57 timeout.tv_sec=0;
58 timeout.tv_usec=1000;
59 os::select(0,0,0,0, &timeout);
60 __asm__ __volatile__ (
61 " lock\n"
62 " cmpxchgl %2,%1\n"
63 " sete %0\n"
64 : "=q" (ret), "=m" (*ptr)
65 : "r" (newVal), "m" (*ptr), "a" (oldVal)
66 : "memory");
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)
73 unsigned char ret;
74 __asm__ __volatile__ ("lock; cmpxchgb %b1, %2"
75 : "=a" (ret)
76 : "m" (*(ptr), "q" (newVal), "0" (oldVal)
77 : "memory");
78 printf("Value of ret is %d\n", ret);
79 //above assembly returns 0 in case of failure
80 if (ret) return 0;
81 struct timeval timeout;
82 timeout.tv_sec=0;
83 timeout.tv_usec=500;
84 os::select(0,0,0,0, &timeout);
85 __asm__ __volatile__ ("lock; cmpxchgb %b1, %2"
86 : "=a" (ret)
87 : "m" (*(ptr), "q" (newVal), "0" (oldVal)
88 : "memory");
89 if(!ret) { printf("DEBUG::CAS Fails\n"); return 1; } else return 0;
90 }*/
93 #endif