1 // Copyright (C) 2004 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
20 // Author : Paul RASCLE (EDF)
24 // Cf. C++ Users Journal, June 2004, Tracing Application Execution, Tomer Abramson
30 #include "LocalTraceBufferPool.hxx"
31 #include "utilities.h"
35 // In case of truncated message, end of trace contains "...\n\0"
36 #define TRUNCATED_MESSAGE "...\n"
37 #define MAXMESS_LENGTH MAX_TRACE_LENGTH-5
39 LocalTraceBufferPool* LocalTraceBufferPool::_singleton = 0;
40 pthread_mutex_t LocalTraceBufferPool::_singletonMutex;
42 // ============================================================================
44 * guarantees a unique object instance of the class (singleton thread safe)
46 // ============================================================================
48 LocalTraceBufferPool* LocalTraceBufferPool::instance()
50 if (_singleton == 0) // no need of lock when singleton already exists
53 ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
54 if (_singleton == 0) // another thread may have got
55 { // the lock after the first test
56 _singleton = new LocalTraceBufferPool();
58 ret = pthread_mutex_unlock(&_singletonMutex); // release lock
63 // ============================================================================
65 * Called by trace producers within their threads. The trace message is copied
66 * in specific buffer from a circular pool of buffers.
67 * Waits until there is a free buffer in the pool, gets the first available
68 * buffer, fills it with the message.
69 * Messages are printed in a separate thread (see retrieve method)
71 // ============================================================================
73 int LocalTraceBufferPool::insert(int traceType, const char* msg)
76 // get immediately a message number to control sequence (mutex protected)
78 unsigned long myMessageNumber = lockedIncrement(_position);
80 // wait until there is a free buffer in the pool
82 int ret = sem_wait(&_freeBufferSemaphore);
84 // get the next free buffer available (mutex protected)
86 unsigned long myInsertPos = lockedIncrement(_insertPos);
88 // fill the buffer with message, thread id and type (normal or abort)
90 strncpy(_myBuffer[myInsertPos%TRACE_BUFFER_SIZE].trace,
92 MAXMESS_LENGTH); // last chars always "...\n\0" if msg too long
93 _myBuffer[myInsertPos%TRACE_BUFFER_SIZE].threadId =pthread_self();//thread id
94 _myBuffer[myInsertPos%TRACE_BUFFER_SIZE].traceType = traceType;
95 _myBuffer[myInsertPos%TRACE_BUFFER_SIZE].position = myMessageNumber;
98 // increment the full buffer semaphore
99 // (if previously 0, awake thread in charge of trace)
101 ret = sem_post(&_fullBufferSemaphore);
103 // returns the number of free buffers
105 sem_getvalue(&_freeBufferSemaphore, &ret);
109 // ============================================================================
111 * Called by the thread in charge of printing trace messages.
112 * Waits until there is a buffer with a message to print.
113 * Gets the first buffer to print, copies it int the provided buffer
115 // ============================================================================
117 int LocalTraceBufferPool::retrieve(LocalTrace_TraceInfo& aTrace)
120 // wait until there is a buffer in the pool, with a message to print
122 int ret = sem_wait(&_fullBufferSemaphore);
124 // get the next buffer to print
126 unsigned long myRetrievePos = lockedIncrement(_retrievePos);
128 // copy the buffer from the pool to the provided buffer
130 memcpy((void*)&aTrace,
131 (void*)&_myBuffer[myRetrievePos%TRACE_BUFFER_SIZE],
134 // increment the free buffer semaphore
135 // (if previously 0, awake one of the threads waiting to put a trace, if any)
136 // there is no way to preserve the order of waiting threads if several
137 // threads are waiting to put a trace: the waken up thread is not
138 // necessarily the first thread to wait.
140 ret = sem_post(&_freeBufferSemaphore);
142 // returns the number of full buffers
144 sem_getvalue(&_fullBufferSemaphore, &ret);
148 // ============================================================================
150 * Gives the number of buffers to print.
151 * Usage : when the thread in charge of messages print id to be stopped,
152 * check if there is still something to print, before stop.
153 * There is no need of mutex here, provided there is only one thread to
154 * retrieve and print the buffers.
156 // ============================================================================
158 unsigned long LocalTraceBufferPool::toCollect()
160 return _insertPos - _retrievePos;
163 // ============================================================================
165 * Constructor : initialize pool of buffers, semaphores and mutex.
167 // ============================================================================
169 LocalTraceBufferPool::LocalTraceBufferPool()
171 //cout << "LocalTraceBufferPool::LocalTraceBufferPool()" << endl;
173 _insertPos = ULONG_MAX; // first increment will give 0
174 _retrievePos = ULONG_MAX;
175 _position=0; // first message will have number = 1
177 memset(_myBuffer, 0, sizeof(_myBuffer)); // to guarantee end of strings = 0
178 for (int i=0; i<TRACE_BUFFER_SIZE; i++)
179 strcpy(&(_myBuffer[i].trace[MAXMESS_LENGTH]),TRUNCATED_MESSAGE);
181 ret=sem_init(&_freeBufferSemaphore, 0, TRACE_BUFFER_SIZE); // all buffer free
182 if (ret!=0) IMMEDIATE_ABORT(ret);
183 ret=sem_init(&_fullBufferSemaphore, 0, 0); // 0 buffer full
184 if (ret!=0) IMMEDIATE_ABORT(ret);
185 ret=pthread_mutex_init(&_incrementMutex,NULL); // default = fast mutex
186 if (ret!=0) IMMEDIATE_ABORT(ret);
189 // ============================================================================
191 * Destructor : release memory associated with semaphores and mutex
193 // ============================================================================
195 LocalTraceBufferPool::~LocalTraceBufferPool()
198 ret=sem_destroy(&_freeBufferSemaphore);
199 ret=sem_destroy(&_fullBufferSemaphore);
200 ret=pthread_mutex_destroy(&_incrementMutex);
203 // ============================================================================
205 * pool counters are incremented under a mutex protection
207 // ============================================================================
209 unsigned long LocalTraceBufferPool::lockedIncrement(unsigned long& pos)
212 ret = pthread_mutex_lock(&_incrementMutex); // lock access to counters
214 ret = pthread_mutex_unlock(&_incrementMutex); // release lock