Salome HOME
CCAR: replace the old way to take the python global interpreter lock
[modules/kernel.git] / src / SALOMELocalTrace / LocalTraceBufferPool.hxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  Author : Paul RASCLE (EDF)
23 //  Module : KERNEL
24 //  $Header$
25 //
26 #ifndef _LOCALTRACEBUFFERPOOL_HXX_
27 #define _LOCALTRACEBUFFERPOOL_HXX_
28
29 #include "SALOME_LocalTrace.hxx"
30
31 #define TRACE_BUFFER_SIZE 512  // number of entries in circular buffer
32                                // must be power of 2
33 #define MAX_TRACE_LENGTH 1024   // messages are truncated at this size
34
35 #include <pthread.h>
36 #include <semaphore.h>
37 #include "BaseTraceCollector.hxx"
38 #include "BasicsGenericDestructor.hxx"
39
40 #define ABORT_MESS  1   // for traceType field in struct LocalTrace_TraceInfo
41 #define NORMAL_MESS 0
42
43 struct SALOMELOCALTRACE_EXPORT LocalTrace_TraceInfo
44 {
45   char trace[MAX_TRACE_LENGTH];
46   pthread_t threadId;
47   int traceType;                 // normal or abort
48   int position;                  // to check sequence
49 };
50
51 class SALOMELOCALTRACE_EXPORT LocalTraceBufferPool : public PROTECTED_DELETE
52 {
53  public:
54   static LocalTraceBufferPool* instance();
55   int insert(int traceType, const char* msg);
56   int retrieve(LocalTrace_TraceInfo& aTrace);
57   unsigned long toCollect();
58
59  protected:
60   LocalTraceBufferPool();
61   virtual ~LocalTraceBufferPool();
62   unsigned long lockedIncrement(unsigned long& pos);
63
64  private:
65   static LocalTraceBufferPool* _singleton;
66   static pthread_mutex_t _singletonMutex;
67   static BaseTraceCollector *_myThreadTrace;
68
69   LocalTrace_TraceInfo _myBuffer[TRACE_BUFFER_SIZE];
70   sem_t _freeBufferSemaphore;       // to wait until there is a free buffer
71   sem_t _fullBufferSemaphore;       // to wait until there is a buffer to print
72   pthread_mutex_t _incrementMutex;  // to lock position variables for increment
73   unsigned long _position;
74   unsigned long _insertPos;
75   unsigned long _retrievePos;
76 };
77
78 #endif