POSIX spinlocks on QNX 6

In our codebase that we’re porting from VxWorks (on a PPC platform), we
have our own implementation of spinlocks written in PPC assembly.

What I would like to know is if we can replace these with POSIX
spinlocks in QNX, or not? The docs don’t give a very detailed
description of exactly what a spinlock does.

Our system has two CPUs on the board which share memory, but we do not
run SMP (of course: this is VxWroks :slight_smile:). Each CPU has its own memory
segment, but they do talk to each other. Our implementation of
spinlocks uses the special PowerPC instructions lwarx and stwcx to
implement locking among multiple processors that share memory. It can
be used for mutual exclusion among all threads of execution on all
processors that share a piece of memory.

Also, we have a spinlock function that does the above while disabling
interrupts.

Some of this code may be legacy, and no longer relevant in QNX; we’ll
investigate that of course, but I’m wondering if anyone has any thoughts
about porting this to POSIX / QNX.

Thx…

Paul D. Smith <pausmith@nortelnetworks.com> HASMAT–HA Software Mthds & Tools
“Please remain calm…I may be mad, but I am a professional.” --Mad Scientist

These are my opinions—Nortel Networks takes no responsibility for them.

I understand that spinlocks are better choice when you run multiple CPUs and
cost of context switch on your system is relatively low compared to cost of
a kernel call (so you avoid kernel calls on contested cases at the cost of
doing more context switches to run spinlocks in parallel). QNX fits that
model. Otherwise mutexes are better choice.

Another question is how good the implementation of POSIX mutexes and
spinlocks is. If they are based on hardware test-and-set instructions, why
not. Otherwise assembler macros are way to go. For example POSIX mutexes are
very sucky on some platforms (can anyone say Linux?) Portability is nice of
course, but POSIX spinlocks are not in official standard afaik and they are
hardly portable at this point.

I don’t know if PPC versions of POSIX mutexes/spinlocks is good on QNX, but
I tend to think they are.

– igor

“Paul D. Smith” <pausmith@nortelnetworks.com> wrote in message
news:p53cvkrpwn.fsf@lemming.engeast.baynetworks.com

In our codebase that we’re porting from VxWorks (on a PPC platform), we
have our own implementation of spinlocks written in PPC assembly.

What I would like to know is if we can replace these with POSIX
spinlocks in QNX, or not? The docs don’t give a very detailed
description of exactly what a spinlock does.

Our system has two CPUs on the board which share memory, but we do not
run SMP (of course: this is VxWroks > :slight_smile:> ). Each CPU has its own memory
segment, but they do talk to each other. Our implementation of
spinlocks uses the special PowerPC instructions lwarx and stwcx to
implement locking among multiple processors that share memory. It can
be used for mutual exclusion among all threads of execution on all
processors that share a piece of memory.

Also, we have a spinlock function that does the above while disabling
interrupts.

Some of this code may be legacy, and no longer relevant in QNX; we’ll
investigate that of course, but I’m wondering if anyone has any thoughts
about porting this to POSIX / QNX.

Thx…


Paul D. Smith <> pausmith@nortelnetworks.com> > HASMAT–HA Software Mthds &
Tools
“Please remain calm…I may be mad, but I am a professional.” --Mad
Scientist


These are my opinions—Nortel Networks takes no responsibility for
them.

Thanks, but I don’t think this really answers my questions. First, as I
said my CPUs are NOT running in SMP, they are separate processors
sharing the same physical memory (they each take a chunk for their
own). That is, I’m running separated instances of the operating system,
for example.

I do not believe that mutexes will allow me to do inter-processor
synchronization in such a scenario.

What about spinlocks?


BTW, POSIX spinlocks are part of the latest standard, although IIRC they
are in the Advanced RT Threads section so they are probably not widely
implemented.

Paul D. Smith <pausmith@nortelnetworks.com> HASMAT–HA Software Mthds & Tools
“Please remain calm…I may be mad, but I am a professional.” --Mad Scientist

These are my opinions—Nortel Networks takes no responsibility for them.

For mutexes, you’re right - they won’t work if it is not real SMP.

For spinlocks I think it does not matter SMP or not because they don’t
depend on kernel to handle contested locks. What matters is ability of
system to execute more than one instruction truely simultaneously. If your
CPUs share memory and your lock is in that memory, you have same problems as
you’d have on SMP.

What I am not sure however is effect of caching on this. SMP systems
implement cache coherency protocol, your system I have no idea what it is
doing… In simplest case your shared memory could be marked as
non-cacheable.

– igor

“Paul D. Smith” <pausmith@nortelnetworks.com> wrote in message
news:p5elf3bc5i.fsf@lemming.engeast.baynetworks.com

Thanks, but I don’t think this really answers my questions. First, as I
said my CPUs are NOT running in SMP, they are separate processors
sharing the same physical memory (they each take a chunk for their
own). That is, I’m running separated instances of the operating system,
for example.

I do not believe that mutexes will allow me to do inter-processor
synchronization in such a scenario.

What about spinlocks?


BTW, POSIX spinlocks are part of the latest standard, although IIRC they
are in the Advanced RT Threads section so they are probably not widely
implemented.


Paul D. Smith <> pausmith@nortelnetworks.com> > HASMAT–HA Software Mthds &
Tools
“Please remain calm…I may be mad, but I am a professional.” --Mad
Scientist


These are my opinions—Nortel Networks takes no responsibility for
them.

Paul D. Smith <pausmith@nortelnetworks.com> wrote:

In our codebase that we’re porting from VxWorks (on a PPC platform), we
have our own implementation of spinlocks written in PPC assembly.

What I would like to know is if we can replace these with POSIX
spinlocks in QNX, or not? The docs don’t give a very detailed
description of exactly what a spinlock does.

Probably not. From what someone else was saying, our implementation
of Posix spinlocks happens to use mutexes.

Our system has two CPUs on the board which share memory, but we do not
run SMP (of course: this is VxWroks > :slight_smile:> ). Each CPU has its own memory
segment, but they do talk to each other. Our implementation of
spinlocks uses the special PowerPC instructions lwarx and stwcx to
implement locking among multiple processors that share memory. It can
be used for mutual exclusion among all threads of execution on all
processors that share a piece of memory.

Also, we have a spinlock function that does the above while disabling
interrupts.

QNX does supply an InterruptLock() function that does a spinlock
(not posix, not mutex based) plus an interrupt disable, and an
InterruptUnlock() which does an free of the spinlock & an enable.

But, all of the QNX pieces are intended for use between processes
and threads running under control of 1 QNX kernel – 1 CPU or
multiple CPUs, rather than the situation you have.

For your situation, if you can run QNX SMP (1 kernel using both
processors) that would tend to be my first choice, then you have
the full flexibility of mutexes, condvars, and all the other
more powerful (and flexible) than spinlock routines. If not,
I would suggest you will probably need to use your own inline
assembly spinlock routines to handle the inter-OS synchronisation.

Some of this code may be legacy, and no longer relevant in QNX; we’ll
investigate that of course, but I’m wondering if anyone has any thoughts
about porting this to POSIX / QNX.

Yes, that may also be worth looking at – are there better ways of
doing this…

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

“David Gibbs” <dagibbs@qnx.com> wrote in message
news:afaa90$iq3$1@nntp.qnx.com

Paul D. Smith <> pausmith@nortelnetworks.com> > wrote:
In our codebase that we’re porting from VxWorks (on a PPC platform), we
have our own implementation of spinlocks written in PPC assembly.

What I would like to know is if we can replace these with POSIX
spinlocks in QNX, or not? The docs don’t give a very detailed
description of exactly what a spinlock does.

Probably not. From what someone else was saying, our implementation
of Posix spinlocks happens to use mutexes.

From what someone else was saying, your implementation should check if
there’s actually more than 1 CPU and only use mutexes if there’s only 1 CPU.
Is that the case or not?

– igor