1919#include "dds__handles.h"
2020#include "dds__types.h"
2121
22- #define HDL_REFCOUNT_MASK (0x03fff000u)
23- #define HDL_REFCOUNT_UNIT (0x00001000u)
24- #define HDL_REFCOUNT_SHIFT 12
25- #define HDL_PINCOUNT_MASK (0x00000fffu)
22+ #if DDSRT_64BIT // see dds__handles.h for reasoning
23+
24+ #define HDL_REFCOUNT_MASK UINT64_C(0x0003ffffff000000)
25+ #define HDL_REFCOUNT_UNIT UINT64_C(0x0000000001000000)
26+ #define HDL_REFCOUNT_SHIFT 24
27+ #define HDL_PINCOUNT_MASK UINT64_C(0x0000000000ffffff)
28+
29+ #else
30+
31+ #define HDL_REFCOUNT_MASK (0x03fffc00u)
32+ #define HDL_REFCOUNT_UNIT (0x00000400u)
33+ #define HDL_REFCOUNT_SHIFT 10
34+ #define HDL_PINCOUNT_MASK (0x000003ffu)
35+
36+ #endif
2637
2738/*
2839"regular" entities other than topics:
@@ -101,9 +112,10 @@ void dds_handle_server_fini (void)
101112 struct ddsrt_hh_iter it ;
102113 for (struct dds_handle_link * link = ddsrt_hh_iter_first (handles .ht , & it ); link != NULL ; link = ddsrt_hh_iter_next (& it ))
103114 {
104- uint32_t cf = ddsrt_atomic_ld32 (& link -> cnt_flags );
105- DDS_ERROR ("handle %" PRId32 " pin %" PRIu32 " refc %" PRIu32 "%s%s%s\n" , link -> hdl ,
106- cf & HDL_PINCOUNT_MASK , (cf & HDL_REFCOUNT_MASK ) >> HDL_REFCOUNT_SHIFT ,
115+ uintptr_t cf = ddsrt_atomic_ldptr (& link -> cnt_flags );
116+ DDS_ERROR ("handle %" PRId32 " pin %" PRIuPTR " refc %" PRIuPTR "%s%s%s\n" , link -> hdl ,
117+ (uintptr_t ) (cf & HDL_PINCOUNT_MASK ),
118+ (uintptr_t ) ((cf & HDL_REFCOUNT_MASK ) >> HDL_REFCOUNT_SHIFT ),
107119 cf & HDL_FLAG_PENDING ? " pending" : "" ,
108120 cf & HDL_FLAG_CLOSING ? " closing" : "" ,
109121 cf & HDL_FLAG_DELETE_DEFERRED ? " delete-deferred" : "" );
@@ -119,11 +131,11 @@ void dds_handle_server_fini (void)
119131
120132static dds_handle_t dds_handle_create_int (struct dds_handle_link * link , bool implicit , bool refc_counts_children , bool user_access )
121133{
122- uint32_t flags = HDL_FLAG_PENDING ;
134+ uintptr_t flags = HDL_FLAG_PENDING ;
123135 flags |= implicit ? HDL_FLAG_IMPLICIT : HDL_REFCOUNT_UNIT ;
124136 flags |= refc_counts_children ? HDL_FLAG_ALLOW_CHILDREN : 0 ;
125137 flags |= user_access ? 0 : HDL_FLAG_NO_USER_ACCESS ;
126- ddsrt_atomic_st32 (& link -> cnt_flags , flags | 1u );
138+ ddsrt_atomic_stptr (& link -> cnt_flags , flags | 1u );
127139 do {
128140 do {
129141 link -> hdl = (int32_t ) (ddsrt_random () & INT32_MAX );
@@ -165,7 +177,7 @@ dds_return_t dds_handle_register_special (struct dds_handle_link *link, bool imp
165177 else
166178 {
167179 handles .count ++ ;
168- ddsrt_atomic_st32 (& link -> cnt_flags , HDL_FLAG_PENDING | (implicit ? HDL_FLAG_IMPLICIT : HDL_REFCOUNT_UNIT ) | (allow_children ? HDL_FLAG_ALLOW_CHILDREN : 0 ) | 1u );
180+ ddsrt_atomic_stptr (& link -> cnt_flags , HDL_FLAG_PENDING | (implicit ? HDL_FLAG_IMPLICIT : HDL_REFCOUNT_UNIT ) | (allow_children ? HDL_FLAG_ALLOW_CHILDREN : 0 ) | 1u );
169181 link -> hdl = handle ;
170182 if (ddsrt_hh_add (handles .ht , link ))
171183 ret = handle ;
@@ -180,21 +192,21 @@ dds_return_t dds_handle_register_special (struct dds_handle_link *link, bool imp
180192void dds_handle_unpend (struct dds_handle_link * link )
181193{
182194#ifndef NDEBUG
183- uint32_t cf = ddsrt_atomic_ld32 (& link -> cnt_flags );
195+ uintptr_t cf = ddsrt_atomic_ldptr (& link -> cnt_flags );
184196 assert ((cf & HDL_FLAG_PENDING ));
185197 assert (!(cf & HDL_FLAG_DELETE_DEFERRED ));
186198 assert (!(cf & HDL_FLAG_CLOSING ));
187199 assert ((cf & HDL_REFCOUNT_MASK ) >= HDL_REFCOUNT_UNIT || (cf & HDL_FLAG_IMPLICIT ));
188200 assert ((cf & HDL_PINCOUNT_MASK ) >= 1u );
189201#endif
190- ddsrt_atomic_and32 (& link -> cnt_flags , ~HDL_FLAG_PENDING );
202+ ddsrt_atomic_andptr (& link -> cnt_flags , ~HDL_FLAG_PENDING );
191203 dds_handle_unpin (link );
192204}
193205
194206int32_t dds_handle_delete (struct dds_handle_link * link )
195207{
196208#ifndef NDEBUG
197- uint32_t cf = ddsrt_atomic_ld32 (& link -> cnt_flags );
209+ uintptr_t cf = ddsrt_atomic_ldptr (& link -> cnt_flags );
198210 if (!(cf & HDL_FLAG_PENDING ))
199211 {
200212 assert (cf & HDL_FLAG_CLOSING );
@@ -210,7 +222,7 @@ int32_t dds_handle_delete (struct dds_handle_link *link)
210222 return DDS_RETCODE_OK ;
211223}
212224
213- static int32_t dds_handle_pin_int (dds_handle_t hdl , uint32_t delta , bool from_user , struct dds_handle_link * * link )
225+ static int32_t dds_handle_pin_int (dds_handle_t hdl , uintptr_t delta , bool from_user , struct dds_handle_link * * link )
214226{
215227 struct dds_handle_link dummy = { .hdl = hdl };
216228 int32_t rc ;
@@ -231,12 +243,12 @@ static int32_t dds_handle_pin_int (dds_handle_t hdl, uint32_t delta, bool from_u
231243 rc = DDS_RETCODE_BAD_PARAMETER ;
232244 else
233245 {
234- uint32_t cf ;
246+ uintptr_t cf ;
235247 /* Assume success; bail out if the object turns out to be in the process of
236248 being deleted */
237249 rc = DDS_RETCODE_OK ;
238250 do {
239- cf = ddsrt_atomic_ld32 (& (* link )-> cnt_flags );
251+ cf = ddsrt_atomic_ldptr (& (* link )-> cnt_flags );
240252 if (cf & (HDL_FLAG_CLOSING | HDL_FLAG_PENDING | HDL_FLAG_NO_USER_ACCESS ))
241253 {
242254 if (cf & (HDL_FLAG_CLOSING | HDL_FLAG_PENDING ))
@@ -250,7 +262,7 @@ static int32_t dds_handle_pin_int (dds_handle_t hdl, uint32_t delta, bool from_u
250262 break ;
251263 }
252264 }
253- } while (!ddsrt_atomic_cas32 (& (* link )-> cnt_flags , cf , cf + delta ));
265+ } while (!ddsrt_atomic_casptr (& (* link )-> cnt_flags , cf , cf + delta ));
254266 }
255267 ddsrt_mutex_unlock (& handles .lock );
256268 return rc ;
@@ -287,11 +299,11 @@ int32_t dds_handle_pin_for_delete (dds_handle_t hdl, bool explicit, bool from_us
287299 rc = DDS_RETCODE_BAD_PARAMETER ;
288300 else
289301 {
290- uint32_t cf , cf1 ;
302+ uintptr_t cf , cf1 ;
291303 /* Assume success; bail out if the object turns out to be in the process of
292304 being deleted */
293305 do {
294- cf = ddsrt_atomic_ld32 (& (* link )-> cnt_flags );
306+ cf = ddsrt_atomic_ldptr (& (* link )-> cnt_flags );
295307
296308 if (from_user && (cf & (HDL_FLAG_NO_USER_ACCESS )))
297309 {
@@ -384,7 +396,7 @@ int32_t dds_handle_pin_for_delete (dds_handle_t hdl, bool explicit, bool from_us
384396 }
385397
386398 rc = ((cf1 & HDL_REFCOUNT_MASK ) == 0 || (cf1 & HDL_FLAG_ALLOW_CHILDREN )) ? DDS_RETCODE_OK : DDS_RETCODE_TRY_AGAIN ;
387- } while (!ddsrt_atomic_cas32 (& (* link )-> cnt_flags , cf , cf1 ));
399+ } while (!ddsrt_atomic_casptr (& (* link )-> cnt_flags , cf , cf1 ));
388400 }
389401 ddsrt_mutex_unlock (& handles .lock );
390402 return rc ;
@@ -394,9 +406,9 @@ bool dds_handle_drop_childref_and_pin (struct dds_handle_link *link, bool may_de
394406{
395407 bool del_parent = false;
396408 ddsrt_mutex_lock (& handles .lock );
397- uint32_t cf , cf1 ;
409+ uintptr_t cf , cf1 ;
398410 do {
399- cf = ddsrt_atomic_ld32 (& link -> cnt_flags );
411+ cf = ddsrt_atomic_ldptr (& link -> cnt_flags );
400412
401413 if (cf & (HDL_FLAG_CLOSING | HDL_FLAG_PENDING ))
402414 {
@@ -430,7 +442,7 @@ bool dds_handle_drop_childref_and_pin (struct dds_handle_link *link, bool may_de
430442 del_parent = false;
431443 }
432444 }
433- } while (!ddsrt_atomic_cas32 (& link -> cnt_flags , cf , cf1 ));
445+ } while (!ddsrt_atomic_casptr (& link -> cnt_flags , cf , cf1 ));
434446 ddsrt_mutex_unlock (& handles .lock );
435447 return del_parent ;
436448}
@@ -442,21 +454,21 @@ int32_t dds_handle_pin_and_ref_with_origin (dds_handle_t hdl, bool from_user, st
442454
443455void dds_handle_repin (struct dds_handle_link * link )
444456{
445- uint32_t x = ddsrt_atomic_inc32_nv (& link -> cnt_flags );
457+ uintptr_t x = ddsrt_atomic_incptr_nv (& link -> cnt_flags );
446458 (void ) x ;
447459}
448460
449461void dds_handle_unpin (struct dds_handle_link * link )
450462{
451463#ifndef NDEBUG
452- uint32_t cf = ddsrt_atomic_ld32 (& link -> cnt_flags );
464+ uintptr_t cf = ddsrt_atomic_ldptr (& link -> cnt_flags );
453465 if (cf & HDL_FLAG_CLOSING )
454466 assert ((cf & HDL_PINCOUNT_MASK ) > 1u );
455467 else
456468 assert ((cf & HDL_PINCOUNT_MASK ) >= 1u );
457469#endif
458470 ddsrt_mutex_lock (& handles .lock );
459- if ((ddsrt_atomic_dec32_nv (& link -> cnt_flags ) & (HDL_FLAG_CLOSING | HDL_PINCOUNT_MASK )) == (HDL_FLAG_CLOSING | 1u ))
471+ if ((ddsrt_atomic_decptr_nv (& link -> cnt_flags ) & (HDL_FLAG_CLOSING | HDL_PINCOUNT_MASK )) == (HDL_FLAG_CLOSING | 1u ))
460472 {
461473 ddsrt_cond_broadcast (& handles .cond );
462474 }
@@ -465,17 +477,17 @@ void dds_handle_unpin (struct dds_handle_link *link)
465477
466478void dds_handle_add_ref (struct dds_handle_link * link )
467479{
468- ddsrt_atomic_add32 (& link -> cnt_flags , HDL_REFCOUNT_UNIT );
480+ ddsrt_atomic_addptr (& link -> cnt_flags , HDL_REFCOUNT_UNIT );
469481}
470482
471483bool dds_handle_drop_ref (struct dds_handle_link * link )
472484{
473- uint32_t old , new ;
485+ uintptr_t old , new ;
474486 do {
475- old = ddsrt_atomic_ld32 (& link -> cnt_flags );
487+ old = ddsrt_atomic_ldptr (& link -> cnt_flags );
476488 assert ((old & HDL_REFCOUNT_MASK ) > 0 );
477489 new = old - HDL_REFCOUNT_UNIT ;
478- } while (!ddsrt_atomic_cas32 (& link -> cnt_flags , old , new ));
490+ } while (!ddsrt_atomic_casptr (& link -> cnt_flags , old , new ));
479491 ddsrt_mutex_lock (& handles .lock );
480492 if ((new & (HDL_FLAG_CLOSING | HDL_PINCOUNT_MASK )) == (HDL_FLAG_CLOSING | 1u ))
481493 {
@@ -487,13 +499,13 @@ bool dds_handle_drop_ref (struct dds_handle_link *link)
487499
488500bool dds_handle_unpin_and_drop_ref (struct dds_handle_link * link )
489501{
490- uint32_t old , new ;
502+ uintptr_t old , new ;
491503 do {
492- old = ddsrt_atomic_ld32 (& link -> cnt_flags );
504+ old = ddsrt_atomic_ldptr (& link -> cnt_flags );
493505 assert ((old & HDL_REFCOUNT_MASK ) > 0 );
494506 assert ((old & HDL_PINCOUNT_MASK ) > 0 );
495507 new = old - HDL_REFCOUNT_UNIT - 1u ;
496- } while (!ddsrt_atomic_cas32 (& link -> cnt_flags , old , new ));
508+ } while (!ddsrt_atomic_casptr (& link -> cnt_flags , old , new ));
497509 ddsrt_mutex_lock (& handles .lock );
498510 if ((new & (HDL_FLAG_CLOSING | HDL_PINCOUNT_MASK )) == (HDL_FLAG_CLOSING | 1u ))
499511 {
@@ -505,27 +517,27 @@ bool dds_handle_unpin_and_drop_ref (struct dds_handle_link *link)
505517
506518bool dds_handle_close (struct dds_handle_link * link )
507519{
508- uint32_t old = ddsrt_atomic_or32_ov (& link -> cnt_flags , HDL_FLAG_CLOSING );
520+ uintptr_t old = ddsrt_atomic_orptr_ov (& link -> cnt_flags , HDL_FLAG_CLOSING );
509521 return (old & HDL_REFCOUNT_MASK ) == 0 ;
510522}
511523
512524void dds_handle_close_wait (struct dds_handle_link * link )
513525{
514526#ifndef NDEBUG
515- uint32_t cf = ddsrt_atomic_ld32 (& link -> cnt_flags );
527+ uintptr_t cf = ddsrt_atomic_ldptr (& link -> cnt_flags );
516528 assert ((cf & HDL_FLAG_CLOSING ));
517529 assert ((cf & HDL_PINCOUNT_MASK ) >= 1u );
518530#endif
519531 ddsrt_mutex_lock (& handles .lock );
520- while ((ddsrt_atomic_ld32 (& link -> cnt_flags ) & HDL_PINCOUNT_MASK ) != 1u )
532+ while ((ddsrt_atomic_ldptr (& link -> cnt_flags ) & HDL_PINCOUNT_MASK ) != 1u )
521533 ddsrt_cond_wait (& handles .cond , & handles .lock );
522534 /* only one thread may call close_wait on a given handle */
523535 ddsrt_mutex_unlock (& handles .lock );
524536}
525537
526538bool dds_handle_is_not_refd (struct dds_handle_link * link )
527539{
528- return ((ddsrt_atomic_ld32 (& link -> cnt_flags ) & HDL_REFCOUNT_MASK ) == 0 );
540+ return ((ddsrt_atomic_ldptr (& link -> cnt_flags ) & HDL_REFCOUNT_MASK ) == 0 );
529541}
530542
531543extern inline bool dds_handle_is_closed (struct dds_handle_link * link );
0 commit comments