tezc / sc

Common libraries and data structures for C.
BSD 3-Clause "New" or "Revised" License
2.23k stars 239 forks source link

sc_sock_poll thread safety #99

Closed svladykin closed 1 year ago

svladykin commented 1 year ago

Fixes https://github.com/tezc/sc/issues/98

For all implementations:

For Linux/FreeBSD/MacOS (epoll and kqueue) :

For Windows (WSAPoll):

codecov[bot] commented 1 year ago

Codecov Report

Merging #99 (7153a7f) into master (611a97d) will decrease coverage by 0.32%. The diff coverage is 89.18%.

Additional details and impacted files [![Impacted file tree graph](https://codecov.io/gh/tezc/sc/pull/99/graphs/tree.svg?width=650&height=150&src=pr&token=O8ZHQ0XZ30&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Ozan+Tezcan)](https://codecov.io/gh/tezc/sc/pull/99?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Ozan+Tezcan) ```diff @@ Coverage Diff @@ ## master #99 +/- ## ========================================== - Coverage 99.81% 99.49% -0.33% ========================================== Files 21 21 Lines 2198 2180 -18 Branches 388 385 -3 ========================================== - Hits 2194 2169 -25 - Misses 0 3 +3 - Partials 4 8 +4 ``` | [Impacted Files](https://codecov.io/gh/tezc/sc/pull/99?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Ozan+Tezcan) | Coverage Δ | | |---|---|---| | [socket/sc\_sock.c](https://codecov.io/gh/tezc/sc/pull/99/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Ozan+Tezcan#diff-c29ja2V0L3NjX3NvY2suYw==) | `98.58% <89.18%> (-1.42%)` | :arrow_down: | ------ [Continue to review full report at Codecov](https://codecov.io/gh/tezc/sc/pull/99?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Ozan+Tezcan). > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Ozan+Tezcan) > `Δ = absolute (impact)`, `ø = not affected`, `? = missing data` > Powered by [Codecov](https://codecov.io/gh/tezc/sc/pull/99?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Ozan+Tezcan). Last update [611a97d...7153a7f](https://codecov.io/gh/tezc/sc/pull/99?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Ozan+Tezcan). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Ozan+Tezcan).
svladykin commented 1 year ago

@tezc Please review.

As expected epoll/kqueue implementations got simplified (no poll->events resizing), windows got complicated (but I tried to comment extensively).

There are no backward incompatible changes from the API point of view: poll->err was replaced with a static thread-local, so now it is thread safe without any change in semantics. Maybe it is worth using this approach for sockets as well to avoid having 128 bytes long err in every socket.

I understand the PR is large, I can try to split it into multiple smaller ones, but the biggest part there is Windows and it would be hard to split.

tezc commented 1 year ago

@svladykin Thanks, I'll have a look in a couple of days.

svladykin commented 1 year ago

@tezc Did you have a chance to take a look?

tezc commented 1 year ago

@svladykin Sorry for the delay. I was a bit busy these days. I had a quick look and pushed some minor changes mostly about style. I need some more time to wrap my head around, hopefully I'll complete the review by tomorrow.

svladykin commented 1 year ago

@tezc No rush, I just wanted to make sure you did not forget about this PR :)

tezc commented 1 year ago

@svladykin Sorry for the delay. I didn't have much time to review and probably it will take more time to wrap my head around the changes fully. Not to make this review longer, considering API is compatible and most changes are on Windows, I think we can merge it and I'll take a look in parallel and ask you questions if I need :)

Added a few comments in the PR regarding return value checks, after those, we can merge the PR I guess. Thanks for the PR and sorry for late review. Last month was a bit busy and looks like there is another busy month upcoming for me :)

svladykin commented 1 year ago

@tezc Thanks for the review. I will address your comments shortly.

svladykin commented 1 year ago

@tezc I have made the requested adjustments. Also I fixed a WSAPoll error handling issue on Windows. Please look.

tezc commented 1 year ago

@svladykin Thank you very much, looks good to me. We have %100 branch coverage on Linux but other OSes like Windows, damn there are many bugs. Thank you for the fixes.

Is there anything else we need to do for this PR? Btw could you add a few lines to the top comment and briefly describe the changes?

svladykin commented 1 year ago

@tezc I believe this PR is good to go. The epoll/kqueue changes seems to be safe, Windows is less safe, but if any issues arise I will fix them.