thread-share 0.1.6

A Rust library for safe and efficient data sharing between threads with zero-copy operations, change detection, and enhanced thread management.
Documentation
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//! # Macros Module
//!
//! This module provides convenient macros for creating and managing thread-safe
//! data structures with minimal boilerplate code.
//!
//! ## ๐Ÿš€ Overview
//!
//! The macros module contains several utility macros that simplify common
//! operations when working with ThreadShare structures:
//!
//! - **`share!`** - Creates `ThreadShare<T>` instances with automatic type inference
//! - **`simple_share!`** - Creates `SimpleShare<T>` instances for basic use cases
//! - **`enhanced_share!`** - Creates `EnhancedThreadShare<T>` instances
//! - **`spawn_workers!`** - Spawns multiple threads with single macro call, returns WorkerManager
//! - **`spawn_threads!`** - Alternative thread spawning macro for ThreadManager
//! - **`thread_setup!`** - Sets up thread management with shared data, returns ThreadManager
//!
//! ## Key Benefits
//!
//! ### ๐ŸŽฏ Simplified Creation
//! ```rust
//! use thread_share::{share, ThreadShare};
//!
//! // Without macros
//! let counter = ThreadShare::new(0);
//! let message = ThreadShare::new(String::from("Hello"));
//! let data = ThreadShare::new(vec![1, 2, 3]);
//!
//! // With macros
//! let counter = share!(0);
//! let message = share!(String::from("Hello"));
//! let data = share!(vec![1, 2, 3]);
//! ```
//!
//! ### ๐Ÿงต Enhanced Thread Management
//! ```rust
//! use thread_share::{enhanced_share, spawn_workers};
//!
//! let data = enhanced_share!(vec![1, 2, 3]);
//!
//! // Single macro call spawns all threads
//! spawn_workers!(data, {
//!     processor: |data| { data.update(|v| v.sort()); },
//!     validator: |data| { assert!(data.get().is_sorted()); }
//! });
//!
//! data.join_all().expect("Failed to join");
//! ```
//!
//! ## Macro Reference
//!
//! ### share! Macro
//!
//! Creates a `ThreadShare<T>` instance with automatic type inference.
//!
//! **Syntax**: `share!(expression)`
//!
//! **Example**:
//! ```rust
//! use thread_share::share;
//!
//! let counter = share!(0);                    // ThreadShare<i32>
//! let message = share!("Hello");              // ThreadShare<&str>
//! let data = share!(vec![1, 2, 3]);          // ThreadShare<Vec<i32>>
//! // let user = share!(User { id: 1, name: "Alice" }); // ThreadShare<User>
//! ```
//!
//! ### simple_share! Macro
//!
//! Creates a `SimpleShare<T>` instance for basic data sharing without change detection.
//!
//! **Syntax**: `simple_share!(expression)`
//!
//! **Example**:
//! ```rust
//! use thread_share::simple_share;
//!
//! let counter = simple_share!(0);             // SimpleShare<i32>
//! let flag = simple_share!(false);            // SimpleShare<bool>
//! let data = simple_share!(String::new());    // SimpleShare<String>
//! ```
//!
//! ### enhanced_share! Macro
//!
//! Creates an `EnhancedThreadShare<T>` instance with automatic thread management.
//!
//! **Syntax**: `enhanced_share!(expression)`
//!
//! **Example**:
//! ```rust
//! use thread_share::enhanced_share;
//!
//! let data = enhanced_share!(vec![1, 2, 3]);
//!
//! // Now you can use enhanced thread management
//! data.spawn("worker", |data| {
//!     data.update(|v| v.push(4));
//! });
//!
//! data.join_all().expect("Failed to join");
//! ```
//!
//! ### spawn_workers! Macro
//!
//! Spawns multiple threads with a single macro call, each with a descriptive name.
//!
//! **Syntax**: `spawn_workers!(data, { name: closure, ... })`
//!
//! **Example**:
//! ```rust
//! use thread_share::{enhanced_share, spawn_workers};
//!
//! let data = enhanced_share!(0);
//!
//! spawn_workers!(data, {
//!     incrementer: |data| {
//!         for _ in 0..100 {
//!             data.update(|x| *x += 1);
//!         }
//!     },
//!     monitor: |data| {
//!         for _ in 0..10 {
//!             std::thread::sleep(std::time::Duration::from_millis(100));
//!             println!("Value: {}", data.get());
//!         }
//!     }
//! });
//!
//! data.join_all().expect("Failed to join");
//! ```
//!
//! ### spawn_threads! Macro
//!
//! Alternative macro for spawning threads with different syntax.
//!
//! **Syntax**: `spawn_threads!(data, [closure1, closure2, ...])`
//!
//! **Example**:
//! ```rust
//! use thread_share::{enhanced_share, spawn_workers};
//!
//! let data = enhanced_share!(String::from("Hello"));
//!
//! spawn_workers!(data, {
//!     worker1: |data| { data.update(|s| s.push_str(" World")); },
//!     worker2: |data| { data.update(|s| s.push_str("!")); }
//! });
//!
//! data.join_all().expect("Failed to join");
//! ```
//!
//! ### thread_setup! Macro
//!
//! Sets up thread management with shared data and multiple worker functions.
//!
//! **Syntax**: `thread_setup!(shared_data, { name: function, ... })`
//!
//! **Example**:
//! ```rust
//! use thread_share::{share, thread_setup};
//!
//! let shared_data = share!(vec![1, 2, 3]);
//!
//! let manager = thread_setup!(shared_data, {
//!     worker1: |data| { data.update(|v| v.push(4)); },
//!     worker2: |data| { data.update(|v| v.push(5)); }
//! });
//!
//! manager.join_all().expect("Failed to join");
//! ```
//!
//! ## Type Inference
//!
//! All macros provide automatic type inference, so you don't need to specify
//! generic types explicitly:
//!
//! ```rust
//! use thread_share::share;
//!
//! // Type is automatically inferred as ThreadShare<i32>
//! let counter = share!(0);
//!
//! // Type is automatically inferred as ThreadShare<Vec<String>>
//! let data = share!(vec![String::from("a"), String::from("b")]);
//!
//! // Type is automatically inferred as ThreadShare<Option<bool>>
//! let flag = share!(Some(true));
//! ```
//!
//! ## Error Handling
//!
//! Basic error handling with standard threads:
//!
//! ```rust
//! use thread_share::share;
//!
//! let data = share!(0);
//! let clone = data.clone();
//!
//! // Spawn thread with error handling
//! let handle = std::thread::spawn(move || {
//!     clone.update(|x| *x = *x + 1);
//! });
//!
//! // Handle join errors
//! if let Err(e) = handle.join() {
//!     eprintln!("Thread execution failed: {:?}", e);
//! }
//! ```
//!
//! ## Best Practices
//!
//! 1. **Use descriptive names** for spawned threads to aid debugging
//! 2. **Keep closures focused** on single responsibilities
//! 3. **Handle errors gracefully** from thread spawning and joining
//! 4. **Prefer `spawn_workers!`** over manual thread management
//! 5. **Use `enhanced_share!`** when you need automatic thread management
//!
//! ## Performance Considerations
//!
//! - **Macro expansion**: Happens at compile time, no runtime overhead
//! - **Type inference**: Compiler optimizations apply normally
//! - **Thread spawning**: Same performance as manual `thread::spawn`
//! - **Memory usage**: No additional overhead from macro usage
//!
//! ## Integration with Other Modules
//!
//! ```

/// Macro for creating ThreadShare with automatic type inference
///
/// This macro creates a `ThreadShare<T>` instance with automatic type inference.
/// It's the most commonly used macro for creating thread-safe shared data.
///
/// ## Syntax
///
/// `share!(expression)`
///
/// ## Arguments
///
/// * `expression` - The data to wrap in ThreadShare
///
/// ## Returns
///
/// A new `ThreadShare<T>` instance where `T` is inferred from the expression.
///
/// ## Example
///
/// ```rust
/// use thread_share::share;
///
/// // Basic types
/// let counter = share!(0);                    // ThreadShare<i32>
/// let message = share!("Hello");              // ThreadShare<&str>
/// let flag = share!(true);                    // ThreadShare<bool>
///
/// // Complex types
/// let data = share!(vec![1, 2, 3]);          // ThreadShare<Vec<i32>>
/// // let user = share!(User { id: 1, name: "Alice" }); // ThreadShare<User>
///
/// // Expressions
/// let result = share!(10 + 20);               // ThreadShare<i32>
/// let computed = share!(vec![1, 2, 3].len()); // ThreadShare<usize>
/// ```
///
/// ## Type Inference
///
/// The macro automatically infers the generic type `T` from the expression:
///
/// ```rust
/// use thread_share::share;
///
/// // No need to specify types explicitly
/// let counter: thread_share::ThreadShare<i32> = share!(0);
/// let data: thread_share::ThreadShare<Vec<String>> = share!(vec![String::new()]);
/// ```
#[macro_export]
macro_rules! share {
    ($data:expr) => {
        $crate::ThreadShare::new($data)
    };
}

/// Macro for creating SimpleShare
///
/// This macro creates a `SimpleShare<T>` instance for basic data sharing
/// without change detection capabilities.
///
/// ## Syntax
///
/// `simple_share!(expression)`
///
/// ## Arguments
///
/// * `expression` - The data to wrap in SimpleShare
///
/// ## Returns
///
/// A new `SimpleShare<T>` instance where `T` is inferred from the expression.
///
/// ## Example
///
/// ```rust
/// use thread_share::simple_share;
///
/// // Basic types
/// let counter = simple_share!(0);             // SimpleShare<i32>
/// let message = simple_share!("Hello");       // SimpleShare<&str>
/// let flag = simple_share!(false);            // SimpleShare<bool>
///
/// // Complex types
/// let data = simple_share!(vec![1, 2, 3]);   // SimpleShare<Vec<i32>>
/// // let user = simple_share!(User { id: 1, name: "Bob" }); // SimpleShare<User>
/// ```
///
/// ## When to Use
///
/// Use `simple_share!` when you need:
/// - Basic data sharing without change detection
/// - Minimal overhead and complexity
/// - Simple producer-consumer patterns
/// - Learning and prototyping
///
/// Use `share!` when you need:
/// - Change detection and waiting mechanisms
/// - Complex synchronization patterns
/// - Maximum flexibility and features
#[macro_export]
macro_rules! simple_share {
    ($data:expr) => {
        $crate::SimpleShare::new($data)
    };
}

/// Macro for creating enhanced thread share with automatic thread management
///
/// This macro creates an `EnhancedThreadShare<T>` instance that provides
/// automatic thread management capabilities.
///
/// ## Syntax
///
/// `enhanced_share!(expression)`
///
/// ## Arguments
///
/// * `expression` - The data to wrap in EnhancedThreadShare
///
/// ## Returns
///
/// A new `EnhancedThreadShare<T>` instance where `T` is inferred from the expression.
///
/// ## Example
///
/// ```rust
/// use thread_share::enhanced_share;
///
/// // Basic types
/// let counter = enhanced_share!(0);                    // EnhancedThreadShare<i32>
/// let message = enhanced_share!(String::from("Hello")); // EnhancedThreadShare<String>
/// let data = enhanced_share!(vec![1, 2, 3]);          // EnhancedThreadShare<Vec<i32>>
/// ```
///
/// ## Key Features
///
/// - **Automatic Thread Management**: Spawn threads with a single method call
/// - **Built-in Thread Tracking**: Monitor active thread count and status
/// - **Automatic Thread Joining**: Wait for all threads to complete with `join_all()`
/// - **Thread Naming**: Give meaningful names to threads for debugging
/// - **All ThreadShare Features**: Inherits all capabilities from `ThreadShare<T>`
///
/// ## When to Use
///
/// Use `enhanced_share!` when you need:
/// - Complex multi-threaded applications
/// - Automatic thread lifecycle management
/// - Thread monitoring and debugging
/// - High-level thread coordination
///
/// Use `share!` when you need:
/// - Simple data sharing without thread management
/// - Manual thread control
/// - Minimal overhead
#[macro_export]
macro_rules! enhanced_share {
    ($data:expr) => {
        $crate::enhanced::EnhancedThreadShare::new($data)
    };
}

/// Macro for simplified multi-threaded setup with WorkerManager
///
/// This macro spawns multiple threads and returns a `WorkerManager` instance
/// that allows you to control individual workers: pause, resume, stop, and monitor them.
///
/// ## Syntax
///
/// `spawn_workers!(shared_data, { name: closure, ... })`
///
/// ## Arguments
///
/// * `shared_data` - An `EnhancedThreadShare<T>` instance to share between workers
/// * `{ name: closure, ... }` - Named closures for each worker thread
///
/// ## Returns
///
/// A `WorkerManager` instance that provides methods to control workers:
/// - `add_worker(name, handle)` - Add a new worker programmatically
/// - `pause_worker(name)` - Mark a worker for pause
/// - `resume_worker(name)` - Resume a paused worker
/// - `remove_worker(name)` - Remove worker from tracking
/// - `get_worker_names()` - Get list of all worker names
/// - `active_workers()` - Get count of active workers
/// - `join_all()` - Wait for all workers to complete
///
/// ## Example
///
/// ```rust
/// use thread_share::{enhanced_share, spawn_workers};
///
/// let data = enhanced_share!(vec![1, 2, 3]);
///
/// // Spawn workers and get manager
/// let manager = spawn_workers!(data, {
///     sorter: |data| {
///         data.update(|v| v.sort());
///     },
///     validator: |data| {
///         assert!(data.get().is_sorted());
///     }
/// });
///
/// // Control workers
/// println!("Workers: {:?}", manager.get_worker_names());
/// println!("Active: {}", manager.active_workers());
///
/// // Wait for completion
/// manager.join_all().expect("Workers failed");
/// ```
///
/// ## Worker Management
///
/// The `WorkerManager` allows fine-grained control over individual workers:
///
/// ```rust
/// use thread_share::{enhanced_share, spawn_workers};
///
/// let data = enhanced_share!(vec![1, 2, 3]);
/// let manager = spawn_workers!(data, {
///     sorter: |data| { /* work */ },
///     validator: |data| { /* work */ }
/// });
///
/// // Pause a specific worker
/// let _ = manager.pause_worker("sorter");
///
/// // Resume a worker
/// let _ = manager.resume_worker("sorter");
///
/// // Add a new worker programmatically
/// let handle = std::thread::spawn(|| { /* work */ });
/// let _ = manager.add_worker("new_worker", handle);
///
/// // Remove from tracking
/// let _ = manager.remove_worker("sorter");
/// ```
///
/// ## Requirements
///
/// - The shared data must be an `EnhancedThreadShare<T>` instance
/// - Each closure must implement `FnOnce(ThreadShare<T>) + Send + 'static`
/// - The type `T` must implement `Send + Sync + 'static`
///
/// ## Performance
///
/// - **Thread Spawning**: Minimal overhead over standard `thread::spawn`
/// - **Worker Management**: Constant-time operations for most management functions
/// - **Memory Usage**: Small overhead for worker tracking structures
/// - **Scalability**: Efficient for up to hundreds of workers
#[macro_export]
macro_rules! spawn_workers {
    ($shared:expr, { $($name:ident: $func:expr),* }) => {
        {
            $(
                $shared.spawn(stringify!($name), $func).expect(&format!("Failed to spawn {}", stringify!($name)));
            )*
            $crate::worker_manager::WorkerManager::new_with_threads($shared.get_threads())
        }
    };
}

/// Macro for simplified thread spawning
///
/// This macro simplifies spawning multiple threads with the same shared data.
/// It creates a vector of thread configurations and calls `spawn_multiple`.
///
/// ## Syntax
///
/// `spawn_threads!(manager, shared_data, { name: function, ... })`
///
/// ## Arguments
///
/// * `manager` - The ThreadManager instance
/// * `shared_data` - The ThreadShare<T> data to share
/// * `{ name: function, ... }` - Named thread functions
///
/// ## Returns
///
/// `Result<(), String>` from `spawn_multiple`.
///
/// ## Performance
///
/// - **Compile-time expansion**: No runtime overhead
/// - **Efficient spawning**: Same performance as manual `spawn_multiple`
/// - **Type safety**: Compile-time type checking
/// - **Memory usage**: No additional allocations
#[macro_export]
macro_rules! spawn_threads {
    ($manager:expr, $shared_data:expr, { $($name:ident: $func:expr),* }) => {
        {
            let configs = vec![
                $(
                    (stringify!($name), $func)
                ),*
            ];
            $manager.spawn_multiple($shared_data, configs)
        }
    };
}

/// Macro for creating a complete thread setup
///
/// This macro creates a ThreadManager and spawns multiple threads with shared data
/// in a single call, returning the manager for further control.
///
/// ## Syntax
///
/// `thread_setup!(shared_data, { name: function, ... })`
///
/// ## Arguments
///
/// * `shared_data` - The ThreadShare<T> data to share
/// * `{ name: function, ... }` - Named thread functions
///
/// ## Returns
///
/// A `ThreadManager` instance that can be used to control the spawned threads.
///
/// ## Example
///
/// ```rust
/// use thread_share::{share, thread_setup};
///
/// let data = share!(0);
///
/// let manager = thread_setup!(data, {
///     counter: |data| { data.update(|x| *x += 1); },
///     monitor: |data| { println!("Value: {}", data.get()); }
/// });
///
/// // Wait for all threads
/// manager.join_all().expect("Failed to join threads");
/// ```
#[macro_export]
macro_rules! thread_setup {
    ($shared_data:expr, { $($name:ident: $func:expr),* }) => {
        {
            let manager = $crate::thread_pool::ThreadManager::new();
            $(
                manager.spawn(stringify!($name), $shared_data.clone(), $func)
                    .expect(&format!("Failed to spawn {}", stringify!($name)));
            )*
            manager
        }
    };
}