ecere/com: Worked around conflicts building for Android
[sdk] / ecere / src / sys / Mutex.ec
1 namespace sys;
2
3 // Platform includes
4 #define uint _uint
5 #define set _set
6 #if defined(__WIN32__)
7 #define WIN32_LEAN_AND_MEAN
8 #include <windows.h>
9 #else
10 #include <pthread.h>
11 #endif
12 #undef uint
13 #undef set
14
15 import "instance"
16
17 // Moved this here from Thread.ec to make compiling ecereCOM in Debug easier
18 public int GetCurrentThreadID()
19 {
20 #if defined(__WIN32__)
21    return (int)GetCurrentThreadId();
22 #else
23    return pthread_self();
24 #endif
25 }
26
27 public class Mutex : struct
28 {
29 //   class_fixed
30 #if defined(__WIN32__)
31 #ifdef _DEBUG
32    HANDLE mutex;
33 #else
34    CRITICAL_SECTION mutex;
35 #endif
36 #else
37    pthread_mutex_t mutex;
38 #endif
39
40 #ifdef _DEBUG
41    int owningThread;
42 #endif
43    int lockCount;
44
45    Mutex()
46    {
47 #if defined(__WIN32__)
48 #ifdef _DEBUG
49       mutex = CreateMutex(null, FALSE, null);
50 #else
51       InitializeCriticalSection(&mutex);
52 #endif
53 #else
54       pthread_mutexattr_t attr;
55       pthread_mutexattr_init(&attr);
56
57 #ifdef __linux__
58       pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
59 #else
60       pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
61 #endif
62
63       pthread_mutex_init(&mutex, &attr);
64       pthread_mutexattr_destroy(&attr);
65 #endif
66       lockCount = 0;
67 #ifdef _DEBUG
68       owningThread = 0;
69 #endif
70       return true;
71    }
72
73    ~Mutex()
74    {
75 #if defined(__WIN32__)
76 #ifdef _DEBUG
77       CloseHandle(mutex);
78 #else
79       DeleteCriticalSection(&mutex);
80 #endif
81 #else
82       pthread_mutex_destroy(&mutex);
83 #endif
84    }
85
86 public:
87    void Wait(void)
88    {
89       if(this)
90       {
91          /*
92          if(this == globalSystem.fileMonitorMutex)
93             printf("[%d] Waiting on Mutex %x\n", GetCurrentThreadID(), this);
94          */
95 #if defined(__WIN32__)
96 #ifdef _DEBUG
97          if(WaitForSingleObject(mutex, INFINITE /*2000*/) == WAIT_TIMEOUT)
98             PrintLn("Deadlock?");
99 #else
100          EnterCriticalSection(&mutex);
101 #endif
102 #else
103          pthread_mutex_lock(&mutex);
104 #endif
105
106 #ifdef _DEBUG
107          owningThread = GetCurrentThreadID();
108 #endif
109          lockCount++;
110       }
111    }
112
113    void Release(void)
114    {
115       if(this)
116       {
117          /*
118          if(this == globalSystem.fileMonitorMutex)
119             printf("[%d] Releasing Mutex %x\n", GetCurrentThreadID(), this);
120          */
121
122          if(!--lockCount)
123 #ifdef _DEBUG
124             owningThread = 0;
125 #else
126             ;
127 #endif
128 #if defined(__WIN32__)
129 #ifdef _DEBUG
130          ReleaseMutex(mutex);
131 #else
132          LeaveCriticalSection(&mutex);
133 #endif
134 #else
135          pthread_mutex_unlock(&mutex);
136 #endif
137       }
138    }
139 };