Salome HOME
Merging with WPDev
[modules/kernel.git] / src / SALOMELocalTrace / LocalTraceBufferPool.hxx
1 //  Copyright (C) 2004  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
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. 
8 // 
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. 
13 // 
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 
17 // 
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  Author : Paul RASCLE (EDF)
21 //  Module : KERNEL
22 //  $Header$
23
24 #ifndef _LOCALTRACEBUFFERPOOL_HXX_
25 #define _LOCALTRACEBUFFERPOOL_HXX_
26
27 #include "SALOME_LocalTrace.hxx"
28
29 #define TRACE_BUFFER_SIZE 256  // number of entries in circular buffer
30                                // must be power of 2
31 #define MAX_TRACE_LENGTH 256   // messages are truncated at this size
32
33 #include <pthread.h>
34 #include <semaphore.h>
35 #include "BaseTraceCollector.hxx"
36 #include "BasicsGenericDestructor.hxx"
37
38 #define ABORT_MESS  1   // for traceType field in struct LocalTrace_TraceInfo
39 #define NORMAL_MESS 0
40
41 struct SALOMELOCALTRACE_EXPORT LocalTrace_TraceInfo
42 {
43   char trace[MAX_TRACE_LENGTH];
44   pthread_t threadId;
45   int traceType;                 // normal or abort
46   int position;                  // to check sequence
47 };
48
49 class SALOMELOCALTRACE_EXPORT LocalTraceBufferPool : public PROTECTED_DELETE
50 {
51  public:
52   static LocalTraceBufferPool* instance();
53   int insert(int traceType, const char* msg);
54   int retrieve(LocalTrace_TraceInfo& aTrace);
55   unsigned long toCollect();
56
57  protected:
58   LocalTraceBufferPool();
59   virtual ~LocalTraceBufferPool();
60   unsigned long lockedIncrement(unsigned long& pos);
61
62  private:
63   static LocalTraceBufferPool* _singleton;
64   static pthread_mutex_t _singletonMutex;
65   static BaseTraceCollector *_myThreadTrace;
66
67   LocalTrace_TraceInfo _myBuffer[TRACE_BUFFER_SIZE];
68   sem_t _freeBufferSemaphore;       // to wait until there is a free buffer
69   sem_t _fullBufferSemaphore;       // to wait until there is a buffer to print
70   pthread_mutex_t _incrementMutex;  // to lock position variables for increment
71   unsigned long _position;
72   unsigned long _insertPos;
73   unsigned long _retrievePos;
74 };
75
76 #endif