Archive for March, 2008

volatile

Monday, March 31st, 2008

[Original: ] http://www.airs.com/blog/archives/154

The volatile qualifier in C/C++ is widely misunderstood. Because it is described so vaguely in language standards, many people interpret it as a do-what-I-mean qualifier.

What the standard says is that accesses to volatile objects must be evaluated strictly according to the abstract machine defined by the language standard; this means that if the C/C++ code reads the volatile object twice, then the machine code must do so as wel. The standard says that volatile objects must be stable at sequence points; this means, approximately, that in between statements all reads and writes of volatile objects must have been completed–the value may not be cached in a register. The standard makes clear that it is possible that the values of volatile objects may change in some unknown way between accesses.

One relatively unimportant misunderstanding is due to the fact that the standard only talks about accesses to volatile objects. It does not talk about accesses via volatile qualified pointers. Some programmers believe that using a pointer-to-volatile should be handled as though it pointed to a volatile object. That is not guaranteed by the standard and is therefore not portable. However, this is relatively unimportant because gcc does in fact treat a pointer-to-volatile as though it pointed to a volatile object.

A way to think about volatile is to observe that it was invented to support memory mapped hardware. Some hardware is controlled by accesses to specific memory addresses. For example, a serial controller often handles input by setting a bit in one memory location and making the new byte available in another memory location. The kernel code must observe that the bit is set, read the byte, and set another bit to tell the serial controller that the byte has been read (I’m skipping the interrupt which is also involved). These accesses should use volatile to make sure that they happen in the exact order written in the program.

The standard also explicitly describes two other uses of volatile. One is for setjmp and is relatively uninteresting. The other is that a variable of type volatile sig_atomic_t may be set in a signal handler and read by code outside the signal handler. In fact, just about all that a portable signal handler may do is set such a variable.

For dealing with memory mapped hardware, volatile is exactly what you want. For most other types of code, including multi-threaded code, volatile does not help.

Using volatile does not mean that the variable is accessed atomically; no locks are used. Using volatile does not mean that other cores in a multi-core system will see the memory accesses; no cache flushes are used. While volatile writes are guaranteed to occur in the program order for the core which is executing them, there is no guarantee that any other core will see the writes in the same order. Using volatile does not imply any sort of memory barrier; the processor can and will rearrange volatile memory accesses (this will not happen for address ranges used for memory mapped hardware, but it will for ordinary memory).

Conversely, if you use the locking primitives which are part of any threading library, then you do not need to use volatile. The locking primitives will include the required memory barriers or cache flushes. They will include whatever special directives are needed to tell the compiler that memory must be stable.

There is one case where volatile may be used for multi-threaded programming with some reliability. You may use a single volatile sig_atomic_t variable to communicate between threads, much as you may use such a variable to communicate between a signal handler and the main program. You should not use more than one such variable to communicate between any pair of threads, as there is no guarantee that the different threads will see the accesses in the same order.

In summary, if you are using volatile for anything other than manipulating memory mapped hardware, or for very limited communication between threads, it is very likely that you are making a mistake. Think carefully about what volatile means and about what it does not mean.

CJKbookmarks with dvipdfm

Friday, March 28th, 2008

Today I eventually managed to create a PDF file with Chinese bookmark.
Suppose the LaTeX source file is foo.tex:

  1. Insert the following code into preamble section:
    \usepackage[CJKbookmarks,dvipdfm,bookmarksnumbered=true,%
    colorlinks=true,linkbordercolor={1 1 1},%
    linkcolor=blue,citecolor=blue,urlcolor=blue]{hyperref}

  2. The following command line create the PDF file.
    1. latex foo.tex
    2. gbk2uni foo.out
    3. latex foo.tex
    4. dvipdfm foo.dvi

The linkbordercolor option will change the color of link border to white other than the default red color.

下水道又通了

Friday, March 28th, 2008

灵光乍现,终于一举恢复了下水道的通畅,所用工具相当符合我IT民工的身份:网线。

厨房的下水道上星期便堵住了。牺牲了去年幸存的一只橡皮手套后还是徒劳无获,于是泼了一缸水并几瓢醋后撒手直飞成都,期待时间流逝,垃圾被醋酸腐蚀。回来后虽然水干了,无奈却依旧堵。下了几通猛料,84洁厕灵,后也未见效果 — 唯一的效果是梦见水管被腐蚀,于是惊醒。\O/

正打算破财消灾请专业人士,昨晚回家时灵光乍现,想到了网线。于是再次将自己的脸蒙成恐怖分子状,携网线一根,奔向水管。过程可以用六个字形容:简单,快捷,奏效。洗心革面,无论如何再也不拿掉过滤网,除非需要清洁。


终于找到那首动听的歌,原来是sissel的,大悦。Code Review的时候派上用场。

A couple of Bash tips

Thursday, March 27th, 2008
  1. Single instance
    # make use of bash `set -C’
    (set -C; : > $lockfile) 2>/dev/null
    if [ $? -ne 0 ]; then
      log_msg “$0 already running, exit”
      exit 0
    fi

  2. Daemonize script
    # `-d’ option indicates to daemonize
    if [ x"$1" = "x-d" ]; then
      $0 2>&1 >/dev/null &
      exit 0
    fi

一千天前的

Tuesday, March 18th, 2008

Many times I imagine that I am lying in the shade of the tree, thinking the meaning of the life and singing songs I’m fond of. The sunshine makes me feel happy and snug.

I don’t know whether I’ve enough experience to talk about love, the most perplexing thing in our life, because I haven’t one love-life yet. But, I can tell the love in my eyes.

Love can’t be compelled and should not be all on one side. It’s so magic that can even drive people crazy. People in love
are always in blind, and may take themselves as the most fortunate in the world. Unfortunately, it mostly turns out to be the completely the different in the end.

In the eyes of me, love is another word for responsibility, Without which the romance and passion comes with it will soon
fade.

It never took me too much time to think about what love is, or how to get my true love. I believe that everything should be natural, just as the sun sets and rises.

In my inner heart, I really hate to spend days and days chasing girls, as if it’s the only thing one can do in the world. Now, I spend hours and hours studying Computer Architecture, hoping that one day I can realize my dream, which might cost me seven years.

Yet, I haven’t a girlfriend. It doesn’t matter. Just be natural.