Index: /branches/eam_branch_20090203/psphot/src/psphotTest.c
===================================================================
--- /branches/eam_branch_20090203/psphot/src/psphotTest.c	(revision 21328)
+++ /branches/eam_branch_20090203/psphot/src/psphotTest.c	(revision 21329)
@@ -1,17 +1,107 @@
 # include "psphotInternal.h"
 
-void psExit (int status, char *process, char *format, ...) {
+bool FillImage_Threaded (psThreadJob *job);
 
-    va_list ap;
+bool SetThreads () {
 
-    va_start (ap, format);
-    fprintf (stderr, "exiting %s\n", process);
-    vfprintf (stderr, format, ap);
-    va_end (ap);
+    psThreadTask *task = NULL;
 
-    exit (status);
+    task = psThreadTaskAlloc("FILL_IMAGE", 6);
+    task->function = &FillImage_Threaded;
+    psThreadTaskAdd(task);
+    psFree(task);
+
+    return true;
+}
+
+bool FillImage (psImage *image, int xs, int ys, int dx, int dy, int value) {
+
+    psRegion region = psRegionSet (xs, xs + dx, ys, ys + dy);
+    psImage *subset = psImageSubset (image, region);
+    psImageInit (subset, value);
+    psFree (subset);
+    return true;
+}
+
+bool FillImage_Threaded (psThreadJob *job) {
+    
+    psImage *image = job->args->data[0];
+    int xs = PS_SCALAR_VALUE(job->args->data[1],S32);
+    int ys = PS_SCALAR_VALUE(job->args->data[2],S32);
+    int dx = PS_SCALAR_VALUE(job->args->data[3],S32);
+    int dy = PS_SCALAR_VALUE(job->args->data[4],S32);
+    int value = PS_SCALAR_VALUE(job->args->data[5],S32);
+
+    // we want the threads to be likely to interact.  run lots of psImageSubsets
+    psRegion region = psRegionSet (xs, xs + dx, ys, ys + dy);
+    for (int i = 0; i < 100; i++) {
+	psImage *subset = psImageSubset (image, region);
+	psImageInit (subset, value + i);
+	psFree (subset);
+    }
+    return true;
 }
 
 int main (int argc, char **argv) {
+
+    if (argc != 3) {
+	fprintf (stderr, "USAGE: psphotTest (output.fits) (nThreads)\n");
+	exit (2);
+    }
+
+    psTraceSetLevel ("psLib.sys.mutex", 3);
+
+    int nThreads = atoi (argv[2]);
+
+    // create the thread pool with number of desired threads, supplying our thread launcher function
+    psThreadPoolInit (nThreads);
+
+    SetThreads();
+
+    psImage *image = psImageAlloc (1000, 1000, PS_TYPE_S32);
+
+    for (int ix = 0; ix < 1000; ix += 100) {
+	for (int iy = 0; iy < 1000; iy += 100) {
+
+	    // allocate a job -- if threads are not defined, this just runs the job
+	    psThreadJob *job = psThreadJobAlloc ("FILL_IMAGE");
+
+	    psArrayAdd(job->args, 1, image);
+	    PS_ARRAY_ADD_SCALAR(job->args, ix, PS_TYPE_S32);
+	    PS_ARRAY_ADD_SCALAR(job->args, iy, PS_TYPE_S32);
+	    PS_ARRAY_ADD_SCALAR(job->args, 100, PS_TYPE_S32);
+	    PS_ARRAY_ADD_SCALAR(job->args, 100, PS_TYPE_S32);
+	    PS_ARRAY_ADD_SCALAR(job->args, ix + iy, PS_TYPE_S32);
+
+	    // FillImage (image, ix, iy, 100, 100, ix + iy);
+
+	    if (!psThreadJobAddPending(job)) {
+		fprintf (stderr, "failure to run FillImage(1)");
+		psFree (job);
+		exit (1);
+	    }
+	    psFree(job);
+	}
+    }
+
+
+    // wait for the threads to finish and manage results
+    if (!psThreadPoolWait (true)) {
+	fprintf (stderr, "failure to run FillImage (2)");
+	exit (1);
+    }
+
+    psFits *fits = psFitsOpen (argv[1], "w");
+    psFitsWriteImage (fits, NULL, image, 0, NULL);
+    psFitsClose (fits);
+
+    psThreadPoolFinalize ();
+    psFree(image);
+
+    fprintf (stderr, "found %d leaks\n", psMemCheckLeaks (0, NULL, stdout, false));
+    exit (0);
+}
+
+# if (0)
 
     psRegion region = {0,0,0,0};        // a region representing the entire array
@@ -39,7 +129,6 @@
     psFree (header);
     psFree (image);
-    exit (0);
-}
 
+# endif
 
 # if (0)
@@ -82,2 +171,18 @@
 
 # endif
+
+# if (0)
+
+void psExit (int status, char *process, char *format, ...) {
+
+    va_list ap;
+
+    va_start (ap, format);
+    fprintf (stderr, "exiting %s\n", process);
+    vfprintf (stderr, format, ap);
+    va_end (ap);
+
+    exit (status);
+}
+
+# endif
