Blocking and unblocking thread from blocked list
Blocking and unblocking thread from blocked list
Hi,
I have been implementing multitasking and it worked fine. I am facing problems with blocking and unblocking threads. I can block multiple threads just by changing its ready state to block state and put it into blocked list and removing it from ready list and force schedule. It worked fine, but if there are multiple threads, unblocking a particular thread from block list causes system faults sometime page fault or sometime gpf. How do you unblock particular blocked thread from block list? Is there any algorithm? For scheduling I use round robin
I have been implementing multitasking and it worked fine. I am facing problems with blocking and unblocking threads. I can block multiple threads just by changing its ready state to block state and put it into blocked list and removing it from ready list and force schedule. It worked fine, but if there are multiple threads, unblocking a particular thread from block list causes system faults sometime page fault or sometime gpf. How do you unblock particular blocked thread from block list? Is there any algorithm? For scheduling I use round robin
-
- Member
- Posts: 5568
- Joined: Mon Mar 25, 2013 7:01 pm
Re: Blocking and unblocking thread from blocked list
This sounds like a problem with your implementation, not your algorithm.
What kind of debugging have you done so far?
What kind of debugging have you done so far?
Re: Blocking and unblocking thread from blocked list
Hi,Octocontrabass wrote:This sounds like a problem with your implementation, not your algorithm.
What kind of debugging have you done so far?
Thank you so much for your reply.. I have not yet used any debugger.. I just use printf statements to confirm which line is causing the problem. And also I check the registers in virtual box debugging console. I use windows and visual c++ for my os project. I suspect, that saving current thread context is causing problem.
- Schol-R-LEA
- Member
- Posts: 1925
- Joined: Fri Oct 27, 2006 9:42 am
- Location: Athens, GA, USA
Re: Blocking and unblocking thread from blocked list
Do you have an offsite repo (on a site such as Github or Sourceforge) where we could review your source code?
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
Re: Blocking and unblocking thread from blocked list
Do you have any locks around that code?
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
Re: Blocking and unblocking thread from blocked list
Hi,Schol-R-LEA wrote:Do you have an offsite repo (on a site such as Github or Sourceforge) where we could review your source code?
For now I don't have any offsite repo..but I will soon upload my code to GitHub. I will share it here. Thank you so much for your reply
Re: Blocking and unblocking thread from blocked list
Hi,Korona wrote:Do you have any locks around that code?
When I block a thread, I don't use any lock and mutex but I disable the scheduler also clear the interrupt flags. Also when I unblock a thread I simply use clear interrupt and start interrupt to avoid data corruption. I also suspect that, it can be a pointer related issue..
By the way, thank you so much for your reply
Re: Blocking and unblocking thread from blocked list
Are you using SMP? With single core and uninterruptible syscall handlers there might be no need for locking, but it's a good advice, because race conditions can cause such errors.Kamal123 wrote:Hi,Korona wrote:Do you have any locks around that code?
When I block a thread, I don't use any lock and mutex but I disable the scheduler also clear the interrupt flags. Also when I unblock a thread I simply use clear interrupt and start interrupt to avoid data corruption. I also suspect that, it can be a pointer related issue..
By the way, thank you so much for your reply
Here's my scheduler queue handling code if you want to learn from it. It's a bit tricky as I use per-core queues, and I have a blocked queue, an alarm queue and 30 waiting queues (one for each priority level). These are the functions you're interested in:
sched_awake - move from blocked queue to one of the waiting queues
sched_block - move from a waiting queue to the blocked queue
sched_pick - select one task from one of the waiting queues as actively running (this is going to be task that the syscall handler returns to)
It is pretty straightforward linked list handling btw.
Cheers,
bzt
Re: Blocking and unblocking thread from blocked list
bzt wrote:Are you using SMP? With single core and uninterruptible syscall handlers there might be no need for locking, but it's a good advice, because race conditions can cause such errors.Kamal123 wrote:Hi,Korona wrote:Do you have any locks around that code?
When I block a thread, I don't use any lock and mutex but I disable the scheduler also clear the interrupt flags. Also when I unblock a thread I simply use clear interrupt and start interrupt to avoid data corruption. I also suspect that, it can be a pointer related issue..
By the way, thank you so much for your reply
Here's my scheduler queue handling code if you want to learn from it. It's a bit tricky as I use per-core queues, and I have a blocked queue, an alarm queue and 30 waiting queues (one for each priority level). These are the functions you're interested in:
sched_awake - move from blocked queue to one of the waiting queues
sched_block - move from a waiting queue to the blocked queue
sched_pick - select one task from one of the waiting queues as actively running (this is going to be task that the syscall handler returns to)
It is pretty straightforward linked list handling btw.
Cheers,
bzt
Thank you so much for your reply.. I looked the provided code, and it's very helpful to me. But still I am unable to fix this issue, suffering from a painful process. I tried blocking two thread with Id -2,3 than I unblocked 2 it worked than I unblocked 3 it caused gpf and also I noticed that tha stack "rsp" value is damaged when the blocked thread causes sched_switch. I think my context switching code is not perfect.
Re: Blocking and unblocking thread from blocked list
What does your context switching code look like? That's something that can be easy to get wrong!
Re: Blocking and unblocking thread from blocked list
Hello, after some days of deeply working with thread blocking and unblocking issue, I finally solved the problem. Now I can block any thread, and unblock any thread. Blocking a thread move it to block list, unblocking a thread move from block list to ready list with state modification. My problem was with kernel_esp {Kernel Stack per user thread}. When a system call occurs with "syscall" instruction the kernel stack is loaded, which was pointing to some invalid address. So I modified that, now the kernel stack is loaded directly from current_thread() structure like this,
This is, how I fixed.
And my scheduling_isr code is
Thank you so much to everyone, for your replies...Now I can happily go for the Composite Window Manager and other GUI applications.
Manas Kamal
Code: Select all
mov rdx, rsp ; store the user stack in rdx
mov r9, [rel current_thread] ;current_thread is an external global variable pointing to currently running thread
mov rsp, [r9 + 0xC8] ;already we stored current_thread address in r9, now from r9 we get kernel stack at 0xC8
; ....do syscall stuff and restore the stack and return from here
And my scheduling_isr code is
Code: Select all
//! Simple round robin scheduler for now!!
if (store_context(current_thread) == 0) {
current_thread->cr3 = get_pml4();
if (current_thread->privl == USER_THREAD)
current_thread->kernel_stack = get_kernel_tss()->rsp[0];
//! before moving to next we send EOI to interrupt controller
apic_local_eoi ();
//! call the scheduler thread switcher
next_thread ();
//! now current thread points to the next thread
if (current_thread->privl == USER_THREAD)
get_kernel_tss()->rsp[0] = current_thread->kernel_stack;
set_pml4 (current_thread->cr3);
//! finally execute the thread context from here
execute_context (current_thread);
}
Manas Kamal