Add VMap lock

This commit is contained in:
augustin64 2024-04-19 09:57:02 +02:00
parent 9bb7f65de1
commit 130eaa54f0
4 changed files with 148 additions and 64 deletions

14
TP/TP2/rendu/README.md Normal file
View File

@ -0,0 +1,14 @@
# TP2
## Tests
Exécuter tous les tests et s'arrêter quand l'un produit un résultat différent de celui attendu
```bash
./make.sh
```
## Implémentation
Pour les différents processus:
- une structure de `(VMap, pid) list` est utilisée pour gérer les VMaps reliées à chaque processus
- Dès qu'une VMap est récupérée, elle est verrouillée (Un mutex par VMap), ne fonctionne pas

View File

@ -0,0 +1,56 @@
#include <string.h>
#include <stdlib.h>
#include "../../vmap.h"
#define N 6
struct args {
Memory* mem;
int count;
int size;
int pid;
};
void* thread(void* void_args) {
struct args* args = (struct args*)void_args;
Memory* mem = args->mem;
int count = args->count;
int size = args->size;
int pid = args->pid;
void** ptrs = malloc(sizeof(void*)*count);
for (int i=0; i < count; i++) {
ptrs[i] = my_malloc(mem, size, pid);
}
for (int i=0; i < count; i++) {
my_free(mem, ptrs[i], pid);
}
free(ptrs);
return NULL;
}
int main() {
struct args* args = malloc(sizeof(struct args));
int pid = 12;
Memory* mem = memory_init(pid);
args->mem = mem;
args->pid = pid;
args->count = 20;
args->size = 4;
pthread_t tid[N];
for (int i=0; i < N; i++) {
pthread_create(&(tid[i]), NULL, &thread, (void*)args);
}
for (int i=0; i < N; i++) {
pthread_join(tid[i], NULL);
}
return 0;
}

View File

@ -103,6 +103,7 @@ void* my_malloc(Memory* mem, int size, int pid) {
}
VMap* vmap = memory_get(mem, pid);
pthread_mutex_lock(&vmap->lock);
// Parcours de la liste pour trouver un candidat. Sinon, nouvelle page
node* current = vmap->pages->head;
while (current) {
@ -112,6 +113,7 @@ void* my_malloc(Memory* mem, int size, int pid) {
page->cursor += size;
page->nb_allocs++;
pthread_mutex_unlock(&vmap->lock);
return ptr;
}
current = current->next;
@ -123,15 +125,17 @@ void* my_malloc(Memory* mem, int size, int pid) {
page->cursor += size;
page->nb_allocs++;
pthread_mutex_unlock(&vmap->lock);
return ptr;
}
void my_free(Memory* mem, void* ptr, int pid) {
VMap* vmap = memory_get(mem, pid);
pthread_mutex_lock(&vmap->lock);
PageMapInfo* page = vmap_get_page(vmap, ptr);
if (!page) {
fprintf(stderr, "my_free: Double free or memory corrupted\n");
fprintf(stderr, "my_free: double free or memory corrupted\n");
exit(1);
}
@ -139,10 +143,12 @@ void my_free(Memory* mem, void* ptr, int pid) {
if (page->nb_allocs == 0) {
vmap_free_page(vmap, page);
}
pthread_mutex_unlock(&vmap->lock);
}
void my_copy(Memory* mem, void* src, void* dst, int size, int pid) {
VMap* vmap = memory_get(mem, pid);
pthread_mutex_lock(&vmap->lock);
PageMapInfo* page_map_src = vmap_get_page(vmap, src);
PageMapInfo* page_map_dst = vmap_get_page(vmap, dst);
@ -172,10 +178,12 @@ void my_copy(Memory* mem, void* src, void* dst, int size, int pid) {
PageInfo* page_dst = page_lookup(vmap->mmap, page_map_dst->key);
memcpy(page_src->data+((int64_t)dst % PAGE_SIZE), page_dst->data+((int64_t)src % PAGE_SIZE), size);
pthread_mutex_unlock(&vmap->lock);
}
void vmap_copy_to_memory(Memory* mem, void *src, void *dst, int size, int pid) {
VMap* vmap = memory_get(mem, pid);
pthread_mutex_lock(&vmap->lock);
PageMapInfo* page_map = vmap_get_page(vmap, src);
PageInfo* page = page_lookup(vmap->mmap, page_map->key);
@ -185,10 +193,12 @@ void vmap_copy_to_memory(Memory* mem, void *src, void *dst, int size, int pid) {
}
memcpy(dst, page->data + ((int64_t)src % PAGE_SIZE), size);
pthread_mutex_unlock(&vmap->lock);
}
void vmap_copy_from_memory(Memory* mem, void *src, void *dst, int size, int pid) {
VMap* vmap = memory_get(mem, pid);
pthread_mutex_lock(&vmap->lock);
PageMapInfo* page_map = vmap_get_page(vmap, dst);
PageInfo* page = page_lookup(vmap->mmap, page_map->key);
@ -198,4 +208,5 @@ void vmap_copy_from_memory(Memory* mem, void *src, void *dst, int size, int pid)
}
memcpy(page->data + ((int64_t)dst % PAGE_SIZE), src, size);
pthread_mutex_unlock(&vmap->lock);
}

View File

@ -1,12 +1,15 @@
#ifndef DEF_VMAP_H
#define DEF_VMAP_H
#include <pthread.h>
#include "mmap.h"
typedef struct VMap {
MMap* mmap;
linkedList* pages;
int index; // index of virt memory
pthread_mutex_t lock;
int index; // size already alloc'ed
} VMap;
typedef struct Memory { // List of VMaps