Page 1 of 1

Blocking and unblocking thread from blocked list

Posted: Wed Jun 23, 2021 2:27 am
by Kamal123
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

Re: Blocking and unblocking thread from blocked list

Posted: Wed Jun 23, 2021 8:22 am
by Octocontrabass
This sounds like a problem with your implementation, not your algorithm.

What kind of debugging have you done so far?

Re: Blocking and unblocking thread from blocked list

Posted: Wed Jun 23, 2021 10:16 am
by Kamal123
Octocontrabass wrote:This sounds like a problem with your implementation, not your algorithm.

What kind of debugging have you done so far?
Hi,
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.

Re: Blocking and unblocking thread from blocked list

Posted: Wed Jun 23, 2021 11:27 am
by Schol-R-LEA
Do you have an offsite repo (on a site such as Github or Sourceforge) where we could review your source code?

Re: Blocking and unblocking thread from blocked list

Posted: Wed Jun 23, 2021 11:38 am
by Korona
Do you have any locks around that code?

Re: Blocking and unblocking thread from blocked list

Posted: Wed Jun 23, 2021 11:47 am
by Kamal123
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?
Hi,
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

Posted: Wed Jun 23, 2021 11:50 am
by Kamal123
Korona wrote:Do you have any locks around that code?
Hi,
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

Posted: Sat Jun 26, 2021 1:58 am
by bzt
Kamal123 wrote:
Korona wrote:Do you have any locks around that code?
Hi,
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
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.

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

Posted: Sat Jun 26, 2021 9:44 am
by Kamal123
bzt wrote:
Kamal123 wrote:
Korona wrote:Do you have any locks around that code?
Hi,
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
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.

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

Posted: Sat Jun 26, 2021 9:49 am
by nexos
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

Posted: Wed Jun 30, 2021 8:32 am
by Kamal123
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,

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 
This is, how I fixed.

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);
               }


                   
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