ecere/sys/ECON: Fixed ECON parsing bug with : inside string
[sdk] / ecere / src / sys / Condition.ec
1 #if defined(BUILDING_ECERE_COM)
2 //#if !defined(__EMSCRIPTEN__)
3 import "Semaphore"
4 //#else
5 //import "ecere"
6 //#endif
7 #else
8 import "ecere"
9 #endif
10
11 namespace sys;
12
13 #if !defined(__EMSCRIPTEN__)
14
15 public class Condition : struct
16 {
17    const char * name;
18    Mutex lock { };
19    int waiting;
20    int signals;
21    Semaphore waitSem { };
22    Semaphore waitDone { };
23
24 public:
25    property const char * name
26    {
27       set { name = value; }
28       get { return name; }
29    }
30
31    void Signal()
32    {
33       lock.Wait();
34       if (waiting > signals)
35       {
36          signals++;
37          waitSem.Release();
38          lock.Release();
39          waitDone.Wait();
40       }
41       else
42          lock.Release();
43    }
44
45    void Wait(Mutex mutex)
46    {
47       lock.Wait();
48       waiting++;
49       lock.Release();
50
51       mutex.Release();
52
53       waitSem.Wait();
54
55       lock.Wait();
56       if(signals > 0)
57       {
58          waitDone.Release();
59          signals--;
60       }
61       waiting--;
62       lock.Release();
63
64       mutex.Wait();
65    }
66 }
67
68 #endif // !defined(__EMSCRIPTEN__)