include/boost/corosio/native/detail/iocp/win_wsa_init.hpp

94.1% Lines (16 / 17) 81.8% Branches (9 / 11) 100.0% Functions (4 / 4)
win_wsa_init.hpp
f(x) Functions (4)
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 inline long&
50 13409x win_wsa_init_count() noexcept
51 {
52 static long count = 0;
53 13409x return count;
54 }
55
56 // Hold Winsock for the life of the process and never release it, so the
57 // count never falls to zero. An asynchronous GetAddrInfoExW keeps
58 // settling inside ws2_32 past its completion routine, with no way to
59 // observe when, and a WSACleanup under it faults on a system DNS thread.
60 // That includes one run from a static destructor at exit; process
61 // teardown reclaims Winsock without it.
62 inline void
63 6642x win_wsa_hold_process() noexcept
64 {
65 [[maybe_unused]] static bool const held =
66
3/4
✓ Branch 2 → 3 taken 91 times.
✓ Branch 2 → 9 taken 6551 times.
✓ Branch 4 → 5 taken 91 times.
✗ Branch 4 → 9 not taken.
6733x (::InterlockedIncrement(&win_wsa_init_count()), true);
67 6642x }
68
69 6659x inline win_wsa_init::win_wsa_init()
70 {
71
2/2
✓ Branch 5 → 6 taken 108 times.
✓ Branch 5 → 14 taken 6551 times.
13318x if (::InterlockedIncrement(&win_wsa_init_count()) == 1)
72 {
73 WSADATA wsaData;
74
1/1
✓ Branch 6 → 7 taken 108 times.
108x int result = ::WSAStartup(MAKEWORD(2, 2), &wsaData);
75
2/2
✓ Branch 7 → 8 taken 17 times.
✓ Branch 7 → 13 taken 91 times.
108x if (result != 0)
76 {
77 17x ::InterlockedDecrement(&win_wsa_init_count());
78 17x throw_system_error(make_err(result));
79 }
80 }
81
82 // After the startup above, which must own the first count.
83 6642x win_wsa_hold_process();
84 6642x }
85
86 6642x inline win_wsa_init::~win_wsa_init()
87 {
88
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 6642 times.
13284x if (::InterlockedDecrement(&win_wsa_init_count()) == 0)
89 ✗ ::WSACleanup();
90 6642x }
91
92 } // namespace boost::corosio::detail
93
94 #endif // BOOST_COROSIO_HAS_IOCP
95
96 #endif // BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_WSA_INIT_HPP
97