1

When disassembling my program I noticed that;

while (!block->meta.lock);

would be compiled into;

0x0040167f <+111>:   jmp    0x40167f <gc_malloc+111>

Clearly this is a useless infinite loop. I'm using C99 so I can't make it volatile.

My current solution:

while (!block->meta.lock) {
    __sync_synchronize();
}

This generates a lot of extra assembly. What I'm really trying to generate is the equivalent of a rep nop lock; See What does "rep; nop;" mean in x86 assembly?

How can I hint this to gcc?

Community
  • 1
  • 1
kvanbere
  • 3,289
  • 3
  • 27
  • 52
  • 7
    What do you mean by “I'm using C99 so I can't make it volatile”? – Pascal Cuoq Jan 20 '14 at 10:37
  • 1
    volatile is in the C89 standard and it was used long before that. – Art Jan 20 '14 at 11:00
  • Really? I'm pretty sure I tried. I'll get back to you on that... – kvanbere Jan 20 '14 at 11:03
  • 1
    Assure that your `lock` field can be read atomically. Even if you use `volatile` this is not portable. Use other functions from `__sync` family (C11's atomics) to guarantee data coherence. – Jens Gustedt Jan 20 '14 at 11:11
  • I don't think `__sync_synchronize` is the right one to use. What is your suggestion? – kvanbere Jan 20 '14 at 11:59
  • I made `block` a `volatile *` and I get the right assembly now. What isn't portable about `volatile` ? It seems to compile using `-pedantic` – kvanbere Jan 20 '14 at 12:36
  • `volatile` is 100% portable on every C and C++ compiler ever made... – Lundin Jan 20 '14 at 12:39
  • @Lundin `volatile` is specified in C89 and subsequent specifications. The previous 2 decades of C, being pre-standard, started without `volatile` and later developed it. Confident `volatile` is neither in K&R 1st nor 2nd Ed and compilers existed that treated `volatile` not as a keyword but like any other identifier like 'foo'. Certainly the `volatile` is ubiquitous now. – chux - Reinstate Monica Jan 20 '14 at 19:45
  • @chux What happened before 1989 is hardly relevant to this question... compilers made before C98/C90 were not standardized - essentially they didn't compile for the C language, but for their own home-brewed version of it. And even though K&R 2nd edition has plenty of errors, it is mostly compatible with the C90 standard. They certainly mentioned volatile, see A.8.2. To cite K&R 2nd ed: "There are no implementation-dependent semantics for volatile objects." – Lundin Jan 21 '14 at 07:39

0 Answers0