first patch for process-shared semaphore

John Baldwin jhb at freebsd.org
Thu Dec 24 13:00:28 UTC 2009


On Wednesday 23 December 2009 8:20:28 pm David Xu wrote:
> John Baldwin wrote:
> > On Tuesday 22 December 2009 8:49:53 pm David Xu wrote:
> >> This is my first attempt to make process-shared mutex work, this means
> >> you can mmap(MAP_SHARED) a memory area, and put semaphore there,
> >> or you can sem_open a named semaphore, and just use it between
> >> processes, the named semaphore uses file system and mmap(), directory 
> >> /tmp/.semaphore is used as IPC directory, any named semaphore
> >> locates in the directory. old semaphore implementation still exists
> >> to make it binary compatible, it uses symbol version.
> >>
> >> http://people.freebsd.org/~davidxu/patch/shared_semaphore_1.patch
> > 
> > I would suggest that you leave named semaphores as they currently exist 
and 
> > follow this approach instead:
> >  
> > 1) Named semaphores use ksem_*() still.
> > 2) sem_init/sem_destroy operate on UTMX-backed semaphores identical to the 
> > ones used in the current libthr code.  The semid_t structure now becomes 
the 
> > full structure that libthr currently allocates with a flag to indicate if 
it 
> > is a "system" semaphore or otherwise.  The pshared flag passed to 
sem_init() 
> > can be used to set the sharing properties of the UMTX.
> > 3) All of sem_init/sem_destroy is just in libc.  Just move the libthr 
> > implementation bits into libc.
> > 
> 
> ksem base shared semaphore is slow because whenever you call
> sem_wait(), it always enters kernel even if count is non-zero,
> sem_post() also always enters kernel even if there is no waiter.
> but the new implementation is as simple as just an atomic operation
> in these cases, I know another competitor OS is doing things in
> this way.

Yes, Solaris uses files in /tmp and Darwin uses special file descriptors 
similar to what we do.  However, you will have to restrict the namespace if 
you go the /tmp route to be safe I think similar to what Solaris does (no path 
separators, just simple names like 'foo').  You might also want to use the 
same naming convention as Solaris if you go the /tmp route (I think they use a 
path other than .semaphore under /tmp IIRC).  Not sure if we want to do 
anything special to ensure that those particular set of files in /tmp always 
get purged on reboot to avoid weird bugs with semaphores unexpectedly 
persisting across reboots.

-- 
John Baldwin


More information about the freebsd-threads mailing list