[01/16] lib: Add new once_define and once macros to eu-config.h

Message ID 20231010134300.53830-1-mark@klomp.org
State Committed
Headers
Series [01/16] lib: Add new once_define and once macros to eu-config.h |

Commit Message

Mark Wielaard Oct. 10, 2023, 1:42 p.m. UTC
  From: Heather McIntyre <hsm2@rice.edu>

	* lib/eu-config.h New macros.
	[USE_LOCKS] (ONCE_CALL): (once_define, once)

Signed-off-by: Heather S. McIntyre <hsm2@rice.edu>
Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 lib/eu-config.h | 7 +++++++
 1 file changed, 7 insertions(+)
  

Comments

Mark Wielaard Oct. 10, 2023, 1:54 p.m. UTC | #1
Hi Heather,

On Tue, 2023-10-10 at 15:42 +0200, Mark Wielaard wrote:
> From: Heather McIntyre <hsm2@rice.edu>
> 
> 	* lib/eu-config.h New macros.
> 	[USE_LOCKS] (ONCE_CALL): (once_define, once)
> 
> Signed-off-by: Heather S. McIntyre <hsm2@rice.edu>
> Signed-off-by: Mark Wielaard <mark@klomp.org>
> ---
>  lib/eu-config.h | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/lib/eu-config.h b/lib/eu-config.h
> index 78a5c4fe..feb079db 100644
> --- a/lib/eu-config.h
> +++ b/lib/eu-config.h
> @@ -33,13 +33,18 @@
>  # include <pthread.h>
>  # include <assert.h>
>  # define rwlock_define(class,name)	class pthread_rwlock_t name
> +# define once_define(class,name)  class pthread_once_t name = PTHREAD_ONCE_INIT
>  # define RWLOCK_CALL(call)		\
>    ({ int _err = pthread_rwlock_ ## call; assert_perror (_err); })
> +# define ONCE_CALL(call)  \
> +  ({ int _err = pthread_ ## call; assert_perror (_err); })
>  # define rwlock_init(lock)		RWLOCK_CALL (init (&lock, NULL))
>  # define rwlock_fini(lock)		RWLOCK_CALL (destroy (&lock))
>  # define rwlock_rdlock(lock)		RWLOCK_CALL (rdlock (&lock))
>  # define rwlock_wrlock(lock)		RWLOCK_CALL (wrlock (&lock))
>  # define rwlock_unlock(lock)		RWLOCK_CALL (unlock (&lock))
> +# define once(once_control, init_routine)  \
> +  ONCE_CALL (once (&once_control, init_routine))
>  #else
>  /* Eventually we will allow multi-threaded applications to use the
>     libraries.  Therefore we will add the necessary locking although
> @@ -50,6 +55,8 @@
>  # define rwlock_rdlock(lock) ((void) (lock))
>  # define rwlock_wrlock(lock) ((void) (lock))
>  # define rwlock_unlock(lock) ((void) (lock))
> +# define once_define(class,name)
> +# define once(once_control, init_routine)	init_routine()
>  #endif	/* USE_LOCKS */
>  
>  #include <libintl.h>

I made sure that only the newly added lines are changed. That made the
diff a bit easier to review.

This looks good. In the case without locks, the init_routine will of
course get called multiple times, but that should in theory be fine
(these init routines are in generally really simple).

Hopefully we'll eventually end up with just the locked version.

Cheers,

Mark
  

Patch

diff --git a/lib/eu-config.h b/lib/eu-config.h
index 78a5c4fe..feb079db 100644
--- a/lib/eu-config.h
+++ b/lib/eu-config.h
@@ -33,13 +33,18 @@ 
 # include <pthread.h>
 # include <assert.h>
 # define rwlock_define(class,name)	class pthread_rwlock_t name
+# define once_define(class,name)  class pthread_once_t name = PTHREAD_ONCE_INIT
 # define RWLOCK_CALL(call)		\
   ({ int _err = pthread_rwlock_ ## call; assert_perror (_err); })
+# define ONCE_CALL(call)  \
+  ({ int _err = pthread_ ## call; assert_perror (_err); })
 # define rwlock_init(lock)		RWLOCK_CALL (init (&lock, NULL))
 # define rwlock_fini(lock)		RWLOCK_CALL (destroy (&lock))
 # define rwlock_rdlock(lock)		RWLOCK_CALL (rdlock (&lock))
 # define rwlock_wrlock(lock)		RWLOCK_CALL (wrlock (&lock))
 # define rwlock_unlock(lock)		RWLOCK_CALL (unlock (&lock))
+# define once(once_control, init_routine)  \
+  ONCE_CALL (once (&once_control, init_routine))
 #else
 /* Eventually we will allow multi-threaded applications to use the
    libraries.  Therefore we will add the necessary locking although
@@ -50,6 +55,8 @@ 
 # define rwlock_rdlock(lock) ((void) (lock))
 # define rwlock_wrlock(lock) ((void) (lock))
 # define rwlock_unlock(lock) ((void) (lock))
+# define once_define(class,name)
+# define once(once_control, init_routine)	init_routine()
 #endif	/* USE_LOCKS */
 
 #include <libintl.h>