Problem on dual-processor Macs
Problem on dual-processor Macs
- Subject: Problem on dual-processor Macs
- From: Tim Hewett <email@hidden>
- Date: Thu, 25 Sep 2003 10:47:50 +0100
This is not strictly Cocoa as such: I have a problem which
only shows itself on dual processor Macs. There are two
processes which communicate using shared memory, this
memory is shared as a memory-mapped file, locked into
RAM using mlock() to stop any page faults. The buffer has
an "in" and "out" index - one process feeds data into the
buffer, incrementing and wrapping the "in" index, the other
takes data out doing the same with the "out" index.
Data is taken out of the buffer by a process at a fixed data
rate, and data is fed into the buffer by the other process
as quickly as it can until the buffer is full.
All this works great on single processor Macs. When it
fails it always fails on dual processor Macs.
It seems that the problem happens when the process
feeding data into the buffer has filled the buffer up and is
waiting for the other process to take data out of it and so
make space for more data to be put in. It waits while the
"in" and "out" indexes are the same, then data is taken out
and the "out" index moves up or is wrapped. It appears
there is a data synchronisation issue associated with
the incrementing/wrapping of the "out" index.
These indexes are C 'int' data types, so are signed 32-bit
integers at the CPU instruction level. I would expect that
incrementing a 32-bit integer to be an atomic operation,
similarly setting one to 0 being atomic too. The increment-
or-wrap code is:
if ( out == bufsize - 1 )
out = 0;
else
out++;
This could well work ok on single processor Macs because
the above code is operating in a realtime thread, which
will not be pre-empted and hence there will be no risk of
synchronisation issues because other threads and processes
will be implicitly locked out because of this. However this is
not the case with multiple CPUs when the realtime thread is
allocated to one CPU and the other process is running on
another one. But I still would not expect any problems
because I'd expect "out" to be updated in an atomic fashion.
This index is the only variable which is referenced by both
processes, there should not be any other data synchronisation
issues.
Can anyone shed any light on what is going on here? Are the
"out++" and "out = 0" lines atomic or should they be protected
by semaphore or some other type of lock?
Regards,
Tim.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.