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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
//! # Reactive Thread
//!
//! A library for Synchronous Reactive System
pub mod scheduler{

use std::fmt::Debug;
use std::sync::{Arc, Condvar, Mutex, MutexGuard, Barrier};
use std::sync::mpsc;
use std::thread;
use std::vec;
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
use std::fmt;
use std::sync::atomic::AtomicIsize;
use std::mem;

pub use crate::Thread_trait::Thread;

pub use crate::signal::Signal;
/******************************************************
                 Numbers of scheduler
******************************************************/  
pub static GLOBAL_SCHEDULER_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
pub static GLOBAL_THREAD_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;

/******************************************************
                 Scheduler
******************************************************/  
#[derive(Debug)]
pub struct  Scheduler  {
  id_scheduler: usize,
  pub NB_thread: Arc<Mutex<isize>>,
  //pub list_thread: Arc<Mutex<Vec<Arc<dyn Thread >>>>,//PAs besoin
  pub list_wait_signaux: Arc<Mutex<Vec<Arc<Signal>>>>,
  pub list_emit_signaux: Arc<Mutex<Vec<Arc<Signal>>>>,

/*********************************************************/

  pub condition_cooperate: Arc<(Mutex<bool>, Condvar)>,//waiting a next instant
  pub condition_wake_up: Arc<(Mutex<bool>, Condvar)>,
  pub condition_emit: Arc<(Mutex<bool>, Condvar)>,
  pub condition_cooperate_Wake: Arc<(Mutex<bool>, Condvar)>,
  pub condition_finish: Arc<(Mutex<bool>, Condvar)>,
  pub condition_instant: Arc<(Mutex<bool>, Condvar)>,
  pub condition_code_signal: Arc<(Mutex<bool>, Condvar)>,

  pub NB_cooperate: Arc<Mutex<i32>>,
  pub NB_wait: Arc<Mutex<i32>>,
  pub NB_finish: Arc<Mutex<i32>>,
  pub NB_instant: Arc<Mutex<i32>>,//a voir sans Mutex
  pub next_instant: Arc<Mutex<i32>>, 
  pub NB_wait_cooperate: Arc<Mutex<i32>>,

  pub check_NB_wait: Arc<Mutex<bool>>,
  pub check_NB_cooperate: Arc<Mutex<bool>>,
  pub return_code: Arc<Mutex<bool>>, 
}

impl Scheduler{

  // add code here
  pub fn scheduler_create()-> Scheduler{
    let old_scheduler_count = GLOBAL_SCHEDULER_COUNT.fetch_add(1, Ordering::SeqCst);

    Scheduler{
      id_scheduler: old_scheduler_count+1,
      NB_thread: Arc::new(Mutex::new(0isize)),

      //list_thread: Arc::new(Mutex::new(Vec::new())),
      list_wait_signaux: Arc::new(Mutex::new(Vec::new())),
      list_emit_signaux: Arc::new(Mutex::new(Vec::new())),
      
      condition_cooperate: Arc::new((Mutex::new(false), Condvar::new())),
      condition_wake_up: Arc::new((Mutex::new(false), Condvar::new())),
      condition_emit: Arc::new((Mutex::new(false), Condvar::new())),
      condition_cooperate_Wake: Arc::new((Mutex::new(false), Condvar::new())),
      condition_finish: Arc::new((Mutex::new(false), Condvar::new())),
      condition_instant: Arc::new((Mutex::new(false), Condvar::new())),
      condition_code_signal: Arc::new((Mutex::new(false), Condvar::new())),

      NB_cooperate: Arc::new(Mutex::new(0i32)),
      NB_wait: Arc::new(Mutex::new(0i32)),
      NB_finish: Arc::new(Mutex::new(0i32)),
      NB_instant: Arc::new(Mutex::new(0i32)),
      next_instant: Arc::new(Mutex::new(1i32)),
      NB_wait_cooperate: Arc::new(Mutex::new(0i32)),
      
      check_NB_wait: Arc::new(Mutex::new(false)),
      check_NB_cooperate: Arc::new(Mutex::new(false)),
      return_code: Arc::new(Mutex::new(false)),
    }
  }

  pub fn increment_nbthread(&self){
    *self.NB_thread.lock().unwrap()+=1;
  }

  pub fn clear_all_liste_wait(&self){
    self.list_wait_signaux.lock().unwrap().clear();
  }

  pub fn clear_all_liste_emit(&self){
    self.list_emit_signaux.lock().unwrap().clear();
  }

  pub fn add_wait_signal(&self, signal: Arc<Signal>){
    self.list_wait_signaux.lock().unwrap().push(signal);
  }

  pub fn add_emit_signal(&self, signal: Arc<Signal>){
    self.list_emit_signaux.lock().unwrap().push(signal);
    
  }

  pub fn return_length_list_wait(&self) -> i32{
    let mut i = 0i32;
    for signal in self.list_wait_signaux.lock().unwrap().iter(){
      if(get_condvar_bool(Arc::clone(&signal)) == true){
        
        i+=1;
    }
    
  }
  let length = self.list_wait_signaux.lock().unwrap().len() as i32;
  return (length - i)

}


  pub fn return_code(&self) -> bool{
      *self.return_code.lock().unwrap()
   }


   pub fn scheduler_emit(&self, signal: Arc<Signal>){
        notify_thread(get_condvar(signal));
  }

  pub fn initialise_all_signaux_emit(&self){
    for signal in self.list_emit_signaux.lock().unwrap().iter(){
      self.return_false_signal(Arc::clone(&signal));

    }
  }

  pub fn initialise_all_signaux_wait(&self){
    for signal in self.list_wait_signaux.lock().unwrap().iter(){
      self.return_false_signal(Arc::clone(&signal));
    }
  }


  pub fn return_false_signal(&self, signal: Arc<Signal>){
    return_condvar_false(get_condvar(signal)); 
  }

  pub fn verify_return_code(&self){
    if(*self.NB_wait.lock().unwrap()== 0 &&  *self.return_code.lock().unwrap()== true){
      self.initialise_all_signaux_wait();
      self.clear_all_liste_wait();
      *self.return_code.lock().unwrap()= false;
      self.notify_return_code();
    }
  }

  pub fn wait_return_code(&self){
          let ( num, cvar) = &*self.condition_emit;
        {
            let mut start = num.lock().unwrap();
            while !*start {
                start = cvar.wait(start).unwrap();
            }
        }
  }

  pub fn add_instant(&self){
    *self.NB_instant.lock().unwrap() +=1;
  }

  pub fn decrement_cooperate(&self){
    *self.NB_cooperate.lock().unwrap() -=1;
    if (*self.NB_cooperate.lock().unwrap() == 0i32){
      if(self.get_condvar_wake_up()){
          self.wait_cooperate_wake();
        }
      self.notify_all_wake_up();
    }
  }

  pub fn decrement_wait_cooperate(&self){
   *self.NB_wait_cooperate.lock().unwrap() -=1;
  }

  pub fn add_next_instant(&self)-> bool{
    if (*self.NB_cooperate.lock().unwrap() == 0i32){
       if(*self.NB_instant.lock().unwrap() == *self.next_instant.lock().unwrap()){
        *self.next_instant.lock().unwrap() +=1;
      }
      return true;
     }
     return false;
 } 

 pub fn notify_return_code(&self){
            let ( num, cvar) = &*self.condition_code_signal;
            let mut start = num.lock().unwrap();
            *start = true;
            cvar.notify_all();
        }

  pub fn wait_code_signal(&self){

          let ( num, cvar) = &*self.condition_code_signal;
        {
            let mut start = num.lock().unwrap();
            while !*start {
               
                start = cvar.wait(start).unwrap();  
            }
        }
  }

  pub fn notify_all_cooperate(&self){
            let ( num, cvar) = &*self.condition_cooperate;
            let mut start = num.lock().unwrap();
            *start = true;
            cvar.notify_all();
        }

  pub fn wait_cooperate(&self){

          let ( num, cvar) = &*self.condition_cooperate;
        {
            let mut start = num.lock().unwrap();
            while !*start {
               
                start = cvar.wait(start).unwrap();  
            }
        }
  }

  pub fn notify_all_wake_up(&self){

            let ( num, cvar) = &*self.condition_wake_up;
            let mut start = num.lock().unwrap();
            *start = true;
            cvar.notify_all();

  }

  pub fn wait_wake_up(&self){
          let ( num, cvar) = &*self.condition_wake_up;
        {
            let mut start = num.lock().unwrap();
            while !*start {
               
                start = cvar.wait(start).unwrap();              
            }
              self.return_cooperatewake_false();
              self.return_finish_false();
              self.return_emit_false();
              self.return_instant_false();
        }
  }

  pub fn get_condvar_wake_up(&self) -> bool{
    let ( num, cvar) = &*self.condition_wake_up;
    let mut start = num.lock().unwrap();
    return *start
   }

   pub fn get_condvar_cooperateWake(&self) -> bool{
    let ( num, cvar) = &*self.condition_cooperate_Wake;
    let mut start = num.lock().unwrap();
    return *start
   }

   pub fn get_condvar_cooperate(&self) -> bool{
    let ( num, cvar) = &*self.condition_cooperate;
    let mut start = num.lock().unwrap();
    return *start
   }

   
  pub fn notify_instant(&self){

            let ( num, cvar) = &*self.condition_instant;
            let mut start = num.lock().unwrap();
            *start = true;
            cvar.notify_all();
     
  }

   pub fn wait_instant(&self){

          let ( num, cvar) = &*self.condition_instant;
        {   self.add_nbwait_cooperate();
            let mut start = num.lock().unwrap();
            while !*start {
               
                start = cvar.wait(start).unwrap();
            }
            self.decrement_wait_cooperate();
        }
  }

  pub fn get_condvar_instant(&self) -> bool{
    let ( num, cvar) = &*self.condition_instant;
    let mut start = num.lock().unwrap();
    return *start
   }


   pub fn notify_all_cooperate_wake(&self){
            let ( num, cvar) = &*self.condition_cooperate_Wake;
            let mut start = num.lock().unwrap();
            *start = true;
            cvar.notify_all();
  }

  pub fn wait_cooperate_wake(&self){

          let ( num, cvar) = &*self.condition_cooperate_Wake;
        {
            let mut start = num.lock().unwrap();
            while !*start {
               
                start = cvar.wait(start).unwrap();
               
            }
        }
  }

  pub fn notify_all_emit(&self){
            let ( num, cvar) = &*self.condition_emit;
            let mut start = num.lock().unwrap();
            *start = true;
            cvar.notify_all();
  }

  pub fn return_emit_false(&self){
    let ( num, cvar) = &*self.condition_emit;
    *num.lock().unwrap() = false;
  }

   pub fn notify_all_finish(&self){
            let ( num, cvar) = &*self.condition_finish;
            let mut start = num.lock().unwrap();
            *start = true;
            cvar.notify_all();

  }

  pub fn wait_finish(&self){

          let ( num, cvar) = &*self.condition_finish;
        {
            let mut start = num.lock().unwrap();
            while !*start {
               
                start = cvar.wait(start).unwrap();   
            }
        }
  }

  pub fn return_cooperatewake_false(&self){
    let ( num, cvar) = &*self.condition_cooperate_Wake;
    *num.lock().unwrap() = false;
  }

  pub fn return_finish_false(&self){
    let ( num, cvar) = &*self.condition_finish;
    *num.lock().unwrap() = false;
  }

  pub fn return_wakeup_false(&self){
    let ( num, cvar) = &*self.condition_wake_up;
    *num.lock().unwrap() = false;
  }

  pub fn return_cooperate_false(&self){
    let ( num, cvar) = &*self.condition_cooperate;
    *num.lock().unwrap() = false;
  }

  pub fn return_code_signal(&self){
    let ( num, cvar) = &*self.condition_code_signal;
    *num.lock().unwrap() = false;
  }

  pub fn return_instant_false(&self){
    if(*self.NB_wait_cooperate.lock().unwrap() == 0){
    let ( num, cvar) = &*self.condition_instant;
    *num.lock().unwrap() = false;
  }
  }

  pub fn add_nbcooperate(&self){

    *self.NB_cooperate.lock().unwrap() +=1;
  }

  pub fn add_nbfinish(&self){

    *self.NB_finish.lock().unwrap() +=1;
  }

  pub fn add_nbwait_cooperate(&self){

    *self.NB_wait_cooperate.lock().unwrap() +=1;
  }

  pub fn add_nbwait(&self){
    //println!("hereee add NB wait");
    *self.NB_wait.lock().unwrap() +=1;
    //println!("add_wait{:?}\n", *self.NB_wait.lock().unwrap());
  }

  pub fn decrement_nbwait(&self){

    *self.NB_wait.lock().unwrap() -=1;
  }

  // pub fn notify_thread_wait(&self){

  //   for signal in self.list_wait_signaux.lock().unwrap().iter(){
  //     if(get_condvar_bool(Arc::clone(&signal)) == false){
  //       *(self.liste_planete.lock().unwrap()) = signal.list_planete.lock().unwrap().clone();
  //       signal.list_planete.lock().unwrap().clear();
  //       self.scheduler_emis(Arc::clone(&signal));
  //   }
      
  //   }
  // }


  pub fn notify_thread_wait(&self){

    for signal in self.list_wait_signaux.lock().unwrap().iter(){
      if(get_condvar_bool(Arc::clone(&signal)) == false){
        
        self.scheduler_emit(Arc::clone(&signal));
        
    }
      
    }
  }


  pub fn thread_not_finish_execution(&self) -> bool{
   // println!("Scheduler: compare_1");
    if(*self.NB_thread.lock().unwrap() > (self.return_length_list_wait() + *self.NB_cooperate.lock().unwrap() + *self.NB_finish.lock().unwrap()) as isize){ //somme as isize converter i32 to isize
      return true
    }
    return false

  }

  pub fn thread_finish_execution(&self)-> bool{
    //println!("\nScheduler: compare_2\n");
    if(*self.NB_thread.lock().unwrap() == (self.return_length_list_wait() + *self.NB_cooperate.lock().unwrap() + *self.NB_finish.lock().unwrap()) as isize){
      if(*self.NB_cooperate.lock().unwrap() == 0 && *self.NB_wait.lock().unwrap() == 0){
        return true;
      }

      else if(*self.NB_cooperate.lock().unwrap() == 0 && *self.NB_wait.lock().unwrap() != 0){
        *self.check_NB_wait.lock().unwrap()=true;//Partie wait pure
        return false;
      }

      else if(*self.NB_cooperate.lock().unwrap() != 0 && *self.NB_wait.lock().unwrap() == 0){
        *self.check_NB_wait.lock().unwrap()=false;//Partie cooperate pure
        return false;
      }

    }
    return false;
  }


/************************************************************/
  pub fn scheduler_start(&self){
    //wait a notification from thread
    let mut done = false;
    while(done == false){
      let mut not_finish_execution = false;
      let mut finish_execution =false;
      let mut verify = false;

       if (*self.NB_cooperate.lock().unwrap() == 0i32){
        self.return_cooperate_false();
       }
      self.wait_wake_up();
      verify = self.add_next_instant();
      self.verify_return_code();
      not_finish_execution = self.thread_not_finish_execution();
      if(!not_finish_execution){
        finish_execution = self.thread_finish_execution();
      }
    if(verify == true){
      self.return_cooperate_false();
      self.notify_instant();
    }
    if(not_finish_execution){
      //println!("not_finish_execution\n");
      self.return_wakeup_false();
      self.notify_all_cooperate_wake();
      self.notify_all_emit();
      self.notify_all_finish();
    }
    else if(finish_execution == false){
      if(self.get_condvar_cooperate() == false){
        if(*self.check_NB_wait.lock().unwrap()== false){// partie cooperate pure
          self.add_instant();
          self.return_wakeup_false();
          *self.return_code.lock().unwrap()=true;
          self.return_code_signal();
          self.notify_thread_wait();
          self.notify_all_cooperate();
          self.notify_all_finish();
          self.notify_all_emit();
          self.initialise_all_signaux_emit();
          self.clear_all_liste_emit();
       }
     
        else if(*self.check_NB_wait.lock().unwrap()== true){// partie wait pure
          *self.return_code.lock().unwrap()=true;
          self.return_code_signal();
          self.notify_thread_wait();
          *self.check_NB_wait.lock().unwrap()== false;
           self.add_instant();
          self.return_wakeup_false();
          self.notify_all_emit();
          self.initialise_all_signaux_emit();
          self.clear_all_liste_emit();
      }

    }
    else {
      self.return_wakeup_false();
        self.notify_all_cooperate_wake();
        self.notify_all_finish();
    }
    }
    else if(finish_execution == true){
      done = true;
    }
  }

  }
}


/******************************************************
                 My_scheduler
******************************************************/  

#[derive(Debug)]
pub struct My_scheduler {
  pub scheduler: Arc<Scheduler>,

}

impl My_scheduler {
  // add code here
  pub fn scheduler_create(sc: Arc<Scheduler>)-> My_scheduler{

    My_scheduler{
      scheduler: sc,
    }
  }

 pub fn scheduler_start(self)-> thread::JoinHandle<()>{
  //let num: u64 = 100_000_000;
        let builder = thread::Builder::new()
    .name("Scheduler".into());
        builder.spawn(move || {
      
            self.scheduler.scheduler_start();
              }).unwrap()

      }

}

/******************************************************
                 Fonctions:
******************************************************/

pub fn get_condvar(signal: Arc<Signal>)-> Arc<(Mutex<bool>, Condvar)> {

  let condvar = signal.condition_variable.clone();
  condvar
}

fn get_condvar_bool(signal: Arc<Signal>) -> bool{
    let ( num, cvar) = &*signal.condition_variable;
    let mut start = num.lock().unwrap();
    return *start
   }

pub fn notify_thread(condvar: Arc<(Mutex<bool>, Condvar)>) {

    
      let &(ref num, ref cvar) = &*condvar;
            *num.lock().unwrap() = true;
            cvar.notify_all();
             
  }

  pub fn return_condvar_false(condvar: Arc<(Mutex<bool>, Condvar)>) {

    
      let &(ref num, ref cvar) = &*condvar;
            *num.lock().unwrap() = false;
            
  }


}
pub mod signal{

use std::sync::{Arc, Condvar, Mutex, MutexGuard};
use std::sync::mpsc;
use std::thread;
use std::vec;
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
use std::fmt;

/******************************************************
                 Numbers of thread and Scheduler
******************************************************/  

pub static GLOBAL_SIGNAL_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
pub use crate::Thread_trait::Thread;

/******************************************************
                 Signal
******************************************************/  
#[derive(Debug)]
//#[derive(Clone)]
pub struct Signal {
  pub id: usize,
  pub condition_variable: Arc<(Mutex<bool>, Condvar)>,

 
  
}

impl Signal {
  // add code here

  pub fn signal_create() -> Signal{
    let old_signal_count = GLOBAL_SIGNAL_COUNT.fetch_add(1, Ordering::SeqCst);

    Signal{
      id: old_signal_count+1,
      condition_variable: Arc::new((Mutex::new(false), Condvar::new())),

    
    }
  }

}

/******************************************************
                 Fonctions:
******************************************************/

pub fn get_condvar(signal: Arc<Mutex<Signal>>)-> Arc<(Mutex<bool>, Condvar)> {

  let condvar = signal.lock().unwrap().condition_variable.clone();
  condvar
}

fn get_condvar_bool(signal: Arc<Mutex<Signal>>) -> bool{
    let ( num, cvar) = &*signal.lock().unwrap().condition_variable;
    let mut start = num.lock().unwrap();
    return *start
   }

pub fn notify_thread(condvar: Arc<(Mutex<bool>, Condvar)>) {

    
      let &(ref num, ref cvar) = &*condvar;
            *num.lock().unwrap() = true;
            cvar.notify_all();
             
  }

  pub fn return_condvar_false(condvar: Arc<(Mutex<bool>, Condvar)>) {

    
      let &(ref num, ref cvar) = &*condvar;
            *num.lock().unwrap() = false;
            
  }

  pub fn remove_liste(liste: Arc<Mutex<Vec<Arc<Mutex<Signal>>>>>, indice: usize){
    liste.lock().unwrap().remove(indice);
  }


}

pub mod Thread_trait{

use std::fmt::Debug;
use std::sync::{Arc, Condvar, Mutex, MutexGuard, Barrier};
use std::sync::mpsc;
use std::thread;
use std::vec;
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
use std::fmt;
use std::sync::atomic::AtomicIsize;


pub use crate::signal::Signal;
pub use crate::scheduler::Scheduler;


/******************************************************
                 macros_rules
******************************************************/ 

macro_rules! signal_check {
    ($code:expr) => {{
        //let state: i32 = 3i32;
        if $code > false {
            println!(" the signal is here");
        }
        else {
          println!(" the signal is not here");
        }
    }};
}
/******************************************************
                 Numbers of thread and Scheduler
******************************************************/  

pub static GLOBAL_THREAD_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;

static N_THREAD: AtomicIsize = AtomicIsize::new(2);

/******************************************************
                 Trait : Thread
******************************************************/ 
pub trait Thread:  Send + Debug + Sync {

  fn return_nb_thread(&self) -> isize;
  fn return_scheduler(&self)-> Arc<Scheduler>;
  fn return_ID(&self)-> usize;

          /***************************************/

  fn thread_emit(&self, signal: Arc<Signal>){

      if(self.return_scheduler().return_code() == true){
        if(*self.return_scheduler().NB_wait.lock().unwrap() == 0){
      if(self.return_scheduler().get_condvar_wake_up()){
          self.return_scheduler().wait_return_code();
         }
    self.return_scheduler().notify_all_wake_up();
    }
      self.return_scheduler().wait_code_signal();
    }

      if(get_condvar_bool(Arc::clone(&signal)) == false){
        self.return_scheduler().add_emit_signal(Arc::clone(&signal));
        if(self.return_scheduler().get_condvar_wake_up()){
          self.return_scheduler().wait_return_code();
        }
       // self.return_scheduler().notify_all_wake_up();
    }
      notify_thread(get_condvar(signal));
        

  }

      /********************************************/
  fn thread_await_signal(&self, signal: Arc<Signal>){

    if(self.return_scheduler().return_code() == true){
    if(*self.return_scheduler().NB_wait.lock().unwrap() == 0){
      if(self.return_scheduler().get_condvar_wake_up()){
          self.return_scheduler().wait_return_code();
         }
    self.return_scheduler().notify_all_wake_up();
    }
      self.return_scheduler().wait_code_signal();
    }


    let mut check = false;
     let condvar = signal.condition_variable.clone();
    {let ( num, cvar) = &*condvar;
  
    if(*num.lock().unwrap() == false){
      check = true;
      self.return_scheduler().add_nbwait();
      self.return_scheduler().add_wait_signal(signal);
    }
  }

    if(self.return_nb_thread() == (self.return_scheduler().return_length_list_wait() + *self.return_scheduler().NB_cooperate.lock().unwrap() + *self.return_scheduler().NB_finish.lock().unwrap()) as isize){
      if(self.return_scheduler().get_condvar_wake_up()){
          //println!("dans wait {:?}",  self.return_ID());
          self.return_scheduler().wait_return_code();
        }
        self.return_scheduler().notify_all_wake_up();
    }
      //println!("wait signal {:?}",  self.return_ID());
      let ( num, cvar) = &*condvar;
            {
            let mut start = num.lock().unwrap();

      
             while !*start {
                let current = cvar.wait(start).unwrap();
                start = current; 
            }
            if(check){
              self.return_scheduler().decrement_nbwait();
            }
         }
          
  }

  fn thread_await(& self, signal: Arc<Signal>){
      //wait un signal
      self.thread_await_signal(signal);

      let code = self.return_scheduler().return_code();
      signal_check!(code);

    }
    
      /****************************************************/

    fn thread_cooperate(&mut self){
      if(*self.return_scheduler().NB_instant.lock().unwrap() == *self.return_scheduler().next_instant.lock().unwrap()){
            self.return_scheduler().wait_instant(); 
          }
      
      if(self.return_scheduler().return_code() == true ){
        if(*self.return_scheduler().NB_wait.lock().unwrap() == 0){
      if(self.return_scheduler().get_condvar_wake_up()){
          self.return_scheduler().wait_cooperate_wake();
         }
      self.return_scheduler().notify_all_wake_up();
     }
      self.return_scheduler().wait_code_signal(); 

          }
        
      if(self.return_scheduler().get_condvar_wake_up()){
          self.return_scheduler().wait_cooperate_wake();
        }
      self.return_scheduler().add_nbcooperate();
      self.return_scheduler().notify_all_wake_up();
      self.return_scheduler().wait_cooperate(); 
      self.return_scheduler().decrement_cooperate();
      println!("=================finishh cooperate================> for the Thread {:?}\n", self.return_ID());
   }

   fn thread_cooperate_n(&mut self, instant: i32){
    for i in (0) ..(instant){
      self.thread_cooperate();
    }

   }
   
      /********************************************************/

    fn thread_execute(&mut self);
    fn thread_run(mut self) -> thread::JoinHandle<()> 
    where Self:'static + std::marker::Sync +  std::marker::Sized
    {

      let builder = thread::Builder::new()
    .name("Scheduler".into());
    builder.spawn(move || {
    self.return_scheduler().increment_nbthread();
    self.thread_execute();
    if(self.return_scheduler().get_condvar_wake_up()){
          self.return_scheduler().wait_finish();
        }
    self.return_scheduler().add_nbfinish();
    self.return_scheduler().notify_all_wake_up();
    //println!("\n=====================finishhh exucte=====================> {:?}\n", self.return_ID());

    }).unwrap()
    
      
  }
}
/******************************************************
                 Fonctions:
******************************************************/

pub fn get_condvar(signal: Arc<Signal>)-> Arc<(Mutex<bool>, Condvar)> {

  let condvar = signal.condition_variable.clone();
  condvar
}

fn get_condvar_bool(signal: Arc<Signal>) -> bool{
    let ( num, cvar) = &*signal.condition_variable;
    let mut start = num.lock().unwrap();
    return *start
   }

pub fn notify_thread(condvar: Arc<(Mutex<bool>, Condvar)>) {

    
      let &(ref num, ref cvar) = &*condvar;
            *num.lock().unwrap() = true;
            cvar.notify_all();
             
  }



}