Commit 9b06556c authored by Berk Geveci's avatar Berk Geveci
Browse files

Replaced use of critical section with runtime initialization.

vtkPolyData class uses a class variable that it was initializing
lazily - which required a mutex to do in a thread safe way. Since
mutexes are expensive, I replaced this with a run-time
initialization, which is thread safe and much less expensive.

Change-Id: I34bcee4b4e5f105a00ebd87129a5debff0170705
parent 004944f0
......@@ -33,6 +33,8 @@
#include "vtkTriangleStrip.h"
#include "vtkVertex.h"
#include "vtkSmartPointer.h"
vtkStandardNewMacro(vtkPolyData);
//----------------------------------------------------------------------------
......@@ -40,9 +42,18 @@ vtkStandardNewMacro(vtkPolyData);
// of verts, lines, polygons, and triangle strips lists. It basically
// "marks" empty lists so that the traveral method "GetNextCell"
// works properly.
vtkCellArray *vtkPolyData::Dummy = NULL;
static vtkSimpleCriticalSection DummyCritSect;
struct vtkPolyDataDummyContainter
{
vtkSmartPointer<vtkCellArray> Dummy;
vtkPolyDataDummyContainter()
{
this->Dummy.TakeReference(vtkCellArray::New());
}
};
vtkPolyDataDummyContainter vtkPolyData::DummyContainer;
vtkPolyData::vtkPolyData ()
{
......@@ -68,20 +79,6 @@ vtkPolyData::vtkPolyData ()
this->Information->Set(vtkDataObject::DATA_NUMBER_OF_PIECES(), 1);
this->Information->Set(vtkDataObject::DATA_NUMBER_OF_GHOST_LEVELS(), 0);
// static variable, initialized only once.
DummyCritSect.Lock();
if (this->Dummy == NULL)
{
this->Dummy = vtkCellArray::New();
this->Dummy->Register(this);
this->Dummy->Delete();
}
else
{
this->Dummy->Register(this);
}
DummyCritSect.Unlock();
this->Cells = NULL;
this->Links = NULL;
}
......@@ -91,20 +88,6 @@ vtkPolyData::~vtkPolyData()
{
this->Cleanup();
// Reference to static dummy persists.
// Keep destructed dummy from being used again.
DummyCritSect.Lock();
if (this->Dummy->GetReferenceCount() == 1)
{
this->Dummy->UnRegister(this);
this->Dummy = NULL;
}
else
{
this->Dummy->UnRegister(this);
}
DummyCritSect.Unlock();
if (this->Vertex)
{
this->Vertex->Delete();
......@@ -655,7 +638,7 @@ void vtkPolyData::ComputeBounds()
// Set the cell array defining vertices.
void vtkPolyData::SetVerts (vtkCellArray* v)
{
if (v == this->Dummy)
if (v == this->DummyContainer.Dummy.GetPointer())
{
v = NULL;
}
......@@ -681,7 +664,7 @@ vtkCellArray* vtkPolyData::GetVerts()
{
if ( !this->Verts )
{
return this->Dummy;
return this->DummyContainer.Dummy.GetPointer();
}
else
{
......@@ -693,7 +676,7 @@ vtkCellArray* vtkPolyData::GetVerts()
// Set the cell array defining lines.
void vtkPolyData::SetLines (vtkCellArray* l)
{
if (l == this->Dummy)
if (l == this->DummyContainer.Dummy.GetPointer())
{
l = NULL;
}
......@@ -719,7 +702,7 @@ vtkCellArray* vtkPolyData::GetLines()
{
if ( !this->Lines )
{
return this->Dummy;
return this->DummyContainer.Dummy.GetPointer();
}
else
{
......@@ -731,7 +714,7 @@ vtkCellArray* vtkPolyData::GetLines()
// Set the cell array defining polygons.
void vtkPolyData::SetPolys (vtkCellArray* p)
{
if(p == this->Dummy)
if(p == this->DummyContainer.Dummy.GetPointer())
{
p = NULL;
}
......@@ -757,7 +740,7 @@ vtkCellArray* vtkPolyData::GetPolys()
{
if ( !this->Polys )
{
return this->Dummy;
return this->DummyContainer.Dummy.GetPointer();
}
else
{
......@@ -769,7 +752,7 @@ vtkCellArray* vtkPolyData::GetPolys()
// Set the cell array defining triangle strips.
void vtkPolyData::SetStrips (vtkCellArray* s)
{
if ( s == this->Dummy)
if ( s == this->DummyContainer.Dummy.GetPointer())
{
s = NULL;
}
......@@ -796,7 +779,7 @@ vtkCellArray* vtkPolyData::GetStrips()
{
if ( !this->Strips )
{
return this->Dummy;
return this->DummyContainer.Dummy.GetPointer();
}
else
{
......
......@@ -69,6 +69,7 @@ class vtkQuad;
class vtkPolygon;
class vtkTriangleStrip;
class vtkEmptyCell;
struct vtkPolyDataDummyContainter;
class VTKCOMMONDATAMODEL_EXPORT vtkPolyData : public vtkPointSet
{
......@@ -451,7 +452,7 @@ protected:
vtkCellArray *Strips;
// dummy static member below used as a trick to simplify traversal
static vtkCellArray *Dummy;
static vtkPolyDataDummyContainter DummyContainer;
// supporting structures for more complex topological operations
// built only when necessary
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment