Wednesday, January 26, 2005

Double Checked Lock Pattern reconsidered

A recent Dr. Dobbs had an article pointing out the weaknesses of the Double Checked Lock Pattern(DCLP). For another presentation on this issue, see this article.

This pattern is most often used in the instance() method of a Singleton (nevermind that Singletons themselves tread mighty close to the dark side!) DCLP looks something like:

Filbert * Filbert::instance()
{
if (instance_ == 0)
{
MutexGuard guard(instanceMutex_);
if (instance == 0)
instance_ = new Filbert();
}
return instance_;
}

The theory being that once you have your instance you never again need to lock the mutex.

The article raised two issues:
1) In a dual processor system with caching, the processor that doesn't create the Filbert could see the value of instance_ but still have cached, stale, information about the memory containing the new Filbert.

2) Even in a single processor system, the compiler is allowed to reorder statements as long as the result appears to be the same as one would see in a single processor, single threaded environment. Strange as it may seem, the compiler is allowed to store the address of the new Filbert into instance_ before calling the constructor of the new object.

#2 is easily resolvable. The root of the problem is that instance_ is overloaded -- serving both as a pointer to Filbert and as a bool indicating that Filbert has been created. Separating these two responsibilities produces the "triple checked locking pattern" (TCLP)


Filbert * Filbert::instance()
{
if(instance_is_valid_)
{
MutexGuard guard(instanceMutex_);
if(instance == 0)
instance_ = new Filbert();
else
instance_is_valid_ = true;
}
return instance_;
}

Instance_is_valid_ is set only when thread one creates the Filbert and exits from the scope of the mutex, then thread 2 enters the scope of the mutex to find that the instance has already been created. I believe that this is safe on a single processor system for any legal reordering of the code. Alas, it still doesn't address the problem on a multi-processor system where each processor has its own cache.

It'll be interesting to see how cache is configured on the new multi-core processor chips that AMD & Intel are talking about.

The remaining problem with both DCLP and TCLP is that there is no way to delete the Filbert safely so resource leaks are gonna happen. Since the the need to delete usually comes at end-of-process time this is a non-issue on many (but not all) platforms (except for the purists and those running leak detection tools.) but still it would be nice to do it right.

Weavers' Guild meeting

At the evening meeting of the Weavers' Guild of St. Louis Tina was giving a presentation on Complex Weaving. She used some of my network drafted work as one of her examples. I mentioned my current project -- the grey on grey scarf. Amy looked at me in disbelief and asked "Why?"

Sometimes I wonder. The weaving is going fine, but I may have invented the stealth pattern.

Monday, January 24, 2005

Subtle vs Invisible

I've woven a couple of feet of this scarf. The new loom is working nicely (most of the time.)

On the other hand...

My goal was to produce a very subtle pattern in the cloth with the variation in the texture, not the color. I may have outdone myself on the subtle part. When does subtle become invisible, or even indetectable?

...maybe when I take it off the loom and finish the cloth the pattern will emerge. (hey, it could happen!) If not, well it'll be a nice, plain chenille scarf.

Tuesday, January 11, 2005

Pet peeve # (n+1) [ I lost count]

Note to those who think in Linux: When someone asks you for help, please resist the urge to grab the keyboard and start typing. Instead dictate what needs to be typed. It has to do with giving fish vs teaching to fish. If nothing else it will make you aware of how truly arcane the "find" command is. ;-)

Monday, January 10, 2005

It's warped

I tried to track my time dressing the loom for this project. Usually when someone looks at something I've woven and says "How long did it take you to weave that?" I stare at them blankly 'cause I really don't know. For this one, it's taken just over 8 hours to wind the warp, slay the reed, thread the heddles, and wind it on (I'm warping from front to back 'cause I always warp from front to back.) I guess I should also count the two hours Tina spent helping me wind it on. It wouldn't have been so bad but I made some mistakes winding the warp that led to some significant tangles that needed to be managed.

And then there's that unmistakable feeling when you finally open a shed and one thread -- right in the middle -- is not behaving properly. Aarrgghh... It was getting late and I was tired so I didn't even try to diagnose the problem -- I went to bed instead.

Of course that means I woke up in the middle of the night wondering whether I was going to have to rethread half the loom, hand tie a heddle or just cut out the bad thread and ignore the minor slaying glitch. It turned out to be the best possible outcome. The "missing heddle" was there, right beside the unattached thread. All I had to do was untie that thread from cloth beam, rethread, and reslay that single thread. Add another 1/2 hour to the "how long did it take?" answer, and it's ready to weave.


Wednesday, January 05, 2005

Weaving Project

My current weaving project is a new (for me) experiment with network drafting and chenille. Chenille needs to be woven in a tight weave structure to prevent it from "worming." The most common form of network drafting is based on a four thread twill. (i.e. a mix of 1/3, 2/2, and 3/1 twill resulting in three thread floats -- however I realized that if you use a three thread twill (a mix of 1/2 and 2/1 twill) the longest float is only two threads. Alas, that means you have many fewer basic units to work with (think black and white rather than greyscale), but there are still some interesting possibilities.

For previous network drafting projects I have used different colors in the warp and the weft to make the pattern visible. For this project I'm using different textures. I have a 10-2 pearle cotton warp and a rayon chenille weft that are very close to the same color and weight (as close as I could find.) I'm hoping that the result will show areas of chenille velvet and areas of pearle cotton smoothness and luster. We'll see. At 24 epi x 12 inches I have a lot of threading to do before I get to see any results.

All of this is going to be woven on our 24 shaft Studio Dobby AVL loom with a compudobby 2. I'm only using 18 of the harnesses because there are only 6 "interesting" three thread twill units (001, 010, 100, 110, 101, 110.) I'm using an antique 386 computer with 16M of RAM and about 200M of hard drive to run the loom. Old computers never die...

Subversion on Debian

I've spent the last week or so (spare time) setting up a debian linux system to support a subversion repository. Some notes to make it easier if I have to do it again. (I'll use "svn" to represent the directory in which repositories are created.)
  • Debian hint, not subversion related: Use the feta command rather than apt-get, et. al. Start with: apt-get install feta
  • feta install subversion
  • Pay close attention to user and group permission in the directory in which you wish to create the repository. Yes, you do need execute permission (?why--i donno?) I chose not to support direct file access except for repository maintenance. Hence: chmod 770 svn.
  • In addition to direct file access, there are two ways to provide remote access to the repository: svnserve, and DAV via apache. They coexist nicely.
  • The svnserv (svn://host/repostory) approach is easier to set up, but the apache (http://host/svn/repository) approach plays better with firewalls, etc. It also provides "free" web browsing of the repository which is cool!.
  • You configure svnserve permissions after creating the repository (via svnadmin create). To do so, edit the svn/repo/conf/svnserve.conf file which was automatically created when you created the repository (note: replace "repo" with the actual repository name.)
  • To install apache support, you need apache2: feta get apache2
  • You also need the module that supports svn via dav: feta get libapache2-svn
  • To configure permissions etc. for apache-based access, edit:
    /etc/apache2/mods-available/dav_svn.conf
  • I added the default user under which apache runs -- www-data -- to the group that has access to the subversion directory.
  • I want to write some python utilities to help maintain the repository: feta install python2.3-subversion
  • I'm planning to take a look at pysvn: http://pysvn.tigris.org/


Tuesday, January 04, 2005

Note to myself: Start a blog today.

Everybody's doin' it. There must be some reason.