atomic system calls

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
psnix
Member
Member
Posts: 50
Joined: Fri Oct 24, 2008 12:34 pm

atomic system calls

Post by psnix »

hi
all system calls must be executed atomically. but how can i do this in multiprocessor system?
(in single processor systems I do it whit disabling interrupts) but what can i do for multiprocessor systems?
psnix
Member
Member
Posts: 50
Joined: Fri Oct 24, 2008 12:34 pm

Re: atomic system calls

Post by psnix »

yes
i implement spin-locks in my OS.
using same lock for all system calls and scheduler or spreate spinlocks for any system call?

sorry for my bad english
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: atomic system calls

Post by gerryg400 »

Megan65 wrote:yes
i implement spin-locks in my OS.
using same lock for all system calls and scheduler or spreate spinlocks for any system call?

sorry for my bad english
No. Neither is correct. Spinlocks should be used to protect data, not code. Therefore you should be protecting the kernel data structures, not kernel code paths. You need a spinlock for each piece of data that might be shared. You need to design your kernel data structures so that sharing is minimised because, in practice, contended spinlocks kill performance.
If a trainstation is where trains stop, what is a workstation ?
psnix
Member
Member
Posts: 50
Joined: Fri Oct 24, 2008 12:34 pm

Re: atomic system calls

Post by psnix »

my problem is in implementing semaphores and mutexes

this my code for semaphore:

Code: Select all

// semaphore_down
void semaphore_down( semaphore_t* _sem )
{
        spin_lock( &_sem->lock );
	
 	_sem->res_count--;
 	if ( _sem->res_count < 0 )
 	{
 	queue_push( _sem->queue, (void*)g_pCurTask );
	spin_unlock( &_sem->lock );
	change_task_state( g_pCurTask, ETS_BLOCKED );
	switch_task();
 	return;
	}
	spin_unlock( &_sem->lock );
}
semaphore_down function muse be atomic and i protect it with a spinlocks but i must unlock spinlock before switching task and this is wrong. what can i do for this function. how can i guarantee that this system call dose not interrupted by scheduler?
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: atomic system calls

Post by gerryg400 »

my problem is in implementing semaphores and mutexes
Why did you waste time (of Berkus and I) by not saying that in the first post ?
If a trainstation is where trains stop, what is a workstation ?
psnix
Member
Member
Posts: 50
Joined: Fri Oct 24, 2008 12:34 pm

Re: atomic system calls

Post by psnix »

problem is in implementing atomic semaphore down and up and atomic system calls.
Why did you waste time (of Berkus and I) by not saying that in the first post ?
sorry
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: atomic system calls

Post by gerryg400 »

The code looks okay to me. I'm not sure I understand your problem.
If a trainstation is where trains stop, what is a workstation ?
psnix
Member
Member
Posts: 50
Joined: Fri Oct 24, 2008 12:34 pm

Re: atomic system calls

Post by psnix »

The code looks okay to me. I'm not sure I understand your problem.
my semaphores implementation can not protect critical regions. i don't understand where is problem. can u check my code (about two months i work on it but i cant find problem):

http://arax.svn.sourceforge.net
evoex
Member
Member
Posts: 103
Joined: Tue Dec 13, 2011 4:11 pm

Re: atomic system calls

Post by evoex »

I reckon it might be useful to actually explain the problem, before you expect people to read through your source code...
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: atomic system calls

Post by Brendan »

Hi,

The problem:

Code: Select all

// semaphore_down
void semaphore_down( semaphore_t* _sem )
{
        spin_lock( &_sem->lock );
	
 	_sem->res_count--;
 	if ( _sem->res_count < 0 )
 	{
 	queue_push( _sem->queue, (void*)g_pCurTask );
	spin_unlock( &_sem->lock );

	/*** Another CPU could unblock this task here, before it has blocked. This       ***/
	/*** would lead to deadlock (tasks waiting for something that they already have) ***/

	change_task_state( g_pCurTask, ETS_BLOCKED );
	switch_task();
 	return;
	}
	spin_unlock( &_sem->lock );
}
One solution:

Code: Select all

// semaphore_down
void semaphore_down( semaphore_t* _sem )
{
        spin_lock( &_sem->lock );
	
 	_sem->res_count--;
 	if ( _sem->res_count < 0 )
 	{
 	queue_push( _sem->queue, (void*)g_pCurTask );
        spin_lock( &scheduler_lock );
	spin_unlock( &_sem->lock );
	change_task_state( g_pCurTask, ETS_BLOCKED );
	switch_task_and_unlock_scheduler_lock();
 	return;
	}
	spin_unlock( &_sem->lock );
}
Another solution:

Code: Select all

// semaphore_down
void semaphore_down( semaphore_t* _sem )
{
        spin_lock( &_sem->lock );
	
 	_sem->res_count--;
 	if ( _sem->res_count < 0 )
 	{
 	queue_push( _sem->queue, (void*)g_pCurTask );
	change_task_state( g_pCurTask, ETS_BLOCKED );
	switch_task_and_unlock( &_sem->lock );
 	return;
	}
	spin_unlock( &_sem->lock );
}
These probably aren't the only possible solutions.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
psnix
Member
Member
Posts: 50
Joined: Fri Oct 24, 2008 12:34 pm

Re: atomic system calls

Post by psnix »

@Brendan:

thank you problem solved [-o< =D>
Post Reply