From 78e3bb748b195a8a1902938101ea2c74bec4d120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Fri, 9 Apr 2021 09:57:41 +0200 Subject: [PATCH] sync: close channels atomically only once (#9645) --- vlib/sync/channels.v | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/vlib/sync/channels.v b/vlib/sync/channels.v index cfabbb820d..bae6526fcd 100644 --- a/vlib/sync/channels.v +++ b/vlib/sync/channels.v @@ -138,7 +138,10 @@ fn new_channel_st(n u32, st u32) &Channel { } pub fn (mut ch Channel) close() { - C.atomic_store_u16(&ch.closed, 1) + open_val := u16(0) + if !C.atomic_compare_exchange_strong_u16(&ch.closed, &open_val, 1) { + return + } mut nulladr := voidptr(0) for !C.atomic_compare_exchange_weak_ptr(&ch.adr_written, &nulladr, voidptr(-1)) { nulladr = voidptr(0)