Skip to content

fiber: add async feature for multithreading #277

Open

Description

@sidyhe

功能需求

在任意一个线程(包括fiber所在线程)可以向任意EVENT投递一个void*
被投递的EVENT有fiber在等待数据, 同时支持超时机制
最好做到. 有序投递, 有序处理 (不考虑多线程并发投递, 1:1)
投递之后可以有一个队列暂存, 若队列满了可以考虑丢弃 (并输出日志)

接口示意

typedef struct ACL_FIBER_ASYNC ACL_FIBER_ASYNC;

FIBER_API ACL_FIBER_ASYNC* acl_fiber_async_create(int max_queue);
FIBER_API int acl_fiber_async_post(ACL_FIBER_ASYNC* async, void* data); // can be use in other thread
FIBER_API int acl_fiber_async_wait(ACL_FIBER_ASYNC* async, void** data, int timeout);
FIBER_API int acl_fiber_async_free(ACL_FIBER_ASYNC* async);

详细描述

目前的多线程通讯是基于socket, 实现方式比较通用, 但个人感觉有些过重了, 也会增加listen socket
最好是利用OS提供的进程内IPC机制
查阅资料和参考libuv之后的实现思路:

  • poll/epoll: eventfd (read/write)
  • iocp: PostQueuedCompletionStatus
  • wmsg: PostMessage

而select和kqueue不了解, 或许可以先用socket pair?
也考虑过匿名pipe实现, 但细节尚未考虑周全

参考资料

https://man7.org/linux/man-pages/man2/eventfd.2.html
https://docs.microsoft.com/en-us/windows/win32/fileio/i-o-completion-ports
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-postmessagea

Activity

  1. zhengshuxin commented on May 6, 2022

    @zhengshuxin
    Member

    你所说的这个功能在 acl::fiber_tbox 类里已经实现,该类是基于 C 代码中的 acl_fiber_event_xxx 系列操作,在 acl_fiber_event_wait() 时一旦发生等待,则针对不同平台采用不同的方法:
    1、Linux:eventfd(),这个仅占用一个句柄;
    2、MacOS:socketpair,占用两个句柄;
    3、Windows:用 TCP socket 模拟的 socketpair,因为在windows存在 iocp 引擎及界面消息引擎,所以使用 tcp socket 更通用些;如果有人提供windows下的优化版本,则不胜感激。

    另外,我曾经想将等待时所用的事件句柄合并成一个放在主循环里来实现,后来没时间弄这块,也许将来可以继续优化。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions