summaryrefslogtreecommitdiff
path: root/debian/patches/0001-libports-use-a-single-hash-table.patch
blob: 492a4cabb1eceb6ae7799746a433e189ffa3b903 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
From 27741dca90e3a6070727fffc22d3105653a33747 Mon Sep 17 00:00:00 2001
From: Justus Winter <4winter@informatik.uni-hamburg.de>
Date: Sat, 3 May 2014 03:53:41 +0200
Subject: [PATCH 1/5] libports: use a single hash table

Previously, libports used a hash table per port bucket.  This makes
looking up a port difficult if one does not know the port bucket, as
one has to iterate over all buckets and do a hash table lookup each.

The having to iterate over the buckets makes it necessary to keep a
list of all buckets, which has to be updated and protected by a lock
as well.

Also, the current code in _ports_bucket_class_iterate iterates over
the hash table associated with the bucket given.  When
ports_class_iterate calls this common function, it obtains a reference
to the bucket from one of the ports in the given class.  This will not
work if a class contains ports in different port buckets.  This
limitation is not documented as far as I can see.  Again, having to
maintain this list has its cost and requires serialization.

Use a single hash table instead.  Furthermore, serialize access to it
using a separate lock.  Remove the linked lists of all buckets and all
ports in a class.

* libports/bucket-iterate.c (ports_bucket_iterate): Acquire
_ports_htable_lock.  Also, generalize ports_bucket_iterate to treat
the bucket argument the same way as the class argument.
* libports/class-iterate.c (ports_class_iterate): Just call the
generalized _ports_bucket_class_iterate with NULL as class.
* libports/ports.h (struct port_info): Remove the port class links.
(struct port_bucket): Remove the hash table, and the all buckets link.
(_ports_all_buckets): Remove declaration.
(_ports_htable): New global hash table.
(_ports_htable_lock): Protected by this lock.
* libports/claim-right.c: Adjust accordingly.
* libports/complete-deallocate.c: Likewise.
* libports/create-bucket.c: Likewise.
* libports/create-class.c: Likewise.
* libports/create-internal.c: Likewise.
* libports/destroy-right.c: Likewise.
* libports/import-port.c: Likewise.
* libports/lookup-port.c: Likewise.
* libports/reallocate-from-external.c: Likewise.
* libports/reallocate-port.c: Likewise.
* libports/transfer-right.c: Likewise.
* libports/inhibit-all-rpcs.c: Iterate over the hash table.
* libports/inhibit-bucket-rpcs.c: Likewise, but filter using bucket.
* libports/inhibit-class-rpcs.c: Likewise, but filter using class.
* libports/init.c (_ports_htable): Initialize.
(_ports_htable_lock): Likewise.
---
 libports/bucket-iterate.c           | 21 +++++++++++++++------
 libports/claim-right.c              |  6 ++++--
 libports/class-iterate.c            | 10 +---------
 libports/complete-deallocate.c      |  8 +++-----
 libports/create-bucket.c            |  7 -------
 libports/create-class.c             |  1 -
 libports/create-internal.c          |  9 +++------
 libports/destroy-right.c            |  6 +++---
 libports/import-port.c              |  9 +++------
 libports/inhibit-all-rpcs.c         | 27 +++++++++++++--------------
 libports/inhibit-bucket-rpcs.c      |  7 +++++--
 libports/inhibit-class-rpcs.c       | 27 ++++++++++++++++++---------
 libports/init.c                     |  7 ++++++-
 libports/lookup-port.c              | 21 ++++++++-------------
 libports/ports.h                    | 11 ++++++-----
 libports/reallocate-from-external.c | 16 ++++++++++------
 libports/reallocate-port.c          | 10 +++++++---
 libports/transfer-right.c           | 20 +++++++++++++-------
 18 files changed, 118 insertions(+), 105 deletions(-)

diff --git a/libports/bucket-iterate.c b/libports/bucket-iterate.c
index babc204..d5f590e 100644
--- a/libports/bucket-iterate.c
+++ b/libports/bucket-iterate.c
@@ -36,35 +36,44 @@ _ports_bucket_class_iterate (struct port_bucket *bucket,
   error_t err;
 
   pthread_mutex_lock (&_ports_lock);
+  pthread_mutex_lock (&_ports_htable_lock);
 
-  if (bucket->htable.nr_items == 0)
+  if (_ports_htable.nr_items == 0)
     {
-      pthread_mutex_unlock (&_ports_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 (&_ports_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))
 	{
 	  pi->refcnt++;
 	  p[n] = pi;
 	  n++;
 	}
     }
+  pthread_mutex_unlock (&_ports_htable_lock);
   pthread_mutex_unlock (&_ports_lock);
 
+  if (n == 0)
+    {
+      free (p);
+      return 0;
+    }
+
   if (n != nr_items)
     {
       /* We allocated too much.  Release unused memory.  */
diff --git a/libports/claim-right.c b/libports/claim-right.c
index 4851ea3..f453a82 100644
--- a/libports/claim-right.c
+++ b/libports/claim-right.c
@@ -34,10 +34,12 @@ ports_claim_right (void *portstruct)
   if (ret == MACH_PORT_NULL)
     return ret;
 
-  pthread_mutex_lock (&_ports_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_move_member (mach_task_self (), ret, MACH_PORT_NULL);
   assert_perror (err);
+  pthread_mutex_lock (&_ports_lock);
   pi->port_right = MACH_PORT_NULL;
   if (pi->flags & PORT_HAS_SENDRIGHTS)
     {
diff --git a/libports/class-iterate.c b/libports/class-iterate.c
index 1f8878a..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_lock);
-  if (class->ports != 0)
-    {
-      struct port_bucket *bucket = class->ports->bucket;
-      pthread_mutex_unlock (&_ports_lock);
-      return _ports_bucket_class_iterate (bucket, class, fun);
-    }
-  pthread_mutex_unlock (&_ports_lock);
-  return 0;
+  return _ports_bucket_class_iterate (NULL, class, fun);
 }
diff --git a/libports/complete-deallocate.c b/libports/complete-deallocate.c
index 8ce095b..1371d92 100644
--- a/libports/complete-deallocate.c
+++ b/libports/complete-deallocate.c
@@ -29,16 +29,14 @@ _ports_complete_deallocate (struct port_info *pi)
 
   if (pi->port_right)
     {
-      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);
       mach_port_mod_refs (mach_task_self (), pi->port_right,
 			  MACH_PORT_RIGHT_RECEIVE, -1);
       pi->port_right = MACH_PORT_NULL;
     }
 
-  *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 52d50c3..9f6821d 100644
--- a/libports/create-bucket.c
+++ b/libports/create-bucket.c
@@ -46,13 +46,6 @@ ports_create_bucket ()
       return NULL;
     }
 
-  hurd_ihash_init (&ret->htable, offsetof (struct port_info, hentry));
   ret->rpcs = ret->flags = ret->count = 0;
-
-  pthread_mutex_lock (&_ports_lock);
-  ret->next = _ports_all_buckets;
-  _ports_all_buckets = ret;
-  pthread_mutex_unlock (&_ports_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 8551297..e773dd6 100644
--- a/libports/create-internal.c
+++ b/libports/create-internal.c
@@ -81,15 +81,12 @@ _ports_create_port_internal (struct port_class *class,
       goto loop;
     }
 
-  err = hurd_ihash_add (&bucket->htable, port, pi);
+  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_lock);
diff --git a/libports/destroy-right.c b/libports/destroy-right.c
index 65e19c7..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 (&_ports_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 (&_ports_lock);
 
       pi->port_right = MACH_PORT_NULL;
 
diff --git a/libports/import-port.c b/libports/import-port.c
index 226f47e..d0b2ea4 100644
--- a/libports/import-port.c
+++ b/libports/import-port.c
@@ -75,15 +75,12 @@ ports_import_port (struct port_class *class, struct port_bucket *bucket,
       goto loop;
     }
 
-  err = hurd_ihash_add (&bucket->htable, port, pi);
+  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_lock);
diff --git a/libports/inhibit-all-rpcs.c b/libports/inhibit-all-rpcs.c
index d4a54ba..83c291f 100644
--- a/libports/inhibit-all-rpcs.c
+++ b/libports/inhibit-all-rpcs.c
@@ -36,24 +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)
 	{
-	  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 (&_ports_htable_lock);
 
       while (_ports_total_rpcs > this_one)
 	{
diff --git a/libports/inhibit-bucket-rpcs.c b/libports/inhibit-bucket-rpcs.c
index 965aa03..ed3e29d 100644
--- a/libports/inhibit-bucket-rpcs.c
+++ b/libports/inhibit-bucket-rpcs.c
@@ -35,10 +35,13 @@ ports_inhibit_bucket_rpcs (struct port_bucket *bucket)
     {
       int this_one = 0;
 
-      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)
 	    {
@@ -49,7 +52,7 @@ ports_inhibit_bucket_rpcs (struct port_bucket *bucket)
 		hurd_thread_cancel (rpc->thread);
 	    }
 	}
-
+      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 7ee8653..1580bdb 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 3ef5388..8fbc7e8 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 <stddef.h>
 
 pthread_mutex_t _ports_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 f79f6f0..fbb13ef 100644
--- a/libports/lookup-port.c
+++ b/libports/lookup-port.c
@@ -26,26 +26,21 @@ ports_lookup_port (struct port_bucket *bucket,
 		   mach_port_t port,
 		   struct port_class *class)
 {
-  struct port_info *pi = 0;
-  
+  struct port_info *pi;
+
   pthread_mutex_lock (&_ports_lock);
+  pthread_mutex_lock (&_ports_htable_lock);
 
-  if (bucket)
-    pi = hurd_ihash_find (&bucket->htable, port);
-  else
-    for (bucket = _ports_all_buckets; bucket; bucket = bucket->next)
-      {
-	pi = hurd_ihash_find (&bucket->htable, port);
-	if (pi)
-	  break;
-      }
-  
-  if (pi && class && pi->class != class)
+  pi = hurd_ihash_find (&_ports_htable, port);
+  if (pi
+      && ((class && pi->class != class)
+          || (bucket && pi->bucket != bucket)))
     pi = 0;
 
   if (pi)
     pi->refcnt++;
 
+  pthread_mutex_unlock (&_ports_htable_lock);
   pthread_mutex_unlock (&_ports_lock);
   
   return pi;
diff --git a/libports/ports.h b/libports/ports.h
index 7f13124..9c5f14d 100644
--- a/libports/ports.h
+++ b/libports/ports.h
@@ -48,7 +48,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;
 
@@ -61,11 +60,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;
-  struct port_bucket *next;
 };
 /* FLAGS above are the following: */
 #define PORT_BUCKET_INHIBITED	PORTS_INHIBITED
@@ -78,7 +75,6 @@ struct port_class
 {
   int flags;
   int rpcs;
-  struct port_info *ports;
   int count;
   void (*clean_routine) (void *);
   void (*dropweak_routine) (void *);
@@ -402,7 +398,12 @@ extern kern_return_t
 /* Private data */
 extern pthread_mutex_t _ports_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 8cccb2a..bbcf9ba 100644
--- a/libports/reallocate-from-external.c
+++ b/libports/reallocate-from-external.c
@@ -43,8 +43,10 @@ ports_reallocate_from_external (void *portstruct, mach_port_t receive)
 			    MACH_PORT_RIGHT_RECEIVE, -1);
   assert_perror (err);
 
-  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);
+
   if ((pi->flags & PORT_HAS_SENDRIGHTS) && !stat.mps_srights)
     {
       dropref = 1;
@@ -59,11 +61,13 @@ ports_reallocate_from_external (void *portstruct, mach_port_t receive)
   pi->port_right = receive;
   pi->cancel_threshold = 0;
   pi->mscount = stat.mps_mscount;
-  
-  err = hurd_ihash_add (&pi->bucket->htable, receive, pi);
-  assert_perror (err);
+
+  pthread_mutex_lock (&_ports_htable_lock);
+  err = hurd_ihash_add (&_ports_htable, receive, pi);
+  pthread_mutex_unlock (&_ports_htable_lock);
   pthread_mutex_unlock (&_ports_lock);
-  
+  assert_perror (err);
+
   mach_port_move_member (mach_task_self (), receive, pi->bucket->portset);
   
   if (stat.mps_srights)
diff --git a/libports/reallocate-port.c b/libports/reallocate-port.c
index d2adaeb..99c4041 100644
--- a/libports/reallocate-port.c
+++ b/libports/reallocate-port.c
@@ -36,7 +36,9 @@ ports_reallocate_port (void *portstruct)
 			    MACH_PORT_RIGHT_RECEIVE, -1);
   assert_perror (err);
 
-  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_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE,
 			    &pi->port_right);
@@ -48,9 +50,11 @@ ports_reallocate_port (void *portstruct)
     }
   pi->cancel_threshold = 0;
   pi->mscount = 0;
-  err = hurd_ihash_add (&pi->bucket->htable, pi->port_right, pi);
-  assert_perror (err);
+  pthread_mutex_lock (&_ports_htable_lock);
+  err = hurd_ihash_add (&_ports_htable, pi->port_right, pi);
+  pthread_mutex_unlock (&_ports_htable_lock);
   pthread_mutex_unlock (&_ports_lock);
+  assert_perror (err);
 
   err = mach_port_move_member (mach_task_self (), pi->port_right, 
 			       pi->bucket->portset);
diff --git a/libports/transfer-right.c b/libports/transfer-right.c
index 72488a9..5a7653d 100644
--- a/libports/transfer-right.c
+++ b/libports/transfer-right.c
@@ -41,7 +41,9 @@ ports_transfer_right (void *tostruct,
   port = frompi->port_right;
   if (port != MACH_PORT_NULL)
     {
-      hurd_ihash_locp_remove (&frompi->bucket->htable, frompi->hentry);
+      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)
 	{
@@ -54,7 +56,9 @@ ports_transfer_right (void *tostruct,
   /* Destroy the existing right in TOPI. */
   if (topi->port_right != MACH_PORT_NULL)
     {
-      hurd_ihash_locp_remove (&topi->bucket->htable, topi->hentry);
+      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);
@@ -74,10 +78,14 @@ ports_transfer_right (void *tostruct,
   topi->port_right = port;
   topi->cancel_threshold = frompi->cancel_threshold;
   topi->mscount = frompi->mscount;
-  
+
+  pthread_mutex_unlock (&_ports_lock);
+
   if (port)
     {
-      err = hurd_ihash_add (&topi->bucket->htable, port, topi);
+      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)
         {
@@ -86,9 +94,7 @@ ports_transfer_right (void *tostruct,
 	  assert_perror (err);
 	}
     }
-  
-  pthread_mutex_unlock (&_ports_lock);
-  
+
   /* Take care of any lowered reference counts. */
   if (dereffrompi)
     ports_port_deref (frompi);
-- 
2.0.0.rc0