On Thu, Nov 20, 2025 at 03:49:04PM +0100, Marco Elver wrote:

> +/**
> + * context_guard_struct() - declare or define a context guard struct
> + * @name: struct name
> + *
> + * Helper to declare or define a struct type that is also a context guard.
> + *
> + * .. code-block:: c
> + *
> + *   context_guard_struct(my_handle) {
> + *           int foo;
> + *           long bar;
> + *   };
> + *
> + *   struct some_state {
> + *           ...
> + *   };
> + *   // ... declared elsewhere ...
> + *   context_guard_struct(some_state);
> + *
> + * Note: The implementation defines several helper functions that can acquire
> + * and release the context guard.
> + */
> +# define context_guard_struct(name, ...)                                     
>                         \
> +     struct __ctx_guard_type(name) __VA_ARGS__ name;                         
>                         \
> +     static __always_inline void __acquire_ctx_guard(const struct name *var) 
>                         \
> +             __attribute__((overloadable)) __no_context_analysis 
> __acquires_ctx_guard(var) { }       \
> +     static __always_inline void __acquire_shared_ctx_guard(const struct 
> name *var)                  \
> +             __attribute__((overloadable)) __no_context_analysis 
> __acquires_shared_ctx_guard(var) { } \
> +     static __always_inline bool __try_acquire_ctx_guard(const struct name 
> *var, bool ret)           \
> +             __attribute__((overloadable)) __no_context_analysis 
> __try_acquires_ctx_guard(1, var)    \
> +     { return ret; }                                                         
>                         \
> +     static __always_inline bool __try_acquire_shared_ctx_guard(const struct 
> name *var, bool ret)    \
> +             __attribute__((overloadable)) __no_context_analysis 
> __try_acquires_shared_ctx_guard(1, var) \
> +     { return ret; }                                                         
>                         \
> +     static __always_inline void __release_ctx_guard(const struct name *var) 
>                         \
> +             __attribute__((overloadable)) __no_context_analysis 
> __releases_ctx_guard(var) { }       \
> +     static __always_inline void __release_shared_ctx_guard(const struct 
> name *var)                  \
> +             __attribute__((overloadable)) __no_context_analysis 
> __releases_shared_ctx_guard(var) { } \
> +     static __always_inline void __assume_ctx_guard(const struct name *var)  
>                         \
> +             __attribute__((overloadable)) __assumes_ctx_guard(var) { }      
>                         \
> +     static __always_inline void __assume_shared_ctx_guard(const struct name 
> *var)                   \
> +             __attribute__((overloadable)) __assumes_shared_ctx_guard(var) { 
> }                       \
> +     struct name

-typedef struct {
+context_guard_struct(rwlock) {
        struct rwbase_rt        rwbase;
        atomic_t                readers;
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
        struct lockdep_map      dep_map;
 #endif
-} rwlock_t;
+};
+typedef struct rwlock rwlock_t;


I must say I find the 'guard' naming here somewhat confusing. This is
not a guard, but an actual lock type.

Reply via email to