File: mt_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 (254 lines) | stat: -rw-r--r-- 5,918 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
/*
   FALCON - The Falcon Programming Language.
   FILE: mt_win.cpp

   Multithreaded extensions - MS-Windows specific.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Sat, 17 Jan 2009 17:06:47 +0100

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

   See LICENSE file for licensing details.
*/

#include <falcon/mt.h>
#include <falcon/memory.h>
#include <process.h>

namespace Falcon
{
static DWORD s_nxd = 0;

//=================================================================================
// Thread Specific destruction sequence.
//

ThreadSpecific::ThreadSpecific( void (*destructor)(void*) )
{
   if ( s_nxd == 0 )
      s_nxd = TlsAlloc();

   m_key = TlsAlloc();
   m_destructor = destructor;
}


ThreadSpecific* ThreadSpecific::clearAndNext()
{
   ThreadSpecific* nextd = m_nextDestructor;
   void* value = TlsGetValue( m_key );
   if( value != 0 )
      m_destructor( value );

   return nextd;
}


void ThreadSpecific::set( void *value )
{
   void* data = TlsGetValue( m_key );

   #ifndef NDEBUG
   BOOL res = TlsSetValue( m_key, value );
   fassert( res );
   #else
   TlsSetValue( m_key, value );
   #endif

   if( data == 0 && value != 0 && m_destructor != 0 )
   {
      m_nextDestructor = (ThreadSpecific*) TlsGetValue( s_nxd );
      TlsSetValue( s_nxd, this );
   }
}


//==================================================================================
// System threads. 
//

SysThread::~SysThread() 
{
   CloseHandle( m_sysdata->hEvtDetach );
   DeleteCriticalSection( &m_sysdata->m_csT );
   memFree( m_sysdata );
}

SysThread::SysThread( Runnable* r ):
   m_runnable( r )
{
   m_sysdata = ( struct SYSTH_DATA* ) memAlloc( sizeof( struct SYSTH_DATA ) );
   // The event isactually a barrier.
   m_sysdata->hEvtDetach = CreateEvent( 0, TRUE, FALSE, 0 );
   m_sysdata->retval = 0;

   m_sysdata->m_bDone = false;
   m_sysdata->m_bDetached = false;
   m_sysdata->m_bJoining = false;
   InitializeCriticalSection( &m_sysdata->m_csT );
}

void SysThread::disengage()
{
   delete this;
}

void SysThread::attachToCurrent()
{
   m_sysdata->hThread = GetCurrentThread();
   m_sysdata->nThreadID = GetCurrentThreadId();
}

extern "C" {
   static unsigned int __stdcall run_a_thread( void *data )
   {
      return (unsigned int) SysThread::RunAThread( data );
   }
}

void* SysThread::RunAThread( void *data )
{
   SysThread* sth = (SysThread*) data;
   void* ret = sth->run();

   // fire the destruction sequence
   ThreadSpecific* tdnext = (ThreadSpecific *) TlsGetValue( s_nxd );
   while( tdnext != 0 )
   {
      tdnext = tdnext->clearAndNext();
   }
   
   return ret;
}


bool SysThread::start( const ThreadParams &params )
{
   m_sysdata->hThread = (HANDLE) _beginthreadex( 0, params.stackSize(), &run_a_thread, this, 0, &m_sysdata->nThreadID );
   if ( m_sysdata->hThread == INVALID_HANDLE_VALUE )
      return false;
   
   if ( params.detached() )
      detach();

   return true;
}

void SysThread::detach()
{
   EnterCriticalSection( &m_sysdata->m_csT );
   if( m_sysdata->m_bDone )
   {
      // if we're done, either we or the joiner must destroy us.
      if ( m_sysdata->m_bJoining )
      {
         m_sysdata->m_bDetached = true;
         SetEvent( m_sysdata->hEvtDetach );
         LeaveCriticalSection( &m_sysdata->m_csT );
      }
      else {
         // this prevents joiners to succed while we destroy ourself
         m_sysdata->m_bJoining = true;
         LeaveCriticalSection( &m_sysdata->m_csT );
         delete this;
      }
   }
   else {
      m_sysdata->m_bDetached = true;
      SetEvent( m_sysdata->hEvtDetach );
      LeaveCriticalSection( &m_sysdata->m_csT );
   }
}
   
bool SysThread::join( void* &result )
{
   // ensure just one thread can join.
   EnterCriticalSection( &m_sysdata->m_csT );
   if ( m_sysdata->m_bJoining || m_sysdata->m_bDetached )
   { 
      LeaveCriticalSection( &m_sysdata->m_csT );
      return false;
   }
   else {
      m_sysdata->m_bJoining = true;
      LeaveCriticalSection( &m_sysdata->m_csT );
   }
   
   HANDLE hs[] = { m_sysdata->hEvtDetach, m_sysdata->hThread };
   DWORD wres = WaitForMultipleObjects( 2, hs, FALSE, INFINITE );
   
   if ( wres == WAIT_OBJECT_0 )
   {
      // The thread was detached -- if it's also done, we must destroy it.
      EnterCriticalSection( &m_sysdata->m_csT );
      if( m_sysdata->m_bDone )
      {
         LeaveCriticalSection( &m_sysdata->m_csT );
         delete this;
         return false;
      }
      
      m_sysdata->m_bJoining = false;
      LeaveCriticalSection( &m_sysdata->m_csT );
      return false;  // can't join anymore.
   }
   else if ( wres == WAIT_OBJECT_0 + 1 )
   {
      // Ok, we joined the thread -- and it terminated.
      result = m_sysdata->retval;
      delete this;
      return true;
   }
   
   // wait failed.
   return false;
}


uint64 SysThread::getID()
{
   return (uint64) m_sysdata->nThreadID;
}
   
uint64 SysThread::getCurrentID()
{
   return (uint64) GetCurrentThreadId();
}
   
bool SysThread::isCurrentThread()
{
   return GetCurrentThreadId() == m_sysdata->nThreadID;
}
   
bool SysThread::equal( const SysThread *th1 ) const
{
   return m_sysdata->nThreadID == th1->m_sysdata->nThreadID;
}

void *SysThread::run()
{
   fassert( m_runnable !=  0 );
   void* data = m_runnable->run();
   m_sysdata->retval = data;

   // if we're detached and not joined, we must destroy ourself.
   EnterCriticalSection( &m_sysdata->m_csT );
   if( m_sysdata->m_bDetached && (! m_sysdata->m_bJoining) )
   {
      LeaveCriticalSection( &m_sysdata->m_csT );
      delete this;
   }
   else {
      // otherwise, just let joiners or detachers to the dirty job.
      m_sysdata->m_bDone = true;
      LeaveCriticalSection( &m_sysdata->m_csT );
   }
   
   return data;
}

}

/* end of mt_win.cpp */