File: mt_posix.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 (312 lines) | stat: -rw-r--r-- 6,536 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
   FALCON - The Falcon Programming Language.
   FILE: mt_posix.cpp

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

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

   See LICENSE file for licensing details.
*/

#define _XOPEN_SOURCE_EXTENDED
#include <time.h>

// this for gettimeofday on macosx
#include <sys/time.h>

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

namespace Falcon
{

static Mutex s_cs;

ThreadSpecific::ThreadSpecific( void (*destructor)(void*) )
{
   #ifndef NDEBUG
   int value = pthread_key_create( &m_key, destructor );
   fassert( value == 0 );
   #else
   pthread_key_create( &m_key, destructor );
   #endif
}

/** Performs an atomic thread safe increment. */
int32 atomicInc( volatile int32 &data )
{
   s_cs.lock();
   register int32 res = ++data;
   s_cs.unlock();
   return res;
}

/** Performs an atomic thread safe decrement. */
int32 atomicDec( volatile int32 &data )
{
   s_cs.lock();
   register int32 res = --data;
   s_cs.unlock();
   return res;
}



void Event::set()
{
   #ifdef NDEBUG
   pthread_mutex_lock( &m_mtx );
   m_bIsSet = true;

   if ( m_bAutoReset )
      pthread_cond_signal( &m_cv );
   else
      pthread_cond_broadcast( &m_cv );

   pthread_mutex_unlock( &m_mtx );
   #else
   int result = pthread_mutex_lock( &m_mtx );
   fassert( result == 0 );
   m_bIsSet = true;

   if ( m_bAutoReset )
      result = pthread_cond_signal( &m_cv );
   else
      result = pthread_cond_broadcast( &m_cv );

   fassert( result == 0 );
   result = pthread_mutex_unlock( &m_mtx );
   fassert( result == 0 );
   #endif
}


bool Event::wait( int32 to )
{
   pthread_mutex_lock( &m_mtx );

   // are we lucky?
   if( m_bIsSet )
   {
      if ( m_bAutoReset )
         m_bIsSet = false;
      pthread_mutex_unlock( &m_mtx );
      return true;
   }

   // No? -- then are we unlucky?
   if ( to == 0 )
   {
      pthread_mutex_unlock( &m_mtx );
      return false;
   }

   // neither? -- then let's wait. How much?
   if ( to <  0 )
   {
      do {
         pthread_cond_wait( &m_cv, &m_mtx );

      } while( ! m_bIsSet );
   }
   else
   {
      // release the mutex for a while
      pthread_mutex_unlock( &m_mtx );

      struct timespec ts;
      #if _POSIX_TIMERS > 0
         clock_gettime(CLOCK_REALTIME, &ts);
      #else
          struct timeval tv;
          gettimeofday(&tv, NULL);
          ts.tv_sec = tv.tv_sec;
          ts.tv_nsec = tv.tv_usec*1000;
      #endif

      ts.tv_sec += to/1000;
      ts.tv_nsec += (to%1000) * 1000000;
      if( ts.tv_nsec >= 1000000000 )
      {
         ++ts.tv_sec;
         ts.tv_nsec -= 1000000000;
      }

      pthread_mutex_lock( &m_mtx );
      while( ! m_bIsSet )
      {
         int res;
         if( (res = pthread_cond_timedwait( &m_cv, &m_mtx, &ts )) == ETIMEDOUT )
         {
            // wait failed
            pthread_mutex_unlock( &m_mtx );
            return false;
         }
         // be sure that we haven't got other reasons to fail.
         fassert( res == 0 );
      }
   }

   // here, m_bIsSet is set...
   if ( m_bAutoReset )
      m_bIsSet = false;
   pthread_mutex_unlock( &m_mtx );

   return true;
}

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

SysThread::~SysThread()
{
   pthread_mutex_destroy( &m_sysdata->m_mtxT );
   memFree( m_sysdata );
}

SysThread::SysThread( Runnable* r ):
   m_runnable( r )
{
   m_sysdata = ( struct SYSTH_DATA* ) memAlloc( sizeof( struct SYSTH_DATA ) );
   m_sysdata->m_bDetached = false;
   m_sysdata->m_bDone = false;
   m_sysdata->m_lastError = 0;
   pthread_mutex_init( &m_sysdata->m_mtxT, NULL );
}

void SysThread::attachToCurrent()
{
   m_sysdata->pth = pthread_self();
}



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

bool SysThread::start( const ThreadParams &params )
{
   pthread_attr_t attr;

   pthread_attr_init( &attr );
   if( params.stackSize() != 0 )
   {
      if( (m_sysdata->m_lastError =  pthread_attr_setstacksize( &attr, params.stackSize() ) ) != 0 )
      {
         pthread_attr_destroy( &attr );
         return false;
      }
   }

   if ( params.detached() )
   {
      if( (m_sysdata->m_lastError =  pthread_attr_setdetachstate( &attr, params.detached() ? 1:0 ) ) != 0 )
      {
         pthread_attr_destroy( &attr );
         return false;
      }
   }

   // time to increment the reference count of our thread that is going to run
   if ( (m_sysdata->m_lastError = pthread_create( &m_sysdata->pth, &attr, &SysThread::RunAThread, this ) ) != 0 )
   {
      pthread_attr_destroy( &attr );
      return false;
   }
   
   if ( params.detached() )
   {
      detach();
   }

   pthread_attr_destroy( &attr );
   return true;
}

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

void SysThread::detach()
{
   // are we already done?
   pthread_mutex_lock( &m_sysdata->m_mtxT );
   if ( m_sysdata->m_bDone ) 
   {
      pthread_mutex_unlock( &m_sysdata->m_mtxT );
      // disengage system joins and free system data.
      pthread_detach( m_sysdata->pth );
      // free app data.
      delete this;
   }
   else {
      // tell the run function to dispose us when done.
      m_sysdata->m_bDetached = true;
      pthread_mutex_unlock( &m_sysdata->m_mtxT );
   }
}

bool SysThread::join( void* &result )
{
   if ( pthread_join( m_sysdata->pth, &result ) == 0 )
   {
      delete this;
      return true;
   }
   return false;
}


uint64 SysThread::getID()
{
   return (uint64) m_sysdata->pth;
}

uint64 SysThread::getCurrentID()
{
   return (uint64) pthread_self();
}

bool SysThread::isCurrentThread()
{
   return pthread_equal( m_sysdata->pth, pthread_self() ) != 0;
}

bool SysThread::equal( const SysThread *th1 ) const
{
   return pthread_equal( m_sysdata->pth, th1->m_sysdata->pth ) != 0;
}

void *SysThread::run()
{
   fassert( m_runnable !=  0 );
   void* data = m_runnable->run();
   
   // have we been detached in the meanwhile? -- we must dispose our data now.
   pthread_mutex_lock( &m_sysdata->m_mtxT );
   if( m_sysdata->m_bDetached ) 
   {
      pthread_mutex_unlock( &m_sysdata->m_mtxT );
      delete this;
   }
   else {
      m_sysdata->m_bDone = true;
      pthread_mutex_unlock( &m_sysdata->m_mtxT );
   }
   
   return data;
}

}

/* end of mt_posix.cpp */