« An important week ahead | Main | ntohl and the left shift »

July 07, 2009

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83452386969e2011571d25798970b

Listed below are links to weblogs that reference Stupid FUTEX and glibc:

Comments

Loïc Domaigné

Hello,

I call this a hard won experience... AFAICS, it's not a problem with glibc, but with your code :(

You can only call a certain class of functions in signal handler: the functions that are async-signal-safe. The Single Unix Specification defines about 100 such a function (see Single Unix Specification, §2.4.3 XSH IEEE Std 1003.1-2008) that are guaranteed to be async signal-safe (roughly, 5% of all available APIs). Your system may support more, but beware if you are concerned by portability aspects!

Why localtime(), ctime() etc. are not async-signal-safe? This is simple: to make these function threads-safe in an effective manner, glibc uses a mutex. Now suppose that your thread is happily logging some message, calling localtime(). The function localtime enters, takes the mutex, is about to compute the local time (which requires to access the time zone) ... when suddenly, a signal is received, and the handler executes. If the handler also calls localtime(), it causes localtime to take the mutex. Wait! The mutex is already locked by the same thread. Uuuupppsss... Deadlock.

Since you are using Pthreads, you have a better option available. Block the interested signal(s), and dedicate a special thread that waits synchronously for the signal(s). Do whatever you need to do upon reception. In this case, you're not restricted to use async-signal-safe functions. See Butenhof's book, Programming with POSIX Threads, §6.6.4.

Cheers,
Loïc.

Taobao agent

Ooh baby, I love that Palenque comp. Great picks Jason!

Tattoo Supply

Very honoured to see your blog, I benefited a lot here, and it brings me a great deal of enjoyment.
I sincerely hope your blog continually up to date. I hope you can write more post better in the future
Thanks for post and i think i will learn something more here.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment