[Krafton Jungle] PintOS 2.0.0
크래프톤 정글 PintOS
 
Loading...
Searching...
No Matches
synch.h File Reference
#include <list.h>
#include <stdbool.h>
Include dependency graph for synch.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  semaphore
 
struct  lock
 
struct  condition
 

Macros

#define barrier()   asm volatile ("" : : : "memory")
 

Functions

void sema_init (struct semaphore *, unsigned value)
 
void sema_down (struct semaphore *)
 
bool sema_try_down (struct semaphore *)
 
void sema_up (struct semaphore *)
 
void sema_self_test (void)
 
void lock_init (struct lock *)
 
void lock_acquire (struct lock *)
 
bool lock_try_acquire (struct lock *)
 
void lock_release (struct lock *)
 
bool lock_held_by_current_thread (const struct lock *)
 
void donate_priority (void)
 
void remove_with_lock (struct lock *)
 
void refresh_priority (void)
 
void cond_init (struct condition *)
 
void cond_wait (struct condition *, struct lock *)
 
void cond_signal (struct condition *, struct lock *)
 
void cond_broadcast (struct condition *, struct lock *)
 

Macro Definition Documentation

◆ barrier

#define barrier ( )    asm volatile ("" : : : "memory")

Function Documentation

◆ cond_broadcast()

void cond_broadcast ( struct condition cond,
struct lock lock 
)
416 {
417 ASSERT (cond != NULL);
418 ASSERT (lock != NULL);
419
420 while (!list_empty (&cond->waiters))
421 cond_signal (cond, lock);
422}
#define ASSERT(CONDITION)
Definition: debug.h:30
bool list_empty(struct list *)
Definition: list.c:296
#define NULL
Definition: stddef.h:4
struct list waiters
Definition: synch.h:37
Definition: synch.h:20
void cond_signal(struct condition *cond, struct lock *lock UNUSED)
Definition: synch.c:396
Here is the call graph for this function:

◆ cond_init()

void cond_init ( struct condition cond)
344 {
345 ASSERT (cond != NULL);
346
347 list_init (&cond->waiters);
348}
void list_init(struct list *)
Definition: list.c:58
Here is the call graph for this function:

◆ cond_signal()

void cond_signal ( struct condition ,
struct lock  
)

◆ cond_wait()

void cond_wait ( struct condition cond,
struct lock lock 
)
371 {
372 struct semaphore_elem waiter;
373
374 ASSERT (cond != NULL);
375 ASSERT (lock != NULL);
376 ASSERT (!intr_context ());
378
379 sema_init (&waiter.semaphore, 0);
380 /*------------------------- [P1] Priority Scheduling - Condition variables --------------------------*/
381 // list_push_back (&cond->waiters, &waiter.elem);
382 list_insert_ordered (&cond->waiters, &waiter.elem, cmp_sem_priority, NULL);
384 sema_down (&waiter.semaphore);
386}
bool intr_context(void)
Definition: interrupt.c:258
void list_insert_ordered(struct list *, struct list_elem *, list_less_func *, void *aux)
Definition: list.c:419
Definition: synch.c:328
void lock_release(struct lock *lock)
Definition: synch.c:243
void sema_init(struct semaphore *sema, unsigned value)
Definition: synch.c:52
bool lock_held_by_current_thread(const struct lock *lock)
Definition: synch.c:258
static bool cmp_sem_priority(const struct list_elem *a, const struct list_elem *b, void *aux UNUSED)
Definition: synch.c:335
void lock_acquire(struct lock *lock)
Definition: synch.c:202
void sema_down(struct semaphore *sema)
Definition: synch.c:68
Here is the call graph for this function:

◆ donate_priority()

void donate_priority ( void  )
269 {
270 ASSERT(thread_current()->wait_on_lock != NULL);
271 struct thread *curr_lock_holder = thread_current()->wait_on_lock->holder; // 현재 내가 기다리는 락을 소유하고 있는 스레드
272 int nested_depth = 8;
273
274 // 만약 내 우선 순위가 lock이 걸린 스레드보다 높다면 우선순위 기부
275 while (thread_get_priority() > curr_lock_holder->priority){
276 if (nested_depth-- <= 0) break; // nested_depth 확인
277 curr_lock_holder->priority = thread_get_priority(); // 내 우선 순위 기부
278
279 // 홀더가 또 다른 락을 기다리고 있을 때 반복문 다시 순회
280 if (curr_lock_holder->wait_on_lock != NULL)
281 curr_lock_holder = curr_lock_holder->wait_on_lock->holder;
282 else
283 break;
284 }
285}
struct thread * holder
Definition: synch.h:21
Definition: thread.h:100
int priority
Definition: thread.h:105
struct lock * wait_on_lock
Definition: thread.h:108
struct thread * thread_current(void)
Definition: thread.c:301
int thread_get_priority(void)
Definition: thread.c:441
Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock_acquire()

void lock_acquire ( struct lock lock)
202 {
203 ASSERT (lock != NULL);
204 ASSERT (!intr_context ());
206
207 if (lock->holder != NULL) { // 해당 lock의 holder가 존재한다면
208 thread_current()->wait_on_lock = lock;// 해당 락을 wait_on_lock에 추가한다.
211 }
213 thread_current()->wait_on_lock = NULL; // 해당 스레드의 wait_on_lock을 NULL로 만든다.
215}
struct semaphore semaphore
Definition: synch.h:22
struct list donations
Definition: thread.h:114
void donate_priority(void)
Definition: synch.c:269
static bool cmp_d_priority(const struct list_elem *, const struct list_elem *, void *aux)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock_held_by_current_thread()

bool lock_held_by_current_thread ( const struct lock lock)
258 {
259 ASSERT (lock != NULL);
260
261 return lock->holder == thread_current ();
262}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock_init()

void lock_init ( struct lock lock)
186 {
187 ASSERT (lock != NULL);
188
189 lock->holder = NULL;
190 sema_init (&lock->semaphore, 1);
191}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock_release()

void lock_release ( struct lock lock)
243 {
244 ASSERT (lock != NULL);
246
249
250 lock->holder = NULL;
252}
void sema_up(struct semaphore *sema)
Definition: synch.c:114
void remove_with_lock(struct lock *)
Definition: synch.c:289
void refresh_priority(void)
Definition: synch.c:304
Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock_try_acquire()

bool lock_try_acquire ( struct lock lock)
224 {
225 bool success;
226
227 ASSERT (lock != NULL);
229
230 success = sema_try_down (&lock->semaphore);
231 if (success)
233 return success;
234}
bool sema_try_down(struct semaphore *sema)
Definition: synch.c:90
Here is the call graph for this function:

◆ refresh_priority()

void refresh_priority ( void  )
304 {
305 struct list_elem *donation_elem;
306 struct list *curr_donations = &thread_current()->donations;
307 int donations_priority;
308
309 thread_current()->priority = thread_current()->priority_base; // 스레드의 우선순위를 기부 받기 전의 우선순위로 변경한다.
310 if (!list_empty(curr_donations)){ // donation에 스레드가 존재하는지를 먼저 확인한다.
311 list_sort(curr_donations, cmp_d_priority, NULL);
312 donation_elem = list_begin(curr_donations);
313 donations_priority = list_entry(donation_elem, struct thread, d_elem)->priority;
314 if (thread_get_priority() < donations_priority)
315 thread_current()->priority = donations_priority;
316 }
317}
void list_sort(struct list *, list_less_func *, void *aux)
Definition: list.c:381
struct list_elem * list_begin(struct list *)
Definition: list.c:68
#define list_entry(LIST_ELEM, STRUCT, MEMBER)
Definition: list.h:103
Definition: list.h:87
Definition: list.h:93
int priority_base
Definition: thread.h:107
Here is the call graph for this function:
Here is the caller graph for this function:

◆ remove_with_lock()

void remove_with_lock ( struct lock lock)
289 {
290 struct list_elem *curr = list_begin(&thread_current()->donations);
291 struct thread *curr_thread;
292
293 while (curr != list_end(&thread_current()->donations)){ // 전체 순회
294 curr_thread = list_entry(curr, struct thread, d_elem);
295 if (curr_thread -> wait_on_lock == lock)
296 curr = list_remove(curr); // donations에서 해당 락을 기다리고 있던 스레드 제거
297 else
298 curr = curr -> next;
299 }
300}
static int next(int pos)
Definition: intq.c:74
struct list_elem * list_remove(struct list_elem *)
Definition: list.c:241
struct list_elem * list_end(struct list *)
Definition: list.c:88
struct list_elem d_elem
Definition: thread.h:115
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sema_down()

void sema_down ( struct semaphore sema)
68 {
69 enum intr_level old_level;
70
71 ASSERT (sema != NULL);
72 ASSERT (!intr_context ());
73
74 old_level = intr_disable ();
75 while (sema->value == 0) { // 공유 자원을 이용할 수 없는 상태
77 // list_push_back (&sema->waiters, &thread_current ()->elem);
78 thread_block ();
79 }
80 sema->value--;
81 intr_set_level (old_level);
82}
enum intr_level intr_set_level(enum intr_level)
Definition: interrupt.c:130
enum intr_level intr_disable(void)
Definition: interrupt.c:151
intr_level
Definition: interrupt.h:8
unsigned value
Definition: synch.h:9
struct list waiters
Definition: synch.h:10
bool cmp_priority(const struct list_elem *a, const struct list_elem *b, void *aux UNUSED)
Definition: thread.c:449
void thread_block(void)
Definition: thread.c:258
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sema_init()

void sema_init ( struct semaphore sema,
unsigned  value 
)
52 {
53 ASSERT (sema != NULL);
54
55 sema->value = value;
56 list_init (&sema->waiters);
57}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sema_self_test()

void sema_self_test ( void  )
141 {
142 struct semaphore sema[2];
143 int i;
144
145 printf ("Testing semaphores...");
146 sema_init (&sema[0], 0);
147 sema_init (&sema[1], 0);
148 thread_create ("sema-test", PRI_DEFAULT, sema_test_helper, &sema);
149 for (i = 0; i < 10; i++)
150 {
151 sema_up (&sema[0]);
152 sema_down (&sema[1]);
153 }
154 printf ("done.\n");
155}
int printf(const char *,...) PRINTF_FORMAT(1
Definition: synch.h:8
static void sema_test_helper(void *sema_)
Definition: synch.c:159
#define PRI_DEFAULT
Definition: thread.h:30
tid_t thread_create(const char *name, int priority, thread_func *, void *)
Definition: thread.c:197
Here is the call graph for this function:

◆ sema_try_down()

bool sema_try_down ( struct semaphore sema)
90 {
91 enum intr_level old_level;
92 bool success;
93
94 ASSERT (sema != NULL);
95
96 old_level = intr_disable ();
97 if (sema->value > 0)
98 {
99 sema->value--;
100 success = true;
101 }
102 else
103 success = false;
104 intr_set_level (old_level);
105
106 return success;
107}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sema_up()

void sema_up ( struct semaphore sema)
114 {
115 enum intr_level old_level;
116
117 ASSERT (sema != NULL);
118
119 old_level = intr_disable ();
120 sema->value++;
121 if (!list_empty (&sema->waiters)){
122 list_sort(&sema->waiters, cmp_priority, NULL); // waiter_list 정렬
123 struct thread *new_lockholder = list_entry(list_pop_front (&sema->waiters), struct thread, elem);
124 thread_unblock (new_lockholder); // 세마포어를 해제하고 레디 상태로 만들어준다.
125 if (thread_get_priority() < new_lockholder->priority ){
126 if (intr_context())
128 else
129 thread_yield();
130 }
131 }
132 intr_set_level (old_level);
133}
void intr_yield_on_return(void)
Definition: interrupt.c:269
struct list_elem * list_pop_front(struct list *)
Definition: list.c:251
struct list_elem elem
Definition: thread.h:112
void thread_unblock(struct thread *)
Definition: thread.c:275
void thread_yield(void)
Definition: thread.c:343
Here is the call graph for this function:
Here is the caller graph for this function: