File tree 1 file changed +11
-6
lines changed
library/std/src/sys/windows
1 file changed +11
-6
lines changed Original file line number Diff line number Diff line change 2
2
3
3
use crate :: cmp;
4
4
use crate :: io:: { self , IoSlice , IoSliceMut , Read } ;
5
+ use crate :: lazy:: SyncOnceCell ;
5
6
use crate :: mem;
6
7
use crate :: net:: { Shutdown , SocketAddr } ;
7
8
use crate :: ptr;
8
- use crate :: sync:: Once ;
9
9
use crate :: sys;
10
10
use crate :: sys:: c;
11
11
use crate :: sys_common:: net;
@@ -26,26 +26,31 @@ pub mod netc {
26
26
27
27
pub struct Socket ( c:: SOCKET ) ;
28
28
29
- static INIT : Once = Once :: new ( ) ;
29
+ static WSA : SyncOnceCell < unsafe extern "system" fn ( ) -> i32 > = SyncOnceCell :: new ( ) ;
30
30
31
31
/// Checks whether the Windows socket interface has been started already, and
32
32
/// if not, starts it.
33
33
pub fn init ( ) {
34
- INIT . call_once ( || unsafe {
34
+ let _ = WSA . get_or_init ( || unsafe {
35
35
let mut data: c:: WSADATA = mem:: zeroed ( ) ;
36
36
let ret = c:: WSAStartup (
37
37
0x202 , // version 2.2
38
38
& mut data,
39
39
) ;
40
40
assert_eq ! ( ret, 0 ) ;
41
+
42
+ // Only register `WSACleanup` if `WSAStartup` is actually ever called.
43
+ // Workaround to prevent linking to `WS2_32.dll` when no network functionality is used.
44
+ // See issue #85441.
45
+ c:: WSACleanup
41
46
} ) ;
42
47
}
43
48
44
49
pub fn cleanup ( ) {
45
- if INIT . is_completed ( ) {
46
- // only close the socket interface if it has actually been started
50
+ // only perform cleanup if network functionality was actually initialized
51
+ if let Some ( cleanup ) = WSA . get ( ) {
47
52
unsafe {
48
- c :: WSACleanup ( ) ;
53
+ cleanup ( ) ;
49
54
}
50
55
}
51
56
}
You can’t perform that action at this time.
0 commit comments