* thread.cc (pthread_rwlock::add_reader): Perform new operation here and return

pointer to allocated RWLOCK_READER structure.
(pthread_rwlock::rdlock): Reorganize to reflect new add_reader functionality.
(pthread_rwlock::tryrdlock): Ditto.  Remove unneeded call to lookup_reader().
* thread.h (pthread_rwlock::RWLOCK_READER::RWLOCK_READER): New constructor.
(pthread_rwlock::add_reader): Reflect new functionality.
This commit is contained in:
Christopher Faylor 2013-01-07 16:01:10 +00:00
parent 6cb6ea9cb4
commit 8f3f61eb96
3 changed files with 27 additions and 19 deletions

View File

@ -1,3 +1,15 @@
2013-01-07 Christopher Faylor <me.cygwin2013@cgf.cx>
* thread.cc (pthread_rwlock::add_reader): Perform new operation here
and return pointer to allocated RWLOCK_READER structure.
(pthread_rwlock::rdlock): Reorganize to reflect new add_reader
functionality.
(pthread_rwlock::tryrdlock): Ditto. Remove unneeded call to
lookup_reader().
* thread.h (pthread_rwlock::RWLOCK_READER::RWLOCK_READER): New
constructor.
(pthread_rwlock::add_reader): Reflect new functionality.
2013-01-03 Christopher Faylor <me.cygwin2013@cgf.cx> 2013-01-03 Christopher Faylor <me.cygwin2013@cgf.cx>
* globals.cc (exit_states): Renumber so that ES_EXIT_STARTING is first, * globals.cc (exit_states): Renumber so that ES_EXIT_STARTING is first,

View File

@ -1391,8 +1391,9 @@ pthread_rwlock::rdlock ()
goto DONE; goto DONE;
} }
reader = new struct RWLOCK_READER; if ((reader = add_reader ()))
if (!reader) ++reader->n;
else
{ {
result = EAGAIN; result = EAGAIN;
goto DONE; goto DONE;
@ -1409,9 +1410,6 @@ pthread_rwlock::rdlock ()
pthread_cleanup_pop (0); pthread_cleanup_pop (0);
} }
reader->thread = self;
reader->n = 1;
add_reader (reader);
DONE: DONE:
mtx.unlock (); mtx.unlock ();
@ -1427,21 +1425,15 @@ pthread_rwlock::tryrdlock ()
mtx.lock (); mtx.lock ();
if (writer || waiting_writers || lookup_reader (self)) if (writer || waiting_writers)
result = EBUSY; result = EBUSY;
else else
{ {
struct RWLOCK_READER *reader; RWLOCK_READER *reader = lookup_reader (self);
if (!reader)
reader = lookup_reader (self); reader = add_reader ();
if (reader && reader->n < ULONG_MAX) if (reader && reader->n < ULONG_MAX)
++reader->n; ++reader->n;
else if ((reader = new struct RWLOCK_READER))
{
reader->thread = self;
reader->n = 1;
add_reader (reader);
}
else else
result = EAGAIN; result = EAGAIN;
} }
@ -1544,10 +1536,13 @@ pthread_rwlock::unlock ()
return result; return result;
} }
void pthread_rwlock::RWLOCK_READER *
pthread_rwlock::add_reader (struct RWLOCK_READER *rd) pthread_rwlock::add_reader ()
{ {
RWLOCK_READER *rd = new RWLOCK_READER;
if (rd)
List_insert (readers, rd); List_insert (readers, rd);
return rd;
} }
void void

View File

@ -555,6 +555,7 @@ public:
struct RWLOCK_READER *next; struct RWLOCK_READER *next;
pthread_t thread; pthread_t thread;
unsigned long n; unsigned long n;
RWLOCK_READER (): next (NULL), thread (pthread::self ()), n (0) {}
} *readers; } *readers;
fast_mutex readers_mx; fast_mutex readers_mx;
@ -583,7 +584,7 @@ public:
private: private:
static List<pthread_rwlock> rwlocks; static List<pthread_rwlock> rwlocks;
void add_reader (struct RWLOCK_READER *rd); RWLOCK_READER *add_reader ();
void remove_reader (struct RWLOCK_READER *rd); void remove_reader (struct RWLOCK_READER *rd);
struct RWLOCK_READER *lookup_reader (pthread_t thread); struct RWLOCK_READER *lookup_reader (pthread_t thread);