[techtalk] SMP/threaded programming

Jeff Dike jdike at karaya.com
Sat Oct 23 00:55:59 EST 1999


> as I said, I was assuming I could garuntee testset was an *atomic*
> function... i.e. indivisible and possibly smaller than the timeslice
> of a scheduler... like a few i/o functions...

Speed doesn't guarantee anything.  A timer interrupt can reschedule you 
anytime, anywhere.  Also, reschedules happen on system call exit, so any 
system call you use is going to make you more vulnerable to unexpected 
rescheduling.

> I'd probably have to look over the SMP kernel source and see what they
> use for semaphores...

What you really want is SysV semaphores.  man semop, semctl, and semget.  If 
semaphores are not suitable (you mentioned possibly needing lots of them), I 
would rethink your locking, and maybe make it less granular.  If that's no 
good, you can get atomic ops in user-mode on IA32 with the xchg intruction and 
the lock prefix.  Alpha has the load-locked/store-conditional instructions...

BTW this is the kernel semaphore down proc (from include/asm-i386/semaphore.h).

Probably not what you are after...

extern inline void down(struct semaphore * sem)
{
#if WAITQUEUE_DEBUG
	CHECK_MAGIC(sem->__magic);
#endif

	__asm__ __volatile__(
		"# atomic down operation\n\t"
#ifdef __SMP__
		"lock ; "
#endif
		"decl (%0)\n\t"     /* --sem->count */
		"js 2f\n"
		"1:\n"
		".section .text.lock,\"ax\"\n"
		"2:\tcall __down_failed\n\t"
		"jmp 1b\n"
		".previous"
		:/* no outputs */
		:"c" (sem)
		:"memory");
}


				Jeff



************
techtalk at linuxchix.org   http://www.linuxchix.org




More information about the Techtalk mailing list