Blocking and unblocking thread from blocked list

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
Kamal123
Member
Member
Posts: 99
Joined: Fri Nov 01, 2019 1:17 am

Blocking and unblocking thread from blocked list

Post 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
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Blocking and unblocking thread from blocked list

Post by Octocontrabass »

This sounds like a problem with your implementation, not your algorithm.

What kind of debugging have you done so far?
Kamal123
Member
Member
Posts: 99
Joined: Fri Nov 01, 2019 1:17 am

Re: Blocking and unblocking thread from blocked list

Post 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.
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: Blocking and unblocking thread from blocked list

Post 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?
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.
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: Blocking and unblocking thread from blocked list

Post by Korona »

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].
Kamal123
Member
Member
Posts: 99
Joined: Fri Nov 01, 2019 1:17 am

Re: Blocking and unblocking thread from blocked list

Post 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
Kamal123
Member
Member
Posts: 99
Joined: Fri Nov 01, 2019 1:17 am

Re: Blocking and unblocking thread from blocked list

Post 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
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: Blocking and unblocking thread from blocked list

Post 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
Kamal123
Member
Member
Posts: 99
Joined: Fri Nov 01, 2019 1:17 am

Re: Blocking and unblocking thread from blocked list

Post 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.
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: Blocking and unblocking thread from blocked list

Post by nexos »

What does your context switching code look like? That's something that can be easy to get wrong!
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
Kamal123
Member
Member
Posts: 99
Joined: Fri Nov 01, 2019 1:17 am

Re: Blocking and unblocking thread from blocked list

Post 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
Post Reply