axboe / liburing

Library providing helpers for the Linux kernel io_uring support
MIT License
2.72k stars 393 forks source link

One thread read, another thread write, how to close(fd) ? #1112

Closed sxstd001 closed 3 months ago

sxstd001 commented 4 months ago

threadA read, threadB write. if threadA close(fd). This synchronization is difficult to implement simply; This is far more complex than EPOLL.

sxstd001 commented 4 months ago

like this?

    // read 
    while(true){
        spinlock.lock();
        if(fd != -1){
            uring_prep_read(fd);
            uring_submit(...)
        }
        spinlock.ulock();
        ...
    }

    // close
    spinlock.lock();
    if(fd != -1){
        uring_prep_close(fd); 
        uring_submit(...)
        fd=-1;
    }
    spinlock.unlock();

    // write    
    while(true){
        spinlock.lock();
        if(fd != -1){
            uring_prep_write(fd);
            using_submit(...);
        }
        spinlock.ulock();
        ...
    }
isilence commented 4 months ago

That would be quite a bother, can't you just dup the file and make your life much easier? Hard to tell more without knowing if it's different rings, if it's using fixed files and so on.