>>> しばらく考えたところ、書き込み側で atomic ops 使えば十分という気がしてきました。
>>> それなら、readerは速度低下0なのでパフォーマンス問題がおこりようがないし。
>>> Linuxでしかテストしてませんが、こんなもんでどうでしょうか?

Windowsと !HAVE_GCC_ATOMIC_BUILTINS でテストしたところ、予想通りバグが見つかったので v2 を送ります。


変更点
・common.mk に依存関係を追記
・Windows で ATOMIC_OR マクロにtypo があったのを修正
・Windows で ATOMIC_OR に InterlockedOr ではなく、_InterlockedOr を
  使うようにし、#pragma intrinsic(_InterlockedOr) をついか
  (InterlockedOr でリンクが通らない理由がよくわかってない)
・ruby_atomic_exchange で戻り値が間違っていたのを修正
RnJvbSAwNjk5OTA0Yzg2MjE5MTYzMDM2MDdiNjAwYTc5OWJkNTU4MTQzZjRkIE1vbiBTZXAgMTcg
MDA6MDA6MDAgMjAwMQpGcm9tOiBLT1NBS0kgTW90b2hpcm8gPGtvc2FraS5tb3RvaGlyb0BnbWFp
bC5jb20+CkRhdGU6IFdlZCwgMTEgTWF5IDIwMTEgMjI6MTk6NTEgKzA5MDAKU3ViamVjdDogW1BB
VENIXSBpbnRlcnJ1cHQgZmxhZwoKU2lnbmVkLW9mZi1ieTogS09TQUtJIE1vdG9oaXJvIDxrb3Nh
a2kubW90b2hpcm9AZ21haWwuY29tPgotLS0KIGF0b21pYy5oICB8ICAgMzcgKysrKysrKysrKysr
KysrKysrKysrKysrKysrKysrKysrKysrKwogY29tbW9uLm1rIHwgICAgOCArKysrLS0tLQogc2ln
bmFsLmMgIHwgICAzNiArKysrKysrKystLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0KIHRocmVh
ZC5jICB8ICAgIDkgKysrKystLS0tCiB2bV9jb3JlLmggfCAgICA5ICsrKysrLS0tLQogNSBmaWxl
cyBjaGFuZ2VkLCA2MCBpbnNlcnRpb25zKCspLCAzOSBkZWxldGlvbnMoLSkKIGNyZWF0ZSBtb2Rl
IDEwMDY0NCBhdG9taWMuaAoKZGlmZiAtLWdpdCBhL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