Test and Set
This commit is contained in:
parent
fb8b7b40d4
commit
33f71e89f6
1 changed files with 57 additions and 28 deletions
|
@ -10,12 +10,10 @@
|
||||||
#include "multitaskingAccumulator.h"
|
#include "multitaskingAccumulator.h"
|
||||||
#include "iAcquisitionManager.h"
|
#include "iAcquisitionManager.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
#include <stdatomic.h>
|
||||||
|
|
||||||
|
#include <sys/syscall.h>
|
||||||
//producer count storage
|
#define gettid() syscall(SYS_gettid)
|
||||||
_Atomic int produceCount = 0;
|
|
||||||
|
|
||||||
volatile unsigned int producedCount = 0;
|
|
||||||
|
|
||||||
pthread_t producers[4];
|
pthread_t producers[4];
|
||||||
|
|
||||||
|
@ -34,9 +32,18 @@ sem_t semaphore_occupe;
|
||||||
* Usefull variables
|
* Usefull variables
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
MSG_BLOCK Buffer[BUFFER_SIZE];
|
||||||
_Atomic int index_libre = 0;
|
_Atomic int index_libre = 0;
|
||||||
_Atomic int thread_count = 0;
|
_Atomic int thread_count = 0;
|
||||||
|
|
||||||
|
|
||||||
|
//producer count storage
|
||||||
|
_Atomic int verrou = 0;
|
||||||
|
volatile unsigned int produceCount = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Creates the synchronization elements.
|
* Creates the synchronization elements.
|
||||||
* @return ERROR_SUCCESS if the init is ok, ERROR_INIT otherwise
|
* @return ERROR_SUCCESS if the init is ok, ERROR_INIT otherwise
|
||||||
|
@ -48,36 +55,58 @@ static unsigned int createSynchronizationObjects(void);
|
||||||
*/
|
*/
|
||||||
static void incrementProducedCount(void);
|
static void incrementProducedCount(void);
|
||||||
|
|
||||||
|
// 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..
|
||||||
|
}
|
||||||
|
|
||||||
|
// Méthode pour libérer un accès
|
||||||
|
static void pCountLockRelease(){
|
||||||
|
verrou = 1;
|
||||||
|
}
|
||||||
|
|
||||||
static unsigned int createSynchronizationObjects(void)
|
static unsigned int createSynchronizationObjects(void)
|
||||||
{
|
{
|
||||||
int error;
|
//Initialisation des sémaphores
|
||||||
|
semaphore_libre = sem_open("/acquisitionManager_semLibre", O_CREAT, 0644, BUFFER_SIZE);
|
||||||
//TODO DONE
|
if (semaphore_libre == SEM_FAILED)
|
||||||
if((error = sem_init(&semaphore_libre, 0, BUFFER_SIZE)) < 0){
|
{
|
||||||
printf("[acquisitionManager]Semaphore L Error No. %d\n",error);
|
perror("[sem_open");
|
||||||
return error;
|
return ERROR_INIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if((error = sem_init(&semaphore_occupe, 0, 0)) < 0){
|
semaphore_occupe = sem_open("/acquisitionManager_semLibre", O_CREAT, 0644, 0);
|
||||||
printf("[acquisitionManager]Semaphore O Error No. %d\n",error);
|
if (semaphore_occupe == SEM_FAILED)
|
||||||
return error;
|
{
|
||||||
|
perror("[sem_open");
|
||||||
|
return ERROR_INIT;
|
||||||
}
|
}
|
||||||
|
//----Fin de l'initialisation sémaphore -----
|
||||||
printf("[acquisitionManager]Semaphore created\n");
|
printf("[acquisitionManager]Semaphore created\n");
|
||||||
return ERROR_SUCCESS;
|
return ERROR_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void incrementProducedCount(void)
|
static void incrementProducedCount(void)
|
||||||
{
|
{
|
||||||
//TODO
|
//On incrément le nombre de threads producteurs
|
||||||
|
pCountLockTake();
|
||||||
produceCount++;
|
produceCount++;
|
||||||
|
pCountLockRelease();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int getProducedCount(void)
|
unsigned int getProducedCount(void)
|
||||||
{
|
{
|
||||||
unsigned int p = 0;
|
unsigned int p = 0;
|
||||||
//TODO
|
//On crée une section actomique
|
||||||
|
pCountLockTake();
|
||||||
p = produceCount;
|
p = produceCount;
|
||||||
|
pCountLockRelease();
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,7 +188,7 @@ void *produce(void* params)
|
||||||
|
|
||||||
sem_post(&semaphore_occupe);
|
sem_post(&semaphore_occupe);
|
||||||
}
|
}
|
||||||
printf("[acquisitionManager] %d termination\n", gettid());
|
printf("[acquisitionManager] %d termination\n", syscall(gettid()));
|
||||||
//TODO
|
//TODO
|
||||||
pthread_exit(NULL);
|
pthread_exit(NULL);
|
||||||
}
|
}
|
Loading…
Reference in a new issue