@@ -69,34 +69,40 @@ public func _stdlib_thread_barrier_init(
6969 InitializeConditionVariable ( barrier. pointee. cond!)
7070#else
7171 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- }
7672 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 ( )
7978 return - 1
8079 }
8180#endif
8281 barrier. pointee. count = count
8382 return 0
8483}
8584
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+
8696public func _stdlib_thread_barrier_destroy(
8797 _ barrier: UnsafeMutablePointer < _stdlib_thread_barrier_t >
88- ) -> CInt {
98+ ) {
8999#if os(Windows)
90100 // Condition Variables do not need to be explicitly destroyed
91101 // Mutexes do not need to be explicitly destroyed
92102#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 " )
100106 }
101107#endif
102108 barrier. pointee. cond!. deinitialize ( count: 1 )
@@ -105,7 +111,7 @@ public func _stdlib_thread_barrier_destroy(
105111 barrier. pointee. mutex!. deinitialize ( count: 1 )
106112 barrier. pointee. mutex!. deallocate ( )
107113
108- return 0
114+ return
109115}
110116
111117public func _stdlib_thread_barrier_wait(
0 commit comments