File: door.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (187 lines) | stat: -rw-r--r-- 5,324 bytes parent folder | download | duplicates (6)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/****************************************************************************
* GCache                                                                    *
* Author: Federico Ponchio                                                  *
*                                                                           *
* Copyright(C) 2011                                                         *
* Visual Computing Lab                                                      *
* ISTI - Italian National Research Council                                  *
*                                                                           *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/


#ifndef CACHE_DOOR_H
#define CACHE_DOOR_H

#include <wrap/system/multithreading/mt.h>
#include <wrap/system/multithreading/atomic_int.h>

#ifdef NEXUS_USE_QT
#include <QWaitCondition>
#endif

#define METHOD_2

#ifdef METHOD_1

class QDoor {
 private:
  mt::semaphore door;
  mt::mutex room;     //lock when entering. unlock when exiting
  QAtomicInt key; //keep tracks of door status

 public:
  QDoor():key(0) {}
  void open() {
    if(key.testAndSetOrdered(0, 1))
      door.release(1);
  }

  void enter() {
    door.acquire(1); //here I am sure that key is 1
    //if here a open appends will have no effect.
    key.testAndSetOrdered(1, 0);
    room.lock();
  }
  void leave() {
    room.unlock();
  }
  void lock() {
    int r = key.fetchAndStoreOrdered(-1);
    if(r == 1) //if the door was open
      door.tryAcquire(1); //might file if whe are between enter acquire and key = 0.
  }
  void unlock() {
    key = 0;
  }
};
#endif

#ifdef METHOD_2

//a door needs to be open for the thread to continue,
//if it is open the thread enter and closes the door
//this mess is to avoid [if(!open.available()) open.release(1)]

class QDoor {
 private:
  mt::semaphore _open;
  mt::semaphore _close;

 public:
  mt::mutex room;
  QDoor(): _open(0), _close(1) {} //this means closed

  void open() {
    if(_close.tryAcquire(1)) //check it is not open
      _open.release(1); //open
  }
  void close() {
    if(_open.tryAcquire(1)) //check not already closed
      _close.release(1);
  }
  void enter(bool close = false) {
    _open.acquire(1);
    if(close)
      _close.release(1); //close door behind
    else
      _open.release(1);  //leave door opened
   room.lock();
  }
  void leave() { room.unlock(); }

  void lock() {
    //door might be open or closed, but we might happen just in the middle
    //of someone opening, closing or entering it.
    while(!_open.tryAcquire(1) && !_close.tryAcquire(1)) {}
    //no resources left, door is locked
  }
  void unlock(bool open = false) {
    if(open)
      _open.release(1);
    else
      _close.release(1);
  }
  bool isWaiting() {
    if(_open.tryAcquire(1)) {
      _close.release(1);
      return false;
    }
    return true;
  }
};


#endif


#ifdef METHOD_3
/**
  A wait condition class that works as a door.
  Should check if the semaphore version is faster.
*/

class QDoor {
 public:

  QDoor(void) : doorOpen(false), waiting(false) {}

  ///opens the door. Threads trying to enter will be awakened
  void open(void) {
    m.lock();
    doorOpen = true;
    m.unlock();
    c.wakeAll(); arglebargle
  }

  ///attempt to enter the door. if the door is closed the thread will wait until the door is opened.
  /// if close is true, the door will be closed after the thread is awakened, this allows to
  ///   have only one thread entering the door each time open() is called
  void enter(bool close = false) {
    m.lock();
    waiting = true;
    while (!doorOpen)
      c.wait(&(m));

    if(close)
      doorOpen = false;
    waiting = false;
    m.unlock();
  }
  void leave() {}
  bool isWaiting() {
    m.lock();
    bool w = waiting;
    m.unlock();
    return w;
  }
  void lock() { //prevend door opening and entering
    m.lock();
  }
  void unlock(bool open = false) { //reverse effect of lock
    doorOpen = open;
    m.unlock();
  }
 private:
  mt::mutex m;
  QWaitCondition c;
  bool doorOpen;
  bool waiting;
};

#endif


#endif //CACHE_DOOR_H