This commit is contained in:
hiGepi 2022-11-22 15:17:33 +01:00
commit f1ebf554d1

View file

@ -10,12 +10,9 @@
#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];
@ -36,9 +33,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
@ -50,6 +56,22 @@ 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; int error;
@ -71,16 +93,20 @@ static unsigned int createSynchronizationObjects(void)
static void incrementProducedCount(void) static void incrementProducedCount(void)
{ {
//TODO //On incrément le nombre de threads producteurs
produceCount++; pCountLockTake();
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
p = produceCount; pCountLockTake();
return p; p = produceCount;
pCountLockRelease();
return p;
} }
@ -161,7 +187,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);
} }