• The Old New Thing

    If a process crashes while holding a mutex, why is its ownership magically transferred to another process?


    A customer was observing strange mutex ownership behavior. They had two processes that used a mutex to coordinate access to some shared resource. When the first process crashed while owning the mutex, they found that the second process somehow magically gained ownership of that mutex. Specifically, when the first process crashed, the second process could take the mutex, but when it released the mutex, the mutex was still not released. They discovered that in order to release the mutex, the second process had to call Release­Mutex twice. It's as if the claim on the mutex from the crashed process was secretly transferred to the second process.

    My psychic powers told me that that's not what was happening. I guessed that their code went something like this:

    // code in italics is wrong
    bool TryToTakeTheMutex()
     return WaitForSingleObject(TheMutex, TimeOut) == WAIT_OBJECT_0;

    The code failed to understand the consequences of WAIT_ABANDONED.

    In the case where the mutex was held by the first process when it crashed, the second process will attempt to claim the mutex, and it will succeed, and the return code from Wait­For­Single­Object will be WAIT_ABANDONED. Their code treated that value as a failure code rather than a modified success code.

    The second program therefore claimed the mutex without realizing it. That is what led the customer to believe that ownership was being magically transferred to the second program. It wasn't magic. The second program misinterpreted the return code.

    The second program saw that Try­To­Take­The­Mutex "failed", and it went off and did something else for a while. Then the next time it called Try­To­Take­The­Mutex, the function succeeded: It was a successful recursive acquisition, but the program thought it was the initial acquisition.

    The customer didn't reply back, so we never found out whether that was the actual problem, but I suspect it was.

  • The Old New Thing

    The struggle against those annoying plastic packages gains a few allies


    Some companies are switching to easy-to-open packaging. Not a moment too soon, in my opinion.

  • The Old New Thing

    I took the Monorail to the Shadow of the Moon


    It turns out that the replacement movie wasn't any of the ones I listed. Instead, I decided to see In the Shadow of the Moon, a mid-week performance in Seattle. Since it was also a fantastically warm sunny day, traffic into Seattle was a nightmare. As the bus crawled along the highway, I had to do some mental calculations. I'm definitely going to miss my connecting bus downtown (the number 4 at 6:05pm); what's the best recovery plan? It turns out that my decision, in retrospect, was the correct one, but it was merely a lucky guess.

    I arrived downtown at 6:17pm, intending to transfer to the 3 or 4 bus to Seattle Center, home of the SIFF Cinema theater. I was just too late for the 6:15pm bus, and right at the start of the transition from peak service to off-peak service. The next bus was at 6:35pm, which would put me at Seattle Center at around 6:45. Add in time for walking from the bus stop to the theater and waiting in line to pick up my tickets, and that puts me dangerously close to missing the 7pm show.

    I briefly considered getting off at REI and taking the 8, but I didn't know how often it ran, and if I missed, I would be out in the middle of nowhere. (Turns out the 8 runs every half hour, and the next bus was the 6:35, which would have gotten me to Seattle Center at 6:45, no better than the safer plan of taking the 3 or 4.)

    Aha, the Monorail runs every ten minutes. It's 6:17. Even in the worst case, I'm still way ahead. (They don't publish a timetable. You just buy your ticket and take your chances.)

    Turns out it was the worst case. I arrived at the ticket booth just as the doors closed. But the next one arrived at 6:27, as promised, and just a few minutes later, I was at the Space Needle. A brisk walk to the SIFF Cinema theater and I had plenty of time to pick up my ticket and get in line.

    I've lived in the Seattle area for over fifteen years, and this was my first Monorail ride.

    Recapping one week of SIFF:

    2 stars out of 5 12:08 East of Bucharest: The real movie was the call-in program that addresses the question of whether there was or wasn't a revolution in the town. The first half of the movie was vignettes of a small town in Romania. They were sometimes interesting, sometimes tedious (do we really have to follow a car for two minutes as it drives through the streets of town?), and they felt like filler and "backstory" for the main story. If you showed up 45 minutes late and just caught the call-in program, you'd still get 90% of the enjoyment of the movie. One brief plot line from the first half of the movie comes back in the second; aside from that, you really didn't miss much. I give it a 2 out of 5 based on the strength of the second half.

    4 stars out of 5 Fair Play: SIFF labelled this a black comedy, and there were indeed a few black-comedic moments at the beginning, but it really would be better-described as a tense thriller. The build-up was well-done, and the twist at the end makes you mentally rewind the movie and replay it in your head so you can view the motivations of one of the characters in a different light. I give it a 4 out of 5, gaining points from the wonderful squash game, but losing points because the canyoneering trip was a bit too convenient from a storytelling point of view.

    5 stars out of 5 In the Shadow of the Moon: This was absolutely wonderful, a documentary consisting of stunning never-before-seen NASA footage from the Apollo missions and interviews with most of the surviving astronauts who have been to the moon. (Of the astronauts, all of whom are extremely well-spoken and quite funny, Michael Collins steals the show.) If this movie goes into general release, I strongly encourage every space buff to run, don't walk, to see it. The footage of the Saturn V launch brought tears to me eyes. I give it a 5 out of 5.

    There's a spectacular shot taken from the inside of a spent stage: You watch the next stage ignite and the spacecraft fade off into the distance, then as the spent stage loses attitude, the earth comes into view before the film finally runs out. During the Q&A after the movie, one person asked the director, "How did the film of that sequence survive re-entry?" The answer: The film was ejected from the spent stage and fell to earth. High-altitude planes were in pursuit with giant nets trailing out behind them. That was one insane game of "catch".

    The special surprise guest at the screening was Bill Anders, the crewmember from Apollo 8 who took the famous Earthrise photo. He quipped that Frank Borman actually took the first Earthrise photo, but Borman had the disadvantage of using the camera loaded with black-and-white film; Anders had color film in his camera.

    Bonus story #1 from Gene Cernan: "My father was alive when the Wright brothers made their first flight; he could hardly believe that I walked on the moon. My son was five years old; he thought it was no big deal."

    Bonus story #2 from Charles Duke: "After I returned, the flight doctor told me that at launch my heart rate was 144." A beat. "John's was 70." Cut to interview with John Young: "I'm old. My heart can't go any faster."


    5 stars out of 5 Would pay money to see again by myself.
    4 stars out of 5 Would see again if it were free or if seeing it with others.
    3 stars out of 5 Would recommend to others.
    2 stars out of 5 Okay, but wouldn't recommend to someone not already interested.
    1 star out of 5 Would advise against.
    0 stars out of 5 Waste of my time.

    Note: The rating scheme has been revised since this article was originally posted.

  • The Old New Thing

    Playing the hippie poetry game for four cents per line


    The party game goes by many names. Hippie poetry, Beat poetry, Dada poetry. To play, have a group of people sit in a circle and give each person a piece of paper and writing implement. To start, each person writes a single line of poetry and hands it to the person to his or her left or right. (The direction isn't important, as long as it's consistent.)

    At each round, you add one line to the growing poem, then fold over the top of the page so that only the line you added is visible. Pass the paper to the left (or right), and repeat. Popular stopping conditions are when the paper is full or when the page returns to the person who started the poem. Once the poem is complete, the paper is unfolded and everyone takes turns reading the results. They are usually quite absurd.

    That was a long and ultimately unsatisfying set-up for the fact that you can make money playing this game. And you do it via the Internet from your home. The catch: You get paid four cents per line. Oh, and the poem is about sex.

  • The Old New Thing

    Microspeak: Turds


    In Microspeak, a turd is a graphics glitch which results in old pixels appearing on the screen when they shouldn't. Perhaps they are not being erased properly, or instead of being erased, they move to an unwanted location. The source of the problem could be failing to mark a region for redrawing after something changed, or it could be that the drawing code ran into a problem and failed to draw over the old pixels. The problem might be that the code whose job it is to remove the object from the screen ran into a problem and didn't actually remove it, resulting in an on-screen object that nobody really is keeping track of. Whatever the reason, these "junk pixels" go by the name turds.

    For non-English speakers: turd is a boorish term for dung.

    The term turds are more generally applied to objects that don't serve any purpose but never got cleaned up. For example, they might be files and registry keys that didn't get deleted when you uninstalled an application, or user accounts for employees who left the company years ago, or C++ objects that some code forgot to delete. If you upgrade your dogfood machine from build to build, there may be files left over from earlier builds that aren't being used by the product any more. A build system may create temporary files that only get erased up when you do a "make clean".

    Note that this particular term turd is not formal Microspeak. You won't find it in a specifications document. But you may encounter it in a bug report or hear it in a casual discussion. Personally, I don't use the term. For the graphics glitches, I prefer to use the word artifact. The leftover stuff that didn't get cleaned up I simply call dirt.

    Bonus chatter: There was one project that actually tried to introduce the word turd as a formal technical term:

    If a transaction has been superseded by another transaction, a marker is left behind in the original transaction record which redirects the resolver to the new transaction. This marker is known as the Transaction Under Redirection Descriptor (TURD).

    I bet the developer who came up with that spent way too much time making up turd-related jokes in other parts of the specification.

  • The Old New Thing

    The introduction of whimsical teasing in Comic Chat


    A few months after my post on the sad demise of whimsical teasing in Comic Chat, I received a piece of email from none other than the author of Comic Chat, DJ Kurlander:

    I was the person that started the Comic Chat project in Microsoft Research and was responsible for that line, "This person is too lazy to create a profile entry."

    Not a whole lot of thought went into the default profile. In maybe the 30 seconds that I put into it, I thought that the line was moderately humorous, fit with the quirky nature of Comic Chat, and might motivate more people to create profiles. When we released version 2, MSN got the following complaint. Yes, this is verbatim (and specifically in reference to the default profile):

    I am very offended that this is type of code is allowed to leave microsoft. I am seriously considering dropping my subscription to MSN. I don't get this kind of crap from CompuServe, and I can get comparable Internet access from a local ISP. I can see that there is a serious lack of customer respect at microsoft and this is probably just the tip of the iceberg.

    MSN was pretty conservative, at least at the time, and this caused a big headache for the team. How dare we insult our customers! Did the project not have enough management overhead? Can we make things any more difficult for the small team? Yes we can! As a result, we were required to remove the message from the 2.1 release. It became "This user has no profile." at least in the version that was released on the MSN CD-ROM.

    Meanwhile, we got some advanced notice about an upcoming PC World review of the 2.0 version that had included the original message. The summary (which was emailed to us), said: "She [the reviewer] praises Microsoft for finally having a sense of humor (loves the note about being 'too lazy to have a bio' message). She claims Comic Chat is an absolute delight."

    I love the part about MS finally having a sense of humor. Well at least we were allowed to add back the message for the 2.5 release!

    Check out DJ's history of Comic Chat, which includes links to videos and other time-wasty goodness.

    Bonus chatter: DJ also has a page on Peedy the Parrot, a research project to develop a more natural user interface. Out in the product groups, we just called it that stupid parrot demo because it became tiresome from overexposure, having been used in a CES keynote and multiple company meetings.

  • The Old New Thing

    How can I write a script that finds my top-rated photos?


    I'm not sure if I'll be able to keep it up, but I'm going to see if I can make Monday "Little Programs" day, where I solve simple problems with little programs.

    Today's little program is a script that goes through your Pictures folder and picks out your top-rated photos.

    The key step here is extracting the rating, which goes by the name System.Rating in the shell property system. The method which does the extraction is Shell­Folder­Item.Extended­Property.

    var shell = new ActiveXObject("Shell.Application");
    var picturesFolder = shell.Namespace(39); // CSIDL_MYPICTURES
    var items = picturesFolder.Items();
    items.Filter(SHCONTF_NONFOLDERS, "*.jpg");
    for (var i = 0; i < items.Count; i++) {
      var item = items.Item(i);
      if (item.ExtendedProperty("System.Rating") >= 80) {

    Wow, that was way easier than doing it in C++!

    That program searches one folder, but let's say we want to do a full recursive search. No problem. Take the code we wrote and shove it into a helper function process­Files­In­Folder, then call it as part of a recursive directory search.

    function processFilesInFolder(folder) {
      var items = folder.Items();
      var SHCONTF_NONFOLDERS = 64;
      items.Filter(SHCONTF_NONFOLDERS, "*.jpg");
      for (var i = 0; i < items.Count; i++) {
        var item = items.Item(i);
        if (item.ExtendedProperty("System.Rating") >= 80) {
    function recursiveProcessFolder(folder) {
      var items = folder.Items();
      var SHCONTF_FOLDERS = 32;
      items.Filter(SHCONTF_FOLDERS, "*");
      for (var i = 0; i < items.Count; i++) {
    var shell = new ActiveXObject("Shell.Application");
    var picturesFolder = shell.Namespace(39);

    You can use this as a jumping-off point for whatever you want to do with your top-rated pictures, like copy them to your digital photo frame.

  • The Old New Thing

    Color-aware ClearType requires access to fixed background pixels, which is a problem if you don't know what the background pixels are, or if they aren't fixed


    ClearType is a technology that blends text pixels with background pixels with the goal of reducing visual artifacts during text rendering.¹ If you go for the full-meal version of ClearType, it uses knowledge about the physical properties of LCD screens in order to carry out its work. Some time ago, I noted one catch with this model, which is that overdraw gives the ClearType algorithm the wrong background pixels.

    Another gotcha is that if you don't know what the background pixels are at all, then you can't use ClearType. For example, you might be rendering a semi-transparent bitmap that will be drawn on top of other content. If you try to use ClearType to render text onto this bitmap, the ClearType engine will see transparent pixels as the background, and blend accordingly. (My guess is that it will treat them as black pixels.) But when you actually draw this bitmap to the screen, those transparent pixels will allow the pixels below the bitmap to shine through, and those underlying pixels are not transparent.

    The result is color fringes because the ClearType engine was given incorrect background pixels.

    Another assumption that the ClearType engine makes is that the bitmap will be drawn on exact pixel boundaries without any stretching or shrinking or rotation. Since ClearType is doing math based on the physical LCD, you break the ClearType model if you scale the bitmap, rotate it, or render it on a fractional-pixel boundary because each carefully-crafted ClearType pixel does not end up as a single pixel on the LCD panel. (The mapping of original pixels to transformed pixels is controlled by something called an interpolation mode. For example, Direct2D offers a variety of interpolation modes.)

    If you break this rule and use ClearType anyway, you will once again get the dreaded color fringes.

    Nearly all of these factors come into play on the Windows 8 Start screen.

    The word Start on that page is rendered onto a transparent bitmap because it needs to overlay on top of your Start background, and most of the Start backgrounds are not fixed; they scroll slowly as you pan through your tiles.

    The tiles themselves translate when you pan the Start screen, and they scale when you go into Customize mode or when you press them, and they rotate when you tap on them. This means that any text drawn onto a tile cannot use the color-aware version of ClearType.

    These concerns apply more generally to any bitmap that will be scrolled. A bitmap that scrolls with subpixel positioning cannot use the color-aware version of ClearType because ClearType assumes integer-pixel positioning. (This is why Internet Explorer doesn't use color-aware ClearType. Scrolling is performed with Direct Manipulation, and Direct Manipulation does subpixel scrolling.)

    Now, of course, you could work around this problem. You could design your interface so that it doesn't require transparent bitmaps, say by using fixed backgrounds. And you could design your interface so it doesn't use subpixel positioning, scaling, or rotation. Or you could simply stop pre-rendering text and instead rerender them on-the-fly each time something changes. The first two workarounds impair your design. The second impairs your performance, since moving a bitmap is no longer a simple update of a sprite's coordinates and transformation matrix; you have to go through a full text rendering pass, including a pixel read-back in order to figure out what the current background pixels are. (And pixel read-back from video memory is not cheap since it forces the composition tree to flatten.)

    Both trade-offs are pretty expensive, and the cheaper alternative is usually simply to stop using color-aware ClearType.

    ¹ Yes, there are people who don't like ClearType. The point of today's article isn't about whether ClearType is good or bad; it's about ClearType's limitations. I tried to remain neutral on the subject by saying that improved text rendering is the goal of ClearType, making no statement about whether it achieves that goal.

  • The Old New Thing

    The Old New Thing (the book) allegedly now stocked at the Microsoft Company Store (Redmond)


    I've been informed that the Redmond branch of the Microsoft Company Store has begun stocking the dead tree edition of my book.

    "But wait, your program isn't printed by Microsoft Press; it's published by Addison-Wesley Professional. I thought the company store only stocked Microsoft Press titles."

    I'm told that this is a pilot program. (And no, I don't know what the success criteria are.)

    When I stopped by the store a few days ago, they were in the process of reorganizing the book section, so not only was my book not up, neither were any others! But it should be there "any day now."

    And remember, I'll gladly sign your book but you have to tell me what to write.

  • The Old New Thing

    The publicity machine doesn't stop: TechNet podcast interview


    The TechNet Magazine Podcast page has just posted their February 2007 entry, which includes an interview with little old me in the second half. I haven't listened to the whole interview yet, but what struck me immediately is that I was pretty darned punchy and goofy, whereas I think the host was trying to take a more serious tone. Oops.

Page 375 of 444 (4,431 items) «373374375376377»