@@ -69,33 +69,38 @@ public func _stdlib_thread_barrier_init(
69
69
InitializeConditionVariable ( barrier. pointee. cond!)
70
70
#else
71
71
barrier. pointee. mutex = UnsafeMutablePointer . allocate ( capacity: 1 )
72
- if pthread_mutex_init ( barrier. pointee. mutex!, nil ) != 0 {
73
- // FIXME: leaking memory.
74
- return - 1
75
- }
76
72
barrier. pointee. cond = UnsafeMutablePointer . allocate ( capacity: 1 )
77
- if pthread_cond_init ( barrier. pointee. cond!, nil ) != 0 {
78
- // FIXME: leaking memory, leaking a mutex.
79
- return - 1
73
+ guard _stdlib_pthread_barrier_mutex_and_cond_init ( barrier) == 0 else {
74
+ barrier. pointee. mutex!. deinitialize ( count: 1 )
75
+ barrier. pointee. mutex!. deallocate ( )
76
+ barrier. pointee. cond!. deinitialize ( count: 1 )
77
+ barrier. pointee. cond!. deallocate ( )
80
78
}
81
79
#endif
82
80
barrier. pointee. count = count
83
81
return 0
84
82
}
85
83
84
+ private func _stdlib_pthread_barrier_mutex_and_cond_init( _ barrier: UnsafeMutablePointer < _stdlib_pthread_barrier_t > ) -> CInt {
85
+ guard pthread_mutex_init ( barrier. pointee. mutex!) == 0 else {
86
+ return - 1
87
+ }
88
+ guard pthread_cond_init ( barrier. pointee. cond!) == 0 else {
89
+ pthread_mutex_destroy ( barrier. pointee. mutex!)
90
+ return - 1
91
+ }
92
+ return 0
93
+ }
94
+
86
95
public func _stdlib_thread_barrier_destroy(
87
96
_ barrier: UnsafeMutablePointer < _stdlib_thread_barrier_t >
88
97
) -> CInt {
89
98
#if os(Windows)
90
99
// Condition Variables do not need to be explicitly destroyed
91
100
// Mutexes do not need to be explicitly destroyed
92
101
#else
93
- if pthread_cond_destroy ( barrier. pointee. cond!) != 0 {
94
- // FIXME: leaking memory, leaking a mutex.
95
- return - 1
96
- }
97
- if pthread_mutex_destroy ( barrier. pointee. mutex!) != 0 {
98
- // FIXME: leaking memory.
102
+ guard pthread_cond_destroy ( barrier. pointee. cond!) == 0 &&
103
+ pthread_mutex_destroy ( barrier. pointee. mutex!) == 0 else {
99
104
return - 1
100
105
}
101
106
#endif
0 commit comments