PhotonLibOS icon indicating copy to clipboard operation
PhotonLibOS copied to clipboard

Do coroutines support local storage (like thread_local)?

Open jiangdongzi opened this issue 3 years ago • 8 comments

jiangdongzi avatar Aug 26 '22 05:08 jiangdongzi

May I know the reason you want this feature? Is that you want better performance when sharing resource among coroutines within a unique cpu core?

beef9999 avatar Aug 26 '22 12:08 beef9999

BTW, we used to migrate some thread-based projects to coroutines with Photon, for example, rocksdb. No matter of std::thread, std::mutex, std::condition_variable and so on, most of the components are very easy to transform to Photon's equivalents, but thread_local is still the thing we are uncertain how to deal with. If you have met the same problem, we would like to hear your situation.

beef9999 avatar Aug 28 '22 01:08 beef9999

@jiangdongzi Photo supports a very limited form of coroutine local storage, via:

void* thread_get_local();
void thread_set_local(void* local);

lihuiba avatar Aug 29 '22 04:08 lihuiba

BTW, we used to migrate some thread-based projects to coroutines with Photon, for example, rocksdb. No matter of std::thread, std::mutex, std::condition_variable and so on, most of the components are very easy to transform to Photon's equivalents, but thread_local is still the thing we are uncertain how to deal with. If you have met the same problem, we would like to hear your situation.

In our business, we hash user to specified thread via uid, every business thread has its own specified data. While coroutine maybe transfered from std::threadA to std::threadB, this will result in problem.

jiangdongzi avatar Aug 30 '22 06:08 jiangdongzi

In our business, we hash user to specified thread via uid, every business thread has its own specified data. While coroutine maybe transfered from std::threadA to std::threadB, this will result in problem.

Coroutines are not necessarily transferred from one std::thread to another, unless you instruct photon to do so. It is possible that you can still use thread_local variables?

lihuiba avatar Aug 30 '22 06:08 lihuiba

In our business, we hash user to specified thread via uid, every business thread has its own specified data. While coroutine maybe transfered from std::threadA to std::threadB, this will result in problem.

Coroutines are not necessarily transferred from one std::thread to another, unless you instruct photon to do so. It is possible that you can still use thread_local variables?

Yeah, if this is the behaviour, there is no problem. But do you have plans of auto transferring coroutines? Such as std::threadA is busy, and std::threadB is idle.

jiangdongzi avatar Aug 30 '22 13:08 jiangdongzi

But do you have plans of auto transferring coroutines? Such as std::threadA is busy, and std::threadB is idle.

Yes, we'll support automatic load balancing of threads (coroutines) among vCPUs (std::threads) by work-stealing. Perhaps within this year.

lihuiba avatar Sep 01 '22 04:09 lihuiba

@jiangdongzi TLS support has been added, similar to C++11 thread_localusage. See doc

beef9999 avatar Oct 11 '22 08:10 beef9999