uqa_execution/routines/
transaction.rs1#[derive(Clone, Copy, PartialEq, Eq, Debug)]
9pub struct RoutineSessionId(pub usize);
10use std::cell::RefCell;
11
12thread_local! {
13 static ROUTINE_TRANSACTION_STACK: RefCell<Vec<RoutineTransactionContext>> = const { RefCell::new(Vec::new()) };
14 static DIRECT_ROUTINE_COMMAND_STACK: RefCell<Vec<RoutineSessionId>> = const { RefCell::new(Vec::new()) };
15}
16
17#[derive(Clone, Copy)]
18struct RoutineTransactionContext {
19 session: RoutineSessionId,
20 nonatomic: bool,
21}
22
23pub struct RoutineTransactionGuard;
24
25pub struct DirectRoutineCommandGuard {
26 session: RoutineSessionId,
27}
28
29impl RoutineTransactionGuard {
30 pub fn enter(session: RoutineSessionId, nonatomic: bool) -> Self {
31 ROUTINE_TRANSACTION_STACK.with(|stack| {
32 stack
33 .borrow_mut()
34 .push(RoutineTransactionContext { session, nonatomic });
35 });
36 Self
37 }
38}
39
40impl Drop for RoutineTransactionGuard {
41 fn drop(&mut self) {
42 ROUTINE_TRANSACTION_STACK.with(|stack| {
43 let removed = stack.borrow_mut().pop();
44 debug_assert!(removed.is_some(), "routine transaction stack underflow");
45 });
46 }
47}
48
49impl DirectRoutineCommandGuard {
50 pub fn enter(session: RoutineSessionId) -> Self {
51 DIRECT_ROUTINE_COMMAND_STACK.with(|stack| stack.borrow_mut().push(session));
52 Self { session }
53 }
54}
55
56impl Drop for DirectRoutineCommandGuard {
57 fn drop(&mut self) {
58 DIRECT_ROUTINE_COMMAND_STACK.with(|stack| {
59 let removed = stack.borrow_mut().pop();
60 debug_assert_eq!(
61 removed,
62 Some(self.session),
63 "direct routine command stack mismatch"
64 );
65 });
66 }
67}
68
69pub fn routine_transaction_control_allowed(session: RoutineSessionId) -> bool {
70 ROUTINE_TRANSACTION_STACK.with(|stack| {
71 stack
72 .borrow()
73 .last()
74 .is_some_and(|context| context.session == session && context.nonatomic)
75 })
76}
77
78pub fn nonatomic_routine_entry_allowed(session: RoutineSessionId, nested_statement: bool) -> bool {
79 if !nested_statement {
80 return true;
81 }
82 routine_transaction_control_allowed(session)
83 && DIRECT_ROUTINE_COMMAND_STACK
84 .with(|stack| stack.borrow().last().is_some_and(|entry| *entry == session))
85}