1 /* 2 * Copyright 2002-2007, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 * 5 * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved. 6 * Distributed under the terms of the NewOS License. 7 */ 8 #ifndef _THREAD_H 9 #define _THREAD_H 10 11 12 #include <OS.h> 13 #include <thread_types.h> 14 #include <arch/thread.h> 15 16 // For the thread blocking inline functions only. 17 #include <kscheduler.h> 18 #include <ksignal.h> 19 20 21 struct kernel_args; 22 struct select_info; 23 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 29 void thread_enqueue(struct thread *t, struct thread_queue *q); 30 struct thread *thread_lookat_queue(struct thread_queue *q); 31 struct thread *thread_dequeue(struct thread_queue *q); 32 struct thread *thread_dequeue_id(struct thread_queue *q, thread_id id); 33 34 void thread_at_kernel_entry(bigtime_t now); 35 // called when the thread enters the kernel on behalf of the thread 36 void thread_at_kernel_exit(void); 37 void thread_at_kernel_exit_no_signals(void); 38 void thread_reset_for_exec(void); 39 40 status_t thread_init(struct kernel_args *args); 41 status_t thread_preboot_init_percpu(struct kernel_args *args, int32 cpuNum); 42 void thread_yield(bool force); 43 void thread_exit(void); 44 45 int32 thread_max_threads(void); 46 int32 thread_used_threads(void); 47 48 #define thread_get_current_thread arch_thread_get_current_thread 49 50 struct thread *thread_get_thread_struct(thread_id id); 51 struct thread *thread_get_thread_struct_locked(thread_id id); 52 53 static thread_id thread_get_current_thread_id(void); 54 static inline thread_id 55 thread_get_current_thread_id(void) 56 { 57 struct thread *thread = thread_get_current_thread(); 58 return thread ? thread->id : 0; 59 } 60 61 static inline bool 62 thread_is_idle_thread(struct thread *thread) 63 { 64 return thread->entry == NULL; 65 } 66 67 thread_id allocate_thread_id(void); 68 thread_id peek_next_thread_id(void); 69 70 thread_id spawn_kernel_thread_etc(thread_func, const char *name, int32 priority, 71 void *args, team_id team, thread_id threadID); 72 status_t wait_for_thread_etc(thread_id id, uint32 flags, bigtime_t timeout, 73 status_t *_returnCode); 74 75 status_t select_thread(int32 object, struct select_info *info, bool kernel); 76 status_t deselect_thread(int32 object, struct select_info *info, bool kernel); 77 78 #define syscall_64_bit_return_value() arch_syscall_64_bit_return_value() 79 80 status_t thread_block(); 81 status_t thread_block_with_timeout(uint32 timeoutFlags, bigtime_t timeout); 82 status_t thread_block_with_timeout_locked(uint32 timeoutFlags, 83 bigtime_t timeout); 84 bool thread_unblock(status_t threadID, status_t status); 85 86 // used in syscalls.c 87 status_t _user_set_thread_priority(thread_id thread, int32 newPriority); 88 status_t _user_rename_thread(thread_id thread, const char *name); 89 status_t _user_suspend_thread(thread_id thread); 90 status_t _user_resume_thread(thread_id thread); 91 status_t _user_rename_thread(thread_id thread, const char *name); 92 thread_id _user_spawn_thread(thread_entry_func entry, const char *name, int32 priority, void *arg1, void *arg2); 93 status_t _user_wait_for_thread(thread_id id, status_t *_returnCode); 94 status_t _user_snooze_etc(bigtime_t timeout, int timebase, uint32 flags); 95 status_t _user_kill_thread(thread_id thread); 96 void _user_thread_yield(void); 97 void _user_exit_thread(status_t return_value); 98 bool _user_has_data(thread_id thread); 99 status_t _user_send_data(thread_id thread, int32 code, const void *buffer, size_t buffer_size); 100 status_t _user_receive_data(thread_id *_sender, void *buffer, size_t buffer_size); 101 thread_id _user_find_thread(const char *name); 102 status_t _user_get_thread_info(thread_id id, thread_info *info); 103 status_t _user_get_next_thread_info(team_id team, int32 *cookie, thread_info *info); 104 105 // ToDo: these don't belong here 106 struct rlimit; 107 int _user_getrlimit(int resource, struct rlimit * rlp); 108 int _user_setrlimit(int resource, const struct rlimit * rlp); 109 110 #ifdef __cplusplus 111 } 112 #endif 113 114 115 /*! 116 \a thread must be the current thread. 117 Thread lock can be, but doesn't need to be held. 118 */ 119 static inline bool 120 thread_is_interrupted(struct thread* thread, uint32 flags) 121 { 122 return ((flags & B_CAN_INTERRUPT) 123 && (thread->sig_pending & ~thread->sig_block_mask) != 0) 124 || ((flags & B_KILL_CAN_INTERRUPT) 125 && (thread->sig_pending & KILL_SIGNALS)); 126 } 127 128 129 static inline bool 130 thread_is_blocked(struct thread* thread) 131 { 132 return thread->wait.status == 1; 133 } 134 135 136 /*! 137 \a thread must be the current thread. 138 Thread lock can be, but doesn't need to be locked. 139 */ 140 static inline void 141 thread_prepare_to_block(struct thread* thread, uint32 flags, uint32 type, 142 const void* object) 143 { 144 thread->wait.flags = flags; 145 thread->wait.type = type; 146 thread->wait.object = object; 147 atomic_set(&thread->wait.status, 1); 148 // Set status last to guarantee that the other fields are initialized 149 // when a thread is waiting. 150 } 151 152 153 static inline status_t 154 thread_block_locked(struct thread* thread) 155 { 156 if (thread->wait.status == 1) { 157 // check for signals, if interruptable 158 if (thread_is_interrupted(thread, thread->wait.flags)) { 159 thread->wait.status = B_INTERRUPTED; 160 } else { 161 thread->next_state = B_THREAD_WAITING; 162 scheduler_reschedule(); 163 } 164 } 165 166 return thread->wait.status; 167 } 168 169 170 static inline bool 171 thread_unblock_locked(struct thread* thread, status_t status) 172 { 173 if (atomic_test_and_set(&thread->wait.status, status, 1) != 1) 174 return false; 175 176 // wake up the thread, if it is sleeping 177 if (thread->state == B_THREAD_WAITING) 178 scheduler_enqueue_in_run_queue(thread); 179 180 return true; 181 } 182 183 184 static inline status_t 185 thread_interrupt(struct thread* thread, bool kill) 186 { 187 if ((thread->wait.flags & B_CAN_INTERRUPT) != 0 188 || (kill && (thread->wait.flags & B_KILL_CAN_INTERRUPT) != 0)) { 189 thread_unblock_locked(thread, B_INTERRUPTED); 190 return B_OK; 191 } 192 193 return B_NOT_ALLOWED; 194 } 195 196 197 #endif /* _THREAD_H */ 198