source: trunk/src/tools/qthreadstorage_pm.cpp

Last change on this file was 8, checked in by dmik, 20 years ago

Transferred Qt for OS/2 version 3.3.1-rc5 sources from the CVS

  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1/****************************************************************************
2** $Id: qthreadstorage_pm.cpp 8 2005-11-16 19:36:46Z dmik $
3**
4** ...
5**
6** Copyright (C) 2003 Trolltech AS. All rights reserved.
7** Copyright (C) 2004 Norman ASA. Initial OS/2 Port.
8** Copyright (C) 2005 netlabs.org. Further OS/2 Development.
9**
10** This file is part of the tools module of the Qt GUI Toolkit.
11**
12** This file may be distributed under the terms of the Q Public License
13** as defined by Trolltech AS of Norway and appearing in the file
14** LICENSE.QPL included in the packaging of this file.
15**
16** This file may be distributed and/or modified under the terms of the
17** GNU General Public License version 2 as published by the Free Software
18** Foundation and appearing in the file LICENSE.GPL included in the
19** packaging of this file.
20**
21** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22** licenses may use this file in accordance with the Qt Commercial License
23** Agreement provided with the Software.
24**
25** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27**
28** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29** information about Qt Commercial License Agreements.
30** See http://www.trolltech.com/qpl/ for QPL licensing information.
31** See http://www.trolltech.com/gpl/ for GPL licensing information.
32**
33** Contact info@trolltech.com if any conditions of this licensing are
34** not clear to you.
35**
36**********************************************************************/
37
38#ifdef QT_THREAD_SUPPORT
39
40#include "qt_os2.h"
41
42#include "qthreadstorage.h"
43#include <private/qthreadinstance_p.h>
44#include <private/qcriticalsection_p.h>
45
46#include <string.h>
47
48// #define QTHREADSTORAGE_DEBUG
49
50
51// keep this in sync with the implementation in qthreadstorage.cpp
52static const int MAX_THREAD_STORAGE = 257; // 256 maximum + 1 used in QRegExp
53
54static bool thread_storage_init = FALSE;
55static struct {
56 bool used;
57 void (*func)( void * );
58} thread_storage_usage[MAX_THREAD_STORAGE];
59
60static void thread_storage_id( int &id, void (*func)( void * ), bool alloc )
61{
62 static QCriticalSection cs;
63 cs.enter();
64
65 if ( alloc ) {
66 // make sure things are initialized
67 if ( ! thread_storage_init )
68 memset( thread_storage_usage, 0, sizeof( thread_storage_usage ) );
69 thread_storage_init = TRUE;
70
71 for ( ; id < MAX_THREAD_STORAGE; ++id ) {
72 if ( !thread_storage_usage[id].used )
73 break;
74 }
75
76 Q_ASSERT( id >= 0 && id < MAX_THREAD_STORAGE );
77 thread_storage_usage[id].used = TRUE;
78 thread_storage_usage[id].func = func;
79
80#ifdef QTHREADSTORAGE_DEBUG
81 qDebug( "QThreadStorageData: allocated id %d", id );
82#endif // QTHREADSTORAGE_DEBUG
83 } else {
84 thread_storage_usage[id].used = FALSE;
85 thread_storage_usage[id].func = 0;
86
87#ifdef QTHREADSTORAGE_DEBUG
88 qDebug( "QThreadStorageData: released id %d", id );
89#endif // QTHREADSTORAGE_DEBUG
90 }
91
92 cs.leave();
93}
94
95
96QThreadStorageData::QThreadStorageData( void (*func)( void * ) )
97 : id( 0 )
98{
99 thread_storage_id( id, func, TRUE );
100}
101
102QThreadStorageData::~QThreadStorageData()
103{
104 thread_storage_id( id, 0, FALSE );
105}
106
107void **QThreadStorageData::get() const
108{
109 QThreadInstance *d = QThreadInstance::current();
110 QMutexLocker locker( d->mutex() );
111 return d->thread_storage && d->thread_storage[id] ? &d->thread_storage[id] : 0;
112}
113
114void **QThreadStorageData::set( void *p )
115{
116 QThreadInstance *d = QThreadInstance::current();
117 QMutexLocker locker( d->mutex() );
118 if ( !d->thread_storage ) {
119#ifdef QTHREADSTORAGE_DEBUG
120 PTIB ptib;
121 DosGetInfoBlocks( &ptib, NULL );
122 qDebug( "QThreadStorageData: allocating storage for thread %lx",
123 (unsigned long) ptib->tib_ptib2->tib2_ultid );
124#endif // QTHREADSTORAGE_DEBUG
125
126 d->thread_storage = new void*[MAX_THREAD_STORAGE];
127 memset( d->thread_storage, 0, sizeof( void* ) * MAX_THREAD_STORAGE );
128 }
129
130 // delete any previous data
131 if ( d->thread_storage[id] )
132 thread_storage_usage[id].func( d->thread_storage[id] );
133
134 // store new data
135 d->thread_storage[id] = p;
136 return &d->thread_storage[id];
137}
138
139void QThreadStorageData::finish( void **thread_storage )
140{
141 if ( ! thread_storage ) return; // nothing to do
142
143#if defined(QTHREADSTORAGE_DEBUG) || defined(QT_CHECK_STATE)
144 PTIB ptib;
145 DosGetInfoBlocks( &ptib, NULL );
146#endif
147#ifdef QTHREADSTORAGE_DEBUG
148 // Note: This method can be called (by QThread::terminate()) from a
149 // thread other than one we are destroying storage for, in which case
150 // we will have the TID of the terminating thread here.
151 qDebug( "QThreadStorageData: destroying storage for thread %lx",
152 (unsigned long) ptib->tib_ptib2->tib2_ultid );
153#endif // QTHREADSTORAGE_DEBUG
154
155 for ( int i = 0; i < MAX_THREAD_STORAGE; ++i ) {
156 if ( ! thread_storage[i] ) continue;
157 if ( ! thread_storage_usage[i].used ) {
158#ifdef QT_CHECK_STATE
159 qWarning( "QThreadStorage: thread %lx exited after QThreadStorage destroyed",
160 (unsigned long) ptib->tib_ptib2->tib2_ultid );
161#endif // QT_CHECK_STATE
162 continue;
163 }
164
165 thread_storage_usage[i].func( thread_storage[i] );
166 }
167
168 delete [] thread_storage;
169}
170
171#endif // QT_THREAD_SUPPORT
Note: See TracBrowser for help on using the repository browser.