@@ -69,34 +69,40 @@ 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.
73
+ guard _stdlib_thread_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 ( )
79
78
return - 1
80
79
}
81
80
#endif
82
81
barrier. pointee. count = count
83
82
return 0
84
83
}
85
84
85
+ private func _stdlib_thread_barrier_mutex_and_cond_init( _ barrier: UnsafeMutablePointer < _stdlib_thread_barrier_t > ) -> CInt {
86
+ guard pthread_mutex_init ( barrier. pointee. mutex!, nil ) == 0 else {
87
+ return - 1
88
+ }
89
+ guard pthread_cond_init ( barrier. pointee. cond!, nil ) == 0 else {
90
+ pthread_mutex_destroy ( barrier. pointee. mutex!)
91
+ return - 1
92
+ }
93
+ return 0
94
+ }
95
+
86
96
public func _stdlib_thread_barrier_destroy(
87
97
_ barrier: UnsafeMutablePointer < _stdlib_thread_barrier_t >
88
- ) -> CInt {
98
+ ) {
89
99
#if os(Windows)
90
100
// Condition Variables do not need to be explicitly destroyed
91
101
// Mutexes do not need to be explicitly destroyed
92
102
#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.
99
- return - 1
103
+ guard pthread_cond_destroy ( barrier. pointee. cond!) == 0 &&
104
+ pthread_mutex_destroy ( barrier. pointee. mutex!) == 0 else {
105
+ fatalError ( " _stdlib_thread_barrier_destroy() failed " )
100
106
}
101
107
#endif
102
108
barrier. pointee. cond!. deinitialize ( count: 1 )
@@ -105,7 +111,7 @@ public func _stdlib_thread_barrier_destroy(
105
111
barrier. pointee. mutex!. deinitialize ( count: 1 )
106
112
barrier. pointee. mutex!. deallocate ( )
107
113
108
- return 0
114
+ return
109
115
}
110
116
111
117
public func _stdlib_thread_barrier_wait(
0 commit comments