| 1 | #include <sched.h> |
|---|
| 2 | #include <module.h> |
|---|
| 3 | #include <slab.h> |
|---|
| 4 | #include <task.h> |
|---|
| 5 | #include <print.h> |
|---|
| 6 | #include <panic.h> |
|---|
| 7 | #include <i386/process.h> |
|---|
| 8 | #include <sys.h> |
|---|
| 9 | #include <preempt.h> |
|---|
| 10 | #include <malloc.h> |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | BYTE exitStack[PAGE_SIZE/4]; |
|---|
| 14 | |
|---|
| 15 | Spinlock thrListLock; |
|---|
| 16 | struct Cache* threadCache; |
|---|
| 17 | extern int nrRunning; |
|---|
| 18 | LIST_HEAD(threadList); |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | struct Thread* ThrCreateThread(struct Process* parent,DWORD entry,int user,DWORD stackP, void* argument) |
|---|
| 37 | { |
|---|
| 38 | struct Thread* ret; |
|---|
| 39 | |
|---|
| 40 | ret=(struct Thread*)MemCacheAlloc(threadCache); |
|---|
| 41 | if (!ret) |
|---|
| 42 | return NULL; |
|---|
| 43 | |
|---|
| 44 | ret->parent = parent; |
|---|
| 45 | ret->refs = 1; |
|---|
| 46 | ret->priority = PRIORITY_DEFAULT; |
|---|
| 47 | ret->state = THR_PAUSED; |
|---|
| 48 | ret->preemptCount = 0; |
|---|
| 49 | |
|---|
| 50 | if (parent) |
|---|
| 51 | ret->id=parent->cId++; |
|---|
| 52 | |
|---|
| 53 | ThrArchCreateThread(ret, entry, user, stackP, argument); |
|---|
| 54 | |
|---|
| 55 | SpinLockIrq(&thrListLock); |
|---|
| 56 | ListAddTail(&ret->list, &threadList); |
|---|
| 57 | SpinUnlockIrq(&thrListLock); |
|---|
| 58 | |
|---|
| 59 | return ret; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | SYMBOL_EXPORT(ThrCreateThread); |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | void ThrSetPriority(struct Thread* thread,int priority) |
|---|
| 78 | { |
|---|
| 79 | if (priority > PRIORITY_TOP) |
|---|
| 80 | priority=PRIORITY_TOP; |
|---|
| 81 | else if (priority < PRIORITY_NONE) |
|---|
| 82 | priority=PRIORITY_NONE; |
|---|
| 83 | |
|---|
| 84 | thread->priority=priority; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | SYMBOL_EXPORT(ThrSetPriority); |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | void _ThrFreeThread(struct Thread* thread) |
|---|
| 103 | { |
|---|
| 104 | MemFree((void*)thread->currStack); |
|---|
| 105 | currThread=NULL; |
|---|
| 106 | MemCacheFree(threadCache, thread); |
|---|
| 107 | ThrSchedule(); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | void ThrFreeThread(struct Thread* thread) |
|---|
| 124 | { |
|---|
| 125 | DWORD flags; |
|---|
| 126 | |
|---|
| 127 | if (thread == idleTask) |
|---|
| 128 | KernelPanic("Trying to free idle thread"); |
|---|
| 129 | |
|---|
| 130 | IrqSaveFlags(flags); |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | ListRemove(&thread->list); |
|---|
| 139 | --nrRunning; |
|---|
| 140 | |
|---|
| 141 | thread->currStack &= ~(ARCH_STACK_SIZE-1); |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | if (thread == currThread) |
|---|
| 146 | ArchSwitchStackCall(exitStack+PAGE_SIZE/4, _ThrFreeThread, thread); |
|---|
| 147 | else{ |
|---|
| 148 | MemFree((void*)(thread->currStack)); |
|---|
| 149 | MemCacheFree(threadCache,thread); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | SYMBOL_EXPORT(ThrFreeThread); |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | static void _ThrStartThread(struct Thread* thread) |
|---|
| 171 | { |
|---|
| 172 | thread->state=THR_RUNNING; |
|---|
| 173 | ++nrRunning; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | void ThrStartThread(struct Thread* thread) |
|---|
| 189 | { |
|---|
| 190 | if (LIKELY(thread->state != THR_RUNNING)) |
|---|
| 191 | { |
|---|
| 192 | SpinLockIrq(&thrListLock); |
|---|
| 193 | _ThrStartThread(thread); |
|---|
| 194 | SpinUnlockIrq(&thrListLock); |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | SYMBOL_EXPORT(ThrStartThread); |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | int ThrDoExitThread() |
|---|
| 213 | { |
|---|
| 214 | currThread->state=THR_DYING; |
|---|
| 215 | |
|---|
| 216 | ThrReleaseThread(currThread); |
|---|
| 217 | ThrArchDestroyThread(currThread); |
|---|
| 218 | ThrSchedule(); |
|---|
| 219 | |
|---|
| 220 | return 0; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | SYMBOL_EXPORT(ThrDoExitThread); |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | void ThrSuspendThread(struct Thread* thread) |
|---|
| 239 | { |
|---|
| 240 | if (thread == idleTask) |
|---|
| 241 | KernelPanic("Idle task being suspended - driver sleeping in interrupt?"); |
|---|
| 242 | |
|---|
| 243 | if (LIKELY(thread->state == THR_RUNNING)) |
|---|
| 244 | { |
|---|
| 245 | SpinLockIrq(&thrListLock); |
|---|
| 246 | --nrRunning; |
|---|
| 247 | |
|---|
| 248 | thread->state=THR_PAUSED; |
|---|
| 249 | SpinUnlockIrq(&thrListLock); |
|---|
| 250 | } |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | SYMBOL_EXPORT(ThrSuspendThread); |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | void ThrResumeThread(struct Thread* thread) |
|---|
| 269 | { |
|---|
| 270 | if (LIKELY(thread->state != THR_RUNNING)) |
|---|
| 271 | { |
|---|
| 272 | ThrStartThread(thread); |
|---|
| 273 | |
|---|
| 274 | if (thread->quantums >= currThread->quantums) |
|---|
| 275 | thrNeedSchedule=true; |
|---|
| 276 | } |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | SYMBOL_EXPORT(ThrResumeThread); |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | int SysCreateThread(DWORD address,DWORD stackP, void* argument) |
|---|
| 295 | { |
|---|
| 296 | struct Thread* thread=ThrCreateThread(current, address, true, stackP, argument); |
|---|
| 297 | |
|---|
| 298 | if (!thread) |
|---|
| 299 | return -ENOMEM; |
|---|
| 300 | |
|---|
| 301 | ThrStartThread(thread); |
|---|
| 302 | |
|---|
| 303 | if (thread->quantums > currThread->quantums) |
|---|
| 304 | ThrSchedule(); |
|---|
| 305 | |
|---|
| 306 | return thread->id; |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | int SysGetCurrentThreadId() |
|---|
| 310 | { |
|---|
| 311 | return currThread->id; |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | struct Thread* ThrGetThreadById(struct Process* process, DWORD threadId) |
|---|
| 315 | { |
|---|
| 316 | struct Thread* curr; |
|---|
| 317 | |
|---|
| 318 | ListForEachEntry(curr, &threadList, list) |
|---|
| 319 | { |
|---|
| 320 | if (!curr->parent) |
|---|
| 321 | continue; |
|---|
| 322 | |
|---|
| 323 | if (curr->id == threadId && curr->parent == process) |
|---|
| 324 | return curr; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | return NULL; |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | int SysSuspendThread(DWORD threadId) |
|---|
| 331 | { |
|---|
| 332 | struct Thread* thread; |
|---|
| 333 | |
|---|
| 334 | thread=ThrGetThreadById(current, threadId); |
|---|
| 335 | |
|---|
| 336 | if (!thread) |
|---|
| 337 | return -EINVAL; |
|---|
| 338 | |
|---|
| 339 | ThrGetThread(thread); |
|---|
| 340 | ThrSuspendThread(thread); |
|---|
| 341 | ThrSchedule(); |
|---|
| 342 | |
|---|
| 343 | ThrReleaseThread(thread); |
|---|
| 344 | |
|---|
| 345 | return 0; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | int SysResumeThread(DWORD threadId) |
|---|
| 349 | { |
|---|
| 350 | struct Thread* thread; |
|---|
| 351 | |
|---|
| 352 | thread=ThrGetThreadById(current, threadId); |
|---|
| 353 | |
|---|
| 354 | if (!thread) |
|---|
| 355 | return -EINVAL; |
|---|
| 356 | |
|---|
| 357 | ThrResumeThread(thread); |
|---|
| 358 | |
|---|
| 359 | return 0; |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | int SysExitThread(int threadId) |
|---|
| 363 | { |
|---|
| 364 | if (threadId == -1) |
|---|
| 365 | ThrDoExitThread(); |
|---|
| 366 | else |
|---|
| 367 | return -ENOTIMPL; |
|---|
| 368 | |
|---|
| 369 | return 0; |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | struct SysCall thrSysCallTable[]= |
|---|
| 373 | { |
|---|
| 374 | SysEntry(SysCreateThread, 12), |
|---|
| 375 | SysEntry(SysGetCurrentThreadId, 0), |
|---|
| 376 | SysEntry(SysSuspendThread, 4), |
|---|
| 377 | SysEntry(SysResumeThread, 4), |
|---|
| 378 | SysEntry(SysExitThread, 4), |
|---|
| 379 | SysEntryEnd() |
|---|
| 380 | }; |
|---|
| 381 | |
|---|
| 382 | int ThreadInit() |
|---|
| 383 | { |
|---|
| 384 | threadCache=MemCacheCreate("Threads", sizeof(struct Thread), NULL, NULL, 0); |
|---|
| 385 | SysRegisterRange(SYS_THREAD_BASE, thrSysCallTable); |
|---|
| 386 | return 0; |
|---|
| 387 | } |
|---|