File: threading_mod.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 (240 lines) | stat: -rw-r--r-- 4,558 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
/*
   FALCON - The Falcon Programming Language.
   FILE: threading_mod.cpp

   Threading module binding extensions - internal implementation.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Thu, 10 Apr 2008 23:26:43 +0200

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

   See LICENSE file for licensing details.
*/

/** \file
   Threading module binding extensions - internal implementation.
*/

#include <falcon/setup.h>
#include <falcon/fassert.h>
#include <falcon/coreobject.h>
#include <falcon/vm.h>
#include <falcon/garbagelock.h>

#include "threading_mod.h"

namespace Falcon {
namespace Ext {

static void vmthread_killer( void *vmt ) 
{
   ThreadImpl *th = static_cast<ThreadImpl*>( vmt );
   th->decref();
}

static ThreadSpecific m_curthread( vmthread_killer );

void setRunningThread( ThreadImpl* th )
{
   ThreadImpl *old = static_cast<ThreadImpl*>( m_curthread.get() );
   if ( old != 0 )
      old->decref();
   
   if ( th != 0 )
      th->incref();

   m_curthread.set( th );
}

ThreadImpl* getRunningThread()
{
   return static_cast<ThreadImpl*>( m_curthread.get() );
}

//========================================================
// Thread carrier
//

ThreadCarrier::ThreadCarrier( ThreadImpl *t ):
   m_thi( t )
{
}

ThreadCarrier::ThreadCarrier( const ThreadCarrier &other ):
   m_thi( other.m_thi )
{
   m_thi->incref();
}

ThreadCarrier::~ThreadCarrier()
{
   m_thi->decref();
}


FalconData *ThreadCarrier::clone() const
{
   return new ThreadCarrier( *this );
}

//========================================================
// VMRunner thread
//

static int s_threadId = 0;

ThreadImpl::ThreadImpl():
   m_nRefCount(1),
   m_sth(0),
   m_vm( new VMachine ),
   m_lastError( 0 ),
   m_id( atomicInc( s_threadId ) )
{
   m_sysData = createSysData();
}

ThreadImpl::ThreadImpl( const String &name ):
   m_nRefCount(1),
   m_sth(0),
   m_vm( new VMachine ),
   m_lastError( 0 ),
   m_id( atomicInc( s_threadId ) ),
   m_name( name )
{
   m_sysData = createSysData();
}


ThreadImpl::ThreadImpl( VMachine *vm ):
   m_nRefCount(1),
   m_vm( vm ),
   m_lastError( 0 ),
   m_id( atomicInc( s_threadId ) )
{
   m_vm->incref();
   m_thstatus.startable(); // an adopted VM is running.
   m_sth = new SysThread;
   m_sth->attachToCurrent();
   
   m_sysData = createSysData();
}

ThreadImpl::~ThreadImpl()
{
   m_vm->decref();
   // don't delete sth; it has been disposed by detach or join.
   if ( m_lastError != 0 )
      m_lastError->decref();
      
   disposeSysData( m_sysData );
   
   if ( m_sth != 0 )
   {
      void *data;
      // ok even if detached.
      m_sth->join( data );
   }
}

void ThreadImpl::incref()
{
   atomicInc( m_nRefCount );
}

void ThreadImpl::decref()
{
   int count = atomicDec( m_nRefCount );
   
   if ( count == 0 )
      delete this;
}


void ThreadImpl::prepareThreadInstance( const Item &instance, const Item &method )
{
   // create the instance of the method for faster identify
   // The caller should have already certified that the instance has a "run" method
   fassert( method.isCallable() );
   m_threadInstance = instance;
   m_method = method;
}


void *ThreadImpl::run()
{
   m_vm->incref();
   setRunningThread( this );
   
   // hold a lock to the item, as it cannot be taken in the vm.
   GarbageLock tiLock( m_threadInstance );
   GarbageLock mthLock( m_method );

   // Perform the call.
   try {
      m_vm->callItem( m_method, 0 );
      m_lastError = 0;
   }
   catch( Error* err )
   {
      m_lastError = err;
   }

   m_vm->finalize();  // and we won't use it anymore
   m_thstatus.terminated();

   return 0;
}


bool ThreadImpl::start( const ThreadParams &params )
{
   // never call this before startable... so.
   fassert( m_sth == 0 );
   m_sth = new SysThread(this);
   return m_sth->start( params );
}

bool ThreadImpl::detach()
{
   if( m_thstatus.detach() )
   {
      m_sth->detach();
      m_sth = 0;
      return true;
   }
   
   return false;
}

//=========================================================
// WaitableCarrier
//

WaitableCarrier::WaitableCarrier( Waitable *t ):
   m_wto( t )
{
   t->incref();
}

WaitableCarrier::WaitableCarrier( const WaitableCarrier &other )
{
   m_wto = other.m_wto;
   m_wto->incref();
}

WaitableCarrier::~WaitableCarrier()
{
   m_wto->decref();
}

FalconData *WaitableCarrier::clone() const
{
   return new WaitableCarrier( *this );
}


}
}
/* end of threading_mod.cpp */