ecere/Mutex: Fixed broken Lucid Lynx builds
[sdk] / ecere / src / sys / Mutex.ec
1 #if defined(__ANDROID__)
2 #include <android/log.h>
3
4 #define printf(...)  ((void)__android_log_print(ANDROID_LOG_VERBOSE, "ecere-app", __VA_ARGS__))
5 #endif
6
7 namespace sys;
8
9 #define _GNU_SOURCE
10 #define __USE_UNIX98
11 // Platform includes
12 #define uint _uint
13 #define set _set
14 #define String _String
15 #if defined(__WIN32__)
16 #define WIN32_LEAN_AND_MEAN
17 #include <windows.h>
18 #else
19 #include <pthread.h>
20 #endif
21 #undef uint
22 #undef set
23 #undef String
24
25 import "instance"
26
27 // Moved this here from Thread.ec to make compiling ecereCOM in Debug easier
28 public int GetCurrentThreadID()
29 {
30 #if defined(__WIN32__)
31    return (int)GetCurrentThreadId();
32 #else
33    return (int)pthread_self();
34 #endif
35 }
36
37 public class Mutex : struct
38 {
39 //   class_fixed
40 #if defined(__WIN32__)
41 #ifdef _DEBUG
42    HANDLE mutex;
43 #else
44    CRITICAL_SECTION mutex;
45 #endif
46 #else
47    pthread_mutex_t mutex;
48 #endif
49
50 #ifdef _DEBUG
51    int owningThread;
52 #endif
53    int lockCount;
54
55    Mutex()
56    {
57 #if defined(__WIN32__)
58 #ifdef _DEBUG
59       mutex = CreateMutex(null, FALSE, null);
60 #else
61       InitializeCriticalSection(&mutex);
62 #endif
63 #else
64       pthread_mutexattr_t attr;
65       pthread_mutexattr_init(&attr);
66
67       // settype is available on Linux now, and hopefully _GNU_SOURCE should ensure it is
68 #if 0 // def __linux__
69       pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
70 #else
71       pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
72 #endif
73
74       pthread_mutex_init(&mutex, &attr);
75       pthread_mutexattr_destroy(&attr);
76 #endif
77       lockCount = 0;
78 #ifdef _DEBUG
79       owningThread = 0;
80 #endif
81       return true;
82    }
83
84    ~Mutex()
85    {
86 #if defined(__WIN32__)
87 #ifdef _DEBUG
88       CloseHandle(mutex);
89 #else
90       DeleteCriticalSection(&mutex);
91 #endif
92 #else
93       pthread_mutex_destroy(&mutex);
94 #endif
95    }
96
97 public:
98    void Wait(void)
99    {
100       if(this)
101       {
102          /*
103          if(this == globalSystem.fileMonitorMutex)
104             printf("[%d] Waiting on Mutex %x\n", GetCurrentThreadID(), this);
105          */
106 #if defined(__WIN32__)
107 #ifdef _DEBUG
108          if(WaitForSingleObject(mutex, INFINITE /*2000*/) == WAIT_TIMEOUT)
109             PrintLn("Deadlock?");
110 #else
111          EnterCriticalSection(&mutex);
112 #endif
113 #else
114 #ifdef _DEBUG
115          {
116             int e;
117             e = pthread_mutex_lock(&mutex);
118             if(e)
119                PrintLn("pthread_mutex_lock returned ", e);
120          }
121 #else
122          pthread_mutex_lock(&mutex);
123 #endif
124 #endif
125
126 #ifdef _DEBUG
127          owningThread = GetCurrentThreadID();
128 #endif
129          lockCount++;
130       }
131    }
132
133    void Release(void)
134    {
135       if(this)
136       {
137          /*
138          if(this == globalSystem.fileMonitorMutex)
139             printf("[%d] Releasing Mutex %x\n", GetCurrentThreadID(), this);
140          */
141 #ifdef _DEBUG
142          if(lockCount && owningThread != GetCurrentThreadID())
143             PrintLn("WARNING: Not in owning thread!!");
144 #endif
145
146          if(!--lockCount)
147 #ifdef _DEBUG
148             owningThread = 0;
149 #else
150             ;
151 #endif
152 #if defined(__WIN32__)
153 #ifdef _DEBUG
154          ReleaseMutex(mutex);
155 #else
156          LeaveCriticalSection(&mutex);
157 #endif
158 #else
159 #ifdef _DEBUG
160          {
161             int e;
162             e = pthread_mutex_unlock(&mutex);
163             if(e)
164                PrintLn("pthread_mutex_unlock returned ", e);
165          }
166 #else
167          pthread_mutex_unlock(&mutex);
168 #endif
169 #endif
170 #ifdef _DEBUG
171          if(lockCount < 0)
172             PrintLn("WARNING: lockCount < 0");
173 #endif
174       }
175    }
176 };