Page 1 of 1

atomic system calls

Posted: Mon Jan 23, 2012 2:08 pm
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?

Re: atomic system calls

Posted: Mon Jan 23, 2012 2:17 pm
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

Re: atomic system calls

Posted: Mon Jan 23, 2012 2:26 pm
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.

Re: atomic system calls

Posted: Mon Jan 23, 2012 2:50 pm
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?

Re: atomic system calls

Posted: Mon Jan 23, 2012 2:59 pm
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 ?

Re: atomic system calls

Posted: Mon Jan 23, 2012 3:04 pm
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

Re: atomic system calls

Posted: Mon Jan 23, 2012 3:12 pm
by gerryg400
The code looks okay to me. I'm not sure I understand your problem.

Re: atomic system calls

Posted: Mon Jan 23, 2012 3:18 pm
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

Re: atomic system calls

Posted: Mon Jan 23, 2012 8:41 pm
by evoex
I reckon it might be useful to actually explain the problem, before you expect people to read through your source code...

Re: atomic system calls

Posted: Mon Jan 23, 2012 9:35 pm
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

Re: atomic system calls

Posted: Tue Jan 24, 2012 3:46 am
by psnix
@Brendan:

thank you problem solved [-o< =D>