+
+/*
+ * wgetch() and other functions with a WINDOW* parameter may use a SCREEN*
+ * internally, and it is useful to allow those to be invoked without switching
+ * SCREEN's, e.g., for multi-threaded applications.
+ */
+NCURSES_EXPORT(SCREEN *)
+_nc_screen_of(WINDOW *win)
+{
+ SCREEN *sp = 0;
+
+ if (win != 0) {
+ sp = WINDOW_EXT(win, screen);
+ }
+ return (sp);
+}
+
+/******************************************************************************/
+#ifdef USE_PTHREADS
+static void
+init_global_mutexes(void)
+{
+ static bool initialized = FALSE;
+
+ if (!initialized) {
+ initialized = TRUE;
+ _nc_mutex_init(&_nc_globals.mutex_curses);
+ _nc_mutex_init(&_nc_globals.mutex_tst_tracef);
+ _nc_mutex_init(&_nc_globals.mutex_tracef);
+ }
+}
+
+NCURSES_EXPORT(void)
+_nc_init_pthreads(void)
+{
+ if (_nc_use_pthreads)
+ return;
+# if USE_WEAK_SYMBOLS
+ if ((pthread_mutex_init) == 0)
+ return;
+ if ((pthread_mutex_lock) == 0)
+ return;
+ if ((pthread_mutex_unlock) == 0)
+ return;
+ if ((pthread_mutex_trylock) == 0)
+ return;
+ if ((pthread_mutexattr_settype) == 0)
+ return;
+# endif
+ _nc_use_pthreads = 1;
+ init_global_mutexes();
+}
+
+/*
+ * Use recursive mutexes if we have them - they're part of Unix98.
+ * For the cases where we do not, _nc_mutex_trylock() is used to avoid a
+ * deadlock, at the expense of memory leaks and unexpected failures that
+ * may not be handled by typical clients.
+ *
+ * FIXME - need configure check for PTHREAD_MUTEX_RECURSIVE, define it to
+ * PTHREAD_MUTEX_NORMAL if not supported.
+ */
+NCURSES_EXPORT(void)
+_nc_mutex_init(pthread_mutex_t * obj)
+{
+ pthread_mutexattr_t recattr;
+
+ if (_nc_use_pthreads) {
+ pthread_mutexattr_init(&recattr);
+ pthread_mutexattr_settype(&recattr, PTHREAD_MUTEX_RECURSIVE);
+ pthread_mutex_init(obj, &recattr);
+ }
+}
+
+NCURSES_EXPORT(int)
+_nc_mutex_lock(pthread_mutex_t * obj)
+{
+ if (_nc_use_pthreads == 0)
+ return 0;
+ return pthread_mutex_lock(obj);
+}
+
+NCURSES_EXPORT(int)
+_nc_mutex_trylock(pthread_mutex_t * obj)
+{
+ if (_nc_use_pthreads == 0)
+ return 0;
+ return pthread_mutex_trylock(obj);
+}
+
+NCURSES_EXPORT(int)
+_nc_mutex_unlock(pthread_mutex_t * obj)
+{
+ if (_nc_use_pthreads == 0)
+ return 0;
+ return pthread_mutex_unlock(obj);
+}
+#endif /* USE_PTHREADS */
+
+#if defined(USE_PTHREADS) || USE_PTHREADS_EINTR
+#if USE_WEAK_SYMBOLS
+/*
+ * NB: sigprocmask(2) is global but pthread_sigmask(3p)
+ * only for the calling thread.
+ */
+NCURSES_EXPORT(int)
+_nc_sigprocmask(int how, const sigset_t * newmask, sigset_t * oldmask)
+{
+ if ((pthread_sigmask))
+ return pthread_sigmask(how, newmask, oldmask);
+ else
+ return (sigprocmask)(how, newmask, oldmask);
+}
+#endif
+#endif /* USE_PTHREADS */