Archive for May, 2007

Bike to work day

Monday, May 21st, 2007

This past Thursday was Bike to Work Day here in Silicon Valley, and while biking to work I thought about why I really like living in the Bay Area while other people seem to hate it. See, I pretty much never drive to work: I work from home three days a week and on the days where I actually go to the office, I ride my bike or light rail.

One of the main complaints I hear about the Bay Area is the traffic, and I can’t disagree really. But the simple solution is just to avoid driving. As I said, I bike to work, and I live downtown where I can walk to almost everywhere else I want to go. When I do get in my car it’s usually to go to the beach or Tahoe or the redwoods or something like that, and living near that stuff is the whole point of being in the Bay Area.

The other usual complaint about the Bay Area is that it’s too expensive, and I guess I can’t argue with that. But pay is higher here too, and the advantage of having a 1300-square-foot house is that I don’t have to worry about finding enough stuff to fill my rooms.

Anyway, if you don’t like the Bay Area, please don’t move here (or move away if you’re already here). We have enough people without you haters and your negative attitude….

Atomic cargo cults

Sunday, May 13th, 2007

Cargo cult programming refers to “ritual inclusion of code or program structures that serve no real purpose.” One annoying example of this that I see a lot in kernel code that I review is the inappropriate use of atomic_t, in the belief that it magically wards off races.

This type of bogosity is usually marked by variables or structure members of type atomic_t, which are only ever accessed through atomic_read() and atomic_set() without ever using a real atomic operation such as atomic_inc() or atomic_dec_and_test(). Such programming reaches its apotheosis in code like:

        atomic_set(&head, (atomic_read(&head) + 1) % size);

(and yes, this is essentially real code, although I’ve paraphrased it to protect the guilty from embarrassment).

The only point of atomic_t is that arithmetic operations (like atomic_inc() et al) are atomic in the sense that there is no window where two racing operations can step on each other. This helps with situations where an int variable might have ++i and --i race with each other; since these operations are probably implemented as read-modify-write. So if i starts out as 0, the value of i after the two operations might be 0, -1 or 1 depending on when each operation reads the value of i and what order they write back the final value.

If you never use any of these atomic operations, then there’s no point in using atomic_set() to assign a variable and atomic_read() to read it. Your code is no more or less safe than if would be just using normal int variables and assignments.

One way to think about atomic operations is that they might be an optimization instead of using a lock to protect access to a variable. Unfortunately I’m not sure if this will help anyone get things right, since I also see plenty of code like:

int x;

int foo(void)

        int y;

        y = x;

        return y;

which is the analogous cargo-cult anti-pattern for spinlocks.

Maybe the best rule would be: if the only functions using atomic_t in your code are atomic_set() and atomic_read(), then you need to write a comment explaining why you’re using atomic at all. Since the vast majority of the time, such a comment will be impossible to write, maybe this will cut down on cargo cult programming a bit. Or more likely it will just make code review more fun by generating nonsensical comments for me to chuckle at.

Let’s try WordPress

Friday, May 4th, 2007

I’ve said “so long” to Typo and migrated my blog to WordPress.  WordPress just seems to have more momentum than Typo, even though PHP seems kind of 90s to me.

Anyway, I’ve set up a redirect so the previous RSS feed should continue to work, but you’ll probably want to update your URL if for some strange reason you’ve actually subscribed to my blog’s feed.