-
Notifications
You must be signed in to change notification settings - Fork 0
/
ud_thread.h
216 lines (198 loc) · 4.76 KB
/
ud_thread.h
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* ud_thread.h
*
* Authors: Sophia Freaney and Connor Onweller
*
* Purpose: Declares and describes thread library functions
* as well as semaphore functions
*/
#include "t_lib.h"
/*
* Function: t_create
* ------------------
* Creates a thread with priority 'pri' running the function 'fct' with thread
*id of 'id' Creates a tcb representation of the new thread and adds it to the
*end of the ready queue
*
* Returns: id
*/
int t_create(void (*fct)(int), int id, int pri);
/*
* Function: t_yield
* ------------------
* The calling thread voluntarily relinquishes the CPU and is placed at the
* end of the ready queue. The first thread in the ready queue resumes execution
* (if there is one).
*
* Returns: void
*/
void t_yield(void);
/*
* Function: t_init
* ------------------
* Initializes the thread library by setting up the running and the ready
*queues; it initializes the running queue with a tcb representation of the main
*thread.
*
* Return: void
*
*/
void t_init(void);
/*
* Function: t_shutdown
* ------------------
* Shuts down the thread library and frees all the dynamically allocated memory
* for all threads in the tcb queues
*
* Return: void
*/
void t_shutdown(void);
/*
* Function: t_terminate
* ------------------
* Terminates the calling thread by removing and freeing its tcb from the
* running queue while resuming the execution of the thread with the thread at
* the head of the ready queue
*
* Return: void
*/
void t_terminate(void);
///////////////////////////////// SEMAPHORES //////////////////////////////////
/*
* Function: sem_init
* ------------------
* Initializes semaphore and its parameters: its count (int) and a queue of waiting
* threads (*tcb)
*
* sp: pointer to semaphore to be initialized
* count: initial int value of sp
*
* Returns: sem_count (int)
*
*/
int sem_init(sem_t **sp, unsigned int count);
/*
* Function: sem_wait
* ------------------
* If the semaphore count is less than 1, it adds the current running thread to the waiting
* queue pointed to by the semaphore and dequeues a new thread from the ready queue
* to be run. Once the semaphore count is at least one, the semaphore is decremented by 1
*
* sp: semaphore used in thread synchronization
*
* Returns: void
*
*/
void sem_wait(sem_t *sp);
/*
* Function: sem_signal
* ------------------
* Increments semaphore by 1, then wakes up a thread (if there is one available) by
* removing it from the waiting queue and adding it to the ready queue
*
* sp: semaphore used in thread synchronization
*
* Returns: void
*
*/
void sem_signal(sem_t *sp);
/*
* Function: sem_destroy
* ------------------
* Frees semaphore memory
*
* sp: pointer to semaphore used in thread synchronization
*
* Returns: void
*
*/
void sem_destroy(sem_t **sp);
////////////////////////////// MESSAGING //////////////////////////////////////
/*
* Function: mbox_create
* ------------------
* Creates a message box, which contains a message string and a semaphore
*
* mb: pointer to mbox pointer to be created
*
* Returns: int
*
*/
int mbox_create(mbox **mb);
/*
* Function: mbox_destroy_helper
* ------------------
* Assists mbox destroy by deleting the mbox and its message list
*
* mb: mbox to be destroyed
*
* Returns: void
*
*/
void mbox_destroy_helper(mbox *mb);
/*
* Function: mbox_destroy
* ------------------
* Deletes the mbox_list as well as the input mbox, which can be called from main
*
* mb: pointer to mbox to be destroyed
*
* Returns: void
*
*/
void mbox_destroy(mbox **mb);
/*
* Function: mbox_deposit
* ------------------
* Sends message from one thread to another
*
* mb: mbox to send message to
* msg: string of message
* len: length of msg
*
* Returns: void
*
*/
void mbox_deposit(mbox *mb, char *msg, int len);
/*
* Function: mbox_withdraw
* ------------------
* Removes message from mbox and deletes the mbox
*
* mb: mbox from which to obtain message
* msg: message obtained
* len: length of message obtained
*
* Returns: void
*
*/
void mbox_withdraw(mbox *mb, char *msg, int *len);
/*
* Function: send
* ------------------
* Send a message to a thread
*
* tid: id of thread to be sent message
* msg: message to send
* len: length of msg
*
* Returns: void
*
*/
void send(int tid, char *msg, int len);
/*
* Function: receive
* ------------------
* Wait for and receive a message from another thread.
* The caller has to specify the sender's tid in tid, or sets tid to 0
* if it intends to receive a message sent by any thread. If there is no
* "matching" message to receive, the calling thread waits
*
* tid: id of matching thread
* msg: message received
* len: length of msg
*
* Returns: void
*
*/
void receive(int *tid, char *msg, int *len);