Ignore:
Timestamp:
Jan 3, 2010, 7:13:32 PM (11 years ago)
Author:
rbri
Message:

DJVU plugin: djvulibre updated to version 3.5.22

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libdjvu/GSmartPointer.cpp

    r206 r280  
    5454//C- +------------------------------------------------------------------
    5555//
    56 // $Id: GSmartPointer.cpp,v 1.12 2007/03/25 20:48:32 leonb Exp $
    57 // $Name: release_3_5_19 $
     56// $Id: GSmartPointer.cpp,v 1.26 2008/08/03 03:54:01 leonb Exp $
     57// $Name: release_3_5_22 $
    5858
    5959#ifdef HAVE_CONFIG_H
     
    7272
    7373#include <string.h>
     74#if PARANOID_DEBUG
     75# include <assert.h>
     76#endif
     77
    7478#include "GThreads.h"
    7579#include "GSmartPointer.h"
    7680#include "GException.h"
    7781
    78 
    7982#ifdef HAVE_NAMESPACES
    8083namespace DJVU {
     
    8588
    8689
    87 // ------ STATIC CRITICAL SECTION
    88 
    89 static GCriticalSection gcsCounter;
    90 
    91 
    9290// ------ GPENABLED
    93 
    9491
    9592GPEnabled::~GPEnabled()
     
    10299GPEnabled::destroy()
    103100{
    104   if (count >= 0)
    105     G_THROW( ERR_MSG("GSmartPointer.suspicious") );
    106   delete this;
    107 }
    108 
    109 void
    110 GPEnabled::ref()
    111 {
    112   gcsCounter.lock();
    113   count++;
    114   gcsCounter.unlock();
    115 }
    116 
    117 void
    118 GPEnabled::unref()
    119 {
    120   gcsCounter.lock();
    121   if (! --count)
    122     count = -1;
    123   gcsCounter.unlock();
    124   if (count < 0)
    125     destroy();
     101  // Only delete if the counter is still zero.
     102  // because someone may have rescued the object...
     103  // If yes, set the counter to -0x7fff to mark
     104  // the object as doomed and make sure things
     105  // will work if the destructor uses a GP...
     106  if (atomicCompareAndSwap(&count, 0, -0x7fff))
     107    delete this;
    126108}
    127109
     
    129111// ------ GPBASE
    130112
     113
     114#define LOCKMASK 0x3f
     115#define LOCKIDX(addr) ((((size_t)(addr))/sizeof(void*))&LOCKMASK)
     116static int volatile locks[LOCKMASK+1];
     117
     118
     119GPBase&
     120GPBase::assign (const GPBase &sptr)
     121{
     122  int volatile *lockb = locks+LOCKIDX(&sptr);
     123  atomicAcquireOrSpin(lockb);
     124  GPEnabled *nptr = sptr.ptr;
     125  if (nptr)
     126    nptr->ref();
     127  atomicRelease(lockb);
     128  int volatile *locka = locks+LOCKIDX(this);
     129  atomicAcquireOrSpin(locka);
     130  GPEnabled *old = ptr;
     131  ptr = nptr;
     132  atomicRelease(locka);
     133  if (old)
     134    old->unref();
     135  return *this;
     136}
    131137
    132138GPBase&
    133139GPBase::assign (GPEnabled *nptr)
    134140{
    135   gcsCounter.lock();
    136141  if (nptr)
    137     {
    138       if (nptr->count >= 0) 
    139         nptr->count++;
    140       else
    141         nptr = 0;
    142     }
    143   if (ptr)
    144     {
    145       GPEnabled *old = ptr;
    146       ptr = nptr;
    147       if (! --old->count)
    148         old->count = -1;
    149       gcsCounter.unlock();     
    150       if (old->count < 0)
    151         old->destroy();
    152     }
    153   else
    154     {
    155       ptr = nptr;
    156       gcsCounter.unlock();
    157     }
     142    nptr->ref();
     143  int volatile *locka = locks+LOCKIDX(this);
     144  atomicAcquireOrSpin(locka);
     145  GPEnabled *old = ptr;
     146  ptr = nptr;
     147  atomicRelease(locka);
     148  if (old)
     149    old->unref();
    158150  return *this;
    159151}
    160152
    161 GPBase&
    162 GPBase::assign (const GPBase &sptr)
    163 {
    164   gcsCounter.lock();
    165   if (sptr.ptr)
    166     {
    167       sptr.ptr->count++;
    168     }
    169   if (ptr)
    170     {
    171       GPEnabled *old = ptr;
    172       ptr = sptr.ptr;
    173       if (! --old->count)
    174         old->count = -1;
    175       gcsCounter.unlock();     
    176       if (old->count < 0)
    177         old->destroy();
    178     }
    179   else
    180     {
    181       ptr = sptr.ptr;
    182       gcsCounter.unlock();
    183     }
    184   return *this;
    185 }
     153
    186154
    187155
Note: See TracChangeset for help on using the changeset viewer.