void
register_temporary_file (const char *absolute_file_name)
{
- IF_MT_DECL;
+ bool mt = gl_multithreaded ();
- IF_MT gl_lock_lock (file_cleanup_list_lock);
+ if (mt) gl_lock_lock (file_cleanup_list_lock);
/* Make sure that this facility and the file_cleanup_list are initialized. */
if (file_cleanup_list == NULL)
if (gl_list_search (file_cleanup_list, absolute_file_name) == NULL)
gl_list_add_first (file_cleanup_list, xstrdup (absolute_file_name));
- IF_MT gl_lock_unlock (file_cleanup_list_lock);
+ if (mt) gl_lock_unlock (file_cleanup_list_lock);
}
/* Unregister the given ABSOLUTE_FILE_NAME as being a file that needs to be
void
unregister_temporary_file (const char *absolute_file_name)
{
- IF_MT_DECL;
+ bool mt = gl_multithreaded ();
- IF_MT gl_lock_lock (file_cleanup_list_lock);
+ if (mt) gl_lock_lock (file_cleanup_list_lock);
gl_list_t list = file_cleanup_list;
if (list != NULL)
}
}
- IF_MT gl_lock_unlock (file_cleanup_list_lock);
+ if (mt) gl_lock_unlock (file_cleanup_list_lock);
}
/* Remove a file, with optional error message.
create_temp_dir (const char *prefix, const char *parentdir,
bool cleanup_verbose)
{
- IF_MT_DECL;
+ bool mt = gl_multithreaded ();
- IF_MT gl_lock_lock (dir_cleanup_list_lock);
+ if (mt) gl_lock_lock (dir_cleanup_list_lock);
struct tempdir * volatile *tmpdirp = NULL;
struct tempdir *tmpdir;
block because then the cleanup handler would not remove the directory
if xstrdup fails. */
tmpdir->dirname = xstrdup (tmpdirname);
- IF_MT gl_lock_unlock (dir_cleanup_list_lock);
+ if (mt) gl_lock_unlock (dir_cleanup_list_lock);
freea (xtemplate);
return (struct temp_dir *) tmpdir;
quit:
- IF_MT gl_lock_unlock (dir_cleanup_list_lock);
+ if (mt) gl_lock_unlock (dir_cleanup_list_lock);
freea (xtemplate);
return NULL;
}
const char *absolute_file_name)
{
struct tempdir *tmpdir = (struct tempdir *)dir;
- IF_MT_DECL;
+ bool mt = gl_multithreaded ();
- IF_MT gl_lock_lock (dir_cleanup_list_lock);
+ if (mt) gl_lock_lock (dir_cleanup_list_lock);
/* Add absolute_file_name to tmpdir->files, without duplicates. */
if (gl_list_search (tmpdir->files, absolute_file_name) == NULL)
gl_list_add_first (tmpdir->files, xstrdup (absolute_file_name));
- IF_MT gl_lock_unlock (dir_cleanup_list_lock);
+ if (mt) gl_lock_unlock (dir_cleanup_list_lock);
}
/* Unregister the given ABSOLUTE_FILE_NAME as being a file inside DIR, that
const char *absolute_file_name)
{
struct tempdir *tmpdir = (struct tempdir *)dir;
- IF_MT_DECL;
+ bool mt = gl_multithreaded ();
- IF_MT gl_lock_lock (dir_cleanup_list_lock);
+ if (mt) gl_lock_lock (dir_cleanup_list_lock);
gl_list_t list = tmpdir->files;
gl_list_node_t node;
free (old_string);
}
- IF_MT gl_lock_unlock (dir_cleanup_list_lock);
+ if (mt) gl_lock_unlock (dir_cleanup_list_lock);
}
/* Register the given ABSOLUTE_DIR_NAME as being a subdirectory inside DIR,
const char *absolute_dir_name)
{
struct tempdir *tmpdir = (struct tempdir *)dir;
- IF_MT_DECL;
+ bool mt = gl_multithreaded ();
- IF_MT gl_lock_lock (dir_cleanup_list_lock);
+ if (mt) gl_lock_lock (dir_cleanup_list_lock);
/* Add absolute_dir_name to tmpdir->subdirs, without duplicates. */
if (gl_list_search (tmpdir->subdirs, absolute_dir_name) == NULL)
gl_list_add_first (tmpdir->subdirs, xstrdup (absolute_dir_name));
- IF_MT gl_lock_unlock (dir_cleanup_list_lock);
+ if (mt) gl_lock_unlock (dir_cleanup_list_lock);
}
/* Unregister the given ABSOLUTE_DIR_NAME as being a subdirectory inside DIR,
const char *absolute_dir_name)
{
struct tempdir *tmpdir = (struct tempdir *)dir;
- IF_MT_DECL;
+ bool mt = gl_multithreaded ();
- IF_MT gl_lock_lock (dir_cleanup_list_lock);
+ if (mt) gl_lock_lock (dir_cleanup_list_lock);
gl_list_t list = tmpdir->subdirs;
gl_list_node_t node;
free (old_string);
}
- IF_MT gl_lock_unlock (dir_cleanup_list_lock);
+ if (mt) gl_lock_unlock (dir_cleanup_list_lock);
}
/* Remove a directory, with optional error message.
int
cleanup_temp_dir (struct temp_dir *dir)
{
- IF_MT_DECL;
+ bool mt = gl_multithreaded ();
- IF_MT gl_lock_lock (dir_cleanup_list_lock);
+ if (mt) gl_lock_lock (dir_cleanup_list_lock);
struct tempdir *tmpdir = (struct tempdir *)dir;
int err = 0;
gl_list_free (tmpdir->subdirs);
free (tmpdir->dirname);
free (tmpdir);
- IF_MT gl_lock_unlock (dir_cleanup_list_lock);
+ if (mt) gl_lock_unlock (dir_cleanup_list_lock);
return err;
}
static void
register_fd (int fd)
{
- IF_MT_DECL;
+ bool mt = gl_multithreaded ();
- IF_MT gl_lock_lock (descriptors_lock);
+ if (mt) gl_lock_lock (descriptors_lock);
if (descriptors == NULL)
descriptors = gl_list_create_empty (GL_LINKED_LIST, NULL, NULL, NULL,
gl_list_add_first (descriptors, element);
- IF_MT gl_lock_unlock (descriptors_lock);
+ if (mt) gl_lock_unlock (descriptors_lock);
}
/* Open a temporary file in a temporary directory.
int result = 0;
int saved_errno = 0;
- IF_MT_DECL;
+ bool mt = gl_multithreaded ();
- IF_MT gl_lock_lock (descriptors_lock);
+ if (mt) gl_lock_lock (descriptors_lock);
gl_list_t list = descriptors;
if (list == NULL)
/* descriptors should already contain fd. */
abort ();
- IF_MT gl_lock_unlock (descriptors_lock);
+ if (mt) gl_lock_unlock (descriptors_lock);
errno = saved_errno;
return result;
int result = 0;
int saved_errno = 0;
- IF_MT_DECL;
+ bool mt = gl_multithreaded ();
- IF_MT gl_lock_lock (descriptors_lock);
+ if (mt) gl_lock_lock (descriptors_lock);
gl_list_t list = descriptors;
if (list == NULL)
/* descriptors should have contained fd. */
abort ();
- IF_MT gl_lock_unlock (descriptors_lock);
+ if (mt) gl_lock_unlock (descriptors_lock);
errno = saved_errno;
return result;
void
at_fatal_signal (action_t action)
{
- IF_MT_DECL;
+ bool mt = gl_multithreaded ();
- IF_MT gl_lock_lock (at_fatal_signal_lock);
+ if (mt) gl_lock_lock (at_fatal_signal_lock);
static bool cleanup_initialized = false;
if (!cleanup_initialized)
actions[actions_count].action = action;
actions_count++;
- IF_MT gl_lock_unlock (at_fatal_signal_lock);
+ if (mt) gl_lock_unlock (at_fatal_signal_lock);
}
void
block_fatal_signals (void)
{
- IF_MT_DECL;
+ bool mt = gl_multithreaded ();
- IF_MT gl_lock_lock (fatal_signals_block_lock);
+ if (mt) gl_lock_lock (fatal_signals_block_lock);
if (fatal_signals_block_counter++ == 0)
{
sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
}
- IF_MT gl_lock_unlock (fatal_signals_block_lock);
+ if (mt) gl_lock_unlock (fatal_signals_block_lock);
}
/* Stop delaying the catchable fatal signals. */
void
unblock_fatal_signals (void)
{
- IF_MT_DECL;
+ bool mt = gl_multithreaded ();
- IF_MT gl_lock_lock (fatal_signals_block_lock);
+ if (mt) gl_lock_lock (fatal_signals_block_lock);
if (fatal_signals_block_counter == 0)
/* There are more calls to unblock_fatal_signals() than to
sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
}
- IF_MT gl_lock_unlock (fatal_signals_block_lock);
+ if (mt) gl_lock_unlock (fatal_signals_block_lock);
}
/* Typical use: In a block or function, use
- IF_MT_DECL;
+ bool mt = gl_multithreaded ();
...
- IF_MT
+ if (mt)
if (pthread_mutex_lock (&lock)) abort ();
...
- IF_MT
+ if (mt)
if (pthread_mutex_unlock (&lock)) abort ();
- The macro IF_MT_DECL establishes local variables for use by IF_MT.
+ The gl_multithreaded () invocation determines whether the program currently
+ is multithreaded.
- IF_MT STATEMENT executes STATEMENT in the multithreaded cases, and skips it
- in the single-threaded case.
+ if (mt) STATEMENT executes STATEMENT in the multithreaded case, and skips
+ it in the single-threaded case.
- The code between IF_MT_DECL and IF_MT must not create threads or invoke
- functions that may indirectly create threads (e.g. 'dlopen' may, indirectly
- through C++ initializers of global variables in the shared library being
- opened, create threads).
+ The code between the gl_multithreaded () invocation and any use of the
+ variable 'mt' must not create threads or invoke functions that may
+ indirectly create threads (e.g. 'dlopen' may, indirectly through C++
+ initializers of global variables in the shared library being opened,
+ create threads).
The lock here is meant to synchronize threads in the same process. The
same optimization cannot be applied to locks that synchronize different
#if HAVE_SYS_SINGLE_THREADED_H /* glibc >= 2.32 */
# include <sys/single_threaded.h>
-# define IF_MT_DECL char optimize_for_single_thread = __libc_single_threaded
-# define IF_MT if (!optimize_for_single_thread)
+# define gl_multithreaded() !__libc_single_threaded
#else
-# define IF_MT_DECL (void)0
-# define IF_MT
+# define gl_multithreaded() 1
#endif
#endif /* _THREAD_OPTIM_H */