IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 18, 2010, 2:25:38 PM (16 years ago)
Author:
Paul Price
Message:

Discovered a race condition in the threading process. The thread process was: psThreadJobAlloc, populate job with arguments, psThreadJobAddPending, psFree the job. This is not thread-safe, because we're decrementing the job's reference counter while the reference count is also being fiddled within the thread system (psThread.c; by pulling the job off the 'pending' queue and putting it on the 'done' queue). The reference count fiddling within the thread system was protected by a mutex, but the external free was not. Therefore, I'm pulling the free into the thread system so it can be protected (without the user having to worry about locks to do the simple case). This means that the user should no longer touch the job once he hands it over to the thread system (unless it's protected by calls to psThreadLock/psThreadUnlock, or it's on the other side of psThreadPoolWait from all the adds so that the threads aren't doing anything).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psphot/src/psphotTest.c

    r24187 r28405  
    2525
    2626bool FillImage_Threaded (psThreadJob *job) {
    27    
     27
    2828    psImage *image = job->args->data[0];
    2929    int xs = PS_SCALAR_VALUE(job->args->data[1],S32);
     
    3636    psRegion region = psRegionSet (xs, xs + dx, ys, ys + dy);
    3737    for (int i = 0; i < 100; i++) {
    38         psImage *subset = psImageSubset (image, region);
    39         psImageInit (subset, value + i);
    40         psFree (subset);
     38        psImage *subset = psImageSubset (image, region);
     39        psImageInit (subset, value + i);
     40        psFree (subset);
    4141    }
    4242    return true;
     
    4646
    4747    if (argc != 3) {
    48         fprintf (stderr, "USAGE: psphotTest (output.fits) (nThreads)\n");
    49         exit (2);
     48        fprintf (stderr, "USAGE: psphotTest (output.fits) (nThreads)\n");
     49        exit (2);
    5050    }
    5151
     
    6262
    6363    for (int ix = 0; ix < 1000; ix += 100) {
    64         for (int iy = 0; iy < 1000; iy += 100) {
     64        for (int iy = 0; iy < 1000; iy += 100) {
    6565
    66             // allocate a job -- if threads are not defined, this just runs the job
    67             psThreadJob *job = psThreadJobAlloc ("FILL_IMAGE");
     66            // allocate a job -- if threads are not defined, this just runs the job
     67            psThreadJob *job = psThreadJobAlloc ("FILL_IMAGE");
    6868
    69             psArrayAdd(job->args, 1, image);
    70             PS_ARRAY_ADD_SCALAR(job->args, ix, PS_TYPE_S32);
    71             PS_ARRAY_ADD_SCALAR(job->args, iy, PS_TYPE_S32);
    72             PS_ARRAY_ADD_SCALAR(job->args, 100, PS_TYPE_S32);
    73             PS_ARRAY_ADD_SCALAR(job->args, 100, PS_TYPE_S32);
    74             PS_ARRAY_ADD_SCALAR(job->args, ix + iy, PS_TYPE_S32);
     69            psArrayAdd(job->args, 1, image);
     70            PS_ARRAY_ADD_SCALAR(job->args, ix, PS_TYPE_S32);
     71            PS_ARRAY_ADD_SCALAR(job->args, iy, PS_TYPE_S32);
     72            PS_ARRAY_ADD_SCALAR(job->args, 100, PS_TYPE_S32);
     73            PS_ARRAY_ADD_SCALAR(job->args, 100, PS_TYPE_S32);
     74            PS_ARRAY_ADD_SCALAR(job->args, ix + iy, PS_TYPE_S32);
    7575
    76             // FillImage (image, ix, iy, 100, 100, ix + iy);
     76            // FillImage (image, ix, iy, 100, 100, ix + iy);
    7777
    78             if (!psThreadJobAddPending(job)) {
    79                 fprintf (stderr, "failure to run FillImage(1)");
    80                 psFree (job);
    81                 exit (1);
    82             }
    83             psFree(job);
    84         }
     78            if (!psThreadJobAddPending(job)) {
     79                fprintf (stderr, "failure to run FillImage(1)");
     80                exit (1);
     81            }
     82        }
    8583    }
    8684
     
    8886    // wait for the threads to finish and manage results
    8987    if (!psThreadPoolWait (true)) {
    90         fprintf (stderr, "failure to run FillImage (2)");
    91         exit (1);
     88        fprintf (stderr, "failure to run FillImage (2)");
     89        exit (1);
    9290    }
    9391
Note: See TracChangeset for help on using the changeset viewer.