summaryrefslogtreecommitdiff
path: root/src/kernel/ipc/object.c
blob: e381c89be96527a8226dc3b1012dfd32dd68f621 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "object.h"
#include <lib/mutex.h>
#include <mem/mem.h>

struct object* obj_new(struct process* owner) {
	struct object* ret = kmalloc(sizeof(struct object));
	ret->owner = owner;
	ret->descriptors = 0;
	ret->busyMutex = MUTEX_UNLOCKED;
	ret->request = 0;
	return ret;
}

void obj_delete(struct object* obj) {
	if (obj->descriptors > 0) return;
	if (obj->busyMutex != MUTEX_UNLOCKED) return;
	if (obj->request != 0) return;
	kfree(obj);
}

int obj_createP(struct process* p) {
	return objdesc_add(p, obj_new(p));
}

void obj_closeP(struct process* p, int id) {
	struct object* obj = objdesc_read(p, id);
	if (obj == 0) return;
	objdesc_rm(p, id);
	if (obj->owner == p) {
		if (obj->descriptors > 0) {	//TODO !!!
			obj->owner = 0; // set object to be invalid
			//if a request was being handled, set it to interrupted (acknowledged = 3) and wake up receiver thread or if nonblocking delete it
			//unlock objects busymutex
		} else {
			obj_delete(obj);
		}
	} else {
		if (obj->descriptors == 0 && obj->owner == 0) {
			obj_delete(obj);
		} else if (obj->descriptors == 1 && obj->owner != 0) {
			//future : send message becuz object closed for everyone
		}
	}
}

void obj_closeall(struct process* p) {
	while (p->objects != 0) obj_closeP(p, p->objects->id);
}

// DESCRIPTORS

int objdesc_add(struct process* proc, struct object* obj) {
	int tmp = objdesc_get(proc, obj);
	if (tmp != -1) { return -1; }	//signal that a descriptor already exists
	struct obj_descriptor *ret = kmalloc(sizeof(struct obj_descriptor));
	ret->obj = obj;
	ret->id = proc->next_objdesc;
	ret->next = proc->objects;
	proc->objects = ret;
	obj->descriptors++;
	proc->next_objdesc++;
	return ret->id;
}

int objdesc_get(struct process* proc, struct object* obj) {
	struct obj_descriptor *it = proc->objects;
	while (it != 0) {
		if (it->obj == obj) return it->id;
		it = it->next;
	}
	return -1;
}

struct object* objdesc_read(struct process* proc, int id) {
	struct obj_descriptor *it = proc->objects;
	while (it != 0) {
		if (it->id == id) return it->obj;
		it = it->next;
	}
	return 0;
}

void objdesc_rm(struct process* proc, int id) {
	struct obj_descriptor *e = proc->objects;
	if (e != 0 && e->id == id) {
		proc->objects = e->next;
		e->obj->descriptors--;
		kfree(e);
		return;
	}
	while (e->next != 0) {
		if (e->next->id == id) {
			e->next = e->next->next;
			e->next->obj->descriptors--;
			kfree(e->next);
			return;
		}
		e = e->next;
	}
}

// SYSCALLS

int object_create() {
	return obj_createP(current_thread->process);
}

int object_owned(int id) {
	struct object *obj = objdesc_read(current_thread->process, id);
	if (obj == 0) return -10;
	if (obj->owner == current_thread->process) return 1;
	return 0;
}

void object_close(int id) {
	obj_closeP(current_thread->process, id);
}