M2_SETI/T2/tp/exercice-1/messageAdder.c
2022-11-22 14:39:55 +01:00

84 lines
1.5 KiB
C
Executable file

#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <unistd.h>
#include <pthread.h>
#include "messageAdder.h"
#include "msg.h"
#include "iMessageAdder.h"
#include "multitaskingAccumulator.h"
#include "iAcquisitionManager.h"
#include "debug.h"
//consumer thread
pthread_t consumer;
//Message computed
volatile MSG_BLOCK out;
//Consumer count storage
volatile unsigned int consumeCount = 0;
/**
* Increments the consume count.
*/
static void incrementConsumeCount(void);
/**
* Consumer entry point.
*/
static void *sum( void *parameters );
MSG_BLOCK getCurrentSum(){
//TODO DONE
return out;
}
unsigned int getConsumedCount(){
//TODO DONE
return consumeCount;
}
void messageAdderInit(void){
out.checksum = 0;
for (size_t i = 0; i < DATA_SIZE; i++)
{
out.mData[i] = 0;
}
//TODO DONE
pthread_create(&consumer, NULL, sum, NULL);
}
void messageAdderJoin(void){
//TODO DONE
pthread_join(consumer, NULL);
}
static void incrementConsumeCount(void){
// pthread_mutex_lock(&m_count);
consumeCount++;
// pthread_mutex_unlock(&m_count);
}
static void *sum( void *parameters )
{
D(printf("[messageAdder]Thread created for sum with id %d\n", gettid()));
unsigned int i = 0;
MSG_BLOCK message;
while(i<ADDER_LOOP_LIMIT){
i++;
sleep(ADDER_SLEEP_TIME);
//TODO DONE
message = getMessage();
if(messageCheck(&message)){
messageAdd(&out,&message);
incrementConsumeCount();
}
// getCurrentSum();
}
printf("[messageAdder] %d termination\n", gettid());
//TODO DONE
pthread_exit(NULL);
}