Salome HOME
d90d45ee0bc7af34121c13e6331556e9ffc2c115
[modules/kernel.git] / src / SALOMELocalTrace / LocalTraceBufferPool.hxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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
23 //  Author : Paul RASCLE (EDF)
24 //  Module : KERNEL
25 //  $Header$
26 //
27 #ifndef _LOCALTRACEBUFFERPOOL_HXX_
28 #define _LOCALTRACEBUFFERPOOL_HXX_
29
30 #include "SALOME_LocalTrace.hxx"
31
32 #define TRACE_BUFFER_SIZE 512  // number of entries in circular buffer
33                                // must be power of 2
34 #define MAX_TRACE_LENGTH 1024   // messages are truncated at this size
35
36 #include <pthread.h>
37 #include <semaphore.h>
38 #ifdef __APPLE__
39 #include <dispatch/dispatch.h>
40 #endif
41 #include "BaseTraceCollector.hxx"
42 #include "BasicsGenericDestructor.hxx"
43
44 #define ABORT_MESS  1   // for traceType field in struct LocalTrace_TraceInfo
45 #define NORMAL_MESS 0
46
47 struct SALOMELOCALTRACE_EXPORT LocalTrace_TraceInfo
48 {
49   char trace[MAX_TRACE_LENGTH];
50   pthread_t threadId;
51   int traceType;                 // normal or abort
52   int position;                  // to check sequence
53 };
54
55 class SALOMELOCALTRACE_EXPORT LocalTraceBufferPool : public PROTECTED_DELETE
56 {
57  public:
58   static LocalTraceBufferPool* instance();
59   int insert(int traceType, const char* msg);
60   int retrieve(LocalTrace_TraceInfo& aTrace);
61   unsigned long toCollect();
62
63  protected:
64   LocalTraceBufferPool();
65   virtual ~LocalTraceBufferPool();
66   unsigned long lockedIncrement(unsigned long& pos);
67
68  private:
69   static LocalTraceBufferPool* _singleton;
70   static pthread_mutex_t _singletonMutex;
71   static BaseTraceCollector *_myThreadTrace;
72
73   LocalTrace_TraceInfo _myBuffer[TRACE_BUFFER_SIZE];
74 #ifdef __APPLE__
75   dispatch_semaphore_t _freeBufferSemaphore;       // to wait until there is a free buffer
76   dispatch_semaphore_t _fullBufferSemaphore;       // to wait until there is a buffer to print
77 #else
78   sem_t _freeBufferSemaphore;       // to wait until there is a free buffer
79   sem_t _fullBufferSemaphore;       // to wait until there is a buffer to print
80 #endif
81   pthread_mutex_t _incrementMutex;  // to lock position variables for increment
82   unsigned long _position;
83   unsigned long _insertPos;
84   unsigned long _retrievePos;
85 };
86
87 #endif