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
// Copyright (c) 2019 Stefan Lankes, RWTH Aachen University
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use alloc::boxed::Box;
use arch::percore::*;
use core::mem;
use scheduler::task::TaskHandlePriorityQueue;

struct CondQueue {
	queue: TaskHandlePriorityQueue,
	id: usize,
}

impl CondQueue {
	pub fn new(id: usize) -> Self {
		CondQueue {
			queue: TaskHandlePriorityQueue::new(),
			id: id,
		}
	}
}

impl Drop for CondQueue {
	fn drop(&mut self) {
		debug!("Drop queue for condition variable with id 0x{:x}", self.id);
	}
}

unsafe fn __sys_destroy_queue(ptr: usize) -> i32 {
	let id = ptr as *mut usize;
	if id.is_null() {
		debug!("sys_wait: ivalid address to condition variable");
		return -1;
	}

	if *id != 0 {
		let cond = Box::from_raw((*id) as *mut CondQueue);
		mem::drop(cond);

		// reset id
		*id = 0;
	}

	0
}

#[no_mangle]
pub unsafe fn sys_destroy_queue(ptr: usize) -> i32 {
	kernel_function!(__sys_destroy_queue(ptr))
}

unsafe fn __sys_notify(ptr: usize, count: i32) -> i32 {
	let id = ptr as *const usize;
	if id.is_null() || *id == 0 {
		// invalid argument
		debug!("sys_notify: invalid address to condition variable");
		return -1;
	}

	let cond = &mut *((*id) as *mut CondQueue);

	if count < 0 {
		// Wake up all task that has been waiting for this condition variable
		while let Some(task) = cond.queue.pop() {
			core_scheduler().custom_wakeup(task);
		}
	} else {
		for _ in 0..count {
			// Wake up any task that has been waiting for this condition variable
			if let Some(task) = cond.queue.pop() {
				core_scheduler().custom_wakeup(task);
			} else {
				debug!("Unable to wakeup task");
			}
		}
	}

	0
}

#[no_mangle]
pub unsafe fn sys_notify(ptr: usize, count: i32) -> i32 {
	kernel_function!(__sys_notify(ptr, count))
}

unsafe fn __sys_add_queue(ptr: usize, timeout_ns: i64) -> i32 {
	let id = ptr as *mut usize;
	if id.is_null() {
		debug!("sys_wait: ivalid address to condition variable");
		return -1;
	}

	if *id == 0 {
		debug!("Create condition variable queue");
		let queue = Box::new(CondQueue::new(ptr));
		*id = Box::into_raw(queue) as usize;
	}

	let wakeup_time = if timeout_ns <= 0 {
		None
	} else {
		Some(timeout_ns as u64 / 1000)
	};

	// Block the current task and add it to the wakeup queue.
	let core_scheduler = core_scheduler();
	core_scheduler.block_current_task(wakeup_time);
	let cond = &mut *((*id) as *mut CondQueue);
	cond.queue.push(core_scheduler.get_current_task_handle());

	0
}

#[no_mangle]
pub unsafe fn sys_add_queue(ptr: usize, timeout_ns: i64) -> i32 {
	kernel_function!(__sys_add_queue(ptr, timeout_ns))
}

fn __sys_wait(_ptr: usize) -> i32 {
	// Switch to the next task.
	core_scheduler().reschedule();

	0
}

#[no_mangle]
pub fn sys_wait(ptr: usize) -> i32 {
	kernel_function!(__sys_wait(ptr))
}