Skip to content

Commit 0d52cec

Browse files
committed
Fix potential race in Metal on exit
1 parent f7ab0b9 commit 0d52cec

File tree

2 files changed

+53
-51
lines changed

2 files changed

+53
-51
lines changed

Diff for: hiro/cocoa/widget/label.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,7 @@ auto pLabel::setForegroundColor(SystemColor color) -> void {
161161
}
162162

163163
auto pLabel::setText(const string& text) -> void {
164-
dispatch_async(dispatch_get_main_queue(), ^{
165-
[cocoaView setNeedsDisplay:YES];
166-
});
164+
[cocoaView setNeedsDisplay:YES];
167165
}
168166

169167
}

Diff for: ruby/video/metal/metal.cpp

+52-48
Original file line numberDiff line numberDiff line change
@@ -159,29 +159,27 @@ struct VideoMetal : VideoDriver, Metal {
159159
}
160160

161161
auto setShader(string pathname) -> bool override {
162-
if (_filterChain != NULL) {
163-
_libra.mtl_filter_chain_free(&_filterChain);
164-
}
165-
166-
if (_preset != NULL) {
167-
_libra.preset_free(&_preset);
168-
}
169-
170-
if(file::exists(pathname)) {
171-
if (auto error = _libra.preset_create(pathname.data(), &_preset)) {
172-
print(string{"Metal: Failed to load shader: ", pathname, "\n"});
173-
_libra.error_print(error);
174-
return false;
162+
dispatch_async(_renderQueue, ^{
163+
if (_filterChain != NULL) {
164+
_libra.mtl_filter_chain_free(&_filterChain);
175165
}
176-
177-
if (auto error = _libra.mtl_filter_chain_create(&_preset, _commandQueue, nil, &_filterChain)) {
178-
print(string{"Metal: Failed to create filter chain for: ", pathname, "\n"});
179-
_libra.error_print(error);
180-
return false;
181-
};
182-
} else {
183-
return false;
184-
}
166+
167+
if (_preset != NULL) {
168+
_libra.preset_free(&_preset);
169+
}
170+
171+
if(file::exists(pathname)) {
172+
if (auto error = _libra.preset_create(pathname.data(), &_preset)) {
173+
print(string{"Metal: Failed to load shader: ", pathname, "\n"});
174+
_libra.error_print(error);
175+
}
176+
177+
if (auto error = _libra.mtl_filter_chain_create(&_preset, _commandQueue, nil, &_filterChain)) {
178+
print(string{"Metal: Failed to create filter chain for: ", pathname, "\n"});
179+
_libra.error_print(error);
180+
};
181+
}
182+
});
185183
return true;
186184
}
187185

@@ -421,9 +419,11 @@ struct VideoMetal : VideoDriver, Metal {
421419

422420
id<CAMetalDrawable> drawable = view.currentDrawable;
423421

422+
__block VideoMetal &strongSelf = self;
423+
424424
if (@available(macOS 10.15.4, *)) {
425425
[drawable addPresentedHandler:^(id<MTLDrawable> drawable) {
426-
self.drawableWasPresented(drawable);
426+
strongSelf.drawableWasPresented(drawable);
427427
depth--;
428428
}];
429429
}
@@ -599,31 +599,35 @@ struct VideoMetal : VideoDriver, Metal {
599599
}
600600

601601
auto terminate() -> void {
602-
_ready = false;
603-
604-
_commandQueue = nullptr;
605-
_library = nullptr;
606-
607-
_vertexBuffer = nullptr;
608-
for (int i = 0; i < kMaxSourceBuffersInFlight; i++) {
609-
_sourceTextures[i] = nullptr;
610-
}
611-
_mtlVertexDescriptor = nullptr;
612-
613-
_renderToTextureRenderPassDescriptor = nullptr;
614-
_renderTargetTexture = nullptr;
615-
_renderToTextureRenderPipeline = nullptr;
616-
617-
_drawableRenderPipeline = nullptr;
618-
619-
if (_filterChain) {
620-
_libra.mtl_filter_chain_free(&_filterChain);
621-
}
622-
_device = nullptr;
623-
624-
if (view) {
625-
[view removeFromSuperview];
626-
view = nil;
602+
if(_renderQueue) {
603+
dispatch_sync(_renderQueue, ^{
604+
_ready = false;
605+
606+
_commandQueue = nullptr;
607+
_library = nullptr;
608+
609+
_vertexBuffer = nullptr;
610+
for (int i = 0; i < kMaxSourceBuffersInFlight; i++) {
611+
_sourceTextures[i] = nullptr;
612+
}
613+
_mtlVertexDescriptor = nullptr;
614+
615+
_renderToTextureRenderPassDescriptor = nullptr;
616+
_renderTargetTexture = nullptr;
617+
_renderToTextureRenderPipeline = nullptr;
618+
619+
_drawableRenderPipeline = nullptr;
620+
621+
if (_filterChain) {
622+
_libra.mtl_filter_chain_free(&_filterChain);
623+
}
624+
_device = nullptr;
625+
626+
if (view) {
627+
[view removeFromSuperview];
628+
view = nil;
629+
}
630+
});
627631
}
628632
}
629633

0 commit comments

Comments
 (0)