ruchy 4.2.1

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
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
# Ruchy Concurrency Model

## Design Philosophy

**Core Principle**: Ruchy uses **exactly the same concurrency primitives as Rust** with **zero abstractions**.

- **1:1 Mapping**: Ruchy syntax transpiles directly to Rust concurrency APIs
-**Zero Overhead**: No runtime wrappers or indirection
-**Memory Safe**: All generated code is thread-safe and data-race-free
-**Zero Unsafe**: Transpiler NEVER generates `unsafe` blocks (GitHub Issue #132)

## ZERO UNSAFE POLICY

**SACRED RULE**: The Ruchy transpiler MUST NEVER generate unsafe Rust code.

### Forbidden Patterns

❌ **NEVER Generate**:
```rust
// ❌ FORBIDDEN: static mut
static mut COUNTER: i32 = 0;

// ❌ FORBIDDEN: unsafe blocks
unsafe {
    COUNTER += 1;
}

// ❌ FORBIDDEN: raw pointers without wrappers
let ptr: *mut i32 = &mut value;
*ptr = 42;
```

### Required Safe Patterns

✅ **ALWAYS Generate**:
```rust
// ✅ CORRECT: LazyLock<Mutex<T>> for globals
static COUNTER: LazyLock<Mutex<i32>> = LazyLock::new(|| Mutex::new(0));
*COUNTER.lock().unwrap() += 1;

// ✅ CORRECT: Arc<Mutex<T>> for shared ownership
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);

// ✅ CORRECT: Channel-based message passing
let (tx, rx) = mpsc::channel();
tx.send(value).unwrap();
```

## Concurrency Primitives

### 1. Threads (`std::thread`)

**Ruchy Syntax** (same as Rust):
```rust
// Spawn a thread
let handle = std::thread::spawn(|| {
    println!("Hello from thread!");
    42
});

// Wait for thread to complete
let result = handle.join().unwrap();  // Returns 42
```

**Transpiled Output** (identical):
```rust
let handle = std::thread::spawn(|| {
    println!("Hello from thread!");
    42
});
let result = handle.join().unwrap();
```

**Thread-Safe Shared State**:
```rust
use std::sync::{Arc, Mutex};

let counter = Arc::new(Mutex::new(0));
let mut handles = vec![];

for _ in 0..10 {
    let counter = Arc::clone(&counter);
    let handle = std::thread::spawn(move || {
        let mut num = counter.lock().unwrap();
        *num += 1;
    });
    handles.push(handle);
}

for handle in handles {
    handle.join().unwrap();
}

println!("Result: {}", *counter.lock().unwrap());  // Prints: 10
```

### 2. Async/Await (Tokio)

**Async Functions**:
```rust
async fn fetch_url(url: String) -> Result<String, reqwest::Error> {
    let response = reqwest::get(url).await?;
    response.text().await
}
```

**Async Main**:
```rust
#[tokio::main]
async fn main() {
    let body = fetch_url("https://www.rust-lang.org").await;
    println!("Body: {:?}", body);
}
```

**Concurrent Async Tasks**:
```rust
use tokio::task;

let tasks: Vec<_> = urls.into_iter()
    .map(|url| task::spawn(fetch_url(url)))
    .collect();

for task in tasks {
    match task.await {
        Ok(Ok(body)) => println!("Got: {}", body),
        Ok(Err(e)) => eprintln!("Error: {}", e),
        Err(e) => eprintln!("Task error: {}", e),
    }
}
```

### 3. Message Passing (Channels)

**MPSC (Multi-Producer Single-Consumer)**:
```rust
use std::sync::mpsc;

// Create channel
let (tx, rx) = mpsc::channel();

// Spawn producer thread
std::thread::spawn(move || {
    tx.send("Hello from thread").unwrap();
});

// Receive message
let msg = rx.recv().unwrap();
println!("{}", msg);
```

**Async Channels (Tokio)**:
```rust
use tokio::sync::mpsc;

// Create async channel with buffer
let (tx, mut rx) = mpsc::channel(100);

// Spawn producer task
tokio::spawn(async move {
    for i in 0..10 {
        tx.send(i).await.unwrap();
    }
});

// Receive messages
while let Some(value) = rx.recv().await {
    println!("Got: {}", value);
}
```

**Broadcast Channels**:
```rust
use tokio::sync::broadcast;

let (tx, mut rx1) = broadcast::channel(16);
let mut rx2 = tx.subscribe();

tokio::spawn(async move {
    assert_eq!(rx1.recv().await.unwrap(), 10);
    assert_eq!(rx1.recv().await.unwrap(), 20);
});

tokio::spawn(async move {
    assert_eq!(rx2.recv().await.unwrap(), 10);
    assert_eq!(rx2.recv().await.unwrap(), 20);
});

tx.send(10).unwrap();
tx.send(20).unwrap();
```

### 4. Atomics (Lock-Free)

**Atomic Counter**:
```rust
use std::sync::atomic::{AtomicUsize, Ordering};

let counter = Arc::new(AtomicUsize::new(0));
let mut handles = vec![];

for _ in 0..10 {
    let counter = Arc::clone(&counter);
    let handle = std::thread::spawn(move || {
        for _ in 0..1000 {
            counter.fetch_add(1, Ordering::SeqCst);
        }
    });
    handles.push(handle);
}

for handle in handles {
    handle.join().unwrap();
}

println!("Result: {}", counter.load(Ordering::SeqCst));  // 10000
```

**Atomic Bool (Flag)**:
```rust
use std::sync::atomic::{AtomicBool, Ordering};

let stop_flag = Arc::new(AtomicBool::new(false));
let flag_clone = Arc::clone(&stop_flag);

std::thread::spawn(move || {
    while !flag_clone.load(Ordering::Relaxed) {
        // Do work...
        std::thread::sleep(Duration::from_millis(100));
    }
});

// Later: signal thread to stop
stop_flag.store(true, Ordering::Relaxed);
```

### 5. Synchronization Primitives

**Mutex (Exclusive Access)**:
```rust
use std::sync::Mutex;

let data = Mutex::new(vec![1, 2, 3]);

{
    let mut d = data.lock().unwrap();
    d.push(4);
}  // Lock automatically released

// Read the data
let d = data.lock().unwrap();
assert_eq!(*d, vec![1, 2, 3, 4]);
```

**RwLock (Read-Heavy Workloads)**:
```rust
use std::sync::RwLock;

let config = RwLock::new(HashMap::new());

// Multiple readers can read simultaneously
{
    let r1 = config.read().unwrap();
    let r2 = config.read().unwrap();
    // Both can read at same time
}

// Single writer has exclusive access
{
    let mut w = config.write().unwrap();
    w.insert("key", "value");
}
```

**Barrier (Synchronization Point)**:
```rust
use std::sync::{Arc, Barrier};

let barrier = Arc::new(Barrier::new(3));
let mut handles = vec![];

for i in 0..3 {
    let c = Arc::clone(&barrier);
    handles.push(std::thread::spawn(move || {
        println!("Thread {} before barrier", i);
        c.wait();  // Wait for all threads
        println!("Thread {} after barrier", i);
    }));
}

for handle in handles {
    handle.join().unwrap();
}
```

**Condvar (Condition Variable)**:
```rust
use std::sync::{Arc, Mutex, Condvar};

let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair_clone = Arc::clone(&pair);

std::thread::spawn(move || {
    let (lock, cvar) = &*pair_clone;
    let mut ready = lock.lock().unwrap();
    *ready = true;
    cvar.notify_one();
});

let (lock, cvar) = &*pair;
let mut ready = lock.lock().unwrap();
while !*ready {
    ready = cvar.wait(ready).unwrap();
}
```

## Global Mutable State

**Problem**: How to share mutable state across functions?

### ❌ Wrong Solution (Unsafe)
```rust
// ❌ FORBIDDEN: This generates data-race-prone code
static mut COUNTER: i32 = 0;

fn increment() {
    unsafe {
        COUNTER += 1;  // Data race if called from multiple threads!
    }
}
```

### ✅ Correct Solution (Thread-Safe)

**Ruchy Code**:
```rust
let mut global_counter = 0;

fn increment() {
    global_counter = global_counter + 1;
}

fn main() {
    increment();
    println!("{}", global_counter);
}
```

**Transpiled Code** (GitHub Issue #132 fix):
```rust
use std::sync::{LazyLock, Mutex};

static GLOBAL_COUNTER: LazyLock<Mutex<i32>> =
    LazyLock::new(|| Mutex::new(0));

fn increment() {
    *GLOBAL_COUNTER.lock().unwrap() += 1;  // ✅ Thread-safe!
}

fn main() {
    increment();
    println!("{}", *GLOBAL_COUNTER.lock().unwrap());
}
```

**Why This Works**:
- `LazyLock`: Lazy initialization (no startup cost)
-`Mutex`: Exclusive access (no data races)
- ✅ Thread-safe: Works correctly with `std::thread::spawn`
- ✅ Zero unsafe: All memory-safe guarantees preserved

## Performance Considerations

### Lock Contention

**Problem**: High lock contention with `Mutex<T>`

**Solutions**:
1. **RwLock**: Use for read-heavy workloads
   ```rust
   static CONFIG: LazyLock<RwLock<HashMap<String, String>>> =
       LazyLock::new(|| RwLock::new(HashMap::new()));

   // Many readers OK
   let config = CONFIG.read().unwrap();
   ```

2. **Thread-Local Storage**: Per-thread state
   ```rust
   use std::cell::RefCell;
   thread_local! {
       static COUNTER: RefCell<i32> = RefCell::new(0);
   }

   COUNTER.with(|c| *c.borrow_mut() += 1);
   ```

3. **Atomics**: Lock-free for simple operations
   ```rust
   static COUNTER: AtomicUsize = AtomicUsize::new(0);
   COUNTER.fetch_add(1, Ordering::Relaxed);
   ```

### Deadlock Avoidance

**Rule**: Always acquire locks in the same order

```rust
// ✅ CORRECT: Always acquire lock1 before lock2
let data1 = LOCK1.lock().unwrap();
let data2 = LOCK2.lock().unwrap();

// ❌ WRONG: Different order in different places
// Thread 1:
let a = LOCK1.lock().unwrap();
let b = LOCK2.lock().unwrap();  // Deadlock!

// Thread 2:
let b = LOCK2.lock().unwrap();
let a = LOCK1.lock().unwrap();  // Deadlock!
```

## Testing Concurrent Code

### Race Detection (Loom)

```rust
#[cfg(test)]
mod tests {
    use loom::sync::Arc;
    use loom::sync::atomic::{AtomicUsize, Ordering};
    use loom::thread;

    #[test]
    fn test_concurrent_increment() {
        loom::model(|| {
            let counter = Arc::new(AtomicUsize::new(0));

            let threads: Vec<_> = (0..2).map(|_| {
                let counter = Arc::clone(&counter);
                thread::spawn(move || {
                    counter.fetch_add(1, Ordering::SeqCst);
                })
            }).collect();

            for t in threads {
                t.join().unwrap();
            }

            assert_eq!(counter.load(Ordering::SeqCst), 2);
        });
    }
}
```

### Stress Testing

```rust
#[test]
fn test_stress_concurrent_access() {
    let counter = Arc::new(AtomicUsize::new(0));
    let mut handles = vec![];

    for _ in 0..100 {
        let counter = Arc::clone(&counter);
        handles.push(std::thread::spawn(move || {
            for _ in 0..1000 {
                counter.fetch_add(1, Ordering::SeqCst);
            }
        }));
    }

    for handle in handles {
        handle.join().unwrap();
    }

    assert_eq!(counter.load(Ordering::SeqCst), 100_000);
}
```

## Implementation Status

| Feature | Status | Notes |
|---------|--------|-------|
| Thread-safe globals | ✅ COMPLETE | v3.194.0, LazyLock<Mutex<T>> |
| `std::thread` syntax | ⚠️ PARTIAL | Parser needs work for `spawn` |
| `async`/`await` keywords | ⚠️ PARTIAL | Runtime exists, syntax incomplete |
| Channels (`mpsc`) | ❌ NOT IMPLEMENTED | Use Rust stdlib directly |
| Atomics | ❌ NOT IMPLEMENTED | Use Rust stdlib directly |
| Arc/Mutex patterns | ✅ COMPLETE | Full support, transparent |

## References

- **GitHub Issue #132**: [CRITICAL] Transpiler must use safe abstractions
- **Rust Book - Concurrency**: https://doc.rust-lang.org/book/ch16-00-concurrency.html
- **Tokio Documentation**: https://tokio.rs/
- **LazyLock RFC**: https://github.com/rust-lang/rust/issues/109736
- **CHANGELOG v3.194.0**: TRANSPILER-SCOPE thread-safe implementation

## Future Work

1. **Parser Support**: Add syntax for `spawn`, `async`, `await`
2. **Ownership Analysis**: Detect `move` closures automatically
3. **Deadlock Detection**: Static analysis for lock ordering
4. **Performance Profiling**: Identify lock contention hotspots
5. **Actor Model**: Explore Actix/Bastion integration

---

**Last Updated**: 2025-11-04 (v3.194.0)