aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCorinna Vinschen <corinna@vinschen.de>2007-11-29 11:14:52 +0000
committerCorinna Vinschen <corinna@vinschen.de>2007-11-29 11:14:52 +0000
commit89ee49de1bb814962a22332d8ab0eb67de651c42 (patch)
tree9d9e9c2e57abba99a016c7a82bbd72e152be020c
parent993ba7b4880b80af97846040487ece6b65f4b791 (diff)
downloadnewlib-89ee49de1bb814962a22332d8ab0eb67de651c42.zip
newlib-89ee49de1bb814962a22332d8ab0eb67de651c42.tar.gz
newlib-89ee49de1bb814962a22332d8ab0eb67de651c42.tar.bz2
Drop old SetResourceLock stuff in favor of mutos.
* dcrt0.cc (_reslock): Remove. (__cygwin_user_data): Accommodate removal of resourcelocks member. (dll_crt0_0): Don't initialize resourcelocks. * exceptions.cc (_cygtls::signal_exit): Drop resourcelocks handling. * mmap.cc (mmap_guard): New muto. (LIST_LOCK): Define. (LIST_UNLOCK): Define. (mmap_list::search_record): Remove. (mmap_list::try_map): Include code for anonymous case from mmap_list::search_record. (mmap_is_attached_or_noreserve): Access bookkeeping lists in a thread safe way. (mmap64): Replace SetResourceLock/ReleaseResourceLock by LIST_LOCK/LIST_UNLOCK. Lock at the latest possible point. (munmap): Replace SetResourceLock/ReleaseResourceLock by LIST_LOCK/LIST_UNLOCK. (msync): Ditto. (mprotect): Ditto. * thread.cc (ResourceLocks::Lock): Remove. (SetResourceLock): Remove. (ReleaseResourceLock): Remove. (ResourceLocks::Init): Remove. (ResourceLocks::Delete): Remove. * thread.h (SetResourceLock): Drop declaration. (ReleaseResourceLock): Ditto. (class ResourceLocks): Drop definition. * include/sys/cygwin.h (class ResourceLocks): Drop forward declaration. (struct per_process): Replace resourcelocks with additional unused2 element. (per_process_overwrite): Accommodate above change. * mmap.cc: Convert usage of dynamically growing cmalloced arrays to cmalloced linked lists throughout. (class mmap_record): Add LIST_ENTRY element. (mmap_record::match): New method, taking over match algorithm from list::search_record. (class mmap_list): Rename from class list. Add LIST_ENTRY. Convert recs to a LIST_HEAD. Drop nrecs and maxrecs members. (mmap_list::get_record): Drop entirely. (mmap_list::free_recs): Drop entirely. (mmap_list::del_record): Take mmap_record to delete as parameter. (mmap_list::search_record): Convert to mmap_record::match. (class mmap_areas): Rename from class map. Convert lists to LIST_HEAD. (mmap_areas::get_list): Drop entirely. (mmap_areas::del_list): Take mmap_list to delete as parameter. (mprotect): Fix indentation.
-rw-r--r--winsup/cygwin/ChangeLog52
-rw-r--r--winsup/cygwin/dcrt0.cc6
-rw-r--r--winsup/cygwin/exceptions.cc3
-rw-r--r--winsup/cygwin/include/sys/cygwin.h7
-rw-r--r--winsup/cygwin/mmap.cc417
-rw-r--r--winsup/cygwin/thread.cc37
-rw-r--r--winsup/cygwin/thread.h18
7 files changed, 240 insertions, 300 deletions
diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog
index 338b594..701433f 100644
--- a/winsup/cygwin/ChangeLog
+++ b/winsup/cygwin/ChangeLog
@@ -1,3 +1,55 @@
+2007-11-27 Corinna Vinschen <corinna@vinschen.de>
+
+ Drop old SetResourceLock stuff in favor of mutos.
+ * dcrt0.cc (_reslock): Remove.
+ (__cygwin_user_data): Accommodate removal of resourcelocks member.
+ (dll_crt0_0): Don't initialize resourcelocks.
+ * exceptions.cc (_cygtls::signal_exit): Drop resourcelocks handling.
+ * mmap.cc (mmap_guard): New muto.
+ (LIST_LOCK): Define.
+ (LIST_UNLOCK): Define.
+ (mmap_list::search_record): Remove.
+ (mmap_list::try_map): Include code for anonymous case from
+ mmap_list::search_record.
+ (mmap_is_attached_or_noreserve): Access bookkeeping lists in a thread
+ safe way.
+ (mmap64): Replace SetResourceLock/ReleaseResourceLock by
+ LIST_LOCK/LIST_UNLOCK. Lock at the latest possible point.
+ (munmap): Replace SetResourceLock/ReleaseResourceLock by
+ LIST_LOCK/LIST_UNLOCK.
+ (msync): Ditto.
+ (mprotect): Ditto.
+ * thread.cc (ResourceLocks::Lock): Remove.
+ (SetResourceLock): Remove.
+ (ReleaseResourceLock): Remove.
+ (ResourceLocks::Init): Remove.
+ (ResourceLocks::Delete): Remove.
+ * thread.h (SetResourceLock): Drop declaration.
+ (ReleaseResourceLock): Ditto.
+ (class ResourceLocks): Drop definition.
+ * include/sys/cygwin.h (class ResourceLocks): Drop forward declaration.
+ (struct per_process): Replace resourcelocks with additional unused2
+ element.
+ (per_process_overwrite): Accommodate above change.
+
+2007-11-27 Corinna Vinschen <corinna@vinschen.de>
+
+ * mmap.cc: Convert usage of dynamically growing cmalloced arrays to
+ cmalloced linked lists throughout.
+ (class mmap_record): Add LIST_ENTRY element.
+ (mmap_record::match): New method, taking over match algorithm from
+ list::search_record.
+ (class mmap_list): Rename from class list. Add LIST_ENTRY. Convert
+ recs to a LIST_HEAD. Drop nrecs and maxrecs members.
+ (mmap_list::get_record): Drop entirely.
+ (mmap_list::free_recs): Drop entirely.
+ (mmap_list::del_record): Take mmap_record to delete as parameter.
+ (mmap_list::search_record): Convert to mmap_record::match.
+ (class mmap_areas): Rename from class map. Convert lists to LIST_HEAD.
+ (mmap_areas::get_list): Drop entirely.
+ (mmap_areas::del_list): Take mmap_list to delete as parameter.
+ (mprotect): Fix indentation.
+
2007-11-23 Christopher Faylor <me+cygwin@cgf.cx>
* cygheap.cc (_crealloc): Avoid memcpy when _cmalloc returns NULL.
diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc
index 727163e..bd8b0e6 100644
--- a/winsup/cygwin/dcrt0.cc
+++ b/winsup/cygwin/dcrt0.cc
@@ -81,7 +81,6 @@ bool NO_COPY cygwin_finished_initializing;
measure to allow an orderly transfer to the new, correct sigmask method. */
unsigned NO_COPY int signal_shift_subtract = 1;
-ResourceLocks _reslock NO_COPY;
MTinterface _mtinterf;
bool NO_COPY _cygwin_testing;
@@ -114,8 +113,8 @@ extern "C"
/* hmodule */ NULL,
/* api_major */ CYGWIN_VERSION_API_MAJOR,
/* api_minor */ CYGWIN_VERSION_API_MINOR,
- /* unused2 */ {0, 0, 0, 0, 0},
- /* resourcelocks */ &_reslock, /* threadinterface */ &_mtinterf,
+ /* unused2 */ {0, 0, 0, 0, 0, 0},
+ /* threadinterface */ &_mtinterf,
/* impure_ptr */ _GLOBAL_REENT,
};
bool ignore_case_with_glob;
@@ -760,7 +759,6 @@ dll_crt0_0 ()
}
}
- user_data->resourcelocks->Init ();
user_data->threadinterface->Init ();
_cygtls::init ();
diff --git a/winsup/cygwin/exceptions.cc b/winsup/cygwin/exceptions.cc
index c05151d..2280f82 100644
--- a/winsup/cygwin/exceptions.cc
+++ b/winsup/cygwin/exceptions.cc
@@ -1294,9 +1294,6 @@ _cygtls::signal_exit (int rc)
stupid. */
SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_TIME_CRITICAL);
- user_data->resourcelocks->Delete ();
- user_data->resourcelocks->Init ();
-
sigproc_printf ("about to call do_exit (%x)", rc);
SetEvent (signal_arrived);
do_exit (rc);
diff --git a/winsup/cygwin/include/sys/cygwin.h b/winsup/cygwin/include/sys/cygwin.h
index 26bfee0..432811c 100644
--- a/winsup/cygwin/include/sys/cygwin.h
+++ b/winsup/cygwin/include/sys/cygwin.h
@@ -139,7 +139,6 @@ extern HANDLE cygwin_logon_user (const struct passwd *, const char *);
*/
#ifdef __cplusplus
-class ResourceLocks;
class MTinterface;
#endif
@@ -197,18 +196,16 @@ struct per_process
DWORD api_minor; /* linked with */
/* For future expansion, so apps won't have to be relinked if we
add an item. */
- DWORD unused2[5];
+ DWORD unused2[6];
#if defined (__INSIDE_CYGWIN__) && defined (__cplusplus)
- ResourceLocks *resourcelocks;
MTinterface *threadinterface;
#else
- void *resourcelocks;
void *threadinterface;
#endif
struct _reent *impure_ptr;
};
-#define per_process_overwrite ((unsigned) &(((struct per_process *) NULL)->resourcelocks))
+#define per_process_overwrite ((unsigned) &(((struct per_process *) NULL)->threadinterface))
extern void cygwin_premain0 (int argc, char **argv, struct per_process *);
extern void cygwin_premain1 (int argc, char **argv, struct per_process *);
diff --git a/winsup/cygwin/mmap.cc b/winsup/cygwin/mmap.cc
index 88b9852..ed8ee73 100644
--- a/winsup/cygwin/mmap.cc
+++ b/winsup/cygwin/mmap.cc
@@ -24,6 +24,7 @@ details. */
#include "pinfo.h"
#include "sys/cygwin.h"
#include "ntdll.h"
+#include <sys/queue.h>
/* __PROT_ATTACH indicates an anonymous mapping which is supposed to be
attached to a file mapping for pages beyond the file's EOF. The idea
@@ -50,6 +51,11 @@ details. */
/* Used for anonymous mappings. */
static fhandler_dev_zero fh_anonymous;
+/* Used for thread synchronization while accessing mmap bookkeeping lists. */
+static NO_COPY muto mmap_guard;
+#define LIST_LOCK() (mmap_guard.init ("mmap_guard")->acquire ())
+#define LIST_UNLOCK() (mmap_guard.release ())
+
/* Small helpers to avoid having lots of flag bit tests in the code. */
static inline bool
priv (int flags)
@@ -422,16 +428,20 @@ mmap_init ()
The class structure:
One member of class map per process, global variable mmapped_areas.
- Contains a dynamic class list array. Each list entry represents all
- mapping to a file, keyed by file descriptor and file name hash.
- Each list entry contains a dynamic class mmap_record array. Each
- mmap_record represents exactly one mapping. For each mapping, there's
+ Contains a singly-linked list of type class mmap_list. Each mmap_list
+ entry represents all mapping to a file, keyed by file descriptor and
+ file name hash.
+ Each list entry contains a singly-linked list of type class mmap_record.
+ Each mmap_record represents exactly one mapping. For each mapping, there's
an additional so called `page_map'. It's an array of bits, one bit
per mapped memory page. The bit is set if the page is accessible,
unset otherwise. */
class mmap_record
{
+ public:
+ LIST_ENTRY (mmap_record) mr_next;
+
private:
int fd;
HANDLE mapping_hdl;
@@ -485,6 +495,7 @@ class mmap_record
void free_page_map () { if (page_map) cfree (page_map); }
DWORD find_unused_pages (DWORD pages) const;
+ bool match (caddr_t addr, DWORD len, caddr_t &m_addr, DWORD &m_len);
_off64_t map_pages (_off64_t off, DWORD len);
bool map_pages (caddr_t addr, DWORD len);
bool unmap_pages (caddr_t addr, DWORD len);
@@ -502,45 +513,39 @@ class mmap_record
bool compatible_flags (int fl) const;
};
-class list
+class mmap_list
{
+ public:
+ LIST_ENTRY (mmap_list) ml_next;
+ LIST_HEAD (, mmap_record) recs;
+
private:
- mmap_record *recs;
- int nrecs, maxrecs;
int fd;
__ino64_t hash;
public:
int get_fd () const { return fd; }
__ino64_t get_hash () const { return hash; }
- mmap_record *get_record (int i) { return i >= nrecs ? NULL : recs + i; }
bool anonymous () const { return fd == -1; }
void set (int nfd);
mmap_record *add_record (mmap_record r);
- bool del_record (int i);
- void free_recs () { if (recs) cfree (recs); }
- mmap_record *search_record (_off64_t off, DWORD len);
- long search_record (caddr_t addr, DWORD len, caddr_t &m_addr, DWORD &m_len,
- long start);
+ bool del_record (mmap_record *rec);
caddr_t try_map (void *addr, size_t len, int flags, _off64_t off);
};
-class map
+class mmap_areas
{
- private:
- list *lists;
- unsigned nlists, maxlists;
-
public:
- list *get_list (unsigned i) { return i >= nlists ? NULL : lists + i; }
- list *get_list_by_fd (int fd);
- list *add_list (int fd);
- void del_list (unsigned i);
+ LIST_HEAD (, mmap_list) lists;
+
+ mmap_list *get_list_by_fd (int fd);
+ mmap_list *add_list (int fd);
+ void del_list (mmap_list *ml);
};
/* This is the global map structure pointer. */
-static map mmapped_areas;
+static mmap_areas mmapped_areas;
bool
mmap_record::compatible_flags (int fl) const
@@ -571,6 +576,25 @@ mmap_record::find_unused_pages (DWORD pages) const
}
bool
+mmap_record::match (caddr_t addr, DWORD len, caddr_t &m_addr, DWORD &m_len)
+{
+ caddr_t low = (addr >= get_address ()) ? addr : get_address ();
+ caddr_t high = get_address ();
+ if (filler ())
+ high += get_len ();
+ else
+ high += (PAGE_CNT (get_len ()) * getsystempagesize ());
+ high = (addr + len < high) ? addr + len : high;
+ if (low < high)
+ {
+ m_addr = low;
+ m_len = high - low;
+ return true;
+ }
+ return false;
+}
+
+bool
mmap_record::alloc_page_map ()
{
/* Allocate one bit per page */
@@ -710,79 +734,23 @@ mmap_record::free_fh (fhandler_base *fh)
}
mmap_record *
-list::add_record (mmap_record r)
+mmap_list::add_record (mmap_record r)
{
- if (nrecs == maxrecs)
- {
- mmap_record *new_recs;
- if (maxrecs == 0)
- new_recs = (mmap_record *)
- cmalloc (HEAP_MMAP, 5 * sizeof (mmap_record));
- else
- new_recs = (mmap_record *)
- crealloc (recs, (maxrecs + 5) * sizeof (mmap_record));
- if (!new_recs)
- return NULL;
- maxrecs += 5;
- recs = new_recs;
- }
- recs[nrecs] = r;
- if (!recs[nrecs].alloc_page_map ())
+ mmap_record *rec = (mmap_record *) cmalloc (HEAP_MMAP, sizeof (mmap_record));
+ if (!rec)
return NULL;
- return recs + nrecs++;
-}
-
-/* Used in mmap() */
-mmap_record *
-list::search_record (_off64_t off, DWORD len)
-{
- if (anonymous () && !off)
- {
- len = PAGE_CNT (len);
- for (int i = 0; i < nrecs; ++i)
- if (recs[i].find_unused_pages (len) != (DWORD)-1)
- return recs + i;
- }
- else
+ *rec = r;
+ if (!rec->alloc_page_map ())
{
- for (int i = 0; i < nrecs; ++i)
- if (off >= recs[i].get_offset ()
- && off + len
- <= recs[i].get_offset ()
- + (PAGE_CNT (recs[i].get_len ()) * getsystempagesize ()))
- return recs + i;
- }
- return NULL;
-}
-
-/* Used in munmap() */
-long
-list::search_record (caddr_t addr, DWORD len, caddr_t &m_addr, DWORD &m_len,
- long start)
-{
- caddr_t low, high;
-
- for (long i = start + 1; i < nrecs; ++i)
- {
- low = (addr >= recs[i].get_address ()) ? addr : recs[i].get_address ();
- high = recs[i].get_address ();
- if (recs[i].filler ())
- high += recs[i].get_len ();
- else
- high += (PAGE_CNT (recs[i].get_len ()) * getsystempagesize ());
- high = (addr + len < high) ? addr + len : high;
- if (low < high)
- {
- m_addr = low;
- m_len = high - low;
- return i;
- }
+ cfree (rec);
+ return NULL;
}
- return -1;
+ LIST_INSERT_HEAD (&recs, rec, mr_next);
+ return rec;
}
void
-list::set (int nfd)
+mmap_list::set (int nfd)
{
fd = nfd;
if (!anonymous ())
@@ -794,27 +762,22 @@ list::set (int nfd)
fstat (nfd, &st);
hash = st.st_ino;
}
- nrecs = maxrecs = 0;
- recs = NULL;
+ LIST_INIT (&recs);
}
bool
-list::del_record (int i)
+mmap_list::del_record (mmap_record *rec)
{
- if (i < nrecs)
- {
- recs[i].free_page_map ();
- for (; i < nrecs - 1; i++)
- recs[i] = recs[i + 1];
- nrecs--;
- }
+ rec->free_page_map ();
+ LIST_REMOVE (rec, mr_next);
+ cfree (rec);
/* Return true if the list is empty which allows the caller to remove
- this list from the list array. */
- return !nrecs;
+ this list from the list of lists. */
+ return !LIST_FIRST(&recs);
}
caddr_t
-list::try_map (void *addr, size_t len, int flags, _off64_t off)
+mmap_list::try_map (void *addr, size_t len, int flags, _off64_t off)
{
mmap_record *rec;
@@ -822,10 +785,13 @@ list::try_map (void *addr, size_t len, int flags, _off64_t off)
{
/* If MAP_FIXED isn't given, check if this mapping matches into the
chunk of another already performed mapping. */
- if ((rec = search_record (off, len)) != NULL
- && rec->compatible_flags (flags))
+ DWORD plen = PAGE_CNT (len);
+ LIST_FOREACH (rec, &recs, mr_next)
+ if (rec->find_unused_pages (plen) != (DWORD) -1)
+ break;
+ if (rec && rec->compatible_flags (flags))
{
- if ((off = rec->map_pages (off, len)) == (_off64_t)-1)
+ if ((off = rec->map_pages (off, len)) == (_off64_t) -1)
return (caddr_t) MAP_FAILED;
return (caddr_t) rec->get_address () + off;
}
@@ -837,11 +803,11 @@ list::try_map (void *addr, size_t len, int flags, _off64_t off)
if a memory region is unmapped and remapped with MAP_FIXED. */
caddr_t u_addr;
DWORD u_len;
- long record_idx = -1;
- if ((record_idx = search_record ((caddr_t) addr, len, u_addr, u_len,
- record_idx)) >= 0)
+ LIST_FOREACH (rec, &recs, mr_next)
+ if (rec->match ((caddr_t) addr, len, u_addr, u_len))
+ break;
+ if (rec)
{
- rec = get_record (record_idx);
if (u_addr > (caddr_t) addr || u_addr + len < (caddr_t) addr + len
|| !rec->compatible_flags (flags))
{
@@ -859,53 +825,40 @@ list::try_map (void *addr, size_t len, int flags, _off64_t off)
return NULL;
}
-list *
-map::get_list_by_fd (int fd)
+mmap_list *
+mmap_areas::get_list_by_fd (int fd)
{
- unsigned i;
- for (i = 0; i < nlists; i++)
+ mmap_list *ml;
+ LIST_FOREACH (ml, &lists, ml_next)
{
- if (fd == -1 && lists[i].anonymous ())
- return lists + i;
+ if (fd == -1 && ml->anonymous ())
+ return ml;
/* The fd isn't sufficient since it could already be the fd of another
file. So we use the inode number as evaluated by fstat to identify
the file. */
struct stat st;
- if (fd != -1 && !fstat (fd, &st) && lists[i].get_hash () == st.st_ino)
- return lists + i;
+ if (fd != -1 && !fstat (fd, &st) && ml->get_hash () == st.st_ino)
+ return ml;
}
return 0;
}
-list *
-map::add_list (int fd)
+mmap_list *
+mmap_areas::add_list (int fd)
{
- if (nlists == maxlists)
- {
- list *new_lists;
- if (maxlists == 0)
- new_lists = (list *) cmalloc (HEAP_MMAP, 5 * sizeof (list));
- else
- new_lists = (list *) crealloc (lists, (maxlists + 5) * sizeof (list));
- if (!new_lists)
- return NULL;
- maxlists += 5;
- lists = new_lists;
- }
- lists[nlists].set (fd);
- return lists + nlists++;
+ mmap_list *ml = (mmap_list *) cmalloc (HEAP_MMAP, sizeof (mmap_list));
+ if (!ml)
+ return NULL;
+ ml->set (fd);
+ LIST_INSERT_HEAD (&lists, ml, ml_next);
+ return ml;
}
void
-map::del_list (unsigned i)
+mmap_areas::del_list (mmap_list *ml)
{
- if (i < nlists)
- {
- lists[i].free_recs ();
- for (; i < nlists - 1; i++)
- lists[i] = lists[i + 1];
- nlists--;
- }
+ LIST_REMOVE (ml, ml_next);
+ cfree (ml);
}
/* This function is called from exception_handler when a segmentation
@@ -929,31 +882,42 @@ map::del_list (unsigned i)
int
mmap_is_attached_or_noreserve_page (ULONG_PTR addr)
{
- list *map_list;
- long record_idx;
+ mmap_list *map_list;
+ mmap_record *rec;
caddr_t u_addr;
DWORD u_len;
DWORD pagesize = getsystempagesize ();
+ int ret;
addr = rounddown (addr, pagesize);
+ LIST_LOCK ();
if (!(map_list = mmapped_areas.get_list_by_fd (-1)))
- return 0;
- if ((record_idx = map_list->search_record ((caddr_t)addr, pagesize,
- u_addr, u_len, -1)) < 0)
- return 0;
- if (map_list->get_record (record_idx)->attached ())
- return 1;
- if (!map_list->get_record (record_idx)->noreserve ())
- return 0;
- DWORD new_prot = map_list->get_record (record_idx)->gen_protect ();
- return VirtualAlloc ((void *)addr, pagesize, MEM_COMMIT, new_prot) ? 2 : 1;
+ ret = 0;
+ else
+ {
+ LIST_FOREACH (rec, &map_list->recs, mr_next)
+ if (rec->match ((caddr_t) addr, pagesize, u_addr, u_len))
+ break;
+ if (!rec)
+ ret = 0;
+ else if (rec->attached ())
+ ret = 1;
+ else if (!rec->noreserve ())
+ ret = 0;
+ else
+ ret = VirtualAlloc ((void *)addr, pagesize, MEM_COMMIT,
+ rec->gen_protect ())
+ ? 2 : 1;
+ }
+ LIST_UNLOCK ();
+ return ret;
}
static caddr_t
mmap_worker (fhandler_base *fh, caddr_t base, size_t len, int prot, int flags,
int fd, _off64_t off)
{
- list *map_list;
+ mmap_list *map_list;
HANDLE h = fh->mmap (&base, len, prot, flags, off);
if (h == INVALID_HANDLE_VALUE)
return NULL;
@@ -983,7 +947,7 @@ mmap64 (void *addr, size_t len, int prot, int flags, int fd, _off64_t off)
fhandler_base *fh = NULL;
fhandler_disk_file *fh_disk_file = NULL; /* Used for reopening a disk file
when necessary. */
- list *map_list = NULL;
+ mmap_list *map_list = NULL;
size_t orig_len = 0;
caddr_t base = NULL;
@@ -993,8 +957,6 @@ mmap64 (void *addr, size_t len, int prot, int flags, int fd, _off64_t off)
fh_anonymous.set_io_handle (INVALID_HANDLE_VALUE);
fh_anonymous.set_access (GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE);
- SetResourceLock (LOCK_MMAP_LIST, READ_LOCK | WRITE_LOCK, "mmap");
-
/* EINVAL error conditions. Note that the addr%pagesize test is deferred
to workaround a serious alignment problem in Windows 98. */
if (off % pagesize
@@ -1184,6 +1146,7 @@ go_ahead:
if (noreserve (flags) && (!anonymous (flags) || !priv (flags)))
flags &= ~MAP_NORESERVE;
+ LIST_LOCK ();
map_list = mmapped_areas.get_list_by_fd (fd);
/* Test if an existing anonymous mapping can be recycled. */
@@ -1195,7 +1158,7 @@ go_ahead:
if (tried)
{
ret = tried;
- goto out;
+ goto out_with_unlock;
}
}
@@ -1204,7 +1167,7 @@ go_ahead:
&& fixed (flags) && ((uintptr_t) addr % pagesize))
{
set_errno (EINVAL);
- goto out;
+ goto out_with_unlock;
}
if (orig_len)
@@ -1227,20 +1190,20 @@ go_ahead:
if (!newaddr)
{
__seterrno ();
- goto out;
+ goto out_with_unlock;
}
}
if (!VirtualFree (newaddr, 0, MEM_RELEASE))
{
__seterrno ();
- goto out;
+ goto out_with_unlock;
}
addr = newaddr;
}
base = mmap_worker (fh, (caddr_t) addr, len, prot, flags, fd, off);
if (!base)
- goto out;
+ goto out_with_unlock;
if (orig_len)
{
@@ -1276,7 +1239,7 @@ go_ahead:
{
fh->munmap (fh->get_handle (), base, len);
set_errno (ENOMEM);
- goto out;
+ goto out_with_unlock;
}
at_base += valid_page_len;
}
@@ -1294,9 +1257,10 @@ go_ahead:
ret = base;
-out:
+out_with_unlock:
+ LIST_UNLOCK ();
- ReleaseResourceLock (LOCK_MMAP_LIST, READ_LOCK | WRITE_LOCK, "mmap");
+out:
if (fh_disk_file)
CloseHandle (fh_disk_file->get_handle ());
@@ -1334,23 +1298,21 @@ munmap (void *addr, size_t len)
}
len = roundup2 (len, pagesize);
- SetResourceLock (LOCK_MMAP_LIST, WRITE_LOCK | READ_LOCK, "munmap");
+ LIST_LOCK ();
/* Iterate through the map, unmap pages between addr and addr+len
in all maps. */
- list *map_list;
- for (unsigned list_idx = 0;
- (map_list = mmapped_areas.get_list (list_idx));
- ++list_idx)
+ mmap_list *map_list, *next_map_list;
+ LIST_FOREACH_SAFE (map_list, &mmapped_areas.lists, ml_next, next_map_list)
{
- long record_idx = -1;
+ mmap_record *rec, *next_rec;
caddr_t u_addr;
DWORD u_len;
- while ((record_idx = map_list->search_record((caddr_t)addr, len, u_addr,
- u_len, record_idx)) >= 0)
+ LIST_FOREACH_SAFE (rec, &map_list->recs, mr_next, next_rec)
{
- mmap_record *rec = map_list->get_record (record_idx);
+ if (!rec->match ((caddr_t) addr, len, u_addr, u_len))
+ continue;
if (rec->unmap_pages (u_addr, u_len))
{
/* The whole record has been unmapped, so we now actually
@@ -1362,18 +1324,18 @@ munmap (void *addr, size_t len)
rec->free_fh (fh);
/* ...and delete the record. */
- if (map_list->del_record (record_idx--))
+ if (map_list->del_record (rec))
{
/* Yay, the last record has been removed from the list,
we can remove the list now, too. */
- mmapped_areas.del_list (list_idx--);
+ mmapped_areas.del_list (map_list);
break;
}
}
}
}
- ReleaseResourceLock (LOCK_MMAP_LIST, WRITE_LOCK | READ_LOCK, "munmap");
+ LIST_UNLOCK ();
syscall_printf ("0 = munmap(): %x", addr);
return 0;
}
@@ -1384,11 +1346,11 @@ extern "C" int
msync (void *addr, size_t len, int flags)
{
int ret = -1;
- list *map_list;
+ mmap_list *map_list;
syscall_printf ("msync (addr: %p, len %u, flags %x)", addr, len, flags);
- SetResourceLock (LOCK_MMAP_LIST, WRITE_LOCK | READ_LOCK, "msync");
+ LIST_LOCK ();
/* See comment in mmap64 for a description. */
size_t pagesize = wincap.has_mmap_alignment_bug () ?
@@ -1406,26 +1368,22 @@ msync (void *addr, size_t len, int flags)
/* Iterate through the map, looking for the mmapped area.
Error if not found. */
- for (unsigned list_idx = 0;
- (map_list = mmapped_areas.get_list (list_idx));
- ++list_idx)
+ LIST_FOREACH (map_list, &mmapped_areas.lists, ml_next)
{
mmap_record *rec;
- for (int record_idx = 0;
- (rec = map_list->get_record (record_idx));
- ++record_idx)
+ LIST_FOREACH (rec, &map_list->recs, mr_next)
{
- if (rec->access ((caddr_t)addr))
+ if (rec->access ((caddr_t) addr))
{
/* Check whole area given by len. */
for (DWORD i = getpagesize (); i < len; i += getpagesize ())
- if (!rec->access ((caddr_t)addr + i))
+ if (!rec->access ((caddr_t) addr + i))
{
set_errno (ENOMEM);
goto out;
}
fhandler_base *fh = rec->alloc_fh ();
- ret = fh->msync (rec->get_handle (), (caddr_t)addr, len, flags);
+ ret = fh->msync (rec->get_handle (), (caddr_t) addr, len, flags);
rec->free_fh (fh);
goto out;
}
@@ -1437,7 +1395,7 @@ msync (void *addr, size_t len, int flags)
out:
syscall_printf ("%d = msync()", ret);
- ReleaseResourceLock (LOCK_MMAP_LIST, WRITE_LOCK | READ_LOCK, "msync");
+ LIST_UNLOCK ();
return ret;
}
@@ -1463,46 +1421,43 @@ mprotect (void *addr, size_t len, int prot)
}
len = roundup2 (len, pagesize);
- SetResourceLock (LOCK_MMAP_LIST, WRITE_LOCK | READ_LOCK, "mprotect");
+ LIST_LOCK ();
/* Iterate through the map, protect pages between addr and addr+len
in all maps. */
- list *map_list;
- for (unsigned list_idx = 0;
- (map_list = mmapped_areas.get_list (list_idx));
- ++list_idx)
- {
- long record_idx = -1;
- caddr_t u_addr;
- DWORD u_len;
-
- while ((record_idx = map_list->search_record((caddr_t)addr, len,
- u_addr, u_len,
- record_idx)) >= 0)
- {
- mmap_record *rec = map_list->get_record (record_idx);
- in_mapped = true;
- if (rec->attached ())
- continue;
- new_prot = gen_protect (prot, rec->get_flags ());
- if (rec->noreserve ())
- {
- if (new_prot == PAGE_NOACCESS)
- ret = VirtualFree (u_addr, u_len, MEM_DECOMMIT);
- else
- ret = !!VirtualAlloc (u_addr, u_len, MEM_COMMIT, new_prot);
- }
- else
- ret = VirtualProtect (u_addr, u_len, new_prot, &old_prot);
- if (!ret)
- {
- __seterrno ();
- break;
- }
- }
- }
-
- ReleaseResourceLock (LOCK_MMAP_LIST, WRITE_LOCK | READ_LOCK, "mprotect");
+ mmap_list *map_list;
+ LIST_FOREACH (map_list, &mmapped_areas.lists, ml_next)
+ {
+ mmap_record *rec;
+ caddr_t u_addr;
+ DWORD u_len;
+
+ LIST_FOREACH (rec, &map_list->recs, mr_next)
+ {
+ if (!rec->match ((caddr_t) addr, len, u_addr, u_len))
+ continue;
+ in_mapped = true;
+ if (rec->attached ())
+ continue;
+ new_prot = gen_protect (prot, rec->get_flags ());
+ if (rec->noreserve ())
+ {
+ if (new_prot == PAGE_NOACCESS)
+ ret = VirtualFree (u_addr, u_len, MEM_DECOMMIT);
+ else
+ ret = !!VirtualAlloc (u_addr, u_len, MEM_COMMIT, new_prot);
+ }
+ else
+ ret = VirtualProtect (u_addr, u_len, new_prot, &old_prot);
+ if (!ret)
+ {
+ __seterrno ();
+ break;
+ }
+ }
+ }
+
+ LIST_UNLOCK ();
if (!in_mapped)
{
@@ -1973,15 +1928,11 @@ int __stdcall
fixup_mmaps_after_fork (HANDLE parent)
{
/* Iterate through the map */
- list *map_list;
- for (unsigned list_idx = 0;
- (map_list = mmapped_areas.get_list (list_idx));
- ++list_idx)
+ mmap_list *map_list;
+ LIST_FOREACH (map_list, &mmapped_areas.lists, ml_next)
{
mmap_record *rec;
- for (int record_idx = 0;
- (rec = map_list->get_record (record_idx));
- ++record_idx)
+ LIST_FOREACH (rec, &map_list->recs, mr_next)
{
debug_printf ("fd %d, h 0x%x, address %p, len 0x%x, prot: 0x%x, "
"flags: 0x%x, offset %X",
diff --git a/winsup/cygwin/thread.cc b/winsup/cygwin/thread.cc
index 12fe599..73bb9b1 100644
--- a/winsup/cygwin/thread.cc
+++ b/winsup/cygwin/thread.cc
@@ -284,43 +284,6 @@ semaphore::is_good_object (sem_t const * sem)
return true;
}
-LPCRITICAL_SECTION
-ResourceLocks::Lock (int _resid)
-{
- return &lock;
-}
-
-void
-SetResourceLock (int _res_id, int _mode, const char *_function)
-{
- EnterCriticalSection (user_data->resourcelocks->Lock (_res_id));
-}
-
-void
-ReleaseResourceLock (int _res_id, int _mode, const char *_function)
-{
- LeaveCriticalSection (user_data->resourcelocks->Lock (_res_id));
-}
-
-void
-ResourceLocks::Init ()
-{
- InitializeCriticalSection (&lock);
- inited = true;
- thread_printf ("lock %p inited by %p , %d", &lock, user_data, myself->pid);
-}
-
-void
-ResourceLocks::Delete ()
-{
- if (inited)
- {
- thread_printf ("Close Resource Locks %p ", &lock);
- DeleteCriticalSection (&lock);
- inited = false;
- }
-}
-
void
MTinterface::Init ()
{
diff --git a/winsup/cygwin/thread.h b/winsup/cygwin/thread.h
index c15ded4..ed2ec54 100644
--- a/winsup/cygwin/thread.h
+++ b/winsup/cygwin/thread.h
@@ -38,13 +38,6 @@ enum cw_cancel_action
cw_no_cancel
};
-extern "C"
-{
-void SetResourceLock (int, int, const char *) __attribute__ ((regparm (3)));
-void ReleaseResourceLock (int, int, const char *)
- __attribute__ ((regparm (3)));
-}
-
DWORD cancelable_wait (HANDLE, DWORD, const cw_cancel_action = cw_cancel_self, const enum cw_sig_wait = cw_sig_nosig)
__attribute__ ((regparm (3)));
@@ -94,17 +87,6 @@ private:
class per_process;
class pinfo;
-class ResourceLocks
-{
-public:
- LPCRITICAL_SECTION Lock (int);
- void Init ();
- void Delete ();
-private:
- CRITICAL_SECTION lock;
- bool inited;
-};
-
#define PTHREAD_MAGIC 0xdf0df045
#define PTHREAD_MUTEX_MAGIC PTHREAD_MAGIC+1
#define PTHREAD_KEY_MAGIC PTHREAD_MAGIC+2