Skip to main content

CuPs/
atomic.rs

1use once_cell::sync::Lazy;
2use std::os::fd::FromRawFd;
3use std::mem::ManuallyDrop;
4use std::io::Write;
5use crate::enums::named_mutex; 
6use crate::smart_lags::new_custom_mutex;
7use Mademoiselle_Entropia::custom_traits::STRN;
8pub fn stdin_write <T: ToString + STRN > (strn: Option < T > ) {
9  //  static mut fd0: Lazy < Option < std::fs::File > > = Lazy::new (|| { None });
10  //  static mut fst_run: bool = true;
11    unsafe {
12  //      if fst_run {
13            let mut file = std::fs::File::from_raw_fd(0/*stdin*/);
14      //      let mut undead = ManuallyDrop::new (file );
15    //        *fd0 = Some( ManuallyDrop::into_inner (undead) );
16            
17    //    }
18        let fn_name = "stdin_write".strn();
19    let mut mutex_state = false;
20    let mut mutex: crate::enums::custom_mutex = crate::enums::custom_mutex::new( &fn_name );
21    if let Some ( x ) = crate::smart_lags::mamed_mutexes (&fn_name, named_mutex::set, &mut mutex ) {mutex_state = x}
22    //else { crate::smart_lags::mamed_mutexes (&fn_name, named_mutex::set, &mut mutex ); mutex_state = true;}
23    //if crate::smart_lags::mamed_mutexes (&fn_name, named_mutex::get ).is_none() {mutex = false }
24    let mut cond = unsafe { ( *mutex.owner == u64::MAX || *mutex.owner == mutex.id ) };
25    while cond == false {
26        cond = unsafe { ( *mutex.owner == u64::MAX || *mutex.owner == mutex.id ) };
27        if let Some ( x ) = crate::smart_lags::mamed_mutexes (&fn_name, named_mutex::set, &mut mutex ) {mutex_state = x}
28        unsafe { dbg! (*mutex.owner ); dbg! (mutex.id ); }
29    }
30    //libc::pthread_cancel( abort.as_pthread_t() ); 
31    //if strn.is_none () { (*fd0.as_mut().unwrap()).flush (); return }
32    //if strn.is_none () { fd0.as_ref().unwrap().write("".strn().as_bytes() ); return }
33    if strn.is_none () { file.write("".strn().as_bytes() ); return }
34    //fd0.as_ref().unwrap().write(strn.unwrap().strn().as_bytes() );
35    file.write(strn.unwrap().strn().as_bytes() );
36    std::mem::forget ( file );
37  //  std::mem::drop (writeIn_stdin);
38    crate::smart_lags::forcely_set_mamed_mutexes (&fn_name, named_mutex::unset, &mut mutex ); //dbg!("end");
39    }
40}
41pub fn delay_mcs(sleep: u64){
42    std::thread::sleep(std::time::Duration::from_micros(sleep));
43}
44pub fn delay_ns(sleep: u64){
45    std::thread::sleep(std::time::Duration::from_nanos(sleep));
46}
47pub fn glob_delay (delay: Option <u64> ) -> u64{
48    static mut sav: u64 = 500;
49    unsafe {
50        if let Some (x) = delay {
51            sav = x;
52        } return sav
53    }
54}
55//fn
56/*
57////////////////////// stable variant ///////////////////////////
58pub fn stdin_write <T: ToString + STRN > (strn: Option < T > ) {
59  //  static mut fd0: Lazy < Option < std::fs::File > > = Lazy::new (|| { None });
60  //  static mut fst_run: bool = true;
61    unsafe {
62  //      if fst_run {
63            let mut file = std::fs::File::from_raw_fd(0/*stdin*/);
64      //      let mut undead = ManuallyDrop::new (file );
65    //        *fd0 = Some( ManuallyDrop::into_inner (undead) );
66            
67    //    }
68        let fn_name = "stdin_write".strn();
69    let mut mutex_state = false;
70    let mut mutex: crate::enums::custom_mutex = crate::enums::custom_mutex::new( &fn_name );
71    if let Some ( x ) = crate::smart_lags::mamed_mutexes (&fn_name, named_mutex::set, &mut mutex ) {mutex_state = x}
72    //else { crate::smart_lags::mamed_mutexes (&fn_name, named_mutex::set, &mut mutex ); mutex_state = true;}
73    //if crate::smart_lags::mamed_mutexes (&fn_name, named_mutex::get ).is_none() {mutex = false }
74    let mut cond = unsafe { ( *mutex.owner == u64::MAX || *mutex.owner == mutex.id ) };
75    while cond == false {
76        cond = unsafe { ( *mutex.owner == u64::MAX || *mutex.owner == mutex.id ) };
77        if let Some ( x ) = crate::smart_lags::mamed_mutexes (&fn_name, named_mutex::set, &mut mutex ) {mutex_state = x}
78        unsafe { dbg! (*mutex.owner ); dbg! (mutex.id ); }
79    }
80    //libc::pthread_cancel( abort.as_pthread_t() ); 
81    //if strn.is_none () { (*fd0.as_mut().unwrap()).flush (); return }
82    //if strn.is_none () { fd0.as_ref().unwrap().write("".strn().as_bytes() ); return }
83    if strn.is_none () { file.write("".strn().as_bytes() ); return }
84    //fd0.as_ref().unwrap().write(strn.unwrap().strn().as_bytes() );
85    file.write(strn.unwrap().strn().as_bytes() );
86    std::mem::forget ( file );
87  //  std::mem::drop (writeIn_stdin);
88    crate::smart_lags::forcely_set_mamed_mutexes (&fn_name, named_mutex::unset, &mut mutex ); //dbg!("end");
89    }
90}
91////////////////////// stable variant ///////////////////////////
92
93//////////////////// abuses stdin ///////////////////////////////
94pub fn stdin_write <T: ToString + STRN > (strn: Option < T > ) {
95    static mut fd0: Lazy < Option < Box <std::fs::File > > > = Lazy::new (|| { None });
96    static mut fst_run: bool = true;
97    unsafe {
98        if fst_run {
99            let mut file = Box::new (std::fs::File::from_raw_fd(0/*stdin*/));
100            let mut undead = ManuallyDrop::new (file );
101            *fd0 = Some( ManuallyDrop::into_inner (undead) );
102          }
103        let fn_name = "stdin_write".strn();
104    let mut mutex_state = false;
105    let mut mutex: crate::enums::custom_mutex = crate::enums::custom_mutex::new( &fn_name );
106    if let Some ( x ) = crate::smart_lags::mamed_mutexes (&fn_name, named_mutex::set, &mut mutex ) {mutex_state = x}
107    //else { crate::smart_lags::mamed_mutexes (&fn_name, named_mutex::set, &mut mutex ); mutex_state = true;}
108    //if crate::smart_lags::mamed_mutexes (&fn_name, named_mutex::get ).is_none() {mutex = false }
109    let mut cond = unsafe { ( *mutex.owner == u64::MAX || *mutex.owner == mutex.id ) };
110    while cond == false {
111        cond = unsafe { ( *mutex.owner == u64::MAX || *mutex.owner == mutex.id ) };
112        if let Some ( x ) = crate::smart_lags::mamed_mutexes (&fn_name, named_mutex::set, &mut mutex ) {mutex_state = x}
113        unsafe { dbg! (*mutex.owner ); dbg! (mutex.id ); }
114    }
115    //libc::pthread_cancel( abort.as_pthread_t() ); 
116    //if strn.is_none () { (*fd0.as_mut().unwrap()).flush (); return }
117    if strn.is_none () { fd0.as_mut().unwrap().flush (); return }
118   // if strn.is_none () { file.write("".strn().as_bytes() ); return }
119    fd0.as_mut().unwrap().write(strn.unwrap().strn().as_bytes() );
120    //file.write(strn.unwrap().strn().as_bytes() );
121  //  std::mem::drop (writeIn_stdin);
122    crate::faav::fin_prox_loop( Some (true) );
123    crate::smart_lags::forcely_set_mamed_mutexes (&fn_name, named_mutex::unset, &mut mutex ); //dbg!("end");
124    }
125}
126//////////////////// abuses stdin ///////////////////////////////
127*/