1 /* 2 * Copyright (c) 2023 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University (SJTU) 3 * Licensed under the Mulan PSL v2. 4 * You can use this software according to the terms and conditions of the Mulan PSL v2. 5 * You may obtain a copy of Mulan PSL v2 at: 6 * http://license.coscl.org.cn/MulanPSL2 7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR 8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR 9 * PURPOSE. 10 * See the Mulan PSL v2 for more details. 11 */ 12 #ifndef IRQ_TIMER_H 13 #define IRQ_TIMER_H 14 15 #include <common/types.h> 16 #include <common/list.h> 17 #include <common/lock.h> 18 #include <machine.h> 19 20 struct thread; 21 22 typedef void (*timer_cb)(struct thread *thread); 23 24 /* Every thread has a sleep_state struct */ 25 struct sleep_state { 26 /* Link sleeping threads on each core */ 27 struct list_head sleep_node; 28 /* Time to wake up */ 29 u64 wakeup_tick; 30 /* The cpu id where the thread is sleeping */ 31 u64 sleep_cpu; 32 /* 33 * When the timeout is caused during notification wait (with timeout), 34 * The following field indicates which notification it is waiting for. 35 * Currently, we record this field for removing the timeout thread from 36 * the waiting queue of the corresponding notification. 37 */ 38 struct notification *pending_notific; 39 /* 40 * Currently 2 type of callbacks: notification and sleep. 41 * If it is NULL, the thread is not waiting for timeout. 42 */ 43 timer_cb cb; 44 45 /* 46 * Get this lock before inserting or removing the thread in or from 47 * a waiting list, i.e., sleep_list and wait_list of a notification. 48 */ 49 struct lock queue_lock; 50 }; 51 52 #define NS_IN_S (1000000000UL) 53 #define US_IN_S (1000000UL) 54 #define NS_IN_US (1000UL) 55 #define US_IN_MS (1000UL) 56 57 extern struct list_head sleep_lists[PLAT_CPU_NUM]; 58 extern u64 tick_per_us; 59 int enqueue_sleeper(struct thread *thread, const struct timespec *timeout, 60 timer_cb cb); 61 bool try_dequeue_sleeper(struct thread *thread); 62 63 void timer_init(void); 64 void plat_timer_init(void); 65 void plat_set_next_timer(u64 tick_delta); 66 void handle_timer_irq(void); 67 void plat_handle_timer_irq(u64 tick_delta); 68 69 u64 plat_get_mono_time(void); 70 u64 plat_get_current_tick(void); 71 72 /* Syscalls */ 73 int sys_clock_gettime(clockid_t clock, struct timespec *ts); 74 int sys_clock_nanosleep(clockid_t clk, int flags, const struct timespec *req, 75 struct timespec *rem); 76 77 #endif /* IRQ_TIMER_H */