bool NO_COPY fhandler_console::invisible_console;
-/* Mutex for AttachConsole()/FreeConsole() in fhandler_tty.cc */
-HANDLE attach_mutex;
-
extern DWORD mutex_timeout; /* defined in fhandler_termios.cc */
-static inline void
-acquire_attach_mutex (DWORD t)
-{
- if (attach_mutex)
- WaitForSingleObject (attach_mutex, t);
-}
-
-static inline void
-release_attach_mutex ()
-{
- if (attach_mutex)
- ReleaseMutex (attach_mutex);
-}
-
/* 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;
DWORD error;
};
-extern HANDLE attach_mutex; /* Defined in fhandler_console.cc */
-static LONG master_cnt = 0;
+HANDLE attach_mutex;
+
+DWORD acquire_attach_mutex (DWORD t)
+{
+ if (!attach_mutex)
+ return WAIT_OBJECT_0;
+ return WaitForSingleObject (attach_mutex, t);
+}
+
+void release_attach_mutex (void)
+{
+ if (!attach_mutex)
+ return;
+ ReleaseMutex (attach_mutex);
+}
inline static bool pcon_pid_alive (DWORD pid);
{
/* Slave attaches to a different console than master.
Therefore reattach here. */
- WaitForSingleObject (attach_mutex, mutex_timeout);
+ acquire_attach_mutex (mutex_timeout);
FreeConsole ();
AttachConsole (target_pid);
cp_to = GetConsoleCP ();
FreeConsole ();
AttachConsole (resume_pid);
- ReleaseMutex (attach_mutex);
+ release_attach_mutex ();
}
else
cp_to = GetConsoleCP ();
master_fwd_thread->detach ();
}
}
- if (InterlockedDecrement (&master_cnt) == 0)
- CloseHandle (attach_mutex);
/* Check if the last master handle has been closed. If so, set
input_available_event to wake up potentially waiting slaves. */
{
/* Slave attaches to a different console than master.
Therefore reattach here. */
- WaitForSingleObject (attach_mutex, mutex_timeout);
+ acquire_attach_mutex (mutex_timeout);
FreeConsole ();
AttachConsole (target_pid);
cp_from = GetConsoleOutputCP ();
FreeConsole ();
AttachConsole (resume_pid);
- ReleaseMutex (attach_mutex);
+ release_attach_mutex ();
}
else
cp_from = GetConsoleOutputCP ();
if (!(pcon_mutex = CreateMutex (&sa, FALSE, buf)))
goto err;
- if (InterlockedIncrement (&master_cnt) == 1)
+ if (!attach_mutex)
attach_mutex = CreateMutex (&sa, FALSE, NULL);
/* Create master control pipe which allows the master to duplicate
close_maybe (input_available_event);
close_maybe (output_mutex);
close_maybe (input_mutex);
- close_maybe (attach_mutex);
close_maybe (from_master_nat);
close_maybe (from_master);
close_maybe (to_master_nat);