/*
INODE
-Initialization, Miscellaneous, Error reporting, BFD front end
+Initialization, Threading, Error reporting, BFD front end
FUNCTION
bfd_init
return BFD_INIT_MAGIC;
}
\f
+
+/*
+INODE
+Threading, Miscellaneous, Initialization, BFD front end
+
+SECTION
+ Threading
+
+ BFD has limited support for thread-safety. Most BFD globals
+ are protected by locks, while the error-related globals are
+ thread-local. A given BFD cannot safely be used from two
+ threads at the same time; it is up to the application to do
+ any needed locking. However, it is ok for different threads
+ to work on different BFD objects at the same time.
+
+SUBSECTION
+ Thread functions.
+
+CODE_FRAGMENT
+.typedef bool (*bfd_lock_unlock_fn_type) (void *);
+*/
+
+/* The lock and unlock functions, if set. */
+static bfd_lock_unlock_fn_type lock_fn;
+static bfd_lock_unlock_fn_type unlock_fn;
+static void *lock_data;
+
+/*
+FUNCTION
+ bfd_thread_init
+
+SYNOPSIS
+ bool bfd_thread_init
+ (bfd_lock_unlock_fn_type lock,
+ bfd_lock_unlock_fn_type unlock,
+ void *data);
+
+DESCRIPTION
+
+ Initialize BFD threading. The functions passed in will be
+ used to lock and unlock global data structures. This may only
+ be called a single time in a given process. Returns true on
+ success and false on error. DATA is passed verbatim to the
+ lock and unlock functions. The lock and unlock functions
+ should return true on success, or set the BFD error and return
+ false on failure.
+*/
+
+bool
+bfd_thread_init (bfd_lock_unlock_fn_type lock, bfd_lock_unlock_fn_type unlock,
+ void *data)
+{
+ /* Both functions must be set, and this cannot have been called
+ before. */
+ if (lock == NULL || unlock == NULL || unlock_fn != NULL)
+ {
+ bfd_set_error (bfd_error_invalid_operation);
+ return false;
+ }
+
+ lock_fn = lock;
+ unlock_fn = unlock;
+ lock_data = data;
+ return true;
+}
+
+/*
+FUNCTION
+ bfd_thread_cleanup
+
+SYNOPSIS
+ void bfd_thread_cleanup (void);
+
+DESCRIPTION
+ Clean up any thread-local state. This should be called by a
+ thread that uses any BFD functions, before the thread exits.
+ It is fine to call this multiple times, or to call it and then
+ later call BFD functions on the same thread again.
+*/
+
+void
+bfd_thread_cleanup (void)
+{
+ _bfd_clear_error_data ();
+}
+
+/*
+INTERNAL_FUNCTION
+ bfd_lock
+
+SYNOPSIS
+ bool bfd_lock (void);
+
+DESCRIPTION
+ Acquire the global BFD lock, if needed. Returns true on
+ success, false on error.
+*/
+
+bool
+bfd_lock (void)
+{
+ if (lock_fn != NULL)
+ return lock_fn (lock_data);
+ return true;
+}
+
+/*
+INTERNAL_FUNCTION
+ bfd_unlock
+
+SYNOPSIS
+ bool bfd_unlock (void);
+
+DESCRIPTION
+ Release the global BFD lock, if needed. Returns true on
+ success, false on error.
+*/
+
+bool
+bfd_unlock (void)
+{
+ if (unlock_fn != NULL)
+ return unlock_fn (lock_data);
+ return true;
+}
+
+
/*
INODE
-Miscellaneous, Memory Usage, Initialization, BFD front end
+Miscellaneous, Memory Usage, Threading, BFD front end
SECTION
Miscellaneous
#include <sys/mman.h>
#endif
+static FILE *_bfd_open_file_unlocked (bfd *abfd);
+
/* In some cases we can optimize cache operation when reopening files.
For instance, a flush is entirely unnecessary if the file is already
closed, so a flush would use CACHE_NO_OPEN. Similarly, a seek using
if (flag & CACHE_NO_OPEN)
return NULL;
- if (bfd_open_file (abfd) == NULL)
+ if (_bfd_open_file_unlocked (abfd) == NULL)
;
else if (!(flag & CACHE_NO_SEEK)
&& _bfd_real_fseek ((FILE *) abfd->iostream,
static file_ptr
cache_btell (struct bfd *abfd)
{
+ if (!bfd_lock ())
+ return -1;
FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
if (f == NULL)
- return abfd->where;
- return _bfd_real_ftell (f);
+ {
+ if (!bfd_unlock ())
+ return -1;
+ return abfd->where;
+ }
+ file_ptr result = _bfd_real_ftell (f);
+ if (!bfd_unlock ())
+ return -1;
+ return result;
}
static int
cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
{
+ if (!bfd_lock ())
+ return -1;
FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : CACHE_NORMAL);
if (f == NULL)
+ {
+ bfd_unlock ();
+ return -1;
+ }
+ int result = _bfd_real_fseek (f, offset, whence);
+ if (!bfd_unlock ())
return -1;
- return _bfd_real_fseek (f, offset, whence);
+ return result;
}
/* Note that archive entries don't have streams; they share their parent's.
static file_ptr
cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
{
+ if (!bfd_lock ())
+ return -1;
file_ptr nread = 0;
FILE *f;
f = bfd_cache_lookup (abfd, CACHE_NORMAL);
if (f == NULL)
- return -1;
+ {
+ bfd_unlock ();
+ return -1;
+ }
/* Some filesystems are unable to handle reads that are too large
(for instance, NetApp shares with oplocks turned off). To avoid
break;
}
+ if (!bfd_unlock ())
+ return -1;
return nread;
}
static file_ptr
cache_bwrite (struct bfd *abfd, const void *from, file_ptr nbytes)
{
+ if (!bfd_lock ())
+ return -1;
file_ptr nwrite;
FILE *f = bfd_cache_lookup (abfd, CACHE_NORMAL);
if (f == NULL)
- return 0;
+ {
+ if (!bfd_unlock ())
+ return -1;
+ return 0;
+ }
nwrite = fwrite (from, 1, nbytes, f);
if (nwrite < nbytes && ferror (f))
{
bfd_set_error (bfd_error_system_call);
+ bfd_unlock ();
return -1;
}
+ if (!bfd_unlock ())
+ return -1;
return nwrite;
}
static int
cache_bclose (struct bfd *abfd)
{
+ /* No locking needed here, it's handled by the callee. */
return bfd_cache_close (abfd) - 1;
}
static int
cache_bflush (struct bfd *abfd)
{
+ if (!bfd_lock ())
+ return -1;
int sts;
FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
if (f == NULL)
- return 0;
+ {
+ if (!bfd_unlock ())
+ return -1;
+ return 0;
+ }
sts = fflush (f);
if (sts < 0)
bfd_set_error (bfd_error_system_call);
+ if (!bfd_unlock ())
+ return -1;
return sts;
}
static int
cache_bstat (struct bfd *abfd, struct stat *sb)
{
+ if (!bfd_lock ())
+ return -1;
int sts;
FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
if (f == NULL)
- return -1;
+ {
+ bfd_unlock ();
+ return -1;
+ }
sts = fstat (fileno (f), sb);
if (sts < 0)
bfd_set_error (bfd_error_system_call);
+ if (!bfd_unlock ())
+ return -1;
return sts;
}
{
void *ret = (void *) -1;
+ if (!bfd_lock ())
+ return ret;
if ((abfd->flags & BFD_IN_MEMORY) != 0)
abort ();
#ifdef HAVE_MMAP
f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
if (f == NULL)
- return ret;
+ {
+ bfd_unlock ();
+ return ret;
+ }
if (pagesize_m1 == 0)
pagesize_m1 = getpagesize () - 1;
}
#endif
+ if (!bfd_unlock ())
+ return (void *) -1;
return ret;
}
&cache_bclose, &cache_bflush, &cache_bstat, &cache_bmmap
};
+static bool
+_bfd_cache_init_unlocked (bfd *abfd)
+{
+ BFD_ASSERT (abfd->iostream != NULL);
+ if (open_files >= bfd_cache_max_open ())
+ {
+ if (! close_one ())
+ return false;
+ }
+ abfd->iovec = &cache_iovec;
+ insert (abfd);
+ abfd->flags &= ~BFD_CLOSED_BY_CACHE;
+ ++open_files;
+ return true;
+}
+
/*
INTERNAL_FUNCTION
bfd_cache_init
bool
bfd_cache_init (bfd *abfd)
{
- BFD_ASSERT (abfd->iostream != NULL);
- if (open_files >= bfd_cache_max_open ())
- {
- if (! close_one ())
- return false;
- }
- abfd->iovec = &cache_iovec;
- insert (abfd);
- abfd->flags &= ~BFD_CLOSED_BY_CACHE;
- ++open_files;
- return true;
+ if (!bfd_lock ())
+ return false;
+ bool result = _bfd_cache_init_unlocked (abfd);
+ if (!bfd_unlock ())
+ return false;
+ return result;
+}
+
+static bool
+_bfd_cache_close_unlocked (bfd *abfd)
+{
+ /* Don't remove this test. bfd_reinit depends on it. */
+ if (abfd->iovec != &cache_iovec)
+ return true;
+
+ if (abfd->iostream == NULL)
+ /* Previously closed. */
+ return true;
+
+ /* Note: no locking needed in this function, as it is handled by
+ bfd_cache_delete. */
+ return bfd_cache_delete (abfd);
}
/*
bool
bfd_cache_close (bfd *abfd)
{
- /* Don't remove this test. bfd_reinit depends on it. */
- if (abfd->iovec != &cache_iovec)
- return true;
-
- if (abfd->iostream == NULL)
- /* Previously closed. */
- return true;
-
- return bfd_cache_delete (abfd);
+ if (!bfd_lock ())
+ return false;
+ bool result = _bfd_cache_close_unlocked (abfd);
+ if (!bfd_unlock ())
+ return false;
+ return result;
}
/*
{
bool ret = true;
+ if (!bfd_lock ())
+ return false;
while (bfd_last_cache != NULL)
{
bfd *prev_bfd_last_cache = bfd_last_cache;
- ret &= bfd_cache_close (bfd_last_cache);
+ ret &= _bfd_cache_close_unlocked (bfd_last_cache);
/* Stop a potential infinite loop should bfd_cache_close()
not update bfd_last_cache. */
break;
}
+ if (!bfd_unlock ())
+ return false;
return ret;
}
return open_files;
}
-/*
-INTERNAL_FUNCTION
- bfd_open_file
-
-SYNOPSIS
- FILE* bfd_open_file (bfd *abfd);
-
-DESCRIPTION
- Call the OS to open a file for @var{abfd}. Return the <<FILE *>>
- (possibly <<NULL>>) that results from this operation. Set up the
- BFD so that future accesses know the file is open. If the <<FILE *>>
- returned is <<NULL>>, then it won't have been put in the
- cache, so it won't have to be removed from it.
-*/
-
-FILE *
-bfd_open_file (bfd *abfd)
+static FILE *
+_bfd_open_file_unlocked (bfd *abfd)
{
abfd->cacheable = true; /* Allow it to be closed later. */
bfd_set_error (bfd_error_system_call);
else
{
- if (! bfd_cache_init (abfd))
+ if (! _bfd_cache_init_unlocked (abfd))
return NULL;
}
return (FILE *) abfd->iostream;
}
+
+/*
+INTERNAL_FUNCTION
+ bfd_open_file
+
+SYNOPSIS
+ FILE* bfd_open_file (bfd *abfd);
+
+DESCRIPTION
+ Call the OS to open a file for @var{abfd}. Return the <<FILE *>>
+ (possibly <<NULL>>) that results from this operation. Set up the
+ BFD so that future accesses know the file is open. If the <<FILE *>>
+ returned is <<NULL>>, then it won't have been put in the
+ cache, so it won't have to be removed from it.
+*/
+
+FILE *
+bfd_open_file (bfd *abfd)
+{
+ if (!bfd_lock ())
+ return NULL;
+ FILE *result = _bfd_open_file_unlocked (abfd);
+ if (!bfd_unlock ())
+ return NULL;
+ return result;
+}