1
- use super :: unix_pthread_wrapper:: PthreadAttr ;
2
-
3
1
#[ cfg( any( target_os = "freebsd" , target_os = "dragonfly" , target_os = "illumos" ) ) ]
4
2
use libc:: pthread_attr_get_np as get_attr;
5
3
#[ cfg( any( target_os = "linux" , target_os = "solaris" , target_os = "netbsd" ) ) ]
@@ -8,13 +6,53 @@ use libc::pthread_getattr_np as get_attr;
8
6
pub unsafe fn guess_os_stack_limit ( ) -> Option < usize > {
9
7
let mut attr = PthreadAttr :: new ( ) ?;
10
8
11
- let res = get_attr ( libc:: pthread_self ( ) , attr. as_mut_ptr ( ) ) ;
12
- attr. handle_pthread_err ( res) ?;
9
+ handle_pthread_err ( get_attr ( libc:: pthread_self ( ) , attr. as_mut_ptr ( ) ) ) ?;
13
10
14
11
let mut stackaddr = std:: ptr:: null_mut ( ) ;
15
12
let mut stacksize = 0 ;
16
- let res = libc:: pthread_attr_getstack ( attr. as_mut_ptr ( ) , & mut stackaddr, & mut stacksize) ;
17
- attr. handle_pthread_err ( res) ?;
13
+ handle_pthread_err ( libc:: pthread_attr_getstack (
14
+ attr. as_mut_ptr ( ) ,
15
+ & mut stackaddr,
16
+ & mut stacksize,
17
+ ) ) ?;
18
18
19
19
Some ( stackaddr as usize )
20
20
}
21
+
22
+ struct PthreadAttr ( std:: mem:: MaybeUninit < libc:: pthread_attr_t > ) ;
23
+
24
+ impl Drop for PthreadAttr {
25
+ fn drop ( & mut self ) {
26
+ unsafe {
27
+ let ret = libc:: pthread_attr_destroy ( self . 0 . as_mut_ptr ( ) ) ;
28
+ if ret != 0 {
29
+ let err = std:: io:: Error :: last_os_error ( ) ;
30
+ panic ! (
31
+ "pthread_attr_destroy failed with error code {}: {}" ,
32
+ ret, err
33
+ ) ;
34
+ }
35
+ }
36
+ }
37
+ }
38
+
39
+ fn handle_pthread_err ( ret : libc:: c_int ) -> Option < ( ) > {
40
+ if ret != 0 {
41
+ return None ;
42
+ }
43
+ Some ( ( ) )
44
+ }
45
+
46
+ impl PthreadAttr {
47
+ unsafe fn new ( ) -> Option < Self > {
48
+ let mut attr = std:: mem:: MaybeUninit :: < libc:: pthread_attr_t > :: uninit ( ) ;
49
+ if libc:: pthread_attr_init ( attr. as_mut_ptr ( ) ) != 0 {
50
+ return None ;
51
+ }
52
+ Some ( PthreadAttr ( attr) )
53
+ }
54
+
55
+ fn as_mut_ptr ( & mut self ) -> * mut libc:: pthread_attr_t {
56
+ self . 0 . as_mut_ptr ( )
57
+ }
58
+ }
0 commit comments