123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #pragma once
- #include <process/atomic.h>
- #include <process/process.h>
- #include <sched/sched.h>
- typedef struct
- {
- struct List wait_list;
- struct process_control_block *pcb;
- } wait_queue_node_t;
- void wait_queue_init(wait_queue_node_t *wait_queue, struct process_control_block *pcb)
- {
- list_init(&wait_queue->wait_list);
- wait_queue->pcb = pcb;
- }
- typedef struct
- {
- atomic_t counter;
- wait_queue_node_t wait_queue;
- } semaphore_t;
- void semaphore_init(semaphore_t *sema, ul count)
- {
- atomic_set(&sema->counter, count);
- wait_queue_init(&sema->wait_queue, NULL);
- }
- void semaphore_down(semaphore_t *sema)
- {
- if (atomic_read(&sema->counter) > 0)
- atomic_dec(&sema->counter);
- else
- {
-
- wait_queue_node_t wait;
- wait_queue_init(&wait, current_pcb);
- current_pcb->state = PROC_UNINTERRUPTIBLE;
- list_append(&sema->wait_queue.wait_list, &wait.wait_list);
-
- sched_cfs();
- }
- }
- void semaphore_up(semaphore_t *sema)
- {
- if (list_empty(&sema->wait_queue.wait_list))
- {
- atomic_inc(&sema->counter);
- }
- else
- {
- wait_queue_node_t *wq = container_of(list_next(&sema->wait_queue.wait_list), wait_queue_node_t, wait_list);
- list_del(&wq->wait_list);
- wq->pcb->state = PROC_RUNNING;
- sched_cfs_enqueue(wq->pcb);
- }
- }
|