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
//Copyright 2022 #UlinProject Denis Kotlyarov (Денис Котляров)

//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at

//	   http://www.apache.org/licenses/LICENSE-2.0

//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
// limitations under the License.

// #Ulin Project 2022
//

/*!

Convenient and simple macro for code synchronization in multithreading.

# Use

### 1. easy/sync

```rust
use synchronized::synchronized;

/*
	Quick implementation examples of blocking anonymous code.
*/

fn main() {
	// #1 Anonymous inter-threaded synchronized code, 
	// in the case of multi-threading, one thread will wait for the completion of another.
	synchronized! {
		println!("1");
	}
	
	// #2 Anonymous inter-threaded synchronized code, 
	// in the case of multi-threading, one thread will wait for the completion of another.
	synchronized!( println!("1"); );
}
```

### 2. sync_static

```rust
use std::thread::spawn;
use synchronized::synchronized;

/*
	A more illustrative example of code blocking implementation 
	for SAFE mutability of two or more static variables.
	
	The code creates 5 threads that try to change static variables at the same 
	time without synchronization, but the code synchronization block 
	does not allow this code to execute at the same time, which makes 
	the implementation of changing static variables SAFE and even somewhat 
	easier and more beneficial in terms of use and performance.
*/

fn main() {
	// An array of handles to wait for all threads to complete.
	let mut join_all = Vec::new();
	
	// Creation of 5 threads to implement a multi-threaded environment.
	for thread_id in 0..5 {
		let join = spawn(move || {
			// Print the thread number and print the 
			// result of the sync_fn function.
			println!("#[id: {}] {}", thread_id, sync_fn());
		});
		
		join_all.push(join);
	}
	
	// We just wait for all threads to finish and look at stdout.
	for tjoin in join_all {
		let _e = tjoin.join();
	}
}

fn sync_fn() -> usize {
	// Create anonymous synchronized code.
	//
	// The code will never run at the same time. If one thread is executing 
	// this code, the second thread will wait for this code to finish executing.
	let result = synchronized! {
		static mut POINT0: usize = 0;
		static mut POINT1: usize = 0;
		
		unsafe {
			POINT1 = POINT0;
			POINT0 += 1;
			
			POINT1
		}
	};
	
	result
}
```

### 3. sync_let

```rust
use std::thread::spawn;
use synchronized::synchronized;

/*
	An example that describes how to quickly create an anonymous 
	sync with a mutable variable.
	
	This code creates 5 threads, each of which tries to update 
	the `sync_let` variable with data while executing the synchronized anonymous code.
*/

fn main() {
	// An array of handles to wait for all threads to complete.
	let mut join_all = Vec::new();
	
	// Creation of 5 threads to implement a multi-threaded environment.
	for thread_id in 0..5 {
		let join = spawn(move || {
			// Create anonymous synchronized code with one mutable variable `sync_let` and `count`.
			let result = synchronized!(
				(sync_let: String = String::new(), count: usize = 0) {
					// If it's the first thread, 
					// then theoretically `sync_let` is String::new().
					if thread_id == 0 {
						assert_eq!(sync_let.is_empty(), true);
						assert_eq!(count, &0);
					}
					
					// We fill the variable `sync_let` and `count` with data.
					sync_let.push_str(&thread_id.to_string());
					sync_let.push_str(" ");
					
					*count += 1;
					
					sync_let.clone()
				}
			);
			
			// Outputting debug information.
			println!("#[id: {}] {}", thread_id, result);
		});
		
		// In order for our `assert_eq!(sync_let.is_empty());` code to 
		// always run correctly, the first thread should always run first 
		// (this is just for the stability of this example).
		if thread_id == 0 {
			let _e = join.join();
			continue;
		}
		
		join_all.push(join);
	}
	
	// We just wait for all threads to finish and look at stdout.
	for tjoin in join_all {
		let _e = tjoin.join();
	}
}
```

### 4. point

```rust
/*
	An example implementation of synchronized code with 
	one non-anonymous synchronization point.
	
	This example creates a set of anonymous sync codes associated with a 
	single named sync point. Each synchronization code executes in the same 
	way as ordinary anonymous code, but execution occurs simultaneously in a 
	multi-threaded environment in only one of them.
	
	!!! In this example, the assembly requires the `point` feature to be active.
*/

#[cfg( feature = "point" )]
use synchronized::synchronized_point;
#[cfg( feature = "point" )]
use synchronized::synchronized;

#[cfg( not(feature = "point") )]
macro_rules! synchronized_point {
	[ $($unk:tt)* ] => {
		println!("!!! This example requires support for the `point` feature. Run the example with `cargo run --example point --all-features`.");
	};
}

fn main() {
	// A sync point named `COMB_SYNC` to group anonymous code syncs by name.
	synchronized_point! {(COMB_SYNC) {
		static mut POINT: usize = 0;
		println!("GeneralSyncPoint, name_point: {}", COMB_SYNC.get_sync_point_name());
		
		// #1 Anonymous synchronized code that operates on a 
		// single named synchronization point.
		//
		// This code is not executed concurrently in a multi-threaded environment, 
		// one thread is waiting for someone else's code to execute in this part of the code.
		let result0 = synchronized! ((->COMB_SYNC) {
			println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name());
			unsafe {
				POINT += 1;
				
				POINT
			}
		});
		
		// This line of code is not synchronized and can run concurrently on all threads.
		println!("Unsynchronized code");
		
		// #2 Anonymous synchronized code that operates on a 
		// single named synchronization point.
		//
		// Note that `result0` and `result1` cannot be calculated at the same time, 
		// this does not happen because `result0` or `result1` are calculated in 
		// synchronized code with a single sync point of the same name.
		let result1 = synchronized! ((->COMB_SYNC) {
			println!("SyncCode, name_point: {}", COMB_SYNC.get_sync_point_name());
			unsafe {
				POINT += 1;
				
				POINT
			}
		});
		
		// Display debug information.
		println!("result, res0: {:?}, res1: {:?}", result0, result1);
	}}
}
```

# Connection

This section only describes how to choose the default synchronization method for a `synchronized` macro.

### 1. PlugAndPlay (minimal, std)

For a `synchronized` macro, use the primitives implemented by the default `std` library.

```rust,ignore
[dependencies.synchronized]
version = "1.0.2"
default-features = false
features = [
	"std",
	#"point",
	#"get_point_name"
]
```

### 2. PlugAndPlay (minimal, parking_lot)

For a `synchronized` macro, use the primitives implemented by the default `parking_lot` library.

```rust,ignore
[dependencies.synchronized]
version = "1.0.2"
default-features = false
features = [
	"parking_lot",
	#"point",
	#"get_point_name"
]
```

# Additionally inf

1. The macro is an alternative to the `synchronized` keyword from the Java programming language for the Rust programming language with all sorts of extensions.

2. This macro was created by an author who has not written in Java for a very long time, inspired by the memory of the Java programming language (versions 1.5-1.6).

*/

#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]

pub mod core;
#[cfg( feature = "point" )]
#[cfg_attr(docsrs, doc(cfg(feature = "point")))]
mod point;

/// Various synchronization primitives used in the `synchronized` macro.
pub mod beh {
	#[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))]
	#[cfg( feature = "parking_lot" )]
	pub mod pl;
	
	#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
	#[cfg( feature = "std" )]
	pub mod std;
	
	// If locking is not selected, then select `std` by default.
	#[cfg(
		all(
			not(feature = "parking_lot"),
			not(feature = "std"),
		) 
	)]
	pub mod std;
}

/// Convenient and simple macro for code synchronization in multithreading.
/// 
/// ### 1. Anonymous code is synchronized in multi-threaded mode.
/// ```rust
/// use synchronized::synchronized;
/// 
/// synchronized! {
///		static mut POINT0: usize = 0;
///		static mut POINT1: usize = 0;
///		
///		unsafe {
///			POINT1 = POINT0;
///			POINT0 += 1;
///			
///			POINT1
///		}
/// };
/// ```
/// 
/// ### 2. Create anonymous synchronized code with one variable to change `sync_let` and `count`.
/// ```rust
///	use synchronized::synchronized;
///	
///	let result = synchronized!((sync_let: String = String::new(), count: usize = 0) {
///		// We fill the variable `sync_let` with data.
///		sync_let.push_str("1 ");
///	
///		*count += 1;
///		sync_let.clone()
///	});
/// ```
#[macro_export]
macro_rules! synchronized {
	{
		// 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;
		let result = {
			$($all)*
		};
		$(
			drop($v_point_name);
		)*
		
		$crate::__synchronized_beh!(#drop_lock(__lock): $sync_point_name);
		
		result
	}};
	
	{
		// 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)*
	} => {{
		$crate::__synchronized_beh!(#new_point<$ty: [$expr]>: $sync_point_name);
		$crate::synchronized! {
			->$sync_point_name ($v_point_name) $($all)*
		}
	}};
	
	{
		// Named sync block $sync_point_name with mutable values written 
		// comma-separated sync name $v_point_name, type $ty and value when 
		// $expr was created.
		$sync_point_name:ident ( $($v_point_name: ident: $ty: ty = $expr:expr),* $(,)? ) $($all:tt)*
	} => {{
		$crate::__synchronized_beh!(#new_point<($($ty),*): [($($expr),*)]>: $sync_point_name);
		$crate::synchronized! {
			->$sync_point_name ( $($v_point_name),* ) $($all)*
		}
	}};
	
	{
		// 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)*
		}
	}};
	
	{
		// Named sync block named `$v_point_name`.
		($v_point_name: ident) $($all:tt)*
	} => {{
		$crate::synchronized! {
			$v_point_name (__empty_value: () = ()) $($all)*
		}
	}};
	{
		// 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! {
			__ANONYMOUS_SYNC_POINT ( $($v_point_name: $ty = $expr),* ) $($all)*
		}
	}};
	
	{
		// 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! {
			(__empty_value: () = ()) $($all)*
		}
	}};
	
	[] => {}
}

/// Describes the selected default lock for the `synchronized` macro. Currently it is `
#[doc = crate::__synchronized_beh!( #name )]
/// ` by default.
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
	}
};

/// Whether synchronized `point` was enabled in this build.
/// 
/// The `synchronized_point` macro allows you to create shared synchronization 
/// points that prevent certain code from being executed at the same time.
pub const IS_SYNC_POINT_SUPPORT: bool = {
	#[cfg( not(feature = "point") )] {
		false
	}
	
	#[cfg( feature = "point" )] {
		true
	}
};