OSDev.org
https://forum.osdev.org/

Blocking and unblocking thread from blocked list
https://forum.osdev.org/viewtopic.php?f=15&t=44570
Page 1 of 1

Author:  Kamal123 [ Wed Jun 23, 2021 2:27 am ]
Post subject:  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

Author:  Octocontrabass [ Wed Jun 23, 2021 8:22 am ]
Post subject:  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?

Author:  Kamal123 [ Wed Jun 23, 2021 10:16 am ]
Post subject:  Re: Blocking and unblocking thread from blocked list

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.

Author:  Schol-R-LEA [ Wed Jun 23, 2021 11:27 am ]
Post subject:  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?

Author:  Korona [ Wed Jun 23, 2021 11:38 am ]
Post subject:  Re: Blocking and unblocking thread from blocked list

Do you have any locks around that code?

Author:  Kamal123 [ Wed Jun 23, 2021 11:47 am ]
Post subject:  Re: Blocking and unblocking thread from blocked list

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

Author:  Kamal123 [ Wed Jun 23, 2021 11:50 am ]
Post subject:  Re: Blocking and unblocking thread from blocked list

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

Author:  bzt [ Sat Jun 26, 2021 1:58 am ]
Post subject:  Re: Blocking and unblocking thread from blocked list

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

Author:  Kamal123 [ Sat Jun 26, 2021 9:44 am ]
Post subject:  Re: Blocking and unblocking thread from blocked list

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.

Author:  nexos [ Sat Jun 26, 2021 9:49 am ]
Post subject:  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!

Author:  Kamal123 [ Wed Jun 30, 2021 8:32 am ]
Post subject:  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,

Code:
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:
//! 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

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/