OSDev.org

The Place to Start for Operating System Developers
It is currently Sun Aug 01, 2021 4:47 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Blocking and unblocking thread from blocked list
PostPosted: Wed Jun 23, 2021 2:27 am 
Offline

Joined: Fri Nov 01, 2019 1:17 am
Posts: 24
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


Top
 Profile  
 
 Post subject: Re: Blocking and unblocking thread from blocked list
PostPosted: Wed Jun 23, 2021 8:22 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 3222
This sounds like a problem with your implementation, not your algorithm.

What kind of debugging have you done so far?


Top
 Profile  
 
 Post subject: Re: Blocking and unblocking thread from blocked list
PostPosted: Wed Jun 23, 2021 10:16 am 
Offline

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


Top
 Profile  
 
 Post subject: Re: Blocking and unblocking thread from blocked list
PostPosted: Wed Jun 23, 2021 11:27 am 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1789
Location: Athens, GA, USA
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
μή εἶναι βασιλικήν ἀτραπόν ἐπί γεωμετρίαν
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.


Top
 Profile  
 
 Post subject: Re: Blocking and unblocking thread from blocked list
PostPosted: Wed Jun 23, 2021 11:38 am 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 956
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].


Top
 Profile  
 
 Post subject: Re: Blocking and unblocking thread from blocked list
PostPosted: Wed Jun 23, 2021 11:47 am 
Offline

Joined: Fri Nov 01, 2019 1:17 am
Posts: 24
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


Top
 Profile  
 
 Post subject: Re: Blocking and unblocking thread from blocked list
PostPosted: Wed Jun 23, 2021 11:50 am 
Offline

Joined: Fri Nov 01, 2019 1:17 am
Posts: 24
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


Top
 Profile  
 
 Post subject: Re: Blocking and unblocking thread from blocked list
PostPosted: Sat Jun 26, 2021 1:58 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1560
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


Top
 Profile  
 
 Post subject: Re: Blocking and unblocking thread from blocked list
PostPosted: Sat Jun 26, 2021 9:44 am 
Offline

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


Top
 Profile  
 
 Post subject: Re: Blocking and unblocking thread from blocked list
PostPosted: Sat Jun 26, 2021 9:49 am 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 754
What does your context switching code look like? That's something that can be easy to get wrong!


Top
 Profile  
 
 Post subject: Re: Blocking and unblocking thread from blocked list
PostPosted: Wed Jun 30, 2021 8:32 am 
Offline

Joined: Fri Nov 01, 2019 1:17 am
Posts: 24
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group