Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ----------------------------------------------------------------------------
- Fonction int ThreadInit(void)
- Cette fonction est utilisée pour pour initialiser la librairie. Elle doit être
- appelée avant la toute première utilisation de vos threads.
- La fonction retourne:
- 1 si tout s'est bien passé
- -1 si vous avez une erreur lors de l'initialisation. */
- int ThreadInit(void){
- TCB *mainThread = malloc (sizeof (struct TCB));
- mainThread = malloc (sizeof(tid));
- mainThread = malloc(sizeof(EtatThread));
- mainThread = malloc(sizeof(ucontext_t));
- mainThread->pSuivant = malloc(sizeof(struct TCB));
- mainThread->pPrecedant = malloc(sizeof(struct TCB));
- mainThread = malloc(sizeof(int));
- mainThread->pWaitListJoinedThreads = malloc(sizeof(WaitList));
- mainThread->id = gNextTID;
- mainThread->etat = THREAD_EXECUTE;
- gNextTID++;
- gNTCB++;
- getcontext(&mainThread->ctx);
- mainThread->ctx.uc_stack.ss_sp = malloc(TAILLE_PILE);
- mainThread->ctx.uc_stack.ss_size = TAILLE_PILE;
- mainThread->pSuivant = mainThread;
- mainThread->pPrecedant = mainThread;
- gpThreadCourant = mainThread;
- if(GetPointerFromTid(100) == mainThread)
- return 1;
- else
- return -1;
- }
- /* ----------------------------------------------------------------------------
- Fonction tid ThreadCreer(void (*fn)(void *), void *arg)
- Cette fonction va créer un thread et le placer dans la file d'ordonnancement.
- Les entrées sont :
- void (*fn)(void *) : une fonction fn qui sera le code exécuté par un thread.
- void *arg : un pointeur sur les données passées au thread lors du démarrage.
- La fonction retourne :
- le tid du thread si tout s'est bien passé
- -1 si il y a une erreur. */
- tid ThreadCreer(void (*fn)(void *), void *arg){
- TCB *thread = malloc (sizeof (struct TCB));
- thread = malloc (sizeof(tid));
- thread = malloc(sizeof(EtatThread));
- thread = malloc(sizeof(ucontext_t));
- thread->pSuivant = malloc(sizeof(struct TCB));
- thread->pPrecedant = malloc(sizeof(struct TCB));
- thread = malloc(sizeof(int));
- thread->pWaitListJoinedThreads = malloc(sizeof(WaitList));
- thread->id = gNextTID;
- gNextTID++;
- thread->waitingCount = 0;
- thread->etat = THREAD_PRET;
- thread->pSuivant = thread;
- thread->pPrecedant = thread;
- getcontext(&thread->ctx);
- thread->ctx.uc_stack.ss_sp = malloc(TAILLE_PILE);
- thread->ctx.uc_stack.ss_size = TAILLE_PILE;
- makecontext(&thread->ctx, (void *)&fn, 1, arg);
- InsertTCBAfter(gpThreadCourant, thread);
- gpThreadCourant = thread;
- if(GetPointerFromTid(thread->id) == thread)
- return thread->id;
- else
- return -1;
- }
- /* ----------------------------------------------------------------------------
- Fonction void ThreadCeder(void)
- Cette fonction va faire passer le fil d'exécution au prochain thread (dans la file
- d'ordonnacement) qui est prêt à être exécuté. L'ordonnancement se fait selon
- l'algorithme du tourniquet. Il faut que le prochain thread à exécuter soit dans
- l'état THREAD_PRET. Il faut donc sauter tous les threads qui sont dans les
- états THREAD_BLOQUE ou THREAD_TERMINE. */
- void ThreadCeder(void){
- TCB *threadCourant = gpThreadCourant;
- printf("ThreadCeder():\n");
- while(gpThreadCourant->pSuivant != threadCourant)
- {
- printf("ThreadID:%d\tÉtat:%c\n", ThreadId(), ThreadEtat(gpThreadCourant->etat) );
- gpThreadCourant = gpThreadCourant->pSuivant;
- }
- printf("ThreadID:%d\tÉtat:%c\n", ThreadId(), ThreadEtat(gpThreadCourant->etat) );
- gpThreadCourant = gpThreadCourant->pSuivant;
- threadCourant = gpThreadCourant;
- while(gpThreadCourant->pSuivant->etat == THREAD_BLOQUE || gpThreadCourant->pSuivant->etat == THREAD_TERMINE)
- {
- gpThreadCourant = gpThreadCourant->pSuivant;
- }
- gpThreadCourant = gpThreadCourant->pSuivant;
- gpThreadCourant->etat = THREAD_EXECUTE;
- swapcontext(&threadCourant->ctx, &gpThreadCourant->ctx);
- }
- /* ----------------------------------------------------------------------------
- Fonction int ThreadJoindre(tid ThreadAJoindre)
- Cette fonction va joindre le thread en cours à l'exécution du thread ThreadAJoindre.
- Cela signifie que a) le thread qui appelle cette fonction bloquera en passant à
- l'état THREAD_BLOQUE b) lorsque le thread ThreadAJoindre quitte, il devra "réveiller"
- le thread appelant. Cette fonction devra invoquer ultimement la fonction ThreadCeder()
- pour faire exécuter un autre thread. La fonction retourne :
- -1 si ThreadAJoindre n'existe pas
- -2 si ThreadAJoindre a déjà terminé (i.e. est dans l'état THREAD_TERMINE).
- 1 si tout se passe bien. */
- int ThreadJoindre(tid ThreadAJoindre){
- TCB *TCBThreadAJoindre = GetPointerFromTid(ThreadAJoindre);
- if(TCBThreadAJoindre == NULL)
- return -1;
- else if(TCBThreadAJoindre->etat == THREAD_TERMINE)
- return -2;
- while(gpThreadCourant->etat != THREAD_EXECUTE)
- {
- gpThreadCourant = gpThreadCourant->pSuivant;
- }
- gpThreadCourant->etat = THREAD_BLOQUE;
- int i;
- for(i = 0; i < gpThreadCourant->waitingCount; i++)
- {
- TCBThreadAJoindre->pWaitListJoinedThreads->pThreadWaiting = TCBThreadAJoindre->pWaitListJoinedThreads->pNext->pThreadWaiting; // On trouve un espace libre pour ajouter un thread qui attend dans la waiting list
- }
- TCBThreadAJoindre->pWaitListJoinedThreads->pThreadWaiting = gpThreadCourant; // On dit que le thread main attend après ce thread (ThreadAJoindre)
- gpThreadCourant->waitingCount++;
- ThreadCeder();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement