145 lines
2.8 KiB
C
145 lines
2.8 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <semaphore.h>
|
||
|
#include <unistd.h>
|
||
|
#include <pthread.h>
|
||
|
#include <fcntl.h>
|
||
|
#include "acquisitionManager.h"
|
||
|
#include "msg.h"
|
||
|
#include "iSensor.h"
|
||
|
#include "mySoftware.h"
|
||
|
#include "iAcquisitionManager.h"
|
||
|
#include "debug.h"
|
||
|
|
||
|
|
||
|
//producer count storage
|
||
|
volatile unsigned int produceCount = 0;
|
||
|
|
||
|
pthread_t producers[4];
|
||
|
|
||
|
static void *produce(void *ithread);
|
||
|
|
||
|
/**
|
||
|
* Semaphores and Mutex
|
||
|
*/
|
||
|
pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
|
||
|
|
||
|
// Ring buffer
|
||
|
MSG_BLOCK buffer[100];
|
||
|
unsigned int buf_head = 0;
|
||
|
unsigned int buf_tail = 0;
|
||
|
|
||
|
/*
|
||
|
* Creates the synchronization elements.
|
||
|
* @return ERROR_SUCCESS if the init is ok, ERROR_INIT otherwise
|
||
|
*/
|
||
|
static unsigned int createSynchronizationObjects(void);
|
||
|
|
||
|
/*
|
||
|
* Increments the produce count.
|
||
|
*/
|
||
|
static void incrementProducerCount(void);
|
||
|
|
||
|
static unsigned int createSynchronizationObjects(void)
|
||
|
{
|
||
|
//TODO
|
||
|
printf("[acquisitionManager]Semaphore created\n");
|
||
|
return ERROR_SUCCESS;
|
||
|
}
|
||
|
|
||
|
static void incrementProducerCount(void)
|
||
|
{
|
||
|
pthread_mutex_lock(&m1);
|
||
|
produceCount++;
|
||
|
pthread_mutex_unlock(&m1);
|
||
|
}
|
||
|
|
||
|
unsigned int getProducerCount(void)
|
||
|
{
|
||
|
unsigned int p = 0;
|
||
|
pthread_mutex_lock(&m1);
|
||
|
p = produceCount;
|
||
|
pthread_mutex_unlock(&m1);
|
||
|
return p;
|
||
|
}
|
||
|
|
||
|
void getMessage(volatile MSG_BLOCK* mBlock) {
|
||
|
volatile MSG_BLOCK msg;
|
||
|
char done = 0;
|
||
|
|
||
|
// Voir TD Vincent David
|
||
|
// Là c'est une version de personne frustrée ne correspondant pas au TD
|
||
|
while (!done) {
|
||
|
pthread_mutex_lock(&m1);
|
||
|
if (buf_tail > buf_head) {
|
||
|
msg = buffer[buf_tail];
|
||
|
buf_tail++;
|
||
|
done = 1;
|
||
|
}
|
||
|
pthread_mutex_unlock(&m1);
|
||
|
}
|
||
|
*mBlock = msg;
|
||
|
}
|
||
|
|
||
|
unsigned int acquisitionManagerInit(void)
|
||
|
{
|
||
|
unsigned int i;
|
||
|
printf("[acquisitionManager]Synchronization initialization in progress...\n");
|
||
|
fflush( stdout );
|
||
|
if (createSynchronizationObjects() == ERROR_INIT)
|
||
|
return ERROR_INIT;
|
||
|
|
||
|
printf("[acquisitionManager]Synchronization initialization done.\n");
|
||
|
|
||
|
for (i = 0; i < PRODUCER_COUNT; i++)
|
||
|
{
|
||
|
pthread_create(&producers[i], NULL, &produce, &i);
|
||
|
}
|
||
|
|
||
|
return ERROR_SUCCESS;
|
||
|
}
|
||
|
|
||
|
void acquisitionManagerJoin(void)
|
||
|
{
|
||
|
unsigned int i;
|
||
|
for (i = 0; i < PRODUCER_COUNT; i++)
|
||
|
{
|
||
|
pthread_join(producers[i], NULL);
|
||
|
}
|
||
|
|
||
|
//TODO
|
||
|
printf("[acquisitionManager]Semaphore cleaned\n");
|
||
|
}
|
||
|
|
||
|
void *produce(void *ithread)
|
||
|
{
|
||
|
D(printf("[acquisitionManager]Producer created with id %d\n", gettid()));
|
||
|
unsigned int i = 0;
|
||
|
MSG_BLOCK msg;
|
||
|
char done = 0;
|
||
|
while (i < PRODUCER_LOOP_LIMIT)
|
||
|
{
|
||
|
i++;
|
||
|
sleep(PRODUCER_SLEEP_TIME+(rand() % 5));
|
||
|
|
||
|
// Acquire input
|
||
|
getInput(*(int *)ithread, &msg);
|
||
|
|
||
|
// Store in ring buffer
|
||
|
pthread_mutex_lock(&m1);
|
||
|
while (!done) {
|
||
|
pthread_mutex_lock(&m1);
|
||
|
if (buf_head < 100) {
|
||
|
buffer[buf_head] = msg;
|
||
|
buf_head++;
|
||
|
done = 1;
|
||
|
}
|
||
|
pthread_mutex_unlock(&m1);
|
||
|
}
|
||
|
pthread_mutex_unlock(&m1);
|
||
|
|
||
|
}
|
||
|
printf("[acquisitionManager] %ld termination\n", gettid());
|
||
|
pthread_exit(NULL);
|
||
|
}
|