12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- #include "semaphore.h"
- #include <sched/sched.h>
- #include <process/process.h>
- 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);
-
- current_pcb->flags |= PF_NEED_SCHED;
- }
- };
|