From 20b94ee904e42c78f54dafa6b7638c2299e07a63 Mon Sep 17 00:00:00 2001 From: Robert Collins Date: Sat, 21 Sep 2002 01:59:46 +0000 Subject: 2002-09-21 Robert Collins * thread.cc: Partial refactoring of pthread_key destructor handling. Loosely based on Thomas Pfaff's work. (pthread_key_destructor_list::Insert): Remove. (pthread_key_destructor_list::Pop): Remove. (pthread_key_destructor_list::IterateNull): Call the key's run_destructor method. (pthread_key::pthread_key): Initialize new member. (pthread_key::get): Mark as const for correctness. (pthread_key::run_destructor): Implement. * thread.h (pthread_key::get): Mark as const for correctness. (pthread_key::run_destructor): Declare. (List): New template class that implements a generic list. (pthread_key_destructor_list): Inherit from List, and remove now duplicate functions. --- winsup/cygwin/thread.h | 60 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 11 deletions(-) (limited to 'winsup/cygwin/thread.h') diff --git a/winsup/cygwin/thread.h b/winsup/cygwin/thread.h index ad4208f5d..05bf9793c 100644 --- a/winsup/cygwin/thread.h +++ b/winsup/cygwin/thread.h @@ -183,9 +183,10 @@ public: class pthread_key *next; int set (const void *); - void *get (); + void *get () const; + void run_destructor () const; - pthread_key (void (*)(void *)); + pthread_key (void (*)(void *)); ~pthread_key (); static void fixup_before_fork(); static void fixup_after_fork(); @@ -194,8 +195,53 @@ private: static pthread_key * keys; void saveKeyToBuffer (); void recreateKeyFromBuffer (); + void (*destructor) (void *); }; +/* interface */ +template class List { +public: + void Insert (ListNode *aNode); + ListNode *Remove ( ListNode *aNode); + ListNode *Pop (); +protected: + ListNode *head; +}; +/* implementation */ +template void +List::Insert (ListNode *aNode) +{ + if (!aNode) + return; + head = aNode->InsertAfter (head); + if (!head) + head = aNode; /*first node special case */ +} +template ListNode * +List::Remove ( ListNode *aNode) +{ + if (!aNode) + return NULL; + if (!head) + return NULL; + if (aNode == head) + return Pop (); + ListNode *resultPrev = head; + while (resultPrev && resultPrev->Next() && !(aNode == resultPrev->Next())) + resultPrev = resultprev->Next(); + if (resultPrev) + return resultPrev->UnlinkNext (); + return NULL; +} +template ListNode * +List::Pop () +{ + ListNode *result = head; + head = head->Next(); + return result; +} + + /* FIXME: test using multiple inheritance and merging key_destructor into pthread_key * for efficiency */ class pthread_key_destructor @@ -211,21 +257,13 @@ public: pthread_key *key; }; -class pthread_key_destructor_list +class pthread_key_destructor_list : public List { public: - void Insert (pthread_key_destructor * node); -/* remove a given dataitem, wherever in the list it is */ - pthread_key_destructor *Remove (pthread_key_destructor * item); -/* get the first item and remove at the same time */ - pthread_key_destructor *Pop (); pthread_key_destructor *Remove (pthread_key * key); void IterateNull (); -private: - pthread_key_destructor * head; }; - class pthread_attr:public verifyable_object { public: -- cgit v1.2.3