1use std::{
2 collections::HashMap,
3 sync::{
4 Arc, Mutex, RwLock,
5 atomic::{AtomicUsize, Ordering},
6 mpsc::{
7 self, Receiver, RecvError, RecvTimeoutError, Sender, SyncSender, TryRecvError,
8 TrySendError,
9 },
10 },
11 thread::JoinHandle,
12 time::Duration,
13};
14
15use crate::core::{file::Lang, frozen_graph::FrozenSyntaxGraph, prng::PRNG};
16
17pub const ERR_SHUT_DOWN: &str = "resrap: instance has been shut down";
18
19#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct CodeGenRes {
22 pub code: String,
23 pub id: String,
24}
25
26#[derive(Clone)]
29pub struct CodeChannel {
30 rx: Arc<Mutex<Receiver<CodeGenRes>>>,
31}
32
33impl CodeChannel {
34 pub fn recv(&self) -> Result<CodeGenRes, RecvError> {
35 self.rx.lock().unwrap().recv()
36 }
37
38 pub fn try_recv(&self) -> Result<CodeGenRes, TryRecvError> {
39 self.rx.lock().unwrap().try_recv()
40 }
41
42 pub fn recv_timeout(&self, timeout: Duration) -> Result<CodeGenRes, RecvTimeoutError> {
43 self.rx.lock().unwrap().recv_timeout(timeout)
44 }
45}
46
47struct CodeGenReq {
48 graph: Option<Arc<FrozenSyntaxGraph>>,
51 start_id: Option<u32>,
52 seed: u64,
53 tokens: usize,
54 complete: bool,
55 id: String,
56}
57
58pub struct ResrapMT {
60 language_graph: Arc<RwLock<HashMap<String, Lang>>>,
61 pool_size: usize,
62 wait_queue_size: usize,
63 pending_tx: Vec<SyncSender<CodeGenReq>>,
66 pending_rx: Option<Vec<Receiver<CodeGenReq>>>,
67 next_worker: AtomicUsize,
68 result_tx: Option<Sender<CodeGenRes>>,
69 code_channel: CodeChannel,
70 workers: Vec<JoinHandle<()>>,
71 started: bool,
72 closed: bool,
73}
74
75impl ResrapMT {
76 pub fn new(pool_size: usize, wait_queue_size: usize) -> Self {
80 let pool_size = pool_size.max(1);
81 let wait_queue_size = wait_queue_size.max(1);
82 let per_worker = wait_queue_size.div_ceil(pool_size).max(1);
83
84 let mut pending_tx = Vec::with_capacity(pool_size);
85 let mut pending_rx = Vec::with_capacity(pool_size);
86 for _ in 0..pool_size {
87 let (tx, rx) = mpsc::sync_channel(per_worker);
88 pending_tx.push(tx);
89 pending_rx.push(rx);
90 }
91
92 let (result_tx, code_rx) = mpsc::channel::<CodeGenRes>();
93
94 ResrapMT {
95 language_graph: Arc::new(RwLock::new(HashMap::new())),
96 pool_size,
97 wait_queue_size,
98 pending_tx,
99 pending_rx: Some(pending_rx),
100 next_worker: AtomicUsize::new(0),
101 result_tx: Some(result_tx),
102 code_channel: CodeChannel {
103 rx: Arc::new(Mutex::new(code_rx)),
104 },
105 workers: Vec::new(),
106 started: false,
107 closed: false,
108 }
109 }
110
111 pub fn pool_size(&self) -> usize {
112 self.pool_size
113 }
114
115 pub fn wait_queue_size(&self) -> usize {
116 self.wait_queue_size
117 }
118
119 pub fn get_code_channel(&self) -> CodeChannel {
121 self.code_channel.clone()
122 }
123
124 pub fn parse_grammar(&mut self, name: &str, grammar: &str) -> Result<(), String> {
125 self.parse_grammar_impl(name, grammar)
126 }
127
128 fn parse_grammar_impl(&mut self, name: &str, grammar: &str) -> Result<(), String> {
129 let mut lang = Lang::new();
130 lang.parse_string(grammar)?;
131 self.language_graph
132 .write()
133 .unwrap()
134 .insert(name.to_string(), lang);
135 Ok(())
136 }
137
138 pub fn parse_grammar_file(&mut self, name: &str, location: &str) -> Result<(), String> {
139 let mut lang = Lang::new();
140 lang.parse_file(location)?;
141 self.language_graph
142 .write()
143 .unwrap()
144 .insert(name.to_string(), lang);
145 Ok(())
146 }
147
148 pub fn add_words(&self, name: &str, rule: &str, words: &[String]) -> Result<(), String> {
150 let graph = self.language_graph.read().unwrap();
151 let lang = graph
152 .get(name)
153 .ok_or_else(|| format!("resrap: grammar '{}' is not loaded", name))?;
154 let graph = lang
155 .get_graph()
156 .ok_or_else(|| format!("resrap: grammar '{}' is not loaded", name))?;
157 graph.set_words(rule, words)
158 }
159
160 pub fn add_keywords(&self, name: &str, rule: &str, words: &[String]) -> Result<(), String> {
161 self.add_words(name, rule, words)
162 }
163
164 fn submit(&self, req: CodeGenReq) -> Result<(), String> {
165 if self.closed || self.pending_tx.is_empty() {
166 return Err(ERR_SHUT_DOWN.to_string());
167 }
168
169 let len = self.pending_tx.len();
170 let start = self.next_worker.fetch_add(1, Ordering::Relaxed) % len;
171 let mut req = req;
172
173 for offset in 0..len {
177 let index = (start + offset) % len;
178 match self.pending_tx[index].try_send(req) {
179 Ok(()) => return Ok(()),
180 Err(TrySendError::Full(returned)) => req = returned,
181 Err(TrySendError::Disconnected(_)) => {
182 return Err(ERR_SHUT_DOWN.to_string());
183 }
184 }
185 }
186 self.pending_tx[start]
187 .send(req)
188 .map_err(|_| ERR_SHUT_DOWN.to_string())
189 }
190
191 fn submit_job(
192 &self,
193 id: &str,
194 name: &str,
195 starting_node: &str,
196 seed: u64,
197 tokens: usize,
198 complete: bool,
199 ) -> Result<(), String> {
200 let (graph, start_id) = {
201 let languages = self.language_graph.read().unwrap();
202 match languages.get(name).and_then(|lang| lang.get_graph_arc()) {
203 Some(graph) => {
204 let start_id = graph.name_map.get(starting_node).copied();
205 (Some(graph), start_id)
206 }
207 None => (None, None),
208 }
209 };
210
211 self.submit(CodeGenReq {
212 graph,
213 start_id,
214 seed,
215 tokens,
216 complete,
217 id: id.to_string(),
218 })
219 }
220
221 pub fn generate_random(
222 &self,
223 id: &str,
224 name: &str,
225 starting_node: &str,
226 tokens: usize,
227 ) -> Result<(), String> {
228 self.submit_job(id, name, starting_node, 0, tokens, false)
229 }
230
231 pub fn generate_with_seeded(
235 &self,
236 id: &str,
237 name: &str,
238 starting_node: &str,
239 seed: u64,
240 tokens: usize,
241 ) -> Result<(), String> {
242 self.submit_job(id, name, starting_node, seed, tokens, false)
243 }
244
245 pub fn generate_complete(
246 &self,
247 id: &str,
248 name: &str,
249 starting_node: &str,
250 tokens: usize,
251 ) -> Result<(), String> {
252 self.submit_job(id, name, starting_node, 0, tokens, true)
253 }
254
255 pub fn generate_complete_with_seeded(
259 &self,
260 id: &str,
261 name: &str,
262 starting_node: &str,
263 seed: u64,
264 tokens: usize,
265 ) -> Result<(), String> {
266 self.submit_job(id, name, starting_node, seed, tokens, true)
267 }
268
269 pub fn start_resrap(&mut self) {
271 if self.started {
272 return;
273 }
274 self.started = true;
275
276 let receivers = match self.pending_rx.take() {
277 Some(receivers) => receivers,
278 None => return,
279 };
280 let result_tx = match self.result_tx.clone() {
281 Some(tx) => tx,
282 None => return,
283 };
284
285 for rx in receivers {
286 let result_tx = result_tx.clone();
287 self.workers.push(std::thread::spawn(move || {
288 while let Ok(job) = rx.recv() {
289 let code = match (job.graph.as_ref(), job.start_id) {
290 (Some(graph), Some(start_id)) => {
291 let prng = PRNG::new(job.seed);
292 if job.complete {
293 graph.walk_graph_id(prng, start_id, job.tokens, true)
294 } else {
295 graph.walk_graph_id(prng, start_id, job.tokens, false)
296 }
297 }
298 _ => String::new(),
301 };
302
303 if result_tx.send(CodeGenRes { code, id: job.id }).is_err() {
304 break;
305 }
306 }
307 }));
308 }
309 }
310
311 pub fn shutdown_resrap(&mut self) {
314 if self.closed {
315 return;
316 }
317 self.closed = true;
318
319 self.pending_tx.clear();
322 for worker in self.workers.drain(..) {
323 let _ = worker.join();
324 }
325
326 self.result_tx.take();
329 self.pending_rx.take();
330 }
331
332 pub fn shut_down_resrap(&mut self) {
334 self.shutdown_resrap();
335 }
336}
337
338impl crate::GrammarLoader for ResrapMT {
339 fn parse_grammar(&mut self, name: &str, grammar: &str) -> Result<(), String> {
340 self.parse_grammar_impl(name, grammar)
341 }
342}
343
344#[cfg(test)]
345mod tests {
346 use super::*;
347 use std::time::Duration;
348
349 const GRAMMAR: &str = "program:function^;\nfunction:'f' '(' ')';\n";
350
351 fn instance() -> ResrapMT {
352 let mut r = ResrapMT::new(2, 10);
353 r.parse_grammar("Test", GRAMMAR).unwrap();
354 r.start_resrap();
355 r
356 }
357
358 #[test]
359 fn clamps_pool_and_queue() {
360 let r = ResrapMT::new(0, 0);
361 assert_eq!(r.pool_size(), 1);
362 assert_eq!(r.wait_queue_size(), 1);
363 }
364
365 #[test]
366 fn generates_a_job_with_its_id() {
367 let mut r = instance();
368 r.generate_random("job1", "Test", "program", 20).unwrap();
369 let res = r
370 .get_code_channel()
371 .recv_timeout(Duration::from_secs(5))
372 .unwrap();
373 assert_eq!(res.id, "job1");
374 assert!(!res.code.is_empty());
375 r.shutdown_resrap();
376 }
377
378 #[test]
379 fn handles_multiple_jobs() {
380 let mut r = instance();
381 for i in 0..10 {
382 r.generate_random(&format!("job{}", i), "Test", "program", 10)
383 .unwrap();
384 }
385 let channel = r.get_code_channel();
386 let mut seen = std::collections::HashSet::new();
387 for _ in 0..10 {
388 let res = channel.recv_timeout(Duration::from_secs(5)).unwrap();
389 assert!(!res.code.is_empty());
390 seen.insert(res.id);
391 }
392 assert_eq!(seen.len(), 10);
393 r.shutdown_resrap();
394 }
395
396 #[test]
397 fn seeded_generation_is_deterministic_across_instances() {
398 let mut a = instance();
399 let mut b = instance();
400 a.generate_with_seeded("1", "Test", "program", 999, 20)
401 .unwrap();
402 b.generate_with_seeded("1", "Test", "program", 999, 20)
403 .unwrap();
404 let ca = a
405 .get_code_channel()
406 .recv_timeout(Duration::from_secs(5))
407 .unwrap();
408 let cb = b
409 .get_code_channel()
410 .recv_timeout(Duration::from_secs(5))
411 .unwrap();
412 assert_eq!(ca.code, cb.code);
413 a.shutdown_resrap();
414 b.shutdown_resrap();
415 }
416
417 #[test]
418 fn unknown_language_returns_empty_result() {
419 let mut r = instance();
420 r.generate_random("job", "Missing", "program", 10).unwrap();
421 let res = r
422 .get_code_channel()
423 .recv_timeout(Duration::from_secs(5))
424 .unwrap();
425 assert_eq!(res.code, "");
426 assert_eq!(res.id, "job");
427 r.shutdown_resrap();
428 }
429
430 #[test]
431 fn submit_after_shutdown_errors() {
432 let mut r = instance();
433 r.shutdown_resrap();
434 assert_eq!(
435 r.generate_random("job", "Test", "program", 10).unwrap_err(),
436 ERR_SHUT_DOWN
437 );
438 r.shutdown_resrap();
439 }
440
441 #[test]
442 fn supports_complete_and_words() {
443 let mut r = instance();
444 r.add_words("Test", "function", &["alpha".to_string()])
445 .unwrap();
446 r.generate_complete_with_seeded("job", "Test", "program", 7, 5)
447 .unwrap();
448 let res = r
449 .get_code_channel()
450 .recv_timeout(Duration::from_secs(5))
451 .unwrap();
452 assert!(!res.code.is_empty());
453 assert!(res.code.starts_with("alpha"));
454 assert!(!res.code.contains('f'));
455 assert!(!res.code.contains('('));
456 r.shutdown_resrap();
457 }
458}