On 21/06/2013, at 9:15 AM, Keir Mierle <[email protected]> wrote: > I was browsing the mod_wsgi.c source code to gain some understanding of the > thread and process model. While looking around, I noticed that there are > several uses of "volatile" variables. The corresponding variables appear to > be accessed across threads. > > I suspect the uses of volatile here are incorrect. Volatile in C does not > offer any guarantee of atomicity, and is instead intended for memory-mapped > reads typically found in embedded code. For example, I see > "--wsgi_request_count" at line 11004 of mod_wsgi.c (version 3.4) has no mutex > around it. There are other uses of "volatile" variables that are > mutex-guarded, but at that point, why use "volatile"? > > I may have this wrong, but I thought I'd ask in case. > > Thanks for the great work on mod_wsgi, > Keir > > p.s. the APR has atomic integer operations: > http://apr.apache.org/docs/apr/0.9/group__apr__atomic.html
I don't remember the exact history around why it is the way it is. I would most likely say it it is because I copied some conventions that Apache itself uses in the implementations of its MPM code. I often do this on the basis that Apache itself is ported to many more different platforms than I can hope to ever access and test on. Thus to try and be a bit more robust in the face of operating systems that don't behave like I would expect I just do what Apache code does. For example, in the Apache worker MPM code they have: /* volatile just in case */ static int volatile shutdown_pending; static int volatile restart_pending; static int volatile is_graceful; static volatile int child_fatal; ap_generation_t volatile ap_my_generation; Now for some of these, as they can be in the mod_wsgi code, they are accessed from a signal handler. From the little bit of reading I did after your email came in, signal handlers appear to be a bit of a murky area as far as volatiles go. Overall I can't say I understand all the language and idiosyncrasies around the use of volatile so not sure what the correct answer is. Your observation though is obviously something I should keep in mind and certainly some of the cases where volatile is used in mod_wsgi may be unnecessary. Thanks for your feedback. Graham -- You received this message because you are subscribed to the Google Groups "modwsgi" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/modwsgi. For more options, visit https://groups.google.com/groups/opt_out.
