-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuthreads.h
131 lines (107 loc) · 4.88 KB
/
uthreads.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
/*
* User-Level Threads Library (uthreads)
* Hebrew University OS course.
* Author: OS, [email protected]
*/
#ifndef _UTHREADS_H
#define _UTHREADS_H
#define MAX_THREAD_NUM 100 /* maximal number of threads */
#define STACK_SIZE 4096 /* stack size per thread (in bytes) */
typedef void (*thread_entry_point)(void);
/* External interface */
/**
* @brief initializes the thread library.
*
* Once this function returns, the main thread (tid == 0) will be set as RUNNING. There is no need to
* provide an entry_point or to create a stack for the main thread - it will be using the "regular" stack and PC.
* You may assume that this function is called before any other thread library function, and that it is called
* exactly once.
* The input to the function is the length of a quantum in micro-seconds.
* It is an error to call this function with non-positive quantum_usecs.
*
* @return On success, return 0. On failure, return -1.
*/
int uthread_init(int quantum_usecs);
/**
* @brief Creates a new thread, whose entry point is the function entry_point with the signature
* void entry_point(void).
*
* The thread is added to the end of the READY threads list.
* The uthread_spawn function should fail if it would cause the number of concurrent threads to exceed the
* limit (MAX_THREAD_NUM).
* Each thread should be allocated with a stack of size STACK_SIZE bytes.
* It is an error to call this function with a null entry_point.
*
* @return On success, return the ID of the created thread. On failure, return -1.
*/
int uthread_spawn(thread_entry_point entry_point);
/**
* @brief Terminates the thread with ID tid and deletes it from all relevant control structures.
*
* All the resources allocated by the library for this thread should be released. If no thread with ID tid exists it
* is considered an error. Terminating the main thread (tid == 0) will result in the termination of the entire
* process using exit(0) (after releasing the assigned library memory).
*
* @return The function returns 0 if the thread was successfully terminated and -1 otherwise. If a thread terminates
* itself or the main thread is terminated, the function does not return.
*/
int uthread_terminate(int tid);
/**
* @brief Blocks the thread with ID tid. The thread may be resumed later using uthread_resume.
*
* If no thread with ID tid exists it is considered as an error. In addition, it is an error to try blocking the
* main thread (tid == 0). If a thread blocks itself, a scheduling decision should be made. Blocking a thread in
* BLOCKED state has no effect and is not considered an error.
*
* @return On success, return 0. On failure, return -1.
*/
int uthread_block(int tid);
/**
* @brief Resumes a blocked thread with ID tid and moves it to the READY state.
*
* Resuming a thread in a RUNNING or READY state has no effect and is not considered as an error. If no thread with
* ID tid exists it is considered an error.
*
* @return On success, return 0. On failure, return -1.
*/
int uthread_resume(int tid);
/**
* @brief Blocks the RUNNING thread for num_quantums quantums.
*
* Immediately after the RUNNING thread transitions to the BLOCKED state a scheduling decision should be made.
* After the sleeping time is over, the thread should go back to the end of the READY queue.
* If the thread which was just RUNNING should also be added to the READY queue, or if multiple threads wake up
* at the same time, the order in which they're added to the end of the READY queue doesn't matter.
* The number of quantums refers to the number of times a new quantum starts, regardless of the reason. Specifically,
* the quantum of the thread which has made the call to uthread_sleep isn’t counted.
* It is considered an error if the main thread (tid == 0) calls this function.
*
* @return On success, return 0. On failure, return -1.
*/
int uthread_sleep(int num_quantums);
/**
* @brief Returns the thread ID of the calling thread.
*
* @return The ID of the calling thread.
*/
int uthread_get_tid();
/**
* @brief Returns the total number of quantums since the library was initialized, including the current quantum.
*
* Right after the call to uthread_init, the value should be 1.
* Each time a new quantum starts, regardless of the reason, this number should be increased by 1.
*
* @return The total number of quantums.
*/
int uthread_get_total_quantums();
/**
* @brief Returns the number of quantums the thread with ID tid was in RUNNING state.
*
* On the first time a thread runs, the function should return 1. Every additional quantum that the thread starts should
* increase this value by 1 (so if the thread with ID tid is in RUNNING state when this function is called, include
* also the current quantum). If no thread with ID tid exists it is considered an error.
*
* @return On success, return the number of quantums of the thread with ID tid. On failure, return -1.
*/
int uthread_get_quantums(int tid);
#endif