Initial git commit -- Transition from CodeGuard repository
[sdk] / ecere / src / sys / Mutex.ec
1 namespace sys;
2
3 // Platform includes
4 #define uint _uint
5 #if defined(__WIN32__)
6 #define WIN32_LEAN_AND_MEAN
7 #include <windows.h>
8 #else
9 #include <pthread.h>
10 #endif
11 #undef uint
12
13 import "instance"
14
15 public class Mutex : struct
16 {
17 //   class_fixed
18 #if defined(__WIN32__)
19    CRITICAL_SECTION mutex;
20 #else
21    pthread_mutex_t mutex;
22 #endif
23
24    Mutex()
25    {
26 #if defined(__WIN32__)
27       InitializeCriticalSection(&mutex);
28 #else
29       pthread_mutexattr_t attr;
30       pthread_mutexattr_init(&attr);
31
32 #ifdef __linux__
33       pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
34 #else
35       pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
36 #endif
37
38       pthread_mutex_init(&mutex, &attr);
39       pthread_mutexattr_destroy(&attr);
40 #endif
41       return true;
42    }
43
44    ~Mutex()
45    {
46 #if defined(__WIN32__)
47       DeleteCriticalSection(&mutex);
48 #else
49       pthread_mutex_destroy(&mutex);
50 #endif
51    }
52
53 public:
54    void Wait(void)
55    {
56       if(this)
57       {
58          /*
59          if(this == globalSystem.fileMonitorMutex)
60             printf("[%d] Waiting on Mutex %x\n", GetCurrentThreadID(), this);
61          */
62 #if defined(__WIN32__)
63          EnterCriticalSection(&mutex);
64 #else
65          pthread_mutex_lock(&mutex);
66 #endif
67       }
68    }
69
70    void Release(void)
71    {
72       if(this)
73       {
74          /*
75          if(this == globalSystem.fileMonitorMutex)
76             printf("[%d] Releasing Mutex %x\n", GetCurrentThreadID(), this);
77          */
78
79 #if defined(__WIN32__)
80          LeaveCriticalSection(&mutex);
81 #else
82          pthread_mutex_unlock(&mutex);
83 #endif
84       }
85    }
86 };