forked from X547/wayland-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaylandEnv.h
97 lines (79 loc) · 1.76 KB
/
WaylandEnv.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#pragma once
#include "WaylandServer.h"
#include <semaphore.h>
#include <Looper.h>
class WaylandEnv {
private:
BHandler *fHandler;
WaylandEnv **fPointer {};
sem_t *fWaitSem {};
static void Assert(bool cond) {if (!cond) abort();}
public:
inline WaylandEnv(BHandler *handler, WaylandEnv **pointer = NULL):
fHandler(handler),
fPointer(pointer)
{
if (fPointer != NULL) {
Assert(*fPointer == NULL);
*fPointer = this;
}
Assert(fHandler->Looper()->CountLocks() == 1);
fHandler->UnlockLooper();
gServerHandler.LockLooper();
}
inline ~WaylandEnv()
{
gServerHandler.UnlockLooper();
fHandler->LockLooper();
if (fPointer != NULL) {
*fPointer = NULL;
if (fWaitSem != NULL) {
sem_post(fWaitSem);
fWaitSem = NULL;
}
}
}
static void Wait(WaylandEnv **pointer, BLooper* looper)
{
int32 globalLocked = 0;
while (gServerHandler.Looper()->IsLocked()) {
gServerHandler.UnlockLooper();
globalLocked++;
}
int32 localLocked = looper->CountLocks() - 1;
for (int32 i = 0; i < localLocked; i++) {
looper->Unlock();
}
while (*pointer != NULL) {
sem_t waitSem;
sem_init(&waitSem, 0, 0);
Assert((*pointer)->fWaitSem == NULL);
(*pointer)->fWaitSem = &waitSem;
looper->Unlock();
while (sem_wait(&waitSem) != 0) {
continue;
}
looper->Lock();
sem_destroy(&waitSem);
}
bool wasLooperUnlocked = false;
if (gServerHandler.Looper()->LockWithTimeout(0) == B_OK) {
globalLocked--;
} else {
looper->Unlock();
localLocked++;
wasLooperUnlocked = true;
}
while (globalLocked != 0) {
gServerHandler.LockLooper();
globalLocked--;
}
while (localLocked != 0) {
looper->Lock();
localLocked--;
}
if (wasLooperUnlocked && *pointer != NULL) {
Wait(pointer, looper);
}
}
};