mirror of
https://gitlab.com/klmp200/LO41.git
synced 2024-11-21 08:13:20 +00:00
Pthread KILL
This commit is contained in:
parent
415ef4807c
commit
3126e57610
@ -122,8 +122,10 @@ void _free__Building(THIS(Building)){
|
||||
DELETE(this->visitors);
|
||||
for (i=0; i<ELEVATOR_NB; i++)
|
||||
DELETE(this->elevators[i]);
|
||||
for (i=0; i<FLOORS; i++)
|
||||
for (i=0; i<FLOORS; i++) {
|
||||
pthread_cond_destroy(this->condition_floors[i]);
|
||||
free(this->condition_floors[i]);
|
||||
}
|
||||
free(this->condition_floors);
|
||||
free(this->mutex_cond_get_inside_elevator);
|
||||
free(this->mutex_cond_get_outside_elevator);
|
||||
@ -156,7 +158,7 @@ void go_to_floor_Building(THIS(Building), int origin, int destination, Passenger
|
||||
int elevator_number;
|
||||
|
||||
if (origin < 0 || origin >= FLOORS) {CRASH("You are trying to start from a non existing floor\n");}
|
||||
if (destination < 0 || origin >= FLOORS) {CRASH("You are trying to reach a non existing floor\n");}
|
||||
if (destination < 0 || destination >= FLOORS) {CRASH("You are trying to reach a non existing floor\n");}
|
||||
|
||||
pthread_cond_wait(this->condition_floors[origin], this->mutex_cond_get_inside_elevator);
|
||||
elevator_number = this->get_inside_elevator(this, origin, passenger, type);
|
||||
|
@ -71,6 +71,8 @@ void *runnable_Elevator(void * void_this){
|
||||
Elevator * this = (Elevator*) void_this;
|
||||
SharedData * data = GET_INSTANCE(SharedData);
|
||||
|
||||
AGENT_OPTIONS
|
||||
|
||||
printf("Je suis l'ascenseur %s\n", this->name);
|
||||
for (;;)
|
||||
data->main_building->signal_elevator_at_floor(data->main_building, this->get_floor(this));
|
||||
@ -79,7 +81,6 @@ void *runnable_Elevator(void * void_this){
|
||||
|
||||
Elevator *_init_Elevator(char * name){
|
||||
Elevator * new_elevator = malloc_or_die(sizeof(Elevator));
|
||||
new_elevator->state = WAITING;
|
||||
new_elevator->name = strdup(name);
|
||||
new_elevator->passenger_ids = NEW(List);
|
||||
pthread_mutex_init(&new_elevator->mutex_passenger, NULL);
|
||||
@ -98,5 +99,8 @@ Elevator *_init_Elevator(char * name){
|
||||
repair
|
||||
);
|
||||
|
||||
new_elevator->set_floor(new_elevator, 0);
|
||||
new_elevator->set_state(new_elevator, WAITING);
|
||||
|
||||
return new_elevator;
|
||||
}
|
@ -11,3 +11,8 @@ void *malloc_or_die(size_t size){
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void agent_exit(int error_code){
|
||||
printf("Agent receiving code %d, exiting\n", error_code);
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
13
Objects.h
13
Objects.h
@ -8,6 +8,9 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <pthread.h>
|
||||
|
||||
void agent_exit(int error_code);
|
||||
|
||||
#define NEW(type, ...) _init_##type(__VA_ARGS__)
|
||||
#define GET_INSTANCE(type, ...) _get_instance_##type(__VA_ARGS__)
|
||||
@ -15,7 +18,15 @@
|
||||
#define DESTRUCTOR(type) void (*_free_)(struct o_##type *this)
|
||||
#define THIS(type) type *this
|
||||
#define _THIS(type) struct o_##type *this
|
||||
#define CRASH(message) perror(message); raise(SIGINT)
|
||||
#define CRASH(message) perror(message); raise(SIGINT); pthread_exit(NULL)
|
||||
|
||||
#define AGENT_OPTIONS sigset_t set; \
|
||||
sigemptyset(&set); \
|
||||
sigaddset(&set, SIGINT); \
|
||||
signal(SIGUSR1, agent_exit); \
|
||||
pthread_sigmask(SIG_BLOCK, &set, NULL); \
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); \
|
||||
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
|
||||
|
||||
#define SYNCHRONIZE
|
||||
#define PRIVATE
|
||||
|
@ -3,6 +3,7 @@
|
||||
//
|
||||
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#include "Resident.h"
|
||||
|
||||
GETTER(Resident, char *, name);
|
||||
@ -11,6 +12,9 @@ GETTER(Resident, int, apartment_floor);
|
||||
|
||||
void * runnable_Resident(void * void_this){
|
||||
Resident * this = (Resident*) void_this;
|
||||
|
||||
AGENT_OPTIONS
|
||||
|
||||
printf("Je suis le resident %s et je suis a l'etage %d en direction de l'etage %d\n",
|
||||
this->name, this->apartment_floor, this->destination);
|
||||
return NULL;
|
||||
|
@ -81,8 +81,10 @@ int use_call_box_SharedData(THIS(SharedData), char * resident_name){
|
||||
void _free__SharedData(THIS(SharedData)){
|
||||
int i;
|
||||
if (this->threads != NULL){
|
||||
for (i=0; i<this->threads_nb; i++)
|
||||
pthread_cancel(this->threads[i]);
|
||||
for (i=0; i<this->threads_nb; i++) {
|
||||
printf("Quitting thread %d\n", (int) this->threads[i]);
|
||||
pthread_kill(this->threads[i], SIGUSR1);
|
||||
}
|
||||
free(this->threads);
|
||||
}
|
||||
if (this->main_building != NULL)
|
||||
|
@ -13,6 +13,9 @@ void * runnable_Visitor(void * void_this){
|
||||
Visitor *this = (Visitor*) void_this;
|
||||
SharedData * data = GET_INSTANCE(SharedData);
|
||||
Passenger passenger;
|
||||
|
||||
AGENT_OPTIONS
|
||||
|
||||
passenger.visitor = this;
|
||||
|
||||
printf("Bonjour, je suis %s et je souhaite rendre visite a %s\n", this->name, this->contact_name);
|
||||
|
Loading…
Reference in New Issue
Block a user