OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I386LINUX / util / I386LINUX / include / vtk / vtkSharedMemoryCommunicator.h
1 /*=========================================================================
2
3   Program:   Visualization Toolkit
4   Module:    $RCSfile: vtkSharedMemoryCommunicator.h,v $
5   Language:  C++
6   Date:      $Date: 2002/08/12 15:20:30 $
7   Version:   $Revision: 1.14 $
8
9   Copyright (c) 1993-2002 Ken Martin, Will Schroeder, Bill Lorensen 
10   All rights reserved.
11   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
12
13      This software is distributed WITHOUT ANY WARRANTY; without even 
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15      PURPOSE.  See the above copyright notice for more information.
16
17 =========================================================================*/
18 // .NAME vtkSharedMemoryCommunicator - Provides communication using shared memory.
19
20 // .SECTION Description
21 // This class is used together with vtkThreadedController for communication
22 // between threads. Once initialized, it creates one communicator
23 // per thread. The messages to be sent are copied to the message list
24 // of the appropriate communicator by the sending thread and then
25 // read by the receiving thread. Mutexes are used to ensure safe
26 // access to the data structures. By default, when an object is sent,
27 // it is copied with DeepCopy. This behavior can be changed by un-setting
28 // ForceDeepCopy.
29
30 // .SECTION See Also
31 // vtkCommunicator vtkThreadedController
32
33 #ifndef __vtkSharedMemoryCommunicator_h
34 #define __vtkSharedMemoryCommunicator_h
35
36 #include "vtkCommunicator.h"
37
38 class vtkThreadedController;
39 class vtkSharedMemoryCommunicatorMessage;
40 class vtkSimpleCriticalSection;
41
42 class VTK_PARALLEL_EXPORT vtkSharedMemoryCommunicator : public vtkCommunicator
43 {
44 public:
45   vtkTypeRevisionMacro( vtkSharedMemoryCommunicator,vtkCommunicator);
46   
47   // Description:
48   // Creates an empty communicator.
49   static vtkSharedMemoryCommunicator* New();
50
51   virtual void PrintSelf(ostream& os, vtkIndent indent);
52
53   // Description:
54   // This method sends data to another process.  Tag eliminates ambiguity
55   // when multiple sends or receives exist in the same process.
56   virtual int Send(int* data, int length, int remoteThreadId, int tag);
57   virtual int Send(unsigned long* data, int length, int remoteThreadId, 
58                    int tag);
59   virtual int Send(char* data, int length, int remoteThreadId, int tag);
60   virtual int Send(unsigned char* data, int length, int remoteThreadId, int tag);
61   virtual int Send(float* data, int length, int remoteThreadId, int tag);
62   virtual int Send(double* data, int length, int remoteThreadId, int tag);
63 #ifdef VTK_USE_64BIT_IDS
64   virtual int Send(vtkIdType* data, int length, int remoteThreadId, int tag);
65 #endif
66   virtual int Send(vtkDataObject* data, int remoteThreadId, int tag);
67   virtual int Send(vtkDataArray* data, int remoteThreadId, int tag);
68
69   // Description:
70   // This method receives data from a corresponding send. It blocks
71   // until the receive is finished.  It calls methods in "data"
72   // to communicate the sending data.
73   virtual int Receive(int* data, int length, int remoteThreadId, 
74                       int tag);
75   virtual int Receive(unsigned long* data, int length, 
76                       int remoteThreadId, int tag);
77   virtual int Receive(char* data, int length, int remoteThreadId, 
78                       int tag);
79   virtual int Receive(unsigned char* data, int length, int remoteThreadId, 
80                       int tag);
81   virtual int Receive(float* data, int length, int remoteThreadId, 
82                       int tag);
83   virtual int Receive(double* data, int length, int remoteThreadId, 
84                       int tag);
85 #ifdef VTK_USE_64BIT_IDS
86   virtual int Receive(vtkIdType* data, int length, int remoteThreadId, 
87                       int tag);
88 #endif
89   virtual int Receive(vtkDataObject *data, int remoteThreadId, int tag);
90   virtual int Receive(vtkDataArray *data, int remoteThreadId, int tag);
91
92 //BTX
93
94   friend class vtkThreadedController;
95
96 //ETX
97
98 protected:
99
100   int NumberOfThreads;
101   int Initialized;
102   void Initialize(int nThreads, int forceDeepCopy);
103
104   int LocalThreadId;
105   int WaitingForId;
106
107   int ForceDeepCopy;
108
109   // It is not enough to block on the messages, we have to mutex 
110   // the whole send interaction.  I was trying to avoid a central 
111   // mutex (oh well).
112   vtkSimpleCriticalSection* MessageListLock;
113
114
115   // Each thread has its own communicator.
116   vtkSharedMemoryCommunicator** Communicators;
117
118   vtkSharedMemoryCommunicator* Parent;
119   
120   // Double linked list.
121   vtkSharedMemoryCommunicatorMessage *MessageListStart;
122   vtkSharedMemoryCommunicatorMessage *MessageListEnd;
123
124   vtkSharedMemoryCommunicator();
125   ~vtkSharedMemoryCommunicator();
126
127   // The generic send and receive methods.
128   int Send(vtkDataObject* object, void *data, int dataLength, 
129            int remoteThreadId, int tag);
130   int Receive(vtkDataObject* object, void *data, int dataLength, 
131               int remoteThreadId, int tag);
132
133   int Send(vtkDataArray* object, int dataLength, 
134            int remoteThreadId, int tag);
135   int Receive(vtkDataArray* object, int dataLength, 
136               int remoteThreadId, int tag);
137
138   vtkSharedMemoryCommunicatorMessage* NewMessage(vtkDataObject* object,
139                                                  void* data, 
140                                                  int dataLength);
141   vtkSharedMemoryCommunicatorMessage* NewMessage(vtkDataArray* object,
142                                                  void* data, 
143                                                  int dataLength);
144   void DeleteMessage(vtkSharedMemoryCommunicatorMessage *message);
145   void AddMessage(vtkSharedMemoryCommunicatorMessage *message);
146   vtkSharedMemoryCommunicatorMessage* FindMessage(int sendId, int tag);
147
148 #ifdef _WIN32
149   // Event signaling the arrival of a new message.
150   // Windows implementation only.
151   HANDLE MessageSignal;
152 #else
153   // This mutex is normally locked.  It is used to block the execution 
154   // of the receiving process when the send has not been called yet.
155   vtkSimpleCriticalSection* Gate;
156 #endif
157
158   void SignalNewMessage(vtkSharedMemoryCommunicator* receiveCommunicator);
159
160   void WaitForNewMessage();
161
162 private:
163   vtkSharedMemoryCommunicator(const vtkSharedMemoryCommunicator&);  // Not implemented.
164   void operator=(const vtkSharedMemoryCommunicator&);  // Not implemented.
165 };
166
167 #endif //  __vtkSharedMemoryCommunicator_h