File: baton_win.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (267 lines) | stat: -rw-r--r-- 6,967 bytes parent folder | download | duplicates (2)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
   FALCON - The Falcon Programming Language.
   FILE: baton_win.cpp

   Baton synchronization structure.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Sat, 14 Mar 2009 00:03:28 +0100

   -------------------------------------------------------------------
   (C) Copyright 2004: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

#include <falcon/memory.h>
#include <falcon/baton.h>
#include <falcon/fassert.h>
#include <falcon/mt_win.h>

namespace Falcon {

typedef struct tag_WIN_BATON_DATA 
{
   HANDLE hEvtIdle;
   HANDLE hEvtUnblocked;
   DWORD nBlockerId;
   CRITICAL_SECTION cs;
} WIN_BATON_DATA;


Baton::Baton( bool bBusy )
{
   m_data = (WIN_BATON_DATA *) memAlloc( sizeof( WIN_BATON_DATA ) );
   WIN_BATON_DATA &p = *(WIN_BATON_DATA *)m_data;
   p.nBlockerId = 0;
   // bBusy is reverse with respect to hEvtIdle
   p.hEvtIdle = CreateEvent( NULL, FALSE, bBusy ? FALSE : TRUE , NULL );
   // Unblocker is manual reset (let's everyone go in).
   p.hEvtUnblocked = CreateEvent( NULL, TRUE, TRUE, NULL );
   InitializeCriticalSection( &p.cs );
}


Baton::~Baton()
{
   WIN_BATON_DATA &p = *(WIN_BATON_DATA *)m_data;
   
   CloseHandle( p.hEvtIdle );
   CloseHandle( p.hEvtUnblocked );
   DeleteCriticalSection( &p.cs );
   memFree( m_data );
}

void Baton::acquire()
{
   WIN_BATON_DATA &p = *(WIN_BATON_DATA *)m_data;
   
   EnterCriticalSection( &p.cs );
   if( p.nBlockerId != GetCurrentThreadId() )
   {
      if ( p.nBlockerId != 0 )
      {
         LeaveCriticalSection( &p.cs );
         // we're sure we hit the block
         onBlockedAcquire();

         // we must wait both for block to be free and idle to be set.
         HANDLE won[] = { p.hEvtIdle, p.hEvtUnblocked };
         DWORD nRes;
         nRes = WaitForMultipleObjects( 2, won, TRUE, INFINITE );
         fassert( nRes == WAIT_OBJECT_0 );
      }
      else 
      {
         // Currently not blocked
         LeaveCriticalSection( &p.cs );
         
         // we must ignore blocking requests incoming now, or we won't be able to signal that we're blocked.
         // In case we receive, the blocker will have to wait for next acquire. Let's say that now,
         // it's already too late to block us.
         DWORD nRes = WaitForSingleObject( p.hEvtIdle, INFINITE );
         fassert( nRes == WAIT_OBJECT_0 );
      }

      // we have acquired the IDLE  event, which is auto reset, so we can proceed.
      return;
   }

   // we are the blocker; must wait only on the idle event.
   LeaveCriticalSection( &p.cs );
   
   DWORD nRes;
   nRes = WaitForSingleObject( p.hEvtIdle, INFINITE );
   fassert( nRes == WAIT_OBJECT_0 );

   EnterCriticalSection( &p.cs );
   p.nBlockerId = 0;
   LeaveCriticalSection( &p.cs );
   
   // no more blocked.
   SetEvent( p.hEvtUnblocked );
}


bool Baton::busy()
{
   WIN_BATON_DATA &p = *(WIN_BATON_DATA *)m_data;
     
   // we must hold the mutex to avoid block to think that the baton is
   // busy, while we're actually keeping it busy here for a short time.
   EnterCriticalSection( &p.cs );
   bool bRet = ! (WaitForSingleObject( p.hEvtIdle, 0 ) == WAIT_OBJECT_0);
   // if it's not busy, we have changed the idle setting; reset it.
   if( ! bRet )
   {
      SetEvent(p.hEvtIdle);
   }
   LeaveCriticalSection( &p.cs );
   return bRet;
}


bool Baton::tryAcquire()
{
   WIN_BATON_DATA &p = *(WIN_BATON_DATA *)m_data;
   
   EnterCriticalSection( &p.cs );
   if( p.nBlockerId != GetCurrentThreadId() )
   {
      LeaveCriticalSection( &p.cs );
      
      // we must wait both for block to be free and idle to be set.
      HANDLE won[] = { p.hEvtIdle, p.hEvtUnblocked };
      DWORD nRes;
      nRes = WaitForMultipleObjects( 2, won, TRUE, 0 );
      return (nRes == WAIT_OBJECT_0);
   }

   // we are the blocker; must wait only on the idle event.
   LeaveCriticalSection( &p.cs );
   
   DWORD nRes;
   nRes = WaitForSingleObject( p.hEvtIdle, 0 );
   if ( nRes == WAIT_OBJECT_0 )
   {   
      EnterCriticalSection( &p.cs );
      p.nBlockerId = 0;
      LeaveCriticalSection( &p.cs );
      
      // no more blocked.
      SetEvent( p.hEvtUnblocked );
      return true;
   }
   
   return false;
}


void Baton::release()
{
   WIN_BATON_DATA &p = *(WIN_BATON_DATA *)m_data;
   
   // Use sync section to be sure I am not idling the baton while someone
   // is trying to block it.
   EnterCriticalSection( &p.cs );
   SetEvent( p.hEvtIdle );
   LeaveCriticalSection( &p.cs );
}


void Baton::checkBlock()
{
   WIN_BATON_DATA &p = *(WIN_BATON_DATA *)m_data;
      
   EnterCriticalSection( &p.cs );
   if( p.nBlockerId == GetCurrentThreadId() )
   {
      LeaveCriticalSection( &p.cs );

      // I am the blocker; there's nothing we should do now.
      return;
   }
   else {
      if( p.nBlockerId != 0 )
      {
         // Idle in a sync section to avoid idling after blockers block.
         SetEvent( p.hEvtIdle );
         LeaveCriticalSection( &p.cs );
         
         onBlockedAcquire();
         
         // wait for the unblocked event to be on; We just set idle, but we won't steal it
         // as we know for sure that EvtUnblocked is on. If it's not on (i.e. because the
         // blocker has forfaited in the meanwhile) we may re-acquire evtIdle, but that's ok.
         HANDLE won[] = { p.hEvtIdle, p.hEvtUnblocked };
         DWORD nRes;
         nRes = WaitForMultipleObjects( 2, won, TRUE, INFINITE );
         fassert(nRes == WAIT_OBJECT_0);
      }
      else {
         LeaveCriticalSection( &p.cs );
      }
   }

}


bool Baton::block()
{
   WIN_BATON_DATA &p = *(WIN_BATON_DATA *)m_data;
   // first, issue a block try
      
   EnterCriticalSection( &p.cs );
   // already blocked? -- we can't do anything.
   if( p.nBlockerId != GetCurrentThreadId() && p.nBlockerId != 0 )
   {
      LeaveCriticalSection( &p.cs );
      return false;
   }
   // Noone can release the baton while we lock the mutex.
   // This gives us the chance to see if the baton is idle.
   if ( WaitForSingleObject( p.hEvtIdle, 0 ) == WAIT_OBJECT_0 )
   {
      // Yes, the baton was idle (we jave just acquired it).
      // This means that noone can reply our block, so we can't perform that.
      // re-idle the baton and go away.
      SetEvent( p.hEvtIdle );
      LeaveCriticalSection( &p.cs );
      return false;
   }
   
   // Ok, we can block. 
   p.nBlockerId = GetCurrentThreadId();
   ResetEvent( p.hEvtUnblocked );
   LeaveCriticalSection( &p.cs );
   
   return true;
}

bool Baton::unblock()
{
   WIN_BATON_DATA &p = *(WIN_BATON_DATA *)m_data;
   EnterCriticalSection( &p.cs );
   if( p.nBlockerId != GetCurrentThreadId() )
   {
      LeaveCriticalSection( &p.cs );
      return false;
   }
   else {
      p.nBlockerId = 0;
      SetEvent( p.hEvtUnblocked );
      LeaveCriticalSection( &p.cs );
   }

   return true;
}

void Baton::onBlockedAcquire()
{
}


}


/* end of baton_win.cpp */