Skip to content
Snippets Groups Projects
Commit ba4813f2 authored by Ken Martin's avatar Ken Martin
Browse files

fix unsafe memory write in vtkImageGradientMagnitude etc

The class was writing to a memory location from
multiple threads bro. Also fix thread issues in
the TestAtomic and TestConditionVariable tests.
parent fa53a52d
Branches
No related tags found
No related merge requests found
......@@ -19,8 +19,6 @@
#include <algorithm>
static int Total = 0;
static vtkTypeInt64 Total64 = 0;
static vtkAtomicInt32 TotalAtomic(0);
static vtkAtomicInt64 TotalAtomic64(0);
static const int Target = 1000000;
......@@ -29,17 +27,27 @@ static int Values64[Target+1];
static vtkMTimeType MTimeValues[Target];
static int NumThreads = 5;
// uncomment the following line if you want to see
// the difference between using atomics and not
//#define SHOW_DIFFERENCE
#ifdef SHOW_DIFFERENCE
static int Total = 0;
static vtkTypeInt64 Total64 = 0;
#endif
VTK_THREAD_RETURN_TYPE MyFunction(void *)
{
vtkNew<vtkObject> AnObject;
for (int i=0; i<Target/NumThreads; i++)
{
#ifdef SHOW_DIFFERENCE
Total++;
Total64++;
#endif
int idx = ++TotalAtomic;
Values32[idx] = 1;
Total64++;
idx = ++TotalAtomic64;
Values64[idx] = 1;
......@@ -96,9 +104,12 @@ VTK_THREAD_RETURN_TYPE MyFunction4(void *)
int TestAtomic(int, char*[])
{
#ifdef SHOW_DIFFERENCE
Total = 0;
TotalAtomic = 0;
Total64 = 0;
#endif
TotalAtomic = 0;
TotalAtomic64 = 0;
for (int i=0; i<=Target; i++)
......@@ -160,8 +171,10 @@ int TestAtomic(int, char*[])
mt->SetSingleMethod(MyFunction4, NULL);
mt->SingleMethodExecute();
#ifdef SHOW_DIFFERENCE
cout << Total << " " << TotalAtomic.load() << endl;
cout << Total64 << " " << TotalAtomic64.load() << endl;
#endif
if (TotalAtomic.load() != Target)
{
......
#include "vtkAtomicTypes.h"
#include "vtkConditionVariable.h"
#include "vtkMultiThreader.h"
#include "vtksys/SystemTools.hxx"
......@@ -8,7 +9,7 @@
typedef struct {
vtkMutexLock* Lock;
vtkConditionVariable* Condition;
int Done;
vtkAtomicInt32 Done;
int NumberOfWorkers;
} vtkThreadUserData;
......
......@@ -143,7 +143,7 @@ void vtkImageGradientMagnitudeExecute(vtkImageGradientMagnitude *self,
unsigned long target;
int axesNum;
int *wholeExtent;
vtkIdType *inIncs;
vtkIdType inIncs[3];
double r[3], d, sum;
int useZMin, useZMax, useYMin, useYMax, useXMin, useXMax;
int *inExt = inData->GetExtent();
......@@ -170,7 +170,7 @@ void vtkImageGradientMagnitudeExecute(vtkImageGradientMagnitude *self,
r[2] = 0.5 / r[2];
// get some other info we need
inIncs = inData->GetIncrements();
inData->GetIncrements(inIncs);
wholeExtent = inData->GetExtent();
// Move the starting pointer to the correct location.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment