include/boost/corosio/native/detail/iocp/win_wsa_init.hpp
84.6% Lines (11/13)
100.0% List of functions (3/3)
85.7% Branches (6/7)
Functions (3)
| Line | Branch | TLA | Hits | Source Code |
|---|---|---|---|---|
| 1 | // | |||
| 2 | // Copyright (c) 2025 Vinnie Falco ([email protected]) | |||
| 3 | // Copyright (c) 2026 Steve Gerbino | |||
| 4 | // | |||
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |||
| 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||
| 7 | // | |||
| 8 | // Official repository: https://github.com/cppalliance/corosio | |||
| 9 | // | |||
| 10 | ||||
| 11 | #ifndef BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_WSA_INIT_HPP | |||
| 12 | #define BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_WSA_INIT_HPP | |||
| 13 | ||||
| 14 | #include <boost/corosio/detail/platform.hpp> | |||
| 15 | ||||
| 16 | #if BOOST_COROSIO_HAS_IOCP | |||
| 17 | ||||
| 18 | #include <boost/corosio/detail/config.hpp> | |||
| 19 | #include <boost/corosio/native/detail/make_err.hpp> | |||
| 20 | #include <boost/corosio/detail/except.hpp> | |||
| 21 | ||||
| 22 | #include <boost/corosio/native/detail/iocp/win_windows.hpp> | |||
| 23 | ||||
| 24 | namespace boost::corosio::detail { | |||
| 25 | ||||
| 26 | /** RAII class for Winsock initialization. | |||
| 27 | ||||
| 28 | Uses reference counting to ensure WSAStartup is called once on | |||
| 29 | first construction and WSACleanup on last destruction. | |||
| 30 | ||||
| 31 | Derive from this class to ensure Winsock is initialized before | |||
| 32 | any socket operations. | |||
| 33 | */ | |||
| 34 | class BOOST_COROSIO_DECL win_wsa_init | |||
| 35 | { | |||
| 36 | protected: | |||
| 37 | win_wsa_init(); | |||
| 38 | ~win_wsa_init(); | |||
| 39 | ||||
| 40 | win_wsa_init(win_wsa_init const&) = delete; | |||
| 41 | win_wsa_init& operator=(win_wsa_init const&) = delete; | |||
| 42 | }; | |||
| 43 | ||||
| 44 | // Process-wide Winsock init refcount. Kept as an inline function-local | |||
| 45 | // static (one shared instance across TUs) rather than a static data member | |||
| 46 | // so win_wsa_init can carry BOOST_COROSIO_DECL: an exported/imported static | |||
| 47 | // data member defined in a header is rejected by MSVC and clang-cl | |||
| 48 | // ("definition of dllimport static field not allowed"). | |||
| 49 | 4920x | inline long& win_wsa_init_count() noexcept | ||
| 50 | { | |||
| 51 | static long count = 0; | |||
| 52 | 4920x | return count; | ||
| 53 | } | |||
| 54 | ||||
| 55 | 2460x | inline win_wsa_init::win_wsa_init() | ||
| 56 | { | |||
| 57 |
2/2✓ Branch 5 → 6 taken 612 times.
✓ Branch 5 → 14 taken 1848 times.
|
4920x | if (::InterlockedIncrement(&win_wsa_init_count()) == 1) | |
| 58 | { | |||
| 59 | WSADATA wsaData; | |||
| 60 |
1/1✓ Branch 6 → 7 taken 612 times.
|
612x | int result = ::WSAStartup(MAKEWORD(2, 2), &wsaData); | |
| 61 |
1/2✗ Branch 7 → 8 not taken.
✓ Branch 7 → 13 taken 612 times.
|
612x | if (result != 0) | |
| 62 | { | |||
| 63 | ✗ | ::InterlockedDecrement(&win_wsa_init_count()); | ||
| 64 | ✗ | throw_system_error(make_err(result)); | ||
| 65 | } | |||
| 66 | } | |||
| 67 | 2460x | } | ||
| 68 | ||||
| 69 | 2460x | inline win_wsa_init::~win_wsa_init() | ||
| 70 | { | |||
| 71 |
2/2✓ Branch 5 → 6 taken 612 times.
✓ Branch 5 → 7 taken 1848 times.
|
4920x | if (::InterlockedDecrement(&win_wsa_init_count()) == 0) | |
| 72 | 612x | ::WSACleanup(); | ||
| 73 | 2460x | } | ||
| 74 | ||||
| 75 | } // namespace boost::corosio::detail | |||
| 76 | ||||
| 77 | #endif // BOOST_COROSIO_HAS_IOCP | |||
| 78 | ||||
| 79 | #endif // BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_WSA_INIT_HPP | |||
| 80 |