Compiler optimization breaks multi-threaded code

2010-11-14 Thread Michal Minich
There is one question on SO which seems like a serious problem for atomic ops. http://stackoverflow.com/questions/4165149/compiler-optimization-breaks- multi-threaded-code in short: shared uint cnt; void atomicInc ( ) { uint o; while ( !cas( &cnt, o, o + 1 ) ) o = cnt; } is compiled with

Re: Compiler optimization breaks multi-threaded code

2010-11-14 Thread Sean Kelly
Michal Minich Wrote: > There is one question on SO which seems like a serious problem for atomic > ops. > > http://stackoverflow.com/questions/4165149/compiler-optimization-breaks- > multi-threaded-code > > in short: > > shared uint cnt; > void atomicInc ( ) { ui

Re: Compiler optimization breaks multi-threaded code

2010-11-15 Thread Kagamin
Sean Kelly Wrote: > > shared uint cnt; > > void atomicInc ( ) { uint o; while ( !cas( &cnt, o, o + 1 ) ) o = cnt; } > > > > is compiled with dmd -O to something like: > > > > shared uint cnt; > > void atomicInc ( ) { while ( !cas( &cnt, cnt, cnt + 1 ) ) { } } > What a mess. DMD isn't supposed

Re: Compiler optimization breaks multi-threaded code

2010-11-16 Thread Sean Kelly
Kagamin wrote: > Sean Kelly Wrote: > >>> shared uint cnt; >>> void atomicInc ( ) { uint o; while ( !cas( &cnt, o, o + 1 ) ) o = > > > cnt; } >>> >>> is compiled with dmd -O to something like: >>> >>> shared uint cnt; >>> void atomicInc ( ) { while ( !cas( &cnt, cnt, cnt + 1 ) ) { } } >> What

Re: Compiler optimization breaks multi-threaded code

2010-11-16 Thread stephan
Am 16.11.2010 18:09, schrieb Sean Kelly: cas() contains an asm block. Though I guess in this case the compiler isn't actually optimizing across it. Does atomic!"+="(&cnt, 1) work correctly? I know the issue with shared would still have to be fixed, but that code uses asm for the load as well, so

Re: Compiler optimization breaks multi-threaded code

2010-11-16 Thread Sean Kelly
stephan Wrote: > Am 16.11.2010 18:09, schrieb Sean Kelly: > > cas() contains an asm block. Though I guess in this case the compiler > > isn't actually optimizing across it. Does atomic!"+="(&cnt, 1) work > > correctly? I know the issue with shared would still have to be fixed, > > but that code u

Re: Compiler optimization breaks multi-threaded code

2010-11-17 Thread stephan
atomicOp uses a CAS loop for the RMW operations. Ignore my comment. I should have looked at the code in core.atomic before commenting. I just had one test case with atomicOp!("+=") that worked, and assumed that atomicOp!("+=") was implemented with "lock xadd". I'm thinking of exposing atomicS