Procmgmt second patch:
[csql.git] / src / server / Mutex.cxx
blobd52582d492ee81dbeeae4b36230f83356ab59aa7
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 #include <os.h>
17 #include <Mutex.h>
18 #include <Debug.h>
19 #include <NanoTimer.h>
20 Mutex::Mutex()
22 #if defined(sparc) || defined(i686)
23 lock =0;
24 #else
25 pthread_mutexattr_t attr;
26 pthread_mutexattr_init(&attr);
27 pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
28 pthread_mutex_init(&mutex_, &attr);
29 #endif
32 int Mutex::init()
34 #if defined(sparc) || defined(i686)
35 lock = 0;
36 #else
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);
43 #endif
44 return 0;
47 #if defined(sparc) || defined(i686)
48 int TSL(Lock *lock)
51 if (lock == 0) {
52 __asm("mov $1, %eax");
53 __asm("mov 20(%ebp), %edx");
54 __asm("xchg %eax, (%edx)");
55 __asm("test %eax %eax");
56 __asm("jnz .L1000");
57 return 0;
58 } else
60 __asm(".L1000:");
61 return 1;
64 /*Was Working in linux
65 char oldval;
66 __asm__ __volatile__(
67 "xchgb %b0,%1"
68 :"=q" (oldval), "=m" (lock)
69 :"0" (0) : "memory");
71 return oldval > 0;
73 #if defined(i686)
74 int* lw;
75 int res;
76 lw = (int*)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) :
86 "ecx" (lw));
87 //printf("after asm %d ret %d\n", *lock, res);
88 return(res);
90 #elif defined (sparc)
91 Lock res;
92 __asm__ __volatile__("ldstub [%2], %0 \n"
93 "=r"(res), "+m"(*lock)
94 "r"(lock)
95 "memory");
96 return (int) res;
97 #endif
99 #endif
101 int Mutex::tryLock()
103 int tries = 0;
104 int ret = 0;
105 struct timeval timeout;
106 timeout.tv_sec = 0;
107 timeout.tv_usec = 1;
108 //TODO::Mutex timeout should come from csql.conf
110 while (tries < 5)
112 #if defined(sparc) || defined(i686)
113 if (TSL(&lock) == 0) return 0;
114 #else
115 ret = pthread_mutex_trylock(&mutex_);
116 if (EBUSY != ret) return 0;
118 #endif
119 os::select(0, 0, 0, 0, &timeout);
120 tries++;
122 printError(ErrLockTimeOut, "Unable to get the mutex");
123 return 1;
127 int Mutex::getLock()
129 int ret=0;
130 #if defined(sparc) || defined(i686)
131 return tryLock();
132 #else
133 ret = pthread_mutex_lock(&mutex_);
134 #endif
135 if (ret == 0) return 0;
136 else
137 return 1;
140 int Mutex::releaseLock()
142 int ret=0;
143 #if defined(sparc) || defined(i686)
144 /*int *lw = &lock;
145 if (*lw == 0) return 0;
146 __asm__ __volatile__("movl $0, %%eax; xchgl (%%ecx), %%eax" :
147 "=m" (*lw) :
148 "ecx" (lw) :
149 "eax");
151 lock = 0;
152 #else
153 ret = pthread_mutex_unlock(&mutex_);
154 #endif
155 if (ret == 0) return 0;
156 else
157 return 1;
160 int Mutex::destroy()
162 #if defined(sparc) || defined(i686)
163 #else
164 return pthread_mutex_destroy(&mutex_);
165 #endif
166 return 0;