M2_SETI/T2/tp/exercice-1/messageAdder.c

85 lines
1.5 KiB
C
Raw Normal View History

2022-11-18 15:07:43 +01:00
#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(){
2022-11-19 16:11:18 +01:00
//TODO DONE
return out;
2022-11-18 15:07:43 +01:00
}
unsigned int getConsumedCount(){
//TODO
2022-11-19 16:11:18 +01:00
return consumeCount;
2022-11-18 15:07:43 +01:00
}
void messageAdderInit(void){
out.checksum = 0;
for (size_t i = 0; i < DATA_SIZE; i++)
{
out.mData[i] = 0;
}
2022-11-19 16:11:18 +01:00
//TODO DONE
pthread_create(&consumer, NULL, sum, NULL);
2022-11-18 15:07:43 +01:00
}
void messageAdderJoin(void){
2022-11-19 16:11:18 +01:00
//TODO DONE
pthread_join(consumer, NULL);
}
static void incrementConsumeCount(void){
// pthread_mutex_lock(&m_count);
consumeCount++;
// pthread_mutex_unlock(&m_count);
2022-11-18 15:07:43 +01:00
}
static void *sum( void *parameters )
{
2022-11-19 16:11:18 +01:00
D(printf("[messageAdder]Thread created for sum with id %d\n", getpid()));
2022-11-18 15:07:43 +01:00
unsigned int i = 0;
2022-11-19 16:11:18 +01:00
MSG_BLOCK message;
2022-11-18 15:07:43 +01:00
while(i<ADDER_LOOP_LIMIT){
i++;
sleep(ADDER_SLEEP_TIME);
//TODO
2022-11-19 16:11:18 +01:00
message = getMessage();
if(messageCheck(&message)){
messageAdd(&out,&message);
incrementConsumeCount();
}
// getCurrentSum();
2022-11-18 15:07:43 +01:00
}
2022-11-19 16:11:18 +01:00
printf("[messageAdder] %d termination\n", getpid());
2022-11-18 15:07:43 +01:00
//TODO
2022-11-19 16:11:18 +01:00
pthread_exit(NULL);
2022-11-18 15:07:43 +01:00
}