How to guarantee read of the most recent value using stdatomic?

In a dual-core embedded system, core0 needs to interrupt core1 for several reasons. An enum paired with a union is used to pass both the reason and the data of the interrupt. The code is written in C.

#include <stdatomic.h>

typedef enum { // the interrupt reason, also indicates which member is valid in the union
  kData0,
  // ...
} InterruptId;

typedef struct {
  _Atomic InterruptId interruptId;
  union { // data associated with interruptId
    int data0;
    // ...
  };
} SharedMemory;

// core0
void InterruptCore1(); // write some registers in ARM GIC to interrupt core1
void PassData0ToCore1(SharedMemory *mem, int data0) {
  mem->data0 = data0;
  atomic_store_explicit(&mem->interruptId, kData0, memory_order_release);
  InterruptCore1();
}

// core1
void ProcessData0(int data0);
void OnCore0Interrupt(SharedMemory *mem) {
  InterruptId id =
      atomic_load_explicit(&mem->interruptId, memory_order_acquire);
  switch (id) {
  case kData0:
    ProcessData0(mem->data0);
    break;
  }
}

The above code seems to be incorrect, as core1 may see an outdated value of interruptId. As I understand it, the Release-Acquire ordering doesn’t guarantee that load can read the most recent value. Maybe atomic_thread_fence is the solution here.

typedef struct {
  InterruptId interruptId;
  union {
    int data0;
    // ...
  };
} SharedMemory;

// core0
void InterruptCore1(); // write some registers in ARM GIC to interrupt core1
void PassData0ToCore1(SharedMemory *mem, int data0) {
  mem->data0 = data0;
  mem->interruptId = kData0;
  atomic_thread_fence(memory_order_release);
  InterruptCore1();
}

// core1
void ProcessData0(int data0);
void OnCore0Interrupt(SharedMemory *mem) {
  atomic_thread_fence(memory_order_acquire);
  InterruptId id = mem->interruptId;
  switch (id) {
  case kData0:
    ProcessData0(mem->data0);
    break;
  }
}

Both the release and acquire version of atomic_thread_fence compile to dmb ish, which seems to be correct in the machine level. But then I come to read the C++ version of atomic_thread_fence and find it also requires the use of atomic variables, and the synchronization only works if load actually reads the released value.

Does that mean the second solution has the same problem as the first (core1 may read an outdated value)? How to guarantee core1 can read the most recent value? Thanks very much.

1

As I understand it, the Release-Acquire ordering doesn’t guarantee that load can read the most recent value.

All memory orders guarantee that all threads see “the most recent” write to all atomic objects. The differences arise from what state you can see in other objects, i.e. whether writes to other objects can be re-ordered around the atomic reads or writes.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật