~ubuntu-branches/ubuntu/raring/clucene-core/raring-proposed

« back to all changes in this revision

Viewing changes to src/shared/CLucene/config/threads.cpp

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2012-08-11 09:33:38 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120811093338-fgrx41ftqew3qt6a
Tags: 2.3.3.4-1
* New upstream release (Closes: #661703).
* Convert package to multiarch.
* Drop obsolete patches:
  - 01_add_missing_include_bug505667.diff
  - 02_posixness_fix_bug530308.diff
* Add patches:
  - Fixing_ZLIB_configuration_in_shared_CMakeLists.patch
  - Fix-pkgconfig-file-by-adding-clucene-shared-library.patch
  - Install-contribs-lib.patch
  - multiarch.patch
* Update debian/compat: bump to 8.
* Update debian/control:
  - update build dependencies (add cmake, libboost-dev and libz-dev).
  - bump Standards-Version to 3.9.3.
  - rename packages due to ABI bump: libclucene0ldbl -> libclucene-core1.
  - add libclucene-contribs1 package.
* Update debian/rules:
  - rewrite to use CMake.
  - add multiarch support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*------------------------------------------------------------------------------
 
2
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
 
3
*
 
4
* Distributable under the terms of either the Apache License (Version 2.0) or
 
5
* the GNU Lesser General Public License, as specified in the COPYING file.
 
6
------------------------------------------------------------------------------*/
 
7
#include "CLucene/_SharedHeader.h"
 
8
#include "CLucene/LuceneThreads.h"
 
9
#include "_threads.h"
 
10
#include <assert.h>
 
11
 
 
12
CL_NS_DEF(util)
 
13
 
 
14
#ifndef _CL_DISABLE_MULTITHREADING
 
15
 
 
16
#if defined(_LUCENE_DONTIMPLEMENT_THREADMUTEX)
 
17
        //do nothing
 
18
        #if defined(_LUCENE_PRAGMA_WARNINGS)
 
19
         #pragma message ("==================Not implementing any thread mutex==================")
 
20
        #else
 
21
         #warning "==================Not implementing any thread mutex=================="
 
22
        #endif
 
23
 
 
24
 
 
25
 
 
26
#elif defined(_CL_HAVE_WIN32_THREADS)
 
27
        struct mutex_thread::Internal{
 
28
            CRITICAL_SECTION mtx;
 
29
        };
 
30
 
 
31
        mutex_thread::mutex_thread(const mutex_thread& clone):
 
32
                _internal(new Internal)
 
33
        {
 
34
                InitializeCriticalSection(&_internal->mtx);
 
35
        }
 
36
        mutex_thread::mutex_thread():
 
37
                _internal(new Internal)
 
38
        {
 
39
                InitializeCriticalSection(&_internal->mtx);
 
40
        }
 
41
 
 
42
        mutex_thread::~mutex_thread()
 
43
        {
 
44
                DeleteCriticalSection(&_internal->mtx);
 
45
                delete _internal;
 
46
        }
 
47
 
 
48
        void mutex_thread::lock()
 
49
        {
 
50
                EnterCriticalSection(&_internal->mtx);
 
51
        }
 
52
 
 
53
        void mutex_thread::unlock()
 
54
        {
 
55
                LeaveCriticalSection(&_internal->mtx);
 
56
        }
 
57
 
 
58
  _LUCENE_THREADID_TYPE mutex_thread::_GetCurrentThreadId(){
 
59
      return GetCurrentThreadId();
 
60
  }
 
61
  void mutex_thread::_exitThread(int val){
 
62
        ExitThread(val);
 
63
  }
 
64
 
 
65
  int32_t mutex_thread::atomic_increment(_LUCENE_ATOMIC_INT *theInteger){
 
66
#ifdef _M_X64
 
67
    return _InterlockedIncrement64(theInteger);
 
68
#else
 
69
    return InterlockedIncrement(theInteger);
 
70
#endif
 
71
  }
 
72
  int32_t mutex_thread::atomic_decrement(_LUCENE_ATOMIC_INT *theInteger){
 
73
#ifdef _M_X64
 
74
    return _InterlockedDecrement64(theInteger);
 
75
#else
 
76
    return InterlockedDecrement(theInteger);
 
77
#endif
 
78
  }
 
79
 
 
80
 
 
81
 
 
82
        class shared_condition::Internal{
 
83
        public:
 
84
            void* _event;
 
85
            Internal(){
 
86
                _event = CreateEventA( NULL, false, false, NULL );
 
87
            }
 
88
            ~Internal(){
 
89
                CloseHandle( _event );
 
90
            }
 
91
        };
 
92
        shared_condition::shared_condition(){
 
93
                _internal = new Internal;
 
94
        }
 
95
        shared_condition::~shared_condition(){
 
96
                delete _internal;
 
97
        }
 
98
        void shared_condition::Wait(mutex_thread* shared_lock){
 
99
        shared_lock->unlock();
 
100
        _cl_dword_t dwRes = WaitForSingleObject( _internal->_event, 0xFFFFFFFF );
 
101
                assert ( 0x0 == dwRes );
 
102
        shared_lock->lock();
 
103
        }
 
104
        void shared_condition::NotifyAll(){
 
105
                bool bRes = SetEvent(_internal->_event);
 
106
        assert( bRes );
 
107
        }
 
108
 
 
109
        _LUCENE_THREADID_TYPE mutex_thread::CreateThread(luceneThreadStartRoutine* func, void* arg){
 
110
            return (_LUCENE_THREADID_TYPE) ::_beginthread (func, 0, arg);
 
111
        }
 
112
        void mutex_thread::JoinThread(_LUCENE_THREADID_TYPE id){
 
113
            WaitForSingleObject((void*)id, 0xFFFFFFFF);
 
114
        }
 
115
 
 
116
 
 
117
#elif defined(_CL_HAVE_PTHREAD)
 
118
  #ifndef _REENTRANT
 
119
      #error ACK! You need to compile with _REENTRANT defined since this uses threads
 
120
  #endif
 
121
 
 
122
        #ifdef _CL_HAVE_PTHREAD_MUTEX_RECURSIVE
 
123
                bool mutex_pthread_attr_initd=false;
 
124
                pthread_mutexattr_t mutex_thread_attr;
 
125
        #endif
 
126
 
 
127
        #ifdef _CL__CND_DEBUG
 
128
                #define _CLPTHREAD_CHECK(c,m) CND_PRECONDITION(c==0,m)
 
129
        #else
 
130
                #define _CLPTHREAD_CHECK(c,m) c;
 
131
        #endif
 
132
 
 
133
        struct mutex_thread::Internal{
 
134
        pthread_mutex_t mtx;
 
135
        #ifndef _CL_HAVE_PTHREAD_MUTEX_RECURSIVE
 
136
        pthread_t lockOwner;
 
137
        unsigned int lockCount;
 
138
        #endif
 
139
  };
 
140
 
 
141
        mutex_thread::mutex_thread(const mutex_thread& /*clone*/):
 
142
                _internal(new Internal)
 
143
        {
 
144
                #ifdef _CL_HAVE_PTHREAD_MUTEX_RECURSIVE
 
145
                        _CLPTHREAD_CHECK(pthread_mutex_init(&_internal->mtx, &mutex_thread_attr), "mutex_thread(clone) constructor failed")
 
146
                #else
 
147
                        #if defined(__hpux) && defined(_DECTHREADS_)
 
148
                                _CLPTHREAD_CHECK(pthread_mutex_init(&_internal->mtx, pthread_mutexattr_default), "mutex_thread(clone) constructor failed")
 
149
                        #else
 
150
                                _CLPTHREAD_CHECK(pthread_mutex_init(&_internal->mtx, 0), "mutex_thread(clone) constructor failed")
 
151
                        #endif
 
152
                        _internal->lockCount=0;
 
153
                        _internal->lockOwner=0;
 
154
                #endif
 
155
        }
 
156
        mutex_thread::mutex_thread():
 
157
                _internal(new Internal)
 
158
        {
 
159
                #ifdef _CL_HAVE_PTHREAD_MUTEX_RECURSIVE
 
160
                if ( mutex_pthread_attr_initd == false ){
 
161
                        pthread_mutexattr_init(&mutex_thread_attr);
 
162
                        pthread_mutexattr_settype(&mutex_thread_attr, PTHREAD_MUTEX_RECURSIVE);
 
163
                        mutex_pthread_attr_initd = true;
 
164
                }
 
165
                _CLPTHREAD_CHECK(pthread_mutex_init(&_internal->mtx, &mutex_thread_attr), "mutex_thread(clone) constructor failed")
 
166
                #else
 
167
                #if defined(__hpux) && defined(_DECTHREADS_)
 
168
                        _CLPTHREAD_CHECK(pthread_mutex_init(&_internal->mtx, pthread_mutexattr_default), "mutex_thread(clone) constructor failed")
 
169
                #else
 
170
                        _CLPTHREAD_CHECK(pthread_mutex_init(&_internal->mtx, 0), "mutex_thread(clone) constructor failed")
 
171
                #endif
 
172
                _internal->lockCount=0;
 
173
                _internal->lockOwner=0;
 
174
                #endif
 
175
        }
 
176
 
 
177
        mutex_thread::~mutex_thread()
 
178
        {
 
179
                _CLPTHREAD_CHECK(pthread_mutex_destroy(&_internal->mtx), "~mutex_thread destructor failed")
 
180
                delete _internal;
 
181
        }
 
182
 
 
183
    _LUCENE_THREADID_TYPE mutex_thread::_GetCurrentThreadId(){
 
184
        return pthread_self();
 
185
    }
 
186
      
 
187
    int32_t atomic_threads::atomic_increment(_LUCENE_ATOMIC_INT *theInteger){
 
188
      #ifdef _CL_HAVE_GCC_ATOMIC_FUNCTIONS
 
189
        return __sync_add_and_fetch(theInteger, 1);
 
190
      #else
 
191
        SCOPED_LOCK_MUTEX(theInteger->THIS_LOCK)
 
192
        return ++theInteger->value;
 
193
      #endif
 
194
    }
 
195
    int32_t atomic_threads::atomic_decrement(_LUCENE_ATOMIC_INT *theInteger){
 
196
      #ifdef _CL_HAVE_GCC_ATOMIC_FUNCTIONS
 
197
        return __sync_sub_and_fetch(theInteger, 1);
 
198
      #else
 
199
        SCOPED_LOCK_MUTEX(theInteger->THIS_LOCK)
 
200
        return --theInteger->value;
 
201
      #endif
 
202
    }
 
203
 
 
204
 
 
205
        _LUCENE_THREADID_TYPE mutex_thread::CreateThread(luceneThreadStartRoutine* func, void* arg){
 
206
            _LUCENE_THREADID_TYPE ret;
 
207
            int nRes = pthread_create(&ret, NULL, func, arg);
 
208
            assert( nRes == 0 );
 
209
            return ret;
 
210
        }
 
211
        void mutex_thread::JoinThread(_LUCENE_THREADID_TYPE id){
 
212
            pthread_join(id, NULL);
 
213
        }
 
214
 
 
215
        void mutex_thread::lock()
 
216
        {
 
217
                #ifndef _CL_HAVE_PTHREAD_MUTEX_RECURSIVE
 
218
                pthread_t currentThread = pthread_self();
 
219
                if( pthread_equal( _internal->lockOwner, currentThread ) ) {
 
220
                        ++_internal->lockCount;
 
221
                } else {
 
222
                        _CLPTHREAD_CHECK(pthread_mutex_lock(&_internal->mtx), "mutex_thread::lock")
 
223
                        _internal->lockOwner = currentThread;
 
224
                        _internal->lockCount = 1;
 
225
                }
 
226
                #else
 
227
                _CLPTHREAD_CHECK(pthread_mutex_lock(&_internal->mtx), "mutex_thread::lock")
 
228
                #endif
 
229
        }
 
230
 
 
231
        void mutex_thread::unlock()
 
232
        {
 
233
                #ifndef _CL_HAVE_PTHREAD_MUTEX_RECURSIVE
 
234
                --_internal->lockCount;
 
235
                if( _internal->lockCount == 0 )
 
236
                {
 
237
                        _internal->lockOwner = 0;
 
238
                        _CLPTHREAD_CHECK(pthread_mutex_unlock(&_internal->mtx), "mutex_thread::unlock")
 
239
                }
 
240
                #else
 
241
                _CLPTHREAD_CHECK(pthread_mutex_unlock(&_internal->mtx), "mutex_thread::unlock")
 
242
                #endif
 
243
        }
 
244
 
 
245
 
 
246
        struct shared_condition::Internal{
 
247
            pthread_cond_t condition;
 
248
            Internal(){
 
249
                  pthread_cond_init (&condition, NULL);
 
250
            }
 
251
            ~Internal(){
 
252
                          pthread_cond_destroy(&condition);
 
253
            }
 
254
        };
 
255
        shared_condition::shared_condition(){
 
256
                _internal = new Internal;
 
257
        }
 
258
        shared_condition::~shared_condition(){
 
259
                delete _internal;
 
260
        }
 
261
        void shared_condition::Wait(mutex_thread* shared_lock){
 
262
        int res = 0;
 
263
        res = pthread_cond_wait(&_internal->condition, &shared_lock->_internal->mtx);
 
264
        assert(res == 0);
 
265
        }
 
266
        void shared_condition::NotifyAll(){
 
267
        int res = 0;
 
268
        res = pthread_cond_broadcast(&_internal->condition);
 
269
        assert(res == 0);
 
270
        }
 
271
 
 
272
 
 
273
#endif //thread impl choice
 
274
 
 
275
 
 
276
mutexGuard::mutexGuard(const mutexGuard& /*clone*/){
 
277
        //no autoclone
 
278
        mrMutex = NULL;
 
279
}
 
280
mutexGuard::mutexGuard( _LUCENE_THREADMUTEX& rMutex ) :
 
281
        mrMutex(&rMutex)
 
282
{
 
283
        mrMutex->lock();
 
284
}
 
285
mutexGuard::~mutexGuard()
 
286
{
 
287
        mrMutex->unlock();
 
288
}
 
289
 
 
290
#endif //!_CL_DISABLE_MULTITHREADING
 
291
 
 
292
CL_NS_END