Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* ----------------------------------------------------------------------------
  2.    Fonction int ThreadInit(void)
  3.    Cette fonction est utilisée pour pour initialiser la librairie. Elle doit être
  4.    appelée avant la toute première utilisation de vos threads.
  5.    La fonction retourne:
  6.         1 si tout s'est bien passé
  7.        -1 si vous avez une erreur lors de l'initialisation. */
  8. int ThreadInit(void){
  9.     TCB *mainThread = malloc (sizeof (struct TCB));
  10.     mainThread = malloc (sizeof(tid));
  11.     mainThread = malloc(sizeof(EtatThread));
  12.     mainThread = malloc(sizeof(ucontext_t));
  13.     mainThread->pSuivant = malloc(sizeof(struct TCB));
  14.     mainThread->pPrecedant = malloc(sizeof(struct TCB));
  15.     mainThread = malloc(sizeof(int));
  16.     mainThread->pWaitListJoinedThreads = malloc(sizeof(WaitList));
  17.     mainThread->id = gNextTID;
  18.     mainThread->etat = THREAD_EXECUTE;
  19.   gNextTID++;
  20.   gNTCB++;
  21.   getcontext(&mainThread->ctx);
  22.   mainThread->ctx.uc_stack.ss_sp = malloc(TAILLE_PILE);
  23.   mainThread->ctx.uc_stack.ss_size = TAILLE_PILE;
  24.  
  25.   mainThread->pSuivant = mainThread;
  26.   mainThread->pPrecedant = mainThread;
  27.   gpThreadCourant = mainThread;
  28.   if(GetPointerFromTid(100) == mainThread)
  29.     return 1;
  30.   else
  31.     return -1;
  32. }
  33.  
  34. /* ----------------------------------------------------------------------------
  35.    Fonction tid ThreadCreer(void (*fn)(void *), void *arg)
  36.    Cette fonction va créer un thread et le placer dans la file d'ordonnancement.
  37.    Les entrées sont :
  38.       void (*fn)(void *) : une fonction fn qui sera le code exécuté par un thread.
  39.       void *arg          : un pointeur sur les données passées au thread lors du démarrage.
  40.    La fonction retourne :
  41.         le tid du thread si tout s'est bien passé
  42.        -1 si il y a une erreur. */
  43. tid ThreadCreer(void (*fn)(void *), void *arg){
  44.  
  45.   TCB *thread = malloc (sizeof (struct TCB));
  46.   thread = malloc (sizeof(tid));
  47.   thread = malloc(sizeof(EtatThread));
  48.   thread = malloc(sizeof(ucontext_t));
  49.   thread->pSuivant = malloc(sizeof(struct TCB));
  50.   thread->pPrecedant = malloc(sizeof(struct TCB));
  51.   thread = malloc(sizeof(int));
  52.   thread->pWaitListJoinedThreads = malloc(sizeof(WaitList));
  53.  
  54.   thread->id = gNextTID;
  55.  
  56.   gNextTID++;
  57.   thread->waitingCount = 0;
  58.   thread->etat = THREAD_PRET;
  59.     thread->pSuivant = thread;
  60.     thread->pPrecedant = thread;
  61.   getcontext(&thread->ctx);
  62.   thread->ctx.uc_stack.ss_sp = malloc(TAILLE_PILE);
  63.   thread->ctx.uc_stack.ss_size = TAILLE_PILE;
  64.   makecontext(&thread->ctx, (void *)&fn, 1, arg);
  65.   InsertTCBAfter(gpThreadCourant, thread);
  66.   gpThreadCourant = thread;
  67.   if(GetPointerFromTid(thread->id) == thread)
  68.     return thread->id;
  69.   else
  70.     return -1;
  71. }
  72.  
  73. /* ----------------------------------------------------------------------------
  74.    Fonction void ThreadCeder(void)
  75.    Cette fonction va faire passer le fil d'exécution au prochain thread (dans la file
  76.    d'ordonnacement) qui est prêt à être exécuté. L'ordonnancement se fait selon
  77.    l'algorithme du tourniquet. Il faut que le prochain thread à exécuter soit dans
  78.    l'état THREAD_PRET. Il faut donc sauter tous les threads qui sont dans les
  79.    états THREAD_BLOQUE ou THREAD_TERMINE. */
  80. void ThreadCeder(void){
  81.   TCB *threadCourant = gpThreadCourant;
  82.   printf("ThreadCeder():\n");
  83.   while(gpThreadCourant->pSuivant != threadCourant)
  84.   {
  85.     printf("ThreadID:%d\tÉtat:%c\n", ThreadId(), ThreadEtat(gpThreadCourant->etat) );
  86.     gpThreadCourant = gpThreadCourant->pSuivant;  
  87.   }
  88.   printf("ThreadID:%d\tÉtat:%c\n", ThreadId(), ThreadEtat(gpThreadCourant->etat) );
  89.   gpThreadCourant = gpThreadCourant->pSuivant;
  90.     threadCourant = gpThreadCourant;
  91.   while(gpThreadCourant->pSuivant->etat == THREAD_BLOQUE || gpThreadCourant->pSuivant->etat == THREAD_TERMINE)
  92.   {
  93.     gpThreadCourant = gpThreadCourant->pSuivant;
  94.   }    
  95.   gpThreadCourant = gpThreadCourant->pSuivant;
  96.   gpThreadCourant->etat = THREAD_EXECUTE;
  97.  
  98.   swapcontext(&threadCourant->ctx, &gpThreadCourant->ctx);
  99. }
  100. /* ----------------------------------------------------------------------------
  101.    Fonction int ThreadJoindre(tid ThreadAJoindre)
  102.    Cette fonction va joindre le thread en cours à l'exécution du thread ThreadAJoindre.
  103.    Cela signifie que a) le thread qui appelle cette fonction bloquera en passant à
  104.    l'état THREAD_BLOQUE b) lorsque le thread ThreadAJoindre quitte, il devra "réveiller"
  105.    le thread appelant. Cette fonction devra invoquer ultimement la fonction ThreadCeder()
  106.    pour faire exécuter un autre thread. La fonction retourne :
  107.          -1 si ThreadAJoindre n'existe pas
  108.          -2 si ThreadAJoindre a déjà terminé (i.e. est dans l'état THREAD_TERMINE).
  109.           1 si tout se passe bien. */
  110. int ThreadJoindre(tid ThreadAJoindre){
  111.  
  112.   TCB *TCBThreadAJoindre = GetPointerFromTid(ThreadAJoindre);
  113.  
  114.   if(TCBThreadAJoindre == NULL)
  115.     return -1;
  116.   else if(TCBThreadAJoindre->etat == THREAD_TERMINE)
  117.     return -2;
  118.   while(gpThreadCourant->etat != THREAD_EXECUTE)
  119.   {
  120.     gpThreadCourant = gpThreadCourant->pSuivant;
  121.   }
  122.   gpThreadCourant->etat = THREAD_BLOQUE;
  123.   int i;
  124.   for(i = 0; i < gpThreadCourant->waitingCount; i++)
  125.   {
  126.     TCBThreadAJoindre->pWaitListJoinedThreads->pThreadWaiting = TCBThreadAJoindre->pWaitListJoinedThreads->pNext->pThreadWaiting;   // On trouve un espace libre pour ajouter un thread qui attend dans la waiting list
  127.   }  
  128.   TCBThreadAJoindre->pWaitListJoinedThreads->pThreadWaiting = gpThreadCourant;  // On dit que le thread main attend après ce thread (ThreadAJoindre)
  129.   gpThreadCourant->waitingCount++;
  130.   ThreadCeder();
  131.   return 1;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement