1 /*=========================================================================
3 Program: Visualization Toolkit
4 Module: $RCSfile: vtkSharedMemoryCommunicator.h,v $
6 Date: $Date: 2002/08/12 15:20:30 $
7 Version: $Revision: 1.14 $
9 Copyright (c) 1993-2002 Ken Martin, Will Schroeder, Bill Lorensen
11 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
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.
17 =========================================================================*/
18 // .NAME vtkSharedMemoryCommunicator - Provides communication using shared memory.
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
31 // vtkCommunicator vtkThreadedController
33 #ifndef __vtkSharedMemoryCommunicator_h
34 #define __vtkSharedMemoryCommunicator_h
36 #include "vtkCommunicator.h"
38 class vtkThreadedController;
39 class vtkSharedMemoryCommunicatorMessage;
40 class vtkSimpleCriticalSection;
42 class VTK_PARALLEL_EXPORT vtkSharedMemoryCommunicator : public vtkCommunicator
45 vtkTypeRevisionMacro( vtkSharedMemoryCommunicator,vtkCommunicator);
48 // Creates an empty communicator.
49 static vtkSharedMemoryCommunicator* New();
51 virtual void PrintSelf(ostream& os, vtkIndent indent);
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,
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);
66 virtual int Send(vtkDataObject* data, int remoteThreadId, int tag);
67 virtual int Send(vtkDataArray* data, int remoteThreadId, int tag);
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,
75 virtual int Receive(unsigned long* data, int length,
76 int remoteThreadId, int tag);
77 virtual int Receive(char* data, int length, int remoteThreadId,
79 virtual int Receive(unsigned char* data, int length, int remoteThreadId,
81 virtual int Receive(float* data, int length, int remoteThreadId,
83 virtual int Receive(double* data, int length, int remoteThreadId,
85 #ifdef VTK_USE_64BIT_IDS
86 virtual int Receive(vtkIdType* data, int length, int remoteThreadId,
89 virtual int Receive(vtkDataObject *data, int remoteThreadId, int tag);
90 virtual int Receive(vtkDataArray *data, int remoteThreadId, int tag);
94 friend class vtkThreadedController;
102 void Initialize(int nThreads, int forceDeepCopy);
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
112 vtkSimpleCriticalSection* MessageListLock;
115 // Each thread has its own communicator.
116 vtkSharedMemoryCommunicator** Communicators;
118 vtkSharedMemoryCommunicator* Parent;
120 // Double linked list.
121 vtkSharedMemoryCommunicatorMessage *MessageListStart;
122 vtkSharedMemoryCommunicatorMessage *MessageListEnd;
124 vtkSharedMemoryCommunicator();
125 ~vtkSharedMemoryCommunicator();
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);
133 int Send(vtkDataArray* object, int dataLength,
134 int remoteThreadId, int tag);
135 int Receive(vtkDataArray* object, int dataLength,
136 int remoteThreadId, int tag);
138 vtkSharedMemoryCommunicatorMessage* NewMessage(vtkDataObject* object,
141 vtkSharedMemoryCommunicatorMessage* NewMessage(vtkDataArray* object,
144 void DeleteMessage(vtkSharedMemoryCommunicatorMessage *message);
145 void AddMessage(vtkSharedMemoryCommunicatorMessage *message);
146 vtkSharedMemoryCommunicatorMessage* FindMessage(int sendId, int tag);
149 // Event signaling the arrival of a new message.
150 // Windows implementation only.
151 HANDLE MessageSignal;
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;
158 void SignalNewMessage(vtkSharedMemoryCommunicator* receiveCommunicator);
160 void WaitForNewMessage();
163 vtkSharedMemoryCommunicator(const vtkSharedMemoryCommunicator&); // Not implemented.
164 void operator=(const vtkSharedMemoryCommunicator&); // Not implemented.
167 #endif // __vtkSharedMemoryCommunicator_h