-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread-worker.h
More file actions
137 lines (110 loc) · 3 KB
/
Copy paththread-worker.h
File metadata and controls
137 lines (110 loc) · 3 KB
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
// File: worker_t.h
// List all group member's name:
// username of iLab:
// iLab Server:
#ifndef WORKER_T_H
#define WORKER_T_H
#define _GNU_SOURCE
/* To use Linux pthread Library in Benchmark, you have to comment the USE_WORKERS macro */
#define USE_WORKERS 1
/* include lib header files that you need here: */
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <ucontext.h>
#include <signal.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
typedef uint worker_t;
typedef struct TCB {
/* add important states in a thread control block */
// thread Id
worker_t id;
// thread status
unsigned int status;
// thread context
ucontext_t context;
// thread stack
// thread priority
unsigned int priority;
unsigned int lastPriorityChange;
double startTime;
// And more ...
unsigned int elapsed;
unsigned int threadFinished;
worker_t waitID;
int mutexID;
void** value_ptr;
void** value_ptr_to_set;
// YOUR CODE HERE
} tcb;
/* mutex struct definition */
typedef struct worker_mutex_t {
/* add something here */
worker_t id;
int mutexSet;
int assignedTCB;
// YOUR CODE HERE
} worker_mutex_t;
/* Priority definitions */
#define NUMPRIO 4
#define HIGH_PRIO 3
#define MEDIUM_PRIO 2
#define DEFAULT_PRIO 1
#define LOW_PRIO 0
/* define your data structures here: */
// Feel free to add your own auxiliary data structures (linked list or queue etc...)
// YOUR CODE HERE
#define TIME_QUANTUM 10
#define REFRESH_QUANTUM 100
#define STACK_SIZE SIGSTKSZ
typedef struct Node {
union {
tcb* entry;
worker_mutex_t* mutex_entry;
};
struct Node* next;
struct Node* prev;
} Node;
typedef struct List {
struct Node* head;
struct Node* tail;
} List;
/* Function Declarations: */
/* create a new thread */
int worker_create(worker_t * thread, pthread_attr_t * attr, void
*(*function)(void*), void * arg);
/* give CPU pocession to other user level worker threads voluntarily */
int worker_yield();
/* terminate a thread */
void worker_exit(void *value_ptr);
/* wait for thread termination */
int worker_join(worker_t thread, void **value_ptr);
/* initial the mutex lock */
int worker_mutex_init(worker_mutex_t *mutex, const pthread_mutexattr_t
*mutexattr);
/* aquire the mutex lock */
int worker_mutex_lock(worker_mutex_t *mutex);
/* release the mutex lock */
int worker_mutex_unlock(worker_mutex_t *mutex);
/* destroy the mutex */
int worker_mutex_destroy(worker_mutex_t *mutex);
void handleTimer(int signum);
/* Function to print global statistics. Do not modify this function.*/
void print_app_stats(void);
#ifdef USE_WORKERS
#define pthread_t worker_t
#define pthread_mutex_t worker_mutex_t
#define pthread_create worker_create
#define pthread_exit worker_exit
#define pthread_join worker_join
#define pthread_mutex_init worker_mutex_init
#define pthread_mutex_lock worker_mutex_lock
#define pthread_mutex_unlock worker_mutex_unlock
#define pthread_mutex_destroy worker_mutex_destroy
#define pthread_setschedprio worker_setschedprio
#endif
#endif