[uClinux-dev] Threading and synchronization questions

Jamie Lokier jamie at shareable.org
Sat Feb 28 16:58:36 EST 2009


Mike Frysinger wrote:
> On Saturday 28 February 2009 13:58:04 Jamie Lokier wrote:
> > Mike Frysinger wrote:
> > > > I think I can use the pipes (with their atomic writes) to get rid of
> > > > any use of mutexes in this case. But it will be otherwise a little more
> > > > complicated than just calling the exposed functions.
> > >
> > > umm, why do you think writes are atomic ?  POSIX states that read/write
> > > functions need not be atomic.  if you want threading synchronization
> > > mechanisms, then use the things designed for exactly that.
> >
> > Pipes are special, and Jan _can_ rely on writes <= PIPE_BUF bytes
> > being atomic.  This is a POSIX requirement and Linux meets it.
> >
> > from the Linux man page for pipe(2):
> >
> >    PIPE_BUF
> >        POSIX.1-2001 says that write(2)s of less than PIPE_BUF bytes
> >        must be atomic: the output data is written to the pipe as a
> >        contiguous sequence.  Writes of more than PIPE_BUF bytes may be
> >        non-atomic: the kernel may interleave the data with data
> >        written by other processes.  POSIX.1-2001 requires PIPE_BUF to
> >        be at least 512 bytes.  (On Linux, PIPE_BUF is 4096 bytes.)
> >
> > It makes no difference if the pipe is between processes or threads, or
> > even from a thread to itself.  Writes <= PIPE_BUF in size are atomic:
> > they are either written whole, or block, or return EAGAIN if non-blocking.
> >
> > Writes > PIPE_BUF in size are not atomic, and can be split up at any
> > position.
> 
> taking a snippet from the spec and removing key words doesnt make the result 
> correct ;).  in the actual link to POSIX that i posted, it clearly uses the 
> word "processes" everywhere.

Good catch.  Nonetheless, it is atomic between threads on Linux, and
documentation for AIX and IRIX says it is explicitly on those.

-- Jamie



More information about the uClinux-dev mailing list