Test and Set

master
stersky 2 years ago
parent fb8b7b40d4
commit 33f71e89f6

@ -10,12 +10,10 @@
#include "multitaskingAccumulator.h"
#include "iAcquisitionManager.h"
#include "debug.h"
#include <stdatomic.h>
//producer count storage
_Atomic int produceCount = 0;
volatile unsigned int producedCount = 0;
#include <sys/syscall.h>
#define gettid() syscall(SYS_gettid)
pthread_t producers[4];
@ -34,9 +32,18 @@ sem_t semaphore_occupe;
* Usefull variables
*/
MSG_BLOCK Buffer[BUFFER_SIZE];
_Atomic int index_libre = 0;
_Atomic int thread_count = 0;
//producer count storage
_Atomic int verrou = 0;
volatile unsigned int produceCount = 0;
/*
* Creates the synchronization elements.
* @return ERROR_SUCCESS if the init is ok, ERROR_INIT otherwise
@ -48,37 +55,59 @@ static unsigned int createSynchronizationObjects(void);
*/
static void incrementProducedCount(void);
static unsigned int createSynchronizationObjects(void)
{
int error;
//TODO DONE
if((error = sem_init(&semaphore_libre, 0, BUFFER_SIZE)) < 0){
printf("[acquisitionManager]Semaphore L Error No. %d\n",error);
return error;
}
// Méthode pour verrouiller un accès
static void pCountLockTake(){
int expected = 0;
//Tant que verrou ne vaut pas 0, on attend et expected vaut 0. Si verrou vaut bien 0, on le met à 1
while(!atomic_compare_exchange_weak(&verrou, &expected, 1))
expected = 0;
//La fonction se termine..
}
if((error = sem_init(&semaphore_occupe, 0, 0)) < 0){
printf("[acquisitionManager]Semaphore O Error No. %d\n",error);
return error;
}
// Méthode pour libérer un accès
static void pCountLockRelease(){
verrou = 1;
}
printf("[acquisitionManager]Semaphore created\n");
return ERROR_SUCCESS;
static unsigned int createSynchronizationObjects(void)
{
//Initialisation des sémaphores
semaphore_libre = sem_open("/acquisitionManager_semLibre", O_CREAT, 0644, BUFFER_SIZE);
if (semaphore_libre == SEM_FAILED)
{
perror("[sem_open");
return ERROR_INIT;
}
semaphore_occupe = sem_open("/acquisitionManager_semLibre", O_CREAT, 0644, 0);
if (semaphore_occupe == SEM_FAILED)
{
perror("[sem_open");
return ERROR_INIT;
}
//----Fin de l'initialisation sémaphore -----
printf("[acquisitionManager]Semaphore created\n");
return ERROR_SUCCESS;
}
static void incrementProducedCount(void)
{
//TODO
produceCount++;
//On incrément le nombre de threads producteurs
pCountLockTake();
produceCount++;
pCountLockRelease();
}
unsigned int getProducedCount(void)
{
unsigned int p = 0;
//TODO
p = produceCount;
return p;
unsigned int p = 0;
//On crée une section actomique
pCountLockTake();
p = produceCount;
pCountLockRelease();
return p;
}
@ -159,7 +188,7 @@ void *produce(void* params)
sem_post(&semaphore_occupe);
}
printf("[acquisitionManager] %d termination\n", gettid());
printf("[acquisitionManager] %d termination\n", syscall(gettid()));
//TODO
pthread_exit(NULL);
}
}

Loading…
Cancel
Save