- libc/sys/sem.c was repocopied to libc/gen/sem.c.

- sem_*(3) manpages were repocopied from libc_r.

Reviewed by:	deischen
Repocopy by:	markm
This commit is contained in:
Ruslan Ermilov 2004-01-14 20:54:16 +00:00
parent 2cb740cc11
commit fccedf067d
16 changed files with 15 additions and 1028 deletions

View file

@ -22,7 +22,7 @@ SRCS+= __xuname.c _pthread_stubs.c _rand48.c _spinlock_stub.c _thread_init.c \
pause.c pmadvise.c popen.c posixshm.c pselect.c \
psignal.c pw_scan.c pwcache.c \
raise.c readdir.c readpassphrase.c rewinddir.c \
scandir.c seed48.c seekdir.c semctl.c \
scandir.c seed48.c seekdir.c sem.c semctl.c \
setdomainname.c sethostname.c setjmperr.c setmode.c \
setproctitle.c setprogname.c \
siginterrupt.c siglist.c signal.c signbit.c \
@ -53,7 +53,9 @@ MAN+= alarm.3 arc4random.3 \
modf.3 msgctl.3 msgget.3 msgrcv.3 msgsnd.3 \
nice.3 nlist.3 pause.3 popen.3 pselect.3 psignal.3 pwcache.3 \
raise.3 rand48.3 readpassphrase.3 rfork_thread.3 \
scandir.3 setjmp.3 setmode.3 setproctitle.3 shm_open.3 \
scandir.3 sem_destroy.3 sem_getvalue.3 sem_init.3 \
sem_open.3 sem_post.3 sem_wait.3 \
setjmp.3 setmode.3 setproctitle.3 shm_open.3 \
siginterrupt.3 signal.3 signbit.3 sigsetops.3 sleep.3 \
statvfs.3 stringlist.3 \
strtofflags.3 sysconf.3 sysctl.3 syslog.3 tcgetpgrp.3 \
@ -125,6 +127,9 @@ MLINKS+=shm_open.3 shm_unlink.3
MLINKS+=sigsetops.3 sigaddset.3 sigsetops.3 sigdelset.3 \
sigsetops.3 sigemptyset.3 sigsetops.3 sigfillset.3 \
sigsetops.3 sigismember.3
MLINKS+=sem_open.3 sem_close.3 \
sem_open.3 sem_unlink.3
MLINKS+=sem_wait.3 sem_trywait.3
MLINKS+=statvfs.3 fstatvfs.3
MLINKS+=stringlist.3 sl_add.3 stringlist.3 sl_find.3 \
stringlist.3 sl_free.3 stringlist.3 sl_init.3

View file

@ -33,7 +33,7 @@
.Nm sem_destroy
.Nd destroy an unnamed semaphore
.Sh LIBRARY
.Lb libc_r
.Lb libc
.Sh SYNOPSIS
.In semaphore.h
.Ft int

View file

@ -33,7 +33,7 @@
.Nm sem_getvalue
.Nd get the value of a semaphore
.Sh LIBRARY
.Lb libc_r
.Lb libc
.Sh SYNOPSIS
.In semaphore.h
.Ft int

View file

@ -33,7 +33,7 @@
.Nm sem_init
.Nd initialize an unnamed semaphore
.Sh LIBRARY
.Lb libc_r
.Lb libc
.Sh SYNOPSIS
.In semaphore.h
.Ft int

View file

@ -35,7 +35,7 @@
.Nm sem_unlink
.Nd named semaphore operations
.Sh LIBRARY
.Lb libc_r
.Lb libc
.Sh SYNOPSIS
.In semaphore.h
.Ft sem_t *

View file

@ -33,7 +33,7 @@
.Nm sem_post
.Nd increment (unlock) a semaphore
.Sh LIBRARY
.Lb libc_r
.Lb libc
.Sh SYNOPSIS
.In semaphore.h
.Ft int

View file

@ -34,7 +34,7 @@
.Nm sem_trywait
.Nd decrement (lock) a semaphore
.Sh LIBRARY
.Lb libc_r
.Lb libc
.Sh SYNOPSIS
.In semaphore.h
.Ft int

View file

@ -18,7 +18,7 @@
.endif
# Sources common to both syscall interfaces:
SRCS+= ftruncate.c lseek.c mmap.c pread.c pwrite.c sem.c truncate.c __error.c
SRCS+= ftruncate.c lseek.c mmap.c pread.c pwrite.c truncate.c __error.c
# Add machine dependent asm sources:
SRCS+=${MDASM}

View file

@ -1,357 +0,0 @@
/*
* Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice(s), this list of conditions and the following disclaimer as
* the first lines of this file unmodified other than the possible
* addition of one or more copyright notices.
* 2. Redistributions in binary form must reproduce the above copyright
* notice(s), this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $FreeBSD$
*/
#include "namespace.h"
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <semaphore.h>
#include <stdarg.h>
#include <pthread.h>
#include <sys/queue.h>
#include <_semaphore.h>
#include "un-namespace.h"
#define _SEM_CHECK_VALIDITY(sem) \
if ((*(sem))->magic != SEM_MAGIC) { \
errno = EINVAL; \
retval = -1; \
goto RETURN; \
}
static sem_t sem_alloc(unsigned int value, semid_t semid, int system_sem);
static void sem_free(sem_t sem);
static LIST_HEAD(, sem) named_sems = LIST_HEAD_INITIALIZER(&named_sems);
static pthread_mutex_t named_sems_mtx = PTHREAD_MUTEX_INITIALIZER;
static void
sem_free(sem_t sem)
{
_pthread_mutex_destroy(&sem->lock);
_pthread_cond_destroy(&sem->gtzero);
sem->magic = 0;
free(sem);
}
static sem_t
sem_alloc(unsigned int value, semid_t semid, int system_sem)
{
sem_t sem;
if (value > SEM_VALUE_MAX) {
errno = EINVAL;
return (NULL);
}
sem = (sem_t)malloc(sizeof(struct sem));
if (sem == NULL) {
errno = ENOSPC;
return (NULL);
}
/*
* Initialize the semaphore.
*/
if (_pthread_mutex_init(&sem->lock, NULL) != 0) {
free(sem);
errno = ENOSPC;
return (NULL);
}
if (_pthread_cond_init(&sem->gtzero, NULL) != 0) {
_pthread_mutex_destroy(&sem->lock);
free(sem);
errno = ENOSPC;
return (NULL);
}
sem->count = (u_int32_t)value;
sem->nwaiters = 0;
sem->magic = SEM_MAGIC;
sem->semid = semid;
sem->syssem = system_sem;
return (sem);
}
int
sem_init(sem_t *sem, int pshared, unsigned int value)
{
int retval, got_system_sem;
semid_t semid;
got_system_sem = 0;
semid = SEM_USER;
/*
* Range check the arguments.
*/
if (pshared != 0) {
retval = ksem_init(&semid, value);
if (retval == -1)
goto RETURN;
got_system_sem = 1;
}
(*sem) = sem_alloc(value, semid, got_system_sem);
if ((*sem) == NULL)
retval = -1;
else
retval = 0;
RETURN:
if (retval != 0 && got_system_sem)
ksem_destroy(semid);
return retval;
}
int
sem_destroy(sem_t *sem)
{
int retval;
_SEM_CHECK_VALIDITY(sem);
_pthread_mutex_lock(&(*sem)->lock);
/*
* If this is a system semaphore let the kernel track it otherwise
* make sure there are no waiters.
*/
if ((*sem)->syssem != 0) {
retval = ksem_destroy((*sem)->semid);
if (retval == -1) {
_pthread_mutex_unlock(&(*sem)->lock);
goto RETURN;
}
} else if ((*sem)->nwaiters > 0) {
_pthread_mutex_unlock(&(*sem)->lock);
errno = EBUSY;
retval = -1;
goto RETURN;
}
_pthread_mutex_unlock(&(*sem)->lock);
sem_free(*sem);
retval = 0;
RETURN:
return retval;
}
sem_t *
sem_open(const char *name, int oflag, ...)
{
sem_t *sem;
sem_t s;
semid_t semid;
mode_t mode;
unsigned int value;
mode = 0;
value = 0;
if ((oflag & O_CREAT) != 0) {
va_list ap;
va_start(ap, oflag);
mode = va_arg(ap, int);
value = va_arg(ap, unsigned int);
va_end(ap);
}
/*
* we can be lazy and let the kernel handle the "oflag",
* we'll just merge duplicate IDs into our list.
*/
if (ksem_open(&semid, name, oflag, mode, value) == -1)
return (SEM_FAILED);
/*
* search for a duplicate ID, we must return the same sem_t *
* if we locate one.
*/
_pthread_mutex_lock(&named_sems_mtx);
LIST_FOREACH(s, &named_sems, entry) {
if (s->semid == semid) {
_pthread_mutex_unlock(&named_sems_mtx);
return (s->backpointer);
}
}
sem = (sem_t *)malloc(sizeof(*sem));
if (sem == NULL)
goto err;
*sem = sem_alloc(value, semid, 1);
if ((*sem) == NULL)
goto err;
LIST_INSERT_HEAD(&named_sems, *sem, entry);
(*sem)->backpointer = sem;
_pthread_mutex_unlock(&named_sems_mtx);
return (sem);
err:
_pthread_mutex_unlock(&named_sems_mtx);
ksem_close(semid);
if (sem != NULL) {
if (*sem != NULL)
sem_free(*sem);
else
errno = ENOSPC;
free(sem);
} else {
errno = ENOSPC;
}
return (SEM_FAILED);
}
int
sem_close(sem_t *sem)
{
if ((*sem)->syssem == 0) {
errno = EINVAL;
return (-1);
}
_pthread_mutex_lock(&named_sems_mtx);
if (ksem_close((*sem)->semid) == -1) {
_pthread_mutex_unlock(&named_sems_mtx);
return (-1);
}
LIST_REMOVE((*sem), entry);
_pthread_mutex_unlock(&named_sems_mtx);
sem_free(*sem);
free(sem);
return (0);
}
int
sem_unlink(const char *name)
{
return (ksem_unlink(name));
}
int
sem_wait(sem_t *sem)
{
int retval;
_SEM_CHECK_VALIDITY(sem);
if ((*sem)->syssem != 0) {
retval = ksem_wait((*sem)->semid);
goto RETURN;
}
_pthread_mutex_lock(&(*sem)->lock);
while ((*sem)->count == 0) {
(*sem)->nwaiters++;
_pthread_cond_wait(&(*sem)->gtzero, &(*sem)->lock);
(*sem)->nwaiters--;
}
(*sem)->count--;
_pthread_mutex_unlock(&(*sem)->lock);
retval = 0;
RETURN:
return retval;
}
int
sem_trywait(sem_t *sem)
{
int retval;
_SEM_CHECK_VALIDITY(sem);
if ((*sem)->syssem != 0) {
retval = ksem_trywait((*sem)->semid);
goto RETURN;
}
_pthread_mutex_lock(&(*sem)->lock);
if ((*sem)->count > 0) {
(*sem)->count--;
retval = 0;
} else {
errno = EAGAIN;
retval = -1;
}
_pthread_mutex_unlock(&(*sem)->lock);
RETURN:
return retval;
}
int
sem_post(sem_t *sem)
{
int retval;
_SEM_CHECK_VALIDITY(sem);
if ((*sem)->syssem != 0) {
retval = ksem_post((*sem)->semid);
goto RETURN;
}
_pthread_mutex_lock(&(*sem)->lock);
(*sem)->count++;
if ((*sem)->nwaiters > 0)
_pthread_cond_signal(&(*sem)->gtzero);
_pthread_mutex_unlock(&(*sem)->lock);
retval = 0;
RETURN:
return retval;
}
int
sem_getvalue(sem_t * __restrict sem, int * __restrict sval)
{
int retval;
_SEM_CHECK_VALIDITY(sem);
if ((*sem)->syssem != 0) {
retval = ksem_getvalue((*sem)->semid, sval);
goto RETURN;
}
_pthread_mutex_lock(&(*sem)->lock);
*sval = (int)(*sem)->count;
_pthread_mutex_unlock(&(*sem)->lock);
retval = 0;
RETURN:
return retval;
}

View file

@ -57,12 +57,6 @@ MAN+= pthread_attr.3 \
pthread_suspend_np.3 \
pthread_switch_add_np.3 \
pthread_testcancel.3 \
sem_destroy.3 \
sem_getvalue.3 \
sem_init.3 \
sem_open.3 \
sem_post.3 \
sem_wait.3 \
sigwait.3
MLINKS+= \
@ -105,7 +99,4 @@ MLINKS+= \
pthread_schedparam.3 pthread_setschedparam.3 \
pthread_switch_add_np.3 pthread_switch_delete_np.3 \
pthread_testcancel.3 pthread_setcancelstate.3 \
pthread_testcancel.3 pthread_setcanceltype.3 \
sem_open.3 sem_close.3 \
sem_open.3 sem_unlink.3 \
sem_wait.3 sem_trywait.3
pthread_testcancel.3 pthread_setcanceltype.3

View file

@ -1,84 +0,0 @@
.\" Copyright (C) 2000 Jason Evans <jasone@FreeBSD.org>.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice(s), this list of conditions and the following disclaimer as
.\" the first lines of this file unmodified other than the possible
.\" addition of one or more copyright notices.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice(s), this list of conditions and the following disclaimer in
.\" the documentation and/or other materials provided with the
.\" distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\" $FreeBSD$
.Dd February 15, 2000
.Dt SEM_DESTROY 3
.Os
.Sh NAME
.Nm sem_destroy
.Nd destroy an unnamed semaphore
.Sh LIBRARY
.Lb libc_r
.Sh SYNOPSIS
.In semaphore.h
.Ft int
.Fn sem_destroy "sem_t *sem"
.Sh DESCRIPTION
The
.Fn sem_destroy
function destroys the unnamed semaphore pointed to by
.Fa sem .
After a successful call to
.Fn sem_destroy ,
.Fa sem
is unusable until re-initialized by another call to
.Fn sem_init .
.Sh RETURN VALUES
.Rv -std sem_destroy
.Sh ERRORS
The
.Fn sem_destroy
function will fail if:
.Bl -tag -width Er
.It Bq Er EINVAL
.Fa sem
points to an invalid semaphore.
.It Bq Er EBUSY
There are currently threads blocked on the semaphore that
.Fa sem
points to.
.El
.Sh SEE ALSO
.Xr sem_init 3 ,
.Xr sem 4
.Sh STANDARDS
The
.Fn sem_destroy
function conforms to
.St -p1003.1-96 .
.Pp
POSIX does not define the behavior of
.Fn sem_destroy
if called while there are threads blocked on
.Fa sem ,
but this implementation is guaranteed to return -1 and set
.Va errno
to
.Er EBUSY
if there are threads blocked on
.Fa sem .

View file

@ -1,76 +0,0 @@
.\" Copyright (C) 2000 Jason Evans <jasone@FreeBSD.org>.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice(s), this list of conditions and the following disclaimer as
.\" the first lines of this file unmodified other than the possible
.\" addition of one or more copyright notices.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice(s), this list of conditions and the following disclaimer in
.\" the documentation and/or other materials provided with the
.\" distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\" $FreeBSD$
.Dd February 15, 2000
.Dt SEM_GETVALUE 3
.Os
.Sh NAME
.Nm sem_getvalue
.Nd get the value of a semaphore
.Sh LIBRARY
.Lb libc_r
.Sh SYNOPSIS
.In semaphore.h
.Ft int
.Fn sem_getvalue "sem_t * restrict sem" "int * restrict sval"
.Sh DESCRIPTION
The
.Fn sem_getvalue
function sets the variable pointed to by
.Fa sval
to the current value of the semaphore pointed to by
.Fa sem ,
as of the time that the call to
.Fn sem_getvalue
is actually run.
.Sh RETURN VALUES
.Rv -std sem_getvalue
.Sh ERRORS
The
.Fn sem_getvalue
function will fail if:
.Bl -tag -width Er
.It Bq Er EINVAL
.Fa sem
points to an invalid semaphore.
.El
.Sh SEE ALSO
.Xr sem_post 3 ,
.Xr sem_trywait 3 ,
.Xr sem_wait 3 ,
.Xr sem 4
.Sh STANDARDS
The
.Fn sem_getvalue
function conforms to
.St -p1003.1-96 .
.Pp
The value of the semaphore is never negative, even if there are threads blocked
on the semaphore. POSIX is somewhat ambiguous in its wording with regard to
what the value of the semaphore should be if there are blocked waiting threads,
but this behavior is conformant, given the wording of the specification.

View file

@ -1,102 +0,0 @@
.\" Copyright (C) 2000 Jason Evans <jasone@FreeBSD.org>.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice(s), this list of conditions and the following disclaimer as
.\" the first lines of this file unmodified other than the possible
.\" addition of one or more copyright notices.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice(s), this list of conditions and the following disclaimer in
.\" the documentation and/or other materials provided with the
.\" distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\" $FreeBSD$
.Dd February 15, 2000
.Dt SEM_INIT 3
.Os
.Sh NAME
.Nm sem_init
.Nd initialize an unnamed semaphore
.Sh LIBRARY
.Lb libc_r
.Sh SYNOPSIS
.In semaphore.h
.Ft int
.Fn sem_init "sem_t *sem" "int pshared" "unsigned int value"
.Sh DESCRIPTION
The
.Fn sem_init
function initializes the unnamed semaphore pointed to by
.Fa sem
to have the value
.Fa value .
A non-zero value for
.Fa pshared
specifies a shared semaphore that can be used by multiple processes, which this
implementation is not capable of.
.Pp
Following a successful call to
.Fn sem_init ,
.Fa sem
can be used as an argument in subsequent calls to
.Xr sem_wait 3 ,
.Xr sem_trywait 3 ,
.Xr sem_post 3 ,
and
.Xr sem_destroy 3 .
The
.Fa sem
argument is no longer valid after a successful call to
.Xr sem_destroy 3 .
.Sh RETURN VALUES
.Rv -std sem_init
.Sh ERRORS
The
.Fn sem_init
function will fail if:
.Bl -tag -width Er
.It Bq Er EINVAL
.Fa value
exceeds SEM_VALUE_MAX.
.It Bq Er ENOSPC
Memory allocation error.
.It Bq Er EPERM
Unable to initialize a shared semaphore.
.El
.Sh SEE ALSO
.Xr sem_destroy 3 ,
.Xr sem_post 3 ,
.Xr sem_trywait 3 ,
.Xr sem_wait 3 ,
.Xr sem 4
.Sh STANDARDS
The
.Fn sem_init
function conforms to
.St -p1003.1-96 .
.Pp
This implementation does not support shared semaphores, and reports this fact
by setting
.Va errno
to
.Er EPERM .
This is perhaps a stretch of the intention of POSIX, but is
compliant, with the caveat that
.Fn sem_init
always reports a permissions error when an attempt to create a shared semaphore
is made.

View file

@ -1,227 +0,0 @@
.\" Copyright (C) 2000 Jason Evans <jasone@FreeBSD.org>.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice(s), this list of conditions and the following disclaimer as
.\" the first lines of this file unmodified other than the possible
.\" addition of one or more copyright notices.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice(s), this list of conditions and the following disclaimer in
.\" the documentation and/or other materials provided with the
.\" distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\" $FreeBSD$
.Dd January 15, 2003
.Dt SEM_OPEN 3
.Os
.Sh NAME
.Nm sem_open ,
.Nm sem_close ,
.Nm sem_unlink
.Nd named semaphore operations
.Sh LIBRARY
.Lb libc_r
.Sh SYNOPSIS
.In semaphore.h
.Ft sem_t *
.Fn sem_open "const char *name" "int oflag" "..."
.Ft int
.Fn sem_close "sem_t *sem"
.Ft int
.Fn sem_unlink "const char *name"
.Sh DESCRIPTION
The
.Fn sem_open
function creates or opens the named semaphore specified by
.Fa name .
The returned semaphore may be used in subsequent calls to
.Fn sem_getvalue ,
.Fn sem_wait ,
.Fn sem_trywait ,
.Fn sem_post
and
.Fn sem_close .
.Pp
The following bits may be set in the
.Fa oflag
argument:
.Bl -tag -width ".Dv O_CREAT"
.It Dv O_CREAT
Create the semaphore if it does not already exist.
.Pp
The third argument to the call to
.Fn sem_open
must be of type
.Vt mode_t
and specifies the mode for the semaphore.
Only the
.Dv S_IWUSR ,
.Dv S_IWGRP ,
and
.Dv S_IWOTH
bits are examined;
it is not possible to grant only
.Dq read
permission on a semaphore.
The mode is modified according to the process's file creation
mask; see
.Xr umask 2 .
.Pp
The fourth argument must be an
.Vt "unsigned int"
and specifies the initial value for the semaphore,
and must be no greater than
.Dv SEM_VALUE_MAX .
.It Dv O_EXCL
Create the semaphore if it does not exist.
If the semaphore already exists,
.Fn sem_open
will fail.
This flag is ignored unless
.Dv O_CREAT
is also specified.
.El
.Pp
The
.Fn sem_close
function closes a named semaphore that was opened by a call to
.Fn sem_open .
.Pp
The
.Fn sem_unlink
function removes the semaphore named
.Fa name .
Resources allocated to the semaphore are only deallocated when all
processes that have the semaphore open close it.
.Sh RETURN VALUES
If successful,
the
.Fn sem_open
function returns the address of the opened semaphore.
If the same
.Fa name
argument is given to multiple calls to
.Fn sem_open
by the same process without an intervening call to
.Fn sem_close ,
the same address is returned each time.
If the semaphore cannot be opened,
.Fn sem_open
returns
.Dv SEM_FAILED
and the global variable
.Va errno
is set to indicate the error.
.Pp
.Rv -std sem_close sem_unlink
.Sh ERRORS
The
.Fn sem_open
function will fail if:
.Bl -tag -width Er
.It Bq Er EACCES
The semaphore exists and the permissions specified by
.Fa oflag
at the time it was created deny access to the this process.
.It Bq Er EACCES
The semaphore does not exist, but permission to create it is denied.
.It Bq Er EEXIST
.Dv O_CREAT
and
.Dv O_EXCL
are set but the semaphore already exists.
.It Bq Er EINTR
The call was interrupted by a signal.
.It Bq Er EINVAL
The
.Fn sem_open
operation is not supported for the given
.Fa name .
.It Bq Er EINVAL
The
.Fa value
argument is greater than
.Dv SEM_VALUE_MAX .
.\"FreeBSD never returns EMFILE
.\".It Bq Er EMFILE
.\"Too many semaphores are in use by this process.
.It Bq Er ENAMETOOLONG
The
.Fa name
argument is too long.
.It Bq Er ENFILE
The system limit on semaphores has been reached.
.It Bq Er ENOENT
.Dv O_CREAT
is set but the named semaphore does not exist.
.It Bq Er ENOSPC
There is not enough space to create the semaphore.
.El
.Pp
The
.Fn sem_close
function will fail if:
.Bl -tag -width Er
.It Bq Er EINVAL
The
.Fa sem
argument is not a valid semaphore.
.El
.Pp
The
.Fn sem_unlink
function will fail if:
.Bl -tag -width Er
.It Bq Er EACCES
Permission is denied to unlink the semaphore.
.It Bq Er ENAMETOOLONG
The specified
.Fa name
is too long.
.It Bq Er ENOENT
The named semaphore does not exist.
.El
.Sh SEE ALSO
.Xr close 2 ,
.Xr open 2 ,
.Xr umask 2 ,
.Xr unlink 2 ,
.Xr sem_getvalue 3 ,
.Xr sem_post 3 ,
.Xr sem_trywait 3 ,
.Xr sem_wait 3 ,
.Xr sem 4
.Sh STANDARDS
The
.Fn sem_open ,
.Fn sem_close ,
and
.Fn sem_unlink
functions conform to
.St -p1003.1-96 .
.Sh HISTORY
Support for named semaphores first appeared in
.Fx 5.0 .
.Sh BUGS
This implementation places strict requirements on the value of
.Fa name :
it must begin with a slash
.Pq Ql / ,
contain no other slash characters,
and be less than 14 characters in length
not including the terminating null character.

View file

@ -1,74 +0,0 @@
.\" Copyright (C) 2000 Jason Evans <jasone@FreeBSD.org>.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice(s), this list of conditions and the following disclaimer as
.\" the first lines of this file unmodified other than the possible
.\" addition of one or more copyright notices.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice(s), this list of conditions and the following disclaimer in
.\" the documentation and/or other materials provided with the
.\" distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\" $FreeBSD$
.Dd February 15, 2000
.Dt SEM_POST 3
.Os
.Sh NAME
.Nm sem_post
.Nd increment (unlock) a semaphore
.Sh LIBRARY
.Lb libc_r
.Sh SYNOPSIS
.In semaphore.h
.Ft int
.Fn sem_post "sem_t *sem"
.Sh DESCRIPTION
The
.Fn sem_post
function increments (unlocks) the semaphore pointed to by
.Fa sem .
If there are threads blocked on the semaphore when
.Fn sem_post
is called, then the highest priority thread that has been blocked the longest on
the semaphore will be allowed to return from
.Fn sem_wait .
.Pp
The
.Fn sem_post
function is signal-reentrant and may be called within signal handlers.
.Sh RETURN VALUES
.Rv -std sem_post
.Sh ERRORS
The
.Fn sem_post
function will fail if:
.Bl -tag -width Er
.It Bq Er EINVAL
.Fa sem
points to an invalid semaphore.
.El
.Sh SEE ALSO
.Xr sem_trywait 3 ,
.Xr sem_wait 3 ,
.Xr sem 4
.Sh STANDARDS
The
.Fn sem_post
function conforms to
.St -p1003.1-96 .

View file

@ -1,89 +0,0 @@
.\" Copyright (C) 2000 Jason Evans <jasone@FreeBSD.org>.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice(s), this list of conditions and the following disclaimer as
.\" the first lines of this file unmodified other than the possible
.\" addition of one or more copyright notices.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice(s), this list of conditions and the following disclaimer in
.\" the documentation and/or other materials provided with the
.\" distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\" $FreeBSD$
.Dd February 15, 2000
.Dt SEM_WAIT 3
.Os
.Sh NAME
.Nm sem_wait ,
.Nm sem_trywait
.Nd decrement (lock) a semaphore
.Sh LIBRARY
.Lb libc_r
.Sh SYNOPSIS
.In semaphore.h
.Ft int
.Fn sem_wait "sem_t *sem"
.Ft int
.Fn sem_trywait "sem_t *sem"
.Sh DESCRIPTION
The
.Fn sem_wait
function decrements (locks) the semaphore pointed to by
.Fa sem ,
but blocks if the value of
.Fa sem
is zero, until the value is non-zero and the value can be decremented.
.Pp
The
.Fn sem_trywait
function decrements (locks) the semaphore pointed to by
.Fa sem
only if the value is non-zero. Otherwise, the semaphore is not decremented and
an error is returned.
.Sh RETURN VALUES
.Rv -std
.Sh ERRORS
The
.Fn sem_wait
and
.Fn sem_trywait
functions will fail if:
.Bl -tag -width Er
.It Bq Er EINVAL
.Fa sem
points to an invalid semaphore.
.El
.Pp
Additionally,
.Fn sem_trywait
will fail if:
.Bl -tag -width Er
.It Bq Er EAGAIN
The semaphore value was zero, and thus could not be decremented.
.El
.Sh SEE ALSO
.Xr sem_post 3 ,
.Xr sem 4
.Sh STANDARDS
The
.Fn sem_wait
and
.Fn sem_trywait
functions conform to
.St -p1003.1-96 .