Skip to content

Commit

Permalink
1. Added minor fixes.
Browse files Browse the repository at this point in the history
2. Added additional comments to the code.
3. Improved `get_point_name` exception functions when building with default value.

*_---
  • Loading branch information
denisandroid committed Aug 17, 2022
1 parent 757c1a7 commit 3665d3c
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 39 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ fn main() {
let result0 = synchronized! ((->COMB_SYNC) {
println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name());
unsafe {
POINT+= 1;
POINT += 1;

POINT
}
Expand All @@ -195,7 +195,7 @@ fn main() {
let result1 = synchronized! ((->COMB_SYNC) {
println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name());
unsafe {
POINT+= 1;
POINT += 1;

POINT
}
Expand Down
4 changes: 2 additions & 2 deletions examples/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() {
let result0 = synchronized! ((->COMB_SYNC) {
println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name());
unsafe {
POINT+= 1;
POINT += 1;

POINT
}
Expand All @@ -44,7 +44,7 @@ fn main() {
let result1 = synchronized! ((->COMB_SYNC) {
println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name());
unsafe {
POINT+= 1;
POINT += 1;

POINT
}
Expand Down
4 changes: 2 additions & 2 deletions examples/point_let.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() {
let result0 = synchronized! ((->COMB_SYNC) {
println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name());
unsafe {
POINT+= 1;
POINT += 1;

POINT
}
Expand All @@ -51,7 +51,7 @@ fn main() {
let result2 = synchronized! ((->COMB_SYNC) {
println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name());
unsafe {
POINT+= 1;
POINT += 1;

POINT
}
Expand Down
5 changes: 3 additions & 2 deletions src/beh/pl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ macro_rules! __synchronized_beh {

// Defining a new synchronization point, usually implements static variables used during synchronization.
{ #new_point<$t: ty : [$t_make:expr]>: $v_point_name:ident } => {
$crate::__make_name!( _HIDDEN_NAME -> stringify!($v_point_name) );
$crate::__make_name!( #new_name<_HIDDEN_NAME>: stringify!($v_point_name) );

#[allow(dead_code)]
static CONST_MUTEX: $crate::beh::pl::Mutex<$t> = $crate::beh::pl::const_mutex(
Expand All @@ -72,7 +72,8 @@ macro_rules! __synchronized_beh {
#[allow(dead_code)]
#[allow(non_upper_case_globals)]
pub static $v_point_name: $crate::core::SyncPoint<
&'static $crate::beh::pl::Mutex<$t>, _HIDDEN_NAME
&'static $crate::beh::pl::Mutex<$t>,
$crate::__make_name!(#get_name<_HIDDEN_NAME>)
> = $crate::core::SyncPoint::new(&CONST_MUTEX);
};
// Creates a new lock on an already created sync point (#new_point)
Expand Down
5 changes: 3 additions & 2 deletions src/beh/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ macro_rules! __synchronized_beh {

// Defining a new synchronization point, usually implements static variables used during synchronization.
{ #new_point<$t: ty : [$t_make:expr]>: $v_point_name:ident } => {
$crate::__make_name!( _HIDDEN_NAME -> stringify!($v_point_name) );
$crate::__make_name!( #new_name<_HIDDEN_NAME>: stringify!($v_point_name) );

#[allow(dead_code)]
static CONST_MUTEX: $crate::beh::std::Mutex<$t> = $crate::beh::std::Mutex::new(
Expand All @@ -77,7 +77,8 @@ macro_rules! __synchronized_beh {
#[allow(dead_code)]
#[allow(non_upper_case_globals)]
pub static $v_point_name: $crate::core::SyncPoint<
&'static $crate::beh::std::Mutex<$t>, _HIDDEN_NAME
&'static $crate::beh::std::Mutex<$t>,
$crate::__make_name!(#get_name<_HIDDEN_NAME>)
> = $crate::core::SyncPoint::new(&CONST_MUTEX);
};
// Creates a new lock on an already created sync point (#new_point)
Expand Down
49 changes: 36 additions & 13 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub struct SyncPoint<T, N> {
mutex_builder: T,

/// The phantom used to implement `SyncPointName`.
_pp2: PhantomData<N>,
phantom_name: PhantomData<N>,
}

impl<T, N> SyncPoint<T, N> where T: SyncPointBeh {
Expand All @@ -67,7 +67,7 @@ impl<T, N> SyncPoint<T, N> where T: SyncPointBeh {
Self {
mutex_builder,

_pp2: PhantomData,
phantom_name: PhantomData,
}
}

Expand Down Expand Up @@ -102,31 +102,48 @@ impl<T, N> SyncPoint<T, N> where T: SyncPointBeh {

#[cfg_attr(docsrs, doc(cfg(feature = "get_point_name")))]
#[cfg( feature = "get_point_name" )]
impl<T, N> SyncPoint<T, N> where T: SyncPointBeh, N: SyncPointName {
impl<T, N> SyncPoint<T, N> where N: SyncPointName {
/// Getting the sync point name
#[cfg_attr(docsrs, doc(cfg(feature = "get_point_name")))]
#[cfg( feature = "get_point_name" )]
#[inline(always)]
pub const fn get_sync_point_name(&self) -> &'static str {
N::NAME
}

/// Getting the sync point name
#[cfg_attr(docsrs, doc(cfg(feature = "get_point_name")))]
#[cfg( feature = "get_point_name" )]
#[inline(always)]
pub const fn get_name() -> &'static str {
N::NAME
}
}

#[cfg( not(feature = "get_point_name") )]
impl<T, N> SyncPoint<T, N> {
/// Getting the sync point name
///
/// Warning since `get_point_name` is disabled,
/// "<unknown>" will always be returned.
#[inline(always)]
pub const fn get_sync_point_name(&self) -> &'static str {
"<unknown>"
}

/// Getting the sync point name
///
/// Warning since `get_point_name` is disabled,
/// "<empty>" will always be returned.
#[inline(always)]
pub const fn get_name() -> &'static str {
"<unknown>"
}
}

/// Generic macro that generates and implements a `SyncPointName`.
#[cfg_attr(docsrs, doc(cfg(feature = "get_point_name")))]
#[cfg( feature = "get_point_name" )]
#[macro_export]
#[doc(hidden)]
macro_rules! __make_name {
[ $name:ident -> $expr:expr $(; $($unk:tt)*)? ] => {
[ #new_name<$name:ident>: $expr:expr $(; $($unk:tt)*)? ] => {
/// An automatically generated enum for
/// the type-based implementation of SyncPointName.
pub enum $name {}
Expand All @@ -142,6 +159,8 @@ macro_rules! __make_name {
)?
};

[ #get_name<$name:ident> ] => { $name };

[] => {}
}

Expand All @@ -151,11 +170,15 @@ macro_rules! __make_name {
#[macro_export]
#[doc(hidden)]
macro_rules! __make_name {
[ $name:ident -> $expr:expr $(; $($unk:tt)*)? ] => {
/// An automatically generated enum for
/// the type-based implementation of SyncPointName.
/// (Just a stub.)
pub enum $name {}
// The `get_point_name` function is disabled,
// only a stub is used.
[ #new_name<$name:ident>: $expr:expr $(; $($unk:tt)*)? ] => {};

// The `get_point_name` function is disabled,
// only a stub is used.
[ #get_name<$name:ident> ] => {
()
};

[] => {}
}
107 changes: 91 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ fn main() {
let result0 = synchronized! ((->COMB_SYNC) {
println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name());
unsafe {
POINT+= 1;
POINT += 1;
POINT
}
Expand All @@ -208,7 +208,7 @@ fn main() {
let result1 = synchronized! ((->COMB_SYNC) {
println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name());
unsafe {
POINT+= 1;
POINT += 1;
POINT
}
Expand Down Expand Up @@ -306,7 +306,13 @@ pub mod beh {
/// ```
#[macro_export]
macro_rules! synchronized {
{ ->$sync_point_name:ident ($v_point_name: ident) $($all:tt)* } => {{ // synchronized point
{
// Named `$sync_point_name` synchronized block with mutable value
// of synchronized name `$v_point_name`, type `$ty` and value when
// `$expr` is created.
// (Use only with `synchronized_point`.)
->$sync_point_name:ident ($v_point_name: ident) $($all:tt)*
} => {{ // synchronized point
$crate::__synchronized_beh!(#new_lock(__lock): $sync_point_name);

let ref mut $v_point_name = *__lock;
Expand All @@ -320,30 +326,65 @@ macro_rules! synchronized {
result
}};

{ $sync_point_name:ident ($v_point_name: ident: $ty: ty = $expr:expr ) $($all:tt)* } => {{ // synchronized point
{
// Named `$sync_point_name` synchronized block with mutable value
// of synchronized name `$v_point_name`, type `$ty` and value when
// `$expr` is created.
$sync_point_name:ident ($v_point_name: ident: $ty: ty = $expr:expr ) $($all:tt)*
} => {{ // synchronized point
$crate::__synchronized_beh!(#new_point<$ty: [$expr]>: $sync_point_name);
$crate::synchronized! {
->$sync_point_name ($v_point_name) $($all)*
}
}};

{ (->$v_point_name: ident) $($all:tt)* } => {{ // sync point
{
// Named sync block named `$v_point_name`.
// (Use only with `synchronized_point`.)
(->$v_point_name: ident) $($all:tt)*
} => {{ // sync point
$crate::synchronized! {
->$v_point_name (__empty_value) $($all)*
}
}};

{ ($v_point_name: ident) $($all:tt)* } => {{
{
// Named sync block named `$v_point_name`.
($v_point_name: ident) $($all:tt)*
} => {{
$crate::synchronized! {
$v_point_name (__empty_value: () = ()) $($all)*
}
}};
{ ( $v_point_name: ident: $ty: ty = $expr:expr ) $($all:tt)* } => {{ // sync value
{
// Anonymous synchronized block with mutable synchronized name value
// `$v_point_name`, type `$ty` and value when `$expr` is created.
( $v_point_name: ident: $ty: ty = $expr:expr ) $($all:tt)*
} => {{ // sync value
$crate::synchronized! {
__PL_SYNC_POINT ($v_point_name: $ty = $expr) $($all)*
}
}};
{ $($all:tt)* } => {{ // nohead synchronized block

{
// COMPILE_ERROR
$(->$_ident1:ident)? /* OR */ $($_ident2:ident)? ($($unk_in:tt)*) $($unk:tt)+
} => {
compile_error!(concat!(
"Error writing macro `synchronized`, incode: ",
$(stringify!(->$_ident1),)?
$(stringify!($_ident2),)?

stringify!(($($unk_in)*)),

stringify!($($unk)+),
));
};

{
// Anonymous synchronized block
$($all:tt)*
} => {{ // nohead synchronized block
$crate::synchronized! {
__PL_SYNC_POINT (__empty_value: () = ()) $($all)*
}
Expand All @@ -368,7 +409,7 @@ macro_rules! synchronized {
/// let result0 = synchronized! ((->COMB_SYNC) {
/// println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name());
/// unsafe {
/// POINT+= 1;
/// POINT += 1;
///
/// POINT
/// }
Expand All @@ -381,7 +422,7 @@ macro_rules! synchronized {
/// let result1 = synchronized! ((->COMB_SYNC) {
/// println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name());
/// unsafe {
/// POINT+= 1;
/// POINT += 1;
///
/// POINT
/// }
Expand All @@ -401,7 +442,7 @@ macro_rules! synchronized {
/// let result0 = synchronized! ((->COMB_SYNC) {
/// println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name());
/// unsafe {
/// POINT+= 1;
/// POINT += 1;
///
/// POINT
/// }
Expand All @@ -423,15 +464,21 @@ macro_rules! synchronized {
/// let result2 = synchronized! ((->COMB_SYNC) {
/// println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name());
/// unsafe {
/// POINT+= 1;
/// POINT += 1;
///
/// POINT
/// }
/// });
/// }}
#[macro_export]
macro_rules! synchronized_point {
{ $sync_point_name:ident ($ty: ty = $expr:expr ) {$($all:tt)*} $(; $($unk:tt)*)? } => {
{
// Named sync point named `$sync_point_name`.
//
// With a mutable synchronized variable of type `$ty`
// with a default value of `$expr`.
$sync_point_name:ident ($ty: ty = $expr:expr ) {$($all:tt)*} $(; $($unk:tt)*)?
} => {
{
$crate::__synchronized_beh!(#new_point<$ty: [$expr]>: $sync_point_name);

Expand All @@ -442,18 +489,46 @@ macro_rules! synchronized_point {
$($unk)*
})?
};
{ ($sync_point_name:ident) {$($all:tt)*} $(; $($unk:tt)*)? } => {
{
// Named sync point named `$sync_point_name`
($sync_point_name:ident) {$($all:tt)*} $(; $($unk:tt)*)?
} => {
$crate::synchronized_point! {
$sync_point_name (() = ()) { $($all)* }

$(; $($unk)*)?
}
};

{
// COMPILE_ERROR

$($unk:tt)+
} => {
compile_error!(concat!(
"Error writing macro `synchronized_point`, incode: ",
stringify!($($unk)+),
));
};

[] => {}
}

/// Describes the selected default lock for the `synchronized` macro. Currently it is `
#[doc = crate::__synchronized_beh!{ #name }]
#[doc = crate::__synchronized_beh!( #name )]
/// ` by default.
pub const CURRENT_DEF_BEH: &'static str = crate::__synchronized_beh!{ #name };
pub const CURRENT_DEF_BEH: &'static str = crate::__synchronized_beh!( #name );

/// Whether `get_point_name` was enabled in this build.
///
/// The `get_point_name` feature determines whether the connection
/// label name can be determined at run time.
pub const IS_GET_POINT_NAME_SUPPORT: bool = {
#[cfg( not(feature = "get_point_name") )] {
false
}

#[cfg( feature = "get_point_name" )] {
true
}
};

0 comments on commit 3665d3c

Please sign in to comment.