From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7868) id ED0D1385841D; Wed, 2 Mar 2022 10:23:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ED0D1385841D Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Takashi Yano To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: console, pty: Revamp the acquire/release_attach_mutex timing. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/master X-Git-Oldrev: 42e23c5ad2641945097699e989427f0f88c7074b X-Git-Newrev: 5f4e7b0368940682706916fec6d22befd4e82703 Message-Id: <20220302102329.ED0D1385841D@sourceware.org> Date: Wed, 2 Mar 2022 10:23:29 +0000 (GMT) X-BeenThere: cygwin-cvs@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component git logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 02 Mar 2022 10:23:30 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D5f4e7b03689= 40682706916fec6d22befd4e82703 commit 5f4e7b0368940682706916fec6d22befd4e82703 Author: Takashi Yano Date: Wed Mar 2 16:00:31 2022 +0900 Cygwin: console, pty: Revamp the acquire/release_attach_mutex timing. =20 - This patch revises the acquiring/releasing timing for attach_mutex to make the period in which it is being acquired shorter. Further, acquiring/releasing are added to where they are missing but needed. Diff: --- winsup/cygwin/fhandler.h | 9 ++- winsup/cygwin/fhandler_console.cc | 138 +++++++++++++++++++++++++++++-----= ---- winsup/cygwin/fhandler_termios.cc | 2 + winsup/cygwin/fhandler_tty.cc | 36 ++++++++-- winsup/cygwin/select.cc | 14 ++-- 5 files changed, 155 insertions(+), 44 deletions(-) diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h index c5eb62136..fbe7135b1 100644 --- a/winsup/cygwin/fhandler.h +++ b/winsup/cygwin/fhandler.h @@ -1882,6 +1882,7 @@ class fhandler_serial: public fhandler_base #define release_output_mutex() \ __release_output_mutex (__PRETTY_FUNCTION__, __LINE__) =20 +extern DWORD mutex_timeout; DWORD acquire_attach_mutex (DWORD t); void release_attach_mutex (void); =20 @@ -2181,7 +2182,13 @@ private: ssize_t __stdcall write (const void *ptr, size_t len); void doecho (const void *str, DWORD len); int close (); - static bool exists () {return !!GetConsoleCP ();} + static bool exists () + { + acquire_attach_mutex (mutex_timeout); + UINT cp =3D GetConsoleCP (); + release_attach_mutex (); + return !!cp; + } =20 int tcflush (int); int tcsetattr (int a, const struct termios *t); diff --git a/winsup/cygwin/fhandler_console.cc b/winsup/cygwin/fhandler_con= sole.cc index 7693ab8e4..241ca48ea 100644 --- a/winsup/cygwin/fhandler_console.cc +++ b/winsup/cygwin/fhandler_console.cc @@ -57,8 +57,6 @@ fhandler_console::console_state NO_COPY *fhandler_console= ::shared_console_info; =20 bool NO_COPY fhandler_console::invisible_console; =20 -extern DWORD mutex_timeout; /* defined in fhandler_termios.cc */ - /* con_ra is shared in the same process. Only one console can exist in a process, therefore, static is suitable.= */ static struct fhandler_base::rabuf_t con_ra; @@ -86,7 +84,9 @@ public: { wchar_t bufw[WPBUF_LEN]; DWORD len =3D sys_mbstowcs (bufw, WPBUF_LEN, buf, ixput); + acquire_attach_mutex (mutex_timeout); WriteConsoleW (handle, bufw, len, NULL, 0); + release_attach_mutex (); } } wpbuf; =20 @@ -217,6 +217,7 @@ fhandler_console::cons_master_thread (handle_set_t *p, = tty *ttyp) switch (cygwait (p->input_handle, (DWORD) 0)) { case WAIT_OBJECT_0: + acquire_attach_mutex (mutex_timeout); ReadConsoleInputW (p->input_handle, input_rec, INREC_SIZE, &total_read); if (total_read =3D=3D INREC_SIZE /* Working space full */ @@ -230,6 +231,7 @@ fhandler_console::cons_master_thread (handle_set_t *p, = tty *ttyp) con.num_processed -=3D n; nowait =3D true; } + release_attach_mutex (); break; case WAIT_TIMEOUT: con.num_processed =3D 0; @@ -318,9 +320,11 @@ remove_record: { INPUT_RECORD tmp[inrec_size]; /* Writeback input records other than interrupt. */ + acquire_attach_mutex (mutex_timeout); WriteConsoleInputW (p->input_handle, input_rec, total_read, &n); /* Check if writeback was successfull. */ PeekConsoleInputW (p->input_handle, tmp, inrec_size, &n); + release_attach_mutex (); if (n < total_read) break; /* Someone has read input without acquiring input_mutex. ConEmu cygwin-connector? */ @@ -332,7 +336,9 @@ remove_record: for (ofst =3D 1; ofst <=3D incr; ofst++) if (memcmp (input_rec, tmp + ofst, m::bytes (total_read)) =3D=3D 0) { + acquire_attach_mutex (mutex_timeout); ReadConsoleInputW (p->input_handle, tmp, inrec_size, &n); + release_attach_mutex (); memcpy (input_rec, tmp + ofst, m::bytes (total_read)); memcpy (input_rec + total_read, tmp, m::bytes (ofst)); if (n > ofst + total_read) @@ -490,6 +496,7 @@ fhandler_console::set_input_mode (tty::cons_mode m, con= st termios *t, { DWORD oflags; WaitForSingleObject (p->input_mutex, mutex_timeout); + acquire_attach_mutex (mutex_timeout); GetConsoleMode (p->input_handle, &oflags); DWORD flags =3D oflags & (ENABLE_EXTENDED_FLAGS | ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE= ); @@ -526,6 +533,7 @@ fhandler_console::set_input_mode (tty::cons_mode m, con= st termios *t, set_output_mode (tty::cygwin, t, p); WriteConsoleW (p->output_handle, L"\033[?1h", 5, NULL, 0); } + release_attach_mutex (); ReleaseMutex (p->input_mutex); } =20 @@ -549,7 +557,9 @@ fhandler_console::set_output_mode (tty::cons_mode m, co= nst termios *t, flags |=3D DISABLE_NEWLINE_AUTO_RETURN; break; } + acquire_attach_mutex (mutex_timeout); SetConsoleMode (p->output_handle, flags); + release_attach_mutex (); ReleaseMutex (p->output_mutex); } =20 @@ -670,9 +680,7 @@ fhandler_console::set_raw_win32_keyboard_mode (bool new= _mode) void fhandler_console::set_cursor_maybe () { - acquire_attach_mutex (mutex_timeout); con.fillin (get_output_handle ()); - release_attach_mutex (); /* Nothing to do for xterm compatible mode. */ if (wincap.has_con_24bit_colors () && !con_is_legacy) return; @@ -694,9 +702,11 @@ fhandler_console::fix_tab_position (HANDLE h) /* Re-setting ENABLE_VIRTUAL_TERMINAL_PROCESSING fixes the tab position. */ DWORD mode; + acquire_attach_mutex (mutex_timeout); GetConsoleMode (h, &mode); SetConsoleMode (h, mode & ~ENABLE_VIRTUAL_TERMINAL_PROCESSING); SetConsoleMode (h, mode); + release_attach_mutex (); } =20 bool @@ -729,7 +739,10 @@ fhandler_console::mouse_aware (MOUSE_EVENT_RECORD& mou= se_event) /* Adjust mouse position by window scroll buffer offset and remember adjusted position in state for use by read() */ CONSOLE_SCREEN_BUFFER_INFO now; - if (!GetConsoleScreenBufferInfo (get_output_handle (), &now)) + acquire_attach_mutex (mutex_timeout); + BOOL r =3D GetConsoleScreenBufferInfo (get_output_handle (), &now); + release_attach_mutex (); + if (!r) /* Cannot adjust position by window scroll buffer offset */ return 0; =20 @@ -818,9 +831,7 @@ wait_retry: =20 int ret; acquire_input_mutex (mutex_timeout); - acquire_attach_mutex (mutex_timeout); ret =3D process_input_message (); - release_attach_mutex (); switch (ret) { case input_error: @@ -887,7 +898,11 @@ fhandler_console::process_input_message (void) DWORD total_read, i; INPUT_RECORD input_rec[INREC_SIZE]; =20 - if (!PeekConsoleInputW (get_handle (), input_rec, INREC_SIZE, &total_rea= d)) + acquire_attach_mutex (mutex_timeout); + BOOL r =3D + PeekConsoleInputW (get_handle (), input_rec, INREC_SIZE, &total_read); + release_attach_mutex (); + if (!r) { termios_printf ("PeekConsoleInput failed, %E"); return input_error; @@ -1273,7 +1288,9 @@ out: if (discard_len) { DWORD discarded; + acquire_attach_mutex (mutex_timeout); ReadConsoleInputW (get_handle (), input_rec, discard_len, &discarded= ); + release_attach_mutex (); con.num_processed -=3D min (con.num_processed, discarded); } return stat; @@ -1282,9 +1299,11 @@ out: bool dev_console::fillin (HANDLE h) { - bool ret; + acquire_attach_mutex (mutex_timeout); + bool ret =3D GetConsoleScreenBufferInfo (h, &b); + release_attach_mutex (); =20 - if ((ret =3D GetConsoleScreenBufferInfo (h, &b))) + if (ret) { dwWinSize.Y =3D 1 + b.srWindow.Bottom - b.srWindow.Top; dwWinSize.X =3D 1 + b.srWindow.Right - b.srWindow.Left; @@ -1335,7 +1354,9 @@ dev_console::scroll_buffer (HANDLE h, int x1, int y1,= int x2, int y2, sr1.Bottom =3D sr2.Bottom; dest.X =3D xn >=3D 0 ? xn : dwWinSize.X - 1; dest.Y =3D yn >=3D 0 ? yn : b.srWindow.Bottom; + acquire_attach_mutex (mutex_timeout); ScrollConsoleScreenBufferW (h, &sr1, &sr2, dest, &fill); + release_attach_mutex (); } =20 inline void @@ -1427,6 +1448,7 @@ fhandler_console::open (int flags, mode_t) bool is_legacy =3D false; DWORD dwMode; /* Check xterm compatible mode in output */ + acquire_attach_mutex (mutex_timeout); GetConsoleMode (get_output_handle (), &dwMode); if (!SetConsoleMode (get_output_handle (), dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) @@ -1438,6 +1460,7 @@ fhandler_console::open (int flags, mode_t) dwMode | ENABLE_VIRTUAL_TERMINAL_INPUT)) is_legacy =3D true; SetConsoleMode (get_handle (), dwMode); + release_attach_mutex (); con.is_legacy =3D is_legacy; extern int sawTERM; if (con_is_legacy && !sawTERM) @@ -1549,9 +1572,7 @@ fhandler_console::ioctl (unsigned int cmd, void *arg) case TIOCGWINSZ: int st; =20 - acquire_attach_mutex (mutex_timeout); st =3D con.fillin (get_output_handle ()); - release_attach_mutex (); if (st) { /* *not* the buffer size, the actual screen size... */ @@ -1610,14 +1631,14 @@ fhandler_console::ioctl (unsigned int cmd, void *ar= g) int ret =3D 0; INPUT_RECORD inp[INREC_SIZE]; acquire_attach_mutex (mutex_timeout); - if (!PeekConsoleInputW (get_handle (), inp, INREC_SIZE, &n)) + BOOL r =3D PeekConsoleInputW (get_handle (), inp, INREC_SIZE, &n); + release_attach_mutex (); + if (!r) { set_errno (EINVAL); - release_attach_mutex (); release_output_mutex (); return -1; } - release_attach_mutex (); bool saw_eol =3D false; for (DWORD i=3D0; i=3D b.dwSize.Y) { /* So we're at the end of the buffer and scrolling the console window @@ -1850,6 +1877,7 @@ dev_console::scroll_window (HANDLE h, int x1, int y1,= int x2, int y2) /* Eventually set cursor to new end position at the top of the window. */ dwEnd.Y++; SetConsoleCursorPosition (h, dwEnd); + release_attach_mutex (); /* Fix up console buffer info. */ fillin (h); return true; @@ -1906,8 +1934,10 @@ dev_console::clear_screen (HANDLE h, int x1, int y1,= int x2, int y2) tlc.X =3D x2; tlc.Y =3D y2; } + acquire_attach_mutex (mutex_timeout); FillConsoleOutputCharacterW (h, L' ', num, tlc, &done); FillConsoleOutputAttribute (h, current_win32_attr, num, tlc, &done); + release_attach_mutex (); } =20 void __reg3 @@ -1940,7 +1970,9 @@ fhandler_console::cursor_set (bool rel_to_top, int x,= int y) =20 pos.X =3D x; pos.Y =3D y; + acquire_attach_mutex (mutex_timeout); SetConsoleCursorPosition (get_output_handle (), pos); + release_attach_mutex (); } =20 void __reg3 @@ -2012,7 +2044,10 @@ fhandler_console::write_console (PWCHAR buf, DWORD l= en, DWORD& done) while (len > 0) { DWORD nbytes =3D len > MAX_WRITE_CHARS ? MAX_WRITE_CHARS : len; - if (!WriteConsoleW (get_output_handle (), buf, nbytes, &done, 0)) + acquire_attach_mutex (mutex_timeout); + BOOL r =3D WriteConsoleW (get_output_handle (), buf, nbytes, &done, = 0); + release_attach_mutex (); + if (!r) { __seterrno (); return false; @@ -2062,7 +2097,9 @@ ReadConsoleOutputWrapper (HANDLE h, PCHAR_INFO buf, C= OORD bufsiz, if ((width =3D=3D 0) || (height =3D=3D 0)) return TRUE; =20 + acquire_attach_mutex (mutex_timeout); BOOL success =3D ReadConsoleOutputW (h, buf, bufsiz, coord, ®ion); + release_attach_mutex (); if (success) /* it worked */; else if (GetLastError () =3D=3D ERROR_NOT_ENOUGH_MEMORY && (width * heig= ht) > 1) @@ -2103,8 +2140,10 @@ dev_console::save_restore (HANDLE h, char c) =20 /* Position at top of buffer */ COORD cob =3D {}; + acquire_attach_mutex (mutex_timeout); if (!SetConsoleCursorPosition (h, cob)) debug_printf ("SetConsoleCursorInfo(%p, ...) failed during save, %E", h); + release_attach_mutex (); =20 /* Clear entire buffer */ clear_screen (h, 0, 0, now.Right, now.Bottom); @@ -2118,7 +2157,9 @@ dev_console::save_restore (HANDLE h, char c) now.Right =3D save_bufsize.X - 1; /* Restore whole buffer */ clear_screen (h, 0, 0, b.dwSize.X - 1, b.dwSize.Y - 1); + acquire_attach_mutex (mutex_timeout); BOOL res =3D WriteConsoleOutputW (h, save_buf, save_bufsize, cob, &n= ow); + release_attach_mutex (); if (!res) debug_printf ("WriteConsoleOutputW failed, %E"); =20 @@ -2129,11 +2170,13 @@ dev_console::save_restore (HANDLE h, char c) cob.Y =3D save_top; /* CGF: NOOP? Doesn't seem to position screen as expected */ /* Temporarily position at top of screen */ + acquire_attach_mutex (mutex_timeout); if (!SetConsoleCursorPosition (h, cob)) debug_printf ("SetConsoleCursorInfo(%p, cob) failed during restore, %E", = h); /* Position where we were previously */ if (!SetConsoleCursorPosition (h, save_cursor)) debug_printf ("SetConsoleCursorInfo(%p, save_cursor) failed during restor= e, %E", h); + release_attach_mutex (); /* Get back correct version of buffer information */ dwEnd.X =3D dwEnd.Y =3D 0; fillin (h); @@ -2253,8 +2296,12 @@ fhandler_console::char_command (char c) /* Just send the sequence */ wpbuf.send (get_output_handle ()); else if (last_char && last_char !=3D L'\n') - for (int i =3D 0; i < con.args[0]; i++) - WriteConsoleW (get_output_handle (), &last_char, 1, 0, 0); + { + acquire_attach_mutex (mutex_timeout); + for (int i =3D 0; i < con.args[0]; i++) + WriteConsoleW (get_output_handle (), &last_char, 1, 0, 0); + release_attach_mutex (); + } break; case 'r': /* DECSTBM */ con.scroll_region.Top =3D con.args[0] ? con.args[0] - 1 : 0; @@ -2272,9 +2319,12 @@ fhandler_console::char_command (char c) break; if (y =3D=3D con.b.srWindow.Bottom) { + acquire_attach_mutex (mutex_timeout); WriteConsoleW (get_output_handle (), L"\033[2K", 4, 0, 0); + release_attach_mutex (); break; } + acquire_attach_mutex (mutex_timeout); if (y =3D=3D con.b.srWindow.Top && srBottom =3D=3D con.b.srWindow.Bottom) { @@ -2299,6 +2349,7 @@ fhandler_console::char_command (char c) __small_swprintf (bufw, L"\033[%d;%dH", y + 1 - con.b.srWindow.Top, x + 1); WriteConsoleW (get_output_handle (), bufw, wcslen (bufw), 0, 0); + release_attach_mutex (); } else { @@ -2316,12 +2367,15 @@ fhandler_console::char_command (char c) break; if (y =3D=3D con.b.srWindow.Bottom) { + acquire_attach_mutex (mutex_timeout); WriteConsoleW (get_output_handle (), L"\033[2K", 4, 0, 0); + release_attach_mutex (); break; } __small_swprintf (bufw, L"\033[%d;%dr", y + 1 - con.b.srWindow.Top, srBottom + 1 - con.b.srWindow.Top); + acquire_attach_mutex (mutex_timeout); WriteConsoleW (get_output_handle (), bufw, wcslen (bufw), 0, 0); wpbuf.put ('S'); wpbuf.send (get_output_handle ()); @@ -2332,6 +2386,7 @@ fhandler_console::char_command (char c) __small_swprintf (bufw, L"\033[%d;%dH", y + 1 - con.b.srWindow.Top, x + 1); WriteConsoleW (get_output_handle (), bufw, wcslen (bufw), 0, 0); + release_attach_mutex (); } else { @@ -2350,6 +2405,7 @@ fhandler_console::char_command (char c) if (con.args[0] =3D=3D 3 && wincap.has_con_broken_csi3j ()) { /* Workaround for broken CSI3J in Win10 1809 */ CONSOLE_SCREEN_BUFFER_INFO sbi; + acquire_attach_mutex (mutex_timeout); GetConsoleScreenBufferInfo (get_output_handle (), &sbi); SMALL_RECT r =3D {0, sbi.srWindow.Top, (SHORT) (sbi.dwSize.X - 1), (SHORT) (sbi.dwSize.Y - 1)}; @@ -2361,6 +2417,7 @@ fhandler_console::char_command (char c) d =3D sbi.dwCursorPosition; d.Y -=3D sbi.srWindow.Top; SetConsoleCursorPosition (get_output_handle (), d); + release_attach_mutex (); } else /* Just send the sequence */ @@ -2597,6 +2654,7 @@ fhandler_console::char_command (char c) if (con.saw_space) { CONSOLE_CURSOR_INFO console_cursor_info; + acquire_attach_mutex (mutex_timeout); GetConsoleCursorInfo (get_output_handle (), &console_cursor_info); switch (con.args[0]) { @@ -2619,6 +2677,7 @@ fhandler_console::char_command (char c) &console_cursor_info); break; } + release_attach_mutex (); } break; case 'h': @@ -2640,12 +2699,14 @@ fhandler_console::char_command (char c) case 25: /* Show/Hide Cursor (DECTCEM) */ { CONSOLE_CURSOR_INFO console_cursor_info; + acquire_attach_mutex (mutex_timeout); GetConsoleCursorInfo (get_output_handle (), & console_cursor_info); if (c =3D=3D 'h') console_cursor_info.bVisible =3D TRUE; else console_cursor_info.bVisible =3D FALSE; SetConsoleCursorInfo (get_output_handle (), & console_cursor_info); + release_attach_mutex (); break; } case 47: /* Save/Restore screen */ @@ -2897,7 +2958,10 @@ check_font (HANDLE hdl) LOGFONTW lf; =20 cfi.cbSize =3D sizeof cfi; - if (!GetCurrentConsoleFontEx (hdl, 0, &cfi)) + acquire_attach_mutex (mutex_timeout); + BOOL r =3D GetCurrentConsoleFontEx (hdl, 0, &cfi); + release_attach_mutex (); + if (!r) return; /* Switched font? */ if (wcscmp (cons_facename, cfi.FaceName) =3D=3D 0) @@ -2969,7 +3033,9 @@ fhandler_console::write_replacement_char () check_font (get_output_handle ()); =20 DWORD done; + acquire_attach_mutex (mutex_timeout); WriteConsoleW (get_output_handle (), &rp_char, 1, &done, 0); + release_attach_mutex (); } =20 const unsigned char * @@ -3126,7 +3192,11 @@ do_print: if (y >=3D srBottom) { if (y >=3D con.b.srWindow.Bottom && !con.scroll_region.Top) - WriteConsoleW (get_output_handle (), L"\n", 1, &done, 0); + { + acquire_attach_mutex (mutex_timeout); + WriteConsoleW (get_output_handle (), L"\n", 1, &done, 0); + release_attach_mutex (); + } else { scroll_buffer (0, srTop + 1, -1, srBottom, 0, srTop); @@ -3160,12 +3230,16 @@ do_print: ret =3D __utf8_mbtowc (_REENT, NULL, (const char *) found + 1, end - found - 1, &ps); if (ret !=3D -1) - while (ret-- > 0) - { - WCHAR w =3D *(found + 1); - WriteConsoleW (get_output_handle (), &w, 1, &done, 0); - found++; - } + { + acquire_attach_mutex (mutex_timeout); + while (ret-- > 0) + { + WCHAR w =3D *(found + 1); + WriteConsoleW (get_output_handle (), &w, 1, &done, 0); + found++; + } + release_attach_mutex (); + } } } break; @@ -3200,7 +3274,6 @@ fhandler_console::write (const void *vsrc, size_t len) while (get_ttyp ()->output_stopped) cygwait (10); =20 - acquire_attach_mutex (mutex_timeout); push_process_state process_state (PID_TTYOU); =20 acquire_output_mutex (mutex_timeout); @@ -3284,8 +3357,10 @@ fhandler_console::write (const void *vsrc, size_t le= n) __small_swprintf (buf, L"\033[%d;1H\033[J\033[%d;%dH", srBottom - con.b.srWindow.Top + 1, y + 1 - con.b.srWindow.Top, x + 1); + acquire_attach_mutex (mutex_timeout); WriteConsoleW (get_output_handle (), buf, wcslen (buf), 0, 0); + release_attach_mutex (); } /* Substitute "CSI Ps T" */ wpbuf.put ('['); @@ -3529,7 +3604,6 @@ fhandler_console::write (const void *vsrc, size_t len) =20 syscall_printf ("%ld =3D fhandler_console::write(...)", len); =20 - release_attach_mutex (); return len; } =20 @@ -3655,7 +3729,9 @@ set_console_title (char *title) wchar_t buf[TITLESIZE + 1]; sys_mbstowcs (buf, TITLESIZE + 1, title); lock_ttys here (15000); + acquire_attach_mutex (mutex_timeout); SetConsoleTitleW (buf); + release_attach_mutex (); debug_printf ("title '%W'", buf); } =20 diff --git a/winsup/cygwin/fhandler_termios.cc b/winsup/cygwin/fhandler_ter= mios.cc index a29129486..3767c6405 100644 --- a/winsup/cygwin/fhandler_termios.cc +++ b/winsup/cygwin/fhandler_termios.cc @@ -365,6 +365,7 @@ fhandler_termios::process_sigs (char c, tty* ttyp, fhan= dler_termios *fh) else resume_pid =3D fhandler_pty_common::get_console_process_id (myself->dwProcessId, false); + acquire_attach_mutex (mutex_timeout); if ((!console_exists || resume_pid) && fh && !fh->is_console ()) { FreeConsole (); @@ -404,6 +405,7 @@ fhandler_termios::process_sigs (char c, tty* ttyp, fhan= dler_termios *fh) init_console_handler (::cygheap->ctty && ::cygheap->ctty->is_console ()); } + release_attach_mutex (); need_discard_input =3D true; } if (p && p->ctty =3D=3D ttyp->ntty && p->pgid =3D=3D pgid) diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc index 673f4167a..a2a9eab99 100644 --- a/winsup/cygwin/fhandler_tty.cc +++ b/winsup/cygwin/fhandler_tty.cc @@ -33,8 +33,6 @@ details. */ #define PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE 0x00020016 #endif /* PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE */ =20 -extern DWORD mutex_timeout; /* defined in fhandler_termios.cc */ - extern "C" int sscanf (const char *, const char *, ...); =20 #define close_maybe(h) \ @@ -979,7 +977,11 @@ fhandler_pty_slave::open (int flags, mode_t) CTRL_C_EVENTs between ptys. */ get_ttyp ()->need_invisible_console =3D true; else - fhandler_console::need_invisible (); + { + acquire_attach_mutex (mutex_timeout); + fhandler_console::need_invisible (); + release_attach_mutex (); + } =20 set_open_status (); return 1; @@ -1157,7 +1159,11 @@ fhandler_pty_slave::reset_switch_to_nat_pipe (void) bool need_restore_handles =3D get_ttyp ()->pcon_activated; WaitForSingleObject (pipe_sw_mutex, INFINITE); if (get_ttyp ()->pcon_activated) - close_pseudoconsole (get_ttyp ()); + { + acquire_attach_mutex (mutex_timeout); + close_pseudoconsole (get_ttyp ()); + release_attach_mutex (); + } else hand_over_only (get_ttyp ()); ReleaseMutex (pipe_sw_mutex); @@ -1238,6 +1244,7 @@ fhandler_pty_slave::reset_switch_to_nat_pipe (void) DuplicateHandle (pcon_owner, get_ttyp ()->h_pcon_in, GetCurrentProcess (), &h_pcon_in, 0, TRUE, DUPLICATE_SAME_ACCESS); + acquire_attach_mutex (mutex_timeout); FreeConsole (); AttachConsole (get_ttyp ()->nat_pipe_owner_pid); init_console_handler (false); @@ -1248,6 +1255,7 @@ fhandler_pty_slave::reset_switch_to_nat_pipe (void) FreeConsole (); AttachConsole (resume_pid); init_console_handler (false); + release_attach_mutex (); CloseHandle (h_pcon_in); } CloseHandle (pcon_owner); @@ -2096,7 +2104,9 @@ fhandler_pty_common::resize_pseudo_console (struct wi= nsize *ws) DuplicateHandle (pcon_owner, get_ttyp ()->h_pcon_write_pipe, GetCurrentProcess (), &hpcon_local.hWritePipe, 0, TRUE, DUPLICATE_SAME_ACCESS); + acquire_attach_mutex (mutex_timeout); ResizePseudoConsole ((HPCON) &hpcon_local, size); + release_attach_mutex (); CloseHandle (pcon_owner); CloseHandle (hpcon_local.hWritePipe); } @@ -2473,8 +2483,10 @@ fhandler_pty_slave::setup_locale (void) if (!get_ttyp ()->term_code_page) { get_ttyp ()->term_code_page =3D __eval_codepage_from_internal_charse= t (); + acquire_attach_mutex (mutex_timeout); SetConsoleCP (get_ttyp ()->term_code_page); SetConsoleOutputCP (get_ttyp ()->term_code_page); + release_attach_mutex (); } } =20 @@ -3794,9 +3806,11 @@ fhandler_pty_slave::create_invisible_console () if (get_ttyp ()->need_invisible_console) { /* Detach from console device and create new invisible console. */ + acquire_attach_mutex (mutex_timeout); FreeConsole(); fhandler_console::need_invisible (true); init_console_handler (false); + release_attach_mutex (); get_ttyp ()->need_invisible_console =3D false; get_ttyp ()->invisible_console_pid =3D myself->pid; } @@ -4054,7 +4068,11 @@ fhandler_pty_slave::setup_for_non_cygwin_app (bool n= opcon, PWCHAR envblock, } bool pcon_enabled =3D false; if (!nopcon) - pcon_enabled =3D setup_pseudoconsole (); + { + acquire_attach_mutex (mutex_timeout); + pcon_enabled =3D setup_pseudoconsole (); + release_attach_mutex (); + } ReleaseMutex (pipe_sw_mutex); /* For pcon enabled case, transfer_input() is called in master::write() = */ if (!pcon_enabled && get_ttyp ()->getpgid () =3D=3D myself->pgid @@ -4083,7 +4101,11 @@ fhandler_pty_slave::cleanup_for_non_cygwin_app (hand= le_set_t *p, tty *ttyp, } WaitForSingleObject (p->pipe_sw_mutex, INFINITE); if (ttyp->pcon_activated) - close_pseudoconsole (ttyp, force_switch_to); + { + acquire_attach_mutex (mutex_timeout); + close_pseudoconsole (ttyp, force_switch_to); + release_attach_mutex (); + } else hand_over_only (ttyp, force_switch_to); ReleaseMutex (p->pipe_sw_mutex); @@ -4157,6 +4179,8 @@ fhandler_pty_slave::release_ownership_of_nat_pipe (tt= y *ttyp, fhandler_pty_master *ptym =3D (fhandler_pty_master *) fh; WaitForSingleObject (ptym->pipe_sw_mutex, INFINITE); if (ttyp->pcon_activated) + /* Do not acquire/release_attach_mutex() here because + it has done in fhandler_termios::process_sigs(). */ close_pseudoconsole (ttyp); else hand_over_only (ttyp); diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc index 80a9527dc..64e35cf12 100644 --- a/winsup/cygwin/select.cc +++ b/winsup/cygwin/select.cc @@ -1095,8 +1095,6 @@ fhandler_fifo::select_except (select_stuff *ss) return s; } =20 -extern DWORD mutex_timeout; /* defined in fhandler_termios.cc */ - static int peek_console (select_record *me, bool) { @@ -1123,7 +1121,6 @@ peek_console (select_record *me, bool) set_handle_or_return_if_not_open (h, me); =20 fh->acquire_input_mutex (mutex_timeout); - acquire_attach_mutex (mutex_timeout); while (!fh->input_ready && !fh->get_cons_readahead_valid ()) { if (fh->bg_check (SIGTTIN, true) <=3D bg_eof) @@ -1132,8 +1129,14 @@ peek_console (select_record *me, bool) fh->release_input_mutex (); return me->read_ready =3D true; } - else if (!PeekConsoleInputW (h, &irec, 1, &events_read) || !events_r= ead) - break; + else + { + acquire_attach_mutex (mutex_timeout); + BOOL r =3D PeekConsoleInputW (h, &irec, 1, &events_read); + release_attach_mutex (); + if (!r || !events_read) + break; + } if (fhandler_console::input_winch =3D=3D fh->process_input_message () && global_sigs[SIGWINCH].sa_handler !=3D SIG_IGN && global_sigs[SIGWINCH].sa_handler !=3D SIG_DFL) @@ -1144,7 +1147,6 @@ peek_console (select_record *me, bool) return -1; } } - release_attach_mutex (); fh->release_input_mutex (); if (fh->input_ready || fh->get_cons_readahead_valid ()) return me->read_ready =3D true;