@@ -4,7 +4,7 @@ use std::ffi::c_void;
44use std:: sync:: atomic:: { AtomicBool , Ordering } ;
55use std:: sync:: mpsc;
66use std:: sync:: Arc ;
7- use std:: thread;
7+ use std:: thread:: { self , JoinHandle } ;
88
99use raw_window_handle:: {
1010 HasRawDisplayHandle , HasRawWindowHandle , RawDisplayHandle , RawWindowHandle , XlibDisplayHandle ,
@@ -31,18 +31,16 @@ use crate::x11::visual_info::WindowVisualConfig;
3131
3232pub struct WindowHandle {
3333 raw_window_handle : Option < RawWindowHandle > ,
34+ event_loop_handle : Option < JoinHandle < ( ) > > ,
3435 close_requested : Arc < AtomicBool > ,
3536 is_open : Arc < AtomicBool > ,
3637}
3738
3839impl WindowHandle {
3940 pub fn close ( & mut self ) {
40- if self . raw_window_handle . take ( ) . is_some ( ) {
41- // FIXME: This will need to be changed from just setting an atomic to somehow
42- // synchronizing with the window being closed (using a synchronous channel, or
43- // by joining on the event loop thread).
44-
45- self . close_requested . store ( true , Ordering :: Relaxed ) ;
41+ self . close_requested . store ( true , Ordering :: Relaxed ) ;
42+ if let Some ( event_loop) = self . event_loop_handle . take ( ) {
43+ let _ = event_loop. join ( ) ;
4644 }
4745 }
4846
@@ -72,9 +70,9 @@ impl ParentHandle {
7270 pub fn new ( ) -> ( Self , WindowHandle ) {
7371 let close_requested = Arc :: new ( AtomicBool :: new ( false ) ) ;
7472 let is_open = Arc :: new ( AtomicBool :: new ( true ) ) ;
75-
7673 let handle = WindowHandle {
7774 raw_window_handle : None ,
75+ event_loop_handle : None ,
7876 close_requested : Arc :: clone ( & close_requested) ,
7977 is_open : Arc :: clone ( & is_open) ,
8078 } ;
@@ -94,16 +92,17 @@ impl Drop for ParentHandle {
9492}
9593
9694pub ( crate ) struct WindowInner {
95+ // GlContext should be dropped **before** XcbConnection is dropped
96+ #[ cfg( feature = "opengl" ) ]
97+ gl_context : Option < GlContext > ,
98+
9799 pub ( crate ) xcb_connection : XcbConnection ,
98100 window_id : XWindow ,
99101 pub ( crate ) window_info : WindowInfo ,
100102 visual_id : Visualid ,
101103 mouse_cursor : Cell < MouseCursor > ,
102104
103105 pub ( crate ) close_requested : Cell < bool > ,
104-
105- #[ cfg( feature = "opengl" ) ]
106- gl_context : Option < GlContext > ,
107106}
108107
109108pub struct Window < ' a > {
@@ -133,17 +132,15 @@ impl<'a> Window<'a> {
133132 } ;
134133
135134 let ( tx, rx) = mpsc:: sync_channel :: < WindowOpenResult > ( 1 ) ;
136-
137135 let ( parent_handle, mut window_handle) = ParentHandle :: new ( ) ;
138-
139- thread:: spawn ( move || {
136+ let join_handle = thread:: spawn ( move || {
140137 Self :: window_thread ( Some ( parent_id) , options, build, tx. clone ( ) , Some ( parent_handle) )
141138 . unwrap ( ) ;
142139 } ) ;
143140
144141 let raw_window_handle = rx. recv ( ) . unwrap ( ) . unwrap ( ) ;
145142 window_handle. raw_window_handle = Some ( raw_window_handle. 0 ) ;
146-
143+ window_handle . event_loop_handle = Some ( join_handle ) ;
147144 window_handle
148145 }
149146
0 commit comments