Atomic Queue
folly의 AtomicQueue는 lock-free MPSC 큐로, 소비자 스레드를 불필요하게 깨우지 않고 효율적으로 작업을 전달하는 메커니즘을 제공한다. 주로 folly::EventBase의 알림 큐로 사용되며, 여러 생산자가 EventBase 스레드에 작업을 전달할 때 최소한의 시스템 호출로 소비자를 깨우는 것을 목표로 한다. 큐는 단일 atomic 변수로 상태를 관리하며, 소비자가 대기 상태에 들어가기 전 큐를 'arm'하고, 생산자가 작업을 푸시할 때만 필요 시 소비자를 깨운다. 이 설계는 epoll 기반 이벤트 루프와 잘 맞으며, 경쟁 조건 없이 안정적인 깨어남을 보장한다.
Atomic Queue
This post is about folly’s AtomicQueue. It is a lock-free MPSC(multiple-producer-single-consumer) queue that only wakes up the consumer when necessary. It is mainly used as the notification queue for folly::EventBase, as a way to pass work from external threads to be executed on the EventBase thread. In this context, the EventBase is the consumer, and it always runs on the same thread. There can be many producers requesting work to be done inside the EventBase. The main loop of folly::EventBase basically does two things Call libevent’s event_base_loop() and block on epoll_wait if there are no pending tasks to be executed. If some event handlers are ready, the callbacks are executed inline (in event_base_loop()). The callbacks can schedule more work to be done on the EventBase and they are called LoopCallbacks. Execute various tasks (drain the notification queue, aka external tasks, and loop callbacks, aka internal tasks).