commit eadebf08f11f1801d086d3878d583fd92855c765 Author: Justus Winter <4winter@informatik.uni-hamburg.de> Date: Sun May 4 10:28:41 2014 +0200 libports: use a single hash table diff --git a/libports/bucket-iterate.c b/libports/bucket-iterate.c index 5a6a56b..bf9f957 100644 --- a/libports/bucket-iterate.c +++ b/libports/bucket-iterate.c @@ -35,35 +35,42 @@ _ports_bucket_class_iterate (struct port_bucket *bucket, size_t i, n, nr_items; error_t err; - pthread_mutex_lock (&bucket->lock); + pthread_mutex_lock (&_ports_htable_lock); - if (bucket->htable.nr_items == 0) + if (_ports_htable.nr_items == 0) { - pthread_mutex_unlock (&bucket->lock); + pthread_mutex_unlock (&_ports_htable_lock); return 0; } - nr_items = bucket->htable.nr_items; + nr_items = _ports_htable.nr_items; p = malloc (nr_items * sizeof *p); if (p == NULL) { - pthread_mutex_unlock (&bucket->lock); + pthread_mutex_unlock (&_ports_htable_lock); return ENOMEM; } n = 0; - HURD_IHASH_ITERATE (&bucket->htable, arg) + HURD_IHASH_ITERATE (&_ports_htable, arg) { struct port_info *const pi = arg; - if (class == 0 || pi->class == class) + if ((bucket == NULL || pi->bucket == bucket) + && (class == NULL || pi->class == class)) { ports_port_ref (pi); p[n] = pi; n++; } } - pthread_mutex_unlock (&bucket->lock); + pthread_mutex_unlock (&_ports_htable_lock); + + if (n == 0) + { + free (p); + return 0; + } if (n != nr_items) { diff --git a/libports/claim-right.c b/libports/claim-right.c index 73e383a..2953310 100644 --- a/libports/claim-right.c +++ b/libports/claim-right.c @@ -34,9 +34,9 @@ ports_claim_right (void *portstruct) if (ret == MACH_PORT_NULL) return ret; - pthread_mutex_lock (&pi->bucket->lock); - hurd_ihash_locp_remove (&pi->bucket->htable, pi->hentry); - pthread_mutex_unlock (&pi->bucket->lock); + pthread_mutex_lock (&_ports_htable_lock); + hurd_ihash_locp_remove (&_ports_htable, pi->hentry); + pthread_mutex_unlock (&_ports_htable_lock); err = mach_port_move_member (mach_task_self (), ret, MACH_PORT_NULL); assert_perror (err); pthread_mutex_lock (&_ports_global_lock); diff --git a/libports/class-iterate.c b/libports/class-iterate.c index 638dd45..5cfcfc8 100644 --- a/libports/class-iterate.c +++ b/libports/class-iterate.c @@ -23,13 +23,5 @@ error_t ports_class_iterate (struct port_class *class, error_t (*fun)(void *)) { - pthread_mutex_lock (&_ports_global_lock); - if (class->ports != 0) - { - struct port_bucket *bucket = class->ports->bucket; - pthread_mutex_unlock (&_ports_global_lock); - return _ports_bucket_class_iterate (bucket, class, fun); - } - pthread_mutex_unlock (&_ports_global_lock); - return 0; + return _ports_bucket_class_iterate (NULL, class, fun); } diff --git a/libports/complete-deallocate.c b/libports/complete-deallocate.c index c08d0ce..f3b7134 100644 --- a/libports/complete-deallocate.c +++ b/libports/complete-deallocate.c @@ -29,9 +29,9 @@ _ports_complete_deallocate (struct port_info *pi) if (pi->port_right) { - pthread_mutex_lock (&pi->bucket->lock); - hurd_ihash_locp_remove (&pi->bucket->htable, pi->hentry); - pthread_mutex_unlock (&pi->bucket->lock); + pthread_mutex_lock (&_ports_htable_lock); + hurd_ihash_locp_remove (&_ports_htable, pi->hentry); + pthread_mutex_unlock (&_ports_htable_lock); mach_port_mod_refs (mach_task_self (), pi->port_right, MACH_PORT_RIGHT_RECEIVE, -1); pi->port_right = MACH_PORT_NULL; @@ -39,10 +39,6 @@ _ports_complete_deallocate (struct port_info *pi) pthread_mutex_lock (&_ports_global_lock); - *pi->prevp = pi->next; - if (pi->next) - pi->next->prevp = pi->prevp; - pi->bucket->count--; pi->class->count--; diff --git a/libports/create-bucket.c b/libports/create-bucket.c index f8a0996..9f6821d 100644 --- a/libports/create-bucket.c +++ b/libports/create-bucket.c @@ -46,14 +46,6 @@ ports_create_bucket () return NULL; } - pthread_mutex_init (&ret->lock, NULL); - hurd_ihash_init (&ret->htable, offsetof (struct port_info, hentry)); ret->rpcs = ret->flags = ret->count = 0; - - pthread_mutex_lock (&_ports_global_lock); - ret->next = _ports_all_buckets; - _ports_all_buckets = ret; - pthread_mutex_unlock (&_ports_global_lock); - return ret; } diff --git a/libports/create-class.c b/libports/create-class.c index 12c8add..782f52b 100644 --- a/libports/create-class.c +++ b/libports/create-class.c @@ -39,7 +39,6 @@ ports_create_class (void (*clean_routine)(void *), cl->dropweak_routine = dropweak_routine; cl->flags = 0; cl->rpcs = 0; - cl->ports = NULL; cl->count = 0; cl->uninhibitable_rpcs = ports_default_uninhibitable_rpcs; diff --git a/libports/create-internal.c b/libports/create-internal.c index 33ee1e1..2c62ca6 100644 --- a/libports/create-internal.c +++ b/libports/create-internal.c @@ -80,17 +80,12 @@ _ports_create_port_internal (struct port_class *class, goto loop; } - pthread_mutex_lock (&bucket->lock); - err = hurd_ihash_add (&bucket->htable, port, pi); - pthread_mutex_unlock (&bucket->lock); + pthread_mutex_lock (&_ports_htable_lock); + err = hurd_ihash_add (&_ports_htable, port, pi); + pthread_mutex_unlock (&_ports_htable_lock); if (err) goto lose; - pi->next = class->ports; - pi->prevp = &class->ports; - if (class->ports) - class->ports->prevp = &pi->next; - class->ports = pi; bucket->count++; class->count++; pthread_mutex_unlock (&_ports_global_lock); diff --git a/libports/destroy-right.c b/libports/destroy-right.c index b12dec9..6ba7302 100644 --- a/libports/destroy-right.c +++ b/libports/destroy-right.c @@ -30,12 +30,12 @@ ports_destroy_right (void *portstruct) if (pi->port_right != MACH_PORT_NULL) { - pthread_mutex_lock (&pi->bucket->lock); - hurd_ihash_locp_remove (&pi->bucket->htable, pi->hentry); + pthread_mutex_lock (&_ports_htable_lock); + hurd_ihash_locp_remove (&_ports_htable, pi->hentry); + pthread_mutex_unlock (&_ports_htable_lock); err = mach_port_mod_refs (mach_task_self (), pi->port_right, MACH_PORT_RIGHT_RECEIVE, -1); assert_perror (err); - pthread_mutex_unlock (&pi->bucket->lock); pi->port_right = MACH_PORT_NULL; diff --git a/libports/import-port.c b/libports/import-port.c index 1aa422a..7bd331d 100644 --- a/libports/import-port.c +++ b/libports/import-port.c @@ -74,17 +74,12 @@ ports_import_port (struct port_class *class, struct port_bucket *bucket, goto loop; } - pthread_mutex_lock (&bucket->lock); - err = hurd_ihash_add (&bucket->htable, port, pi); - pthread_mutex_unlock (&bucket->lock); + pthread_mutex_lock (&_ports_htable_lock); + err = hurd_ihash_add (&_ports_htable, port, pi); + pthread_mutex_unlock (&_ports_htable_lock); if (err) goto lose; - pi->next = class->ports; - pi->prevp = &class->ports; - if (class->ports) - class->ports->prevp = &pi->next; - class->ports = pi; bucket->count++; class->count++; pthread_mutex_unlock (&_ports_global_lock); diff --git a/libports/inhibit-all-rpcs.c b/libports/inhibit-all-rpcs.c index 337d068..421dc4a 100644 --- a/libports/inhibit-all-rpcs.c +++ b/libports/inhibit-all-rpcs.c @@ -36,26 +36,23 @@ ports_inhibit_all_rpcs () struct port_bucket *bucket; int this_one = 0; - for (bucket = _ports_all_buckets; bucket; bucket = bucket->next) + pthread_mutex_lock (&_ports_htable_lock); + HURD_IHASH_ITERATE (&_ports_htable, portstruct) { - pthread_mutex_lock (&bucket->lock); - HURD_IHASH_ITERATE (&bucket->htable, portstruct) + struct rpc_info *rpc; + struct port_info *pi = portstruct; + + for (rpc = pi->current_rpcs; rpc; rpc = rpc->next) { - struct rpc_info *rpc; - struct port_info *pi = portstruct; - - for (rpc = pi->current_rpcs; rpc; rpc = rpc->next) - { - /* Avoid cancelling the calling thread if it's currently - handling a RPC. */ - if (rpc->thread == hurd_thread_self ()) - this_one = 1; - else - hurd_thread_cancel (rpc->thread); - } + /* Avoid cancelling the calling thread if it's currently + handling a RPC. */ + if (rpc->thread == hurd_thread_self ()) + this_one = 1; + else + hurd_thread_cancel (rpc->thread); } - pthread_mutex_unlock (&bucket->lock); } + pthread_mutex_unlock (&_ports_htable_lock); while (_ports_total_rpcs > this_one) { diff --git a/libports/inhibit-bucket-rpcs.c b/libports/inhibit-bucket-rpcs.c index d55798e..ee6000e 100644 --- a/libports/inhibit-bucket-rpcs.c +++ b/libports/inhibit-bucket-rpcs.c @@ -35,11 +35,13 @@ ports_inhibit_bucket_rpcs (struct port_bucket *bucket) { int this_one = 0; - pthread_mutex_lock (&bucket->lock); - HURD_IHASH_ITERATE (&bucket->htable, portstruct) + pthread_mutex_lock (&_ports_htable_lock); + HURD_IHASH_ITERATE (&_ports_htable, portstruct) { struct rpc_info *rpc; struct port_info *pi = portstruct; + if (pi->bucket != bucket) + continue; for (rpc = pi->current_rpcs; rpc; rpc = rpc->next) { @@ -50,7 +52,7 @@ ports_inhibit_bucket_rpcs (struct port_bucket *bucket) hurd_thread_cancel (rpc->thread); } } - pthread_mutex_unlock (&bucket->lock); + pthread_mutex_unlock (&_ports_htable_lock); while (bucket->rpcs > this_one) { diff --git a/libports/inhibit-class-rpcs.c b/libports/inhibit-class-rpcs.c index fe26f27..0019b37 100644 --- a/libports/inhibit-class-rpcs.c +++ b/libports/inhibit-class-rpcs.c @@ -36,15 +36,24 @@ ports_inhibit_class_rpcs (struct port_class *class) struct rpc_info *rpc; int this_one = 0; - for (pi = class->ports; pi; pi = pi->next) - for (rpc = pi->current_rpcs; rpc; rpc = rpc->next) - { - /* Avoid cancelling the calling thread. */ - if (rpc->thread == hurd_thread_self ()) - this_one = 1; - else - hurd_thread_cancel (rpc->thread); - } + pthread_mutex_lock (&_ports_htable_lock); + HURD_IHASH_ITERATE (&_ports_htable, portstruct) + { + struct rpc_info *rpc; + struct port_info *pi = portstruct; + if (pi->class != class) + continue; + + for (rpc = pi->current_rpcs; rpc; rpc = rpc->next) + { + /* Avoid cancelling the calling thread. */ + if (rpc->thread == hurd_thread_self ()) + this_one = 1; + else + hurd_thread_cancel (rpc->thread); + } + } + pthread_mutex_unlock (&_ports_htable_lock); while (class->rpcs > this_one) { diff --git a/libports/init.c b/libports/init.c index 36e42a0..bab4884 100644 --- a/libports/init.c +++ b/libports/init.c @@ -19,9 +19,14 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "ports.h" +#include pthread_mutex_t _ports_global_lock = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t _ports_block = PTHREAD_COND_INITIALIZER; -struct port_bucket *_ports_all_buckets; + +struct hurd_ihash _ports_htable = + HURD_IHASH_INITIALIZER (offsetof (struct port_info, hentry)); +pthread_mutex_t _ports_htable_lock = PTHREAD_MUTEX_INITIALIZER; + int _ports_total_rpcs; int _ports_flags; diff --git a/libports/lookup-port.c b/libports/lookup-port.c index c919266..c488667 100644 --- a/libports/lookup-port.c +++ b/libports/lookup-port.c @@ -26,29 +26,14 @@ ports_lookup_port (struct port_bucket *bucket, mach_port_t port, struct port_class *class) { - struct port_info *pi = 0; - - if (bucket) - { - pthread_mutex_lock (&bucket->lock); - pi = hurd_ihash_find (&bucket->htable, port); - pthread_mutex_unlock (&bucket->lock); - } - else - { - pthread_mutex_lock (&_ports_global_lock); - for (bucket = _ports_all_buckets; bucket; bucket = bucket->next) - { - pthread_mutex_lock (&bucket->lock); - pi = hurd_ihash_find (&bucket->htable, port); - pthread_mutex_unlock (&bucket->lock); - if (pi) - break; - } - pthread_mutex_unlock (&_ports_global_lock); - } - - if (pi && class && pi->class != class) + struct port_info *pi; + pthread_mutex_lock (&_ports_htable_lock); + pi = hurd_ihash_find (&_ports_htable, port); + pthread_mutex_unlock (&_ports_htable_lock); + + if (pi + && ((class && pi->class != class) + || (bucket && pi->bucket != bucket))) pi = 0; if (pi) diff --git a/libports/ports.h b/libports/ports.h index c5bf8eb..ea99a7f 100644 --- a/libports/ports.h +++ b/libports/ports.h @@ -50,7 +50,6 @@ struct port_info struct rpc_info *current_rpcs; struct port_bucket *bucket; hurd_ihash_locp_t hentry; - struct port_info *next, **prevp; /* links on port_class list */ }; typedef struct port_info *port_info_t; @@ -63,13 +62,9 @@ typedef struct port_info *port_info_t; struct port_bucket { mach_port_t portset; - struct hurd_ihash htable; int rpcs; int flags; int count; - pthread_mutex_t lock; - - struct port_bucket *next; }; /* FLAGS above are the following: */ #define PORT_BUCKET_INHIBITED PORTS_INHIBITED @@ -82,7 +77,6 @@ struct port_class { int flags; int rpcs; - struct port_info *ports; int count; void (*clean_routine) (void *); void (*dropweak_routine) (void *); @@ -406,7 +400,12 @@ extern kern_return_t /* Private data */ extern pthread_mutex_t _ports_global_lock; extern pthread_cond_t _ports_block; -extern struct port_bucket *_ports_all_buckets; + +/* A hash table mapping port names to port_info objects. */ +extern struct hurd_ihash _ports_htable; +/* Access to the hash table is protected by this lock. */ +extern pthread_mutex_t _ports_htable_lock; + extern int _ports_total_rpcs; extern int _ports_flags; #define _PORTS_INHIBITED PORTS_INHIBITED diff --git a/libports/reallocate-from-external.c b/libports/reallocate-from-external.c index 84fecc9..3a01d69 100644 --- a/libports/reallocate-from-external.c +++ b/libports/reallocate-from-external.c @@ -43,9 +43,9 @@ ports_reallocate_from_external (void *portstruct, mach_port_t receive) MACH_PORT_RIGHT_RECEIVE, -1); assert_perror (err); - pthread_mutex_lock (&pi->bucket->lock); - hurd_ihash_locp_remove (&pi->bucket->htable, pi->hentry); - pthread_mutex_unlock (&pi->bucket->lock); + pthread_mutex_lock (&_ports_htable_lock); + hurd_ihash_locp_remove (&_ports_htable, pi->hentry); + pthread_mutex_unlock (&_ports_htable_lock); if ((pi->flags & PORT_HAS_SENDRIGHTS) && !stat.mps_srights) { @@ -63,9 +63,9 @@ ports_reallocate_from_external (void *portstruct, mach_port_t receive) pi->cancel_threshold = 0; pi->mscount = stat.mps_mscount; - pthread_mutex_lock (&pi->bucket->lock); - err = hurd_ihash_add (&pi->bucket->htable, receive, pi); - pthread_mutex_unlock (&pi->bucket->lock); + pthread_mutex_lock (&_ports_htable_lock); + err = hurd_ihash_add (&_ports_htable, receive, pi); + pthread_mutex_unlock (&_ports_htable_lock); assert_perror (err); mach_port_move_member (mach_task_self (), receive, pi->bucket->portset); diff --git a/libports/reallocate-port.c b/libports/reallocate-port.c index f68865e..461ca72 100644 --- a/libports/reallocate-port.c +++ b/libports/reallocate-port.c @@ -36,9 +36,9 @@ ports_reallocate_port (void *portstruct) MACH_PORT_RIGHT_RECEIVE, -1); assert_perror (err); - pthread_mutex_lock (&pi->bucket->lock); - hurd_ihash_locp_remove (&pi->bucket->htable, pi->hentry); - pthread_mutex_unlock (&pi->bucket->lock); + pthread_mutex_lock (&_ports_htable_lock); + hurd_ihash_locp_remove (&_ports_htable, pi->hentry); + pthread_mutex_unlock (&_ports_htable_lock); err = mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE, &pi->port_right); @@ -52,9 +52,9 @@ ports_reallocate_port (void *portstruct) pi->cancel_threshold = 0; pi->mscount = 0; - pthread_mutex_lock (&pi->bucket->lock); - err = hurd_ihash_add (&pi->bucket->htable, pi->port_right, pi); - pthread_mutex_unlock (&pi->bucket->lock); + pthread_mutex_lock (&_ports_htable_lock); + err = hurd_ihash_add (&_ports_htable, pi->port_right, pi); + pthread_mutex_unlock (&_ports_htable_lock); assert_perror (err); err = mach_port_move_member (mach_task_self (), pi->port_right, diff --git a/libports/transfer-right.c b/libports/transfer-right.c index 01c08e9..04d896f 100644 --- a/libports/transfer-right.c +++ b/libports/transfer-right.c @@ -41,9 +41,9 @@ ports_transfer_right (void *tostruct, port = frompi->port_right; if (port != MACH_PORT_NULL) { - pthread_mutex_lock (&frompi->bucket->lock); - hurd_ihash_locp_remove (&frompi->bucket->htable, frompi->hentry); - pthread_mutex_unlock (&frompi->bucket->lock); + pthread_mutex_lock (&_ports_htable_lock); + hurd_ihash_locp_remove (&_ports_htable, frompi->hentry); + pthread_mutex_unlock (&_ports_htable_lock); frompi->port_right = MACH_PORT_NULL; if (frompi->flags & PORT_HAS_SENDRIGHTS) { @@ -56,9 +56,9 @@ ports_transfer_right (void *tostruct, /* Destroy the existing right in TOPI. */ if (topi->port_right != MACH_PORT_NULL) { - pthread_mutex_lock (&topi->bucket->lock); - hurd_ihash_locp_remove (&topi->bucket->htable, topi->hentry); - pthread_mutex_unlock (&topi->bucket->lock); + pthread_mutex_lock (&_ports_htable_lock); + hurd_ihash_locp_remove (&_ports_htable, topi->hentry); + pthread_mutex_unlock (&_ports_htable_lock); err = mach_port_mod_refs (mach_task_self (), topi->port_right, MACH_PORT_RIGHT_RECEIVE, -1); assert_perror (err); @@ -83,9 +83,9 @@ ports_transfer_right (void *tostruct, if (port) { - pthread_mutex_lock (&topi->bucket->lock); - err = hurd_ihash_add (&topi->bucket->htable, port, topi); - pthread_mutex_unlock (&topi->bucket->lock); + pthread_mutex_lock (&_ports_htable_lock); + err = hurd_ihash_add (&_ports_htable, port, topi); + pthread_mutex_unlock (&_ports_htable_lock); assert_perror (err); if (topi->bucket != frompi->bucket) {