douban / greenify

Make blocking C library work with gevent
BSD 3-Clause "New" or "Revised" License
430 stars 58 forks source link

Support for C++ conditional variables #27

Closed kentslaney closed 5 months ago

kentslaney commented 9 months ago

Threaded libmc clients maintain consistent cache response times by queueing requests to acquire a mutex from the client pool. In order for this to work, threads releasing the client have to notify the waiting threads that a client is now available. This is done through a C++ condition_variable on the stack to avoid heap allocation. The problem right now is that greenlets swap the stacks in memory, meaning that any stack pointer from another thread is no longer valid as it now points to the notifying stack. In contrast, C++ uses virtual address spaces to separate the threads' stack addresses.

[update: still pending as of 2024-03-03; backlogged]

kentslaney commented 5 months ago

The overlapping stack is important for the execution context setup in gevent, so there’s no good way to pass stack pointers across greenlets (which each get their own C thread for the memcached client) meaning there’s no solution on the greenify side (heaps are per-greenlet too). It’s possible that the libmc implementation could be modified to detect gevent usage, but it defeats the idea of an alternate drop in threading solution. Having the threaded client be native python thread exclusive seems reasonable anyways.