[BACK]Return to sem.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / librt

File: [cvs.NetBSD.org] / src / lib / librt / sem.c (download)

Revision 1.7, Sat Mar 10 19:59:21 2012 UTC (7 years, 8 months ago) by joerg
Branch: MAIN
CVS Tags: yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-8-base, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, matt-nb8-mediatek-base, matt-nb8-mediatek, localcount-20160914, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, agc-symver-base, agc-symver
Branch point for: phil-wifi
Changes since 1.6: +12 -9 lines

Keep in sync with libpthread: Fix error handling.

/*	$NetBSD: sem.c,v 1.7 2012/03/10 19:59:21 joerg Exp $	*/

/*-
 * Copyright (c) 2003 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jason R. Thorpe.
 *
 * 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, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``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 FOUNDATION OR CONTRIBUTORS
 * 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.
 */

/*
 * 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.
 */

#include <sys/cdefs.h>
__RCSID("$NetBSD: sem.c,v 1.7 2012/03/10 19:59:21 joerg Exp $");

/*
 * If an application is linked against both librt and libpthread, the
 * libpthread versions must be used.  Provide weak aliases to cause
 * this behavior.
 */
#define	sem_init	_librt_sem_init
#define	sem_destroy	_librt_sem_destroy
#define	sem_open	_librt_sem_open
#define	sem_close	_librt_sem_close
#define	sem_unlink	_librt_sem_unlink
#define	sem_wait	_librt_sem_wait
#define	sem_timedwait	_librt_sem_timedwait
#define	sem_trywait	_librt_sem_trywait
#define	sem_post	_librt_sem_post
#define	sem_getvalue	_librt_sem_getvalue

#define	_LIBC

#include <sys/types.h>
#include <sys/ksem.h>
#include <sys/queue.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <semaphore.h>
#include <stdarg.h>

struct _sem_st {
	unsigned int	ksem_magic;
#define	KSEM_MAGIC	0x90af0421U

	LIST_ENTRY(_sem_st) ksem_list;
	intptr_t		ksem_semid;	/* 0 -> user (non-shared) */
	sem_t		*ksem_identity;
};

static int sem_alloc(unsigned int value, intptr_t semid, sem_t *semp);
static void sem_free(sem_t sem);

static LIST_HEAD(, _sem_st) named_sems = LIST_HEAD_INITIALIZER(&named_sems);

#ifdef __weak_alias
__weak_alias(sem_init,_librt_sem_init)
__weak_alias(sem_destroy,_librt_sem_destroy)
__weak_alias(sem_open,_librt_sem_open)
__weak_alias(sem_close,_librt_sem_close)
__weak_alias(sem_unlink,_librt_sem_unlink)
__weak_alias(sem_wait,_librt_sem_wait)
__weak_alias(sem_timedwait,_librt_sem_timedwait)
__weak_alias(sem_trywait,_librt_sem_trywait)
__weak_alias(sem_post,_librt_sem_post)
__weak_alias(sem_getvalue,_librt_sem_getvalue)
#endif

static void
sem_free(sem_t sem)
{

	sem->ksem_magic = 0;
	free(sem);
}

static int
sem_alloc(unsigned int value, intptr_t semid, sem_t *semp)
{
	sem_t sem;

	if (value > SEM_VALUE_MAX)
		return (EINVAL);

	if ((sem = malloc(sizeof(struct _sem_st))) == NULL)
		return (ENOSPC);

	sem->ksem_magic = KSEM_MAGIC;
	sem->ksem_semid = semid;
 
	*semp = sem;
	return (0);
}

/* ARGSUSED */
int
sem_init(sem_t *sem, int pshared, unsigned int value)
{
	intptr_t	semid;
	int error;

	if (_ksem_init(value, &semid) == -1)
		return (-1);

	if ((error = sem_alloc(value, semid, sem)) != 0) {
		_ksem_destroy(semid);
		errno = error;
		return (-1);
	}

	return (0);
}

int
sem_destroy(sem_t *sem)
{
	int error, save_errno;

#ifdef ERRORCHECK
	if (sem == NULL || *sem == NULL || (*sem)->ksem_magic != KSEM_MAGIC) {
		errno = EINVAL;
		return (-1);
	}
#endif

	error = _ksem_destroy((*sem)->ksem_semid);
	save_errno = errno;
	sem_free(*sem);
	errno = save_errno;

	return error;
}

sem_t *
sem_open(const char *name, int oflag, ...)
{
	sem_t *sem, s;
	intptr_t semid;
	mode_t mode;
	unsigned int value;
	int error;
	va_list ap;

	mode = 0;
	value = 0;

	if (oflag & O_CREAT) {
		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(name, oflag, mode, value, &semid) == -1)
		return (SEM_FAILED);

	/*
	 * Search for a duplicate ID, we must return the same sem_t *
	 * if we locate one.
	 */
	LIST_FOREACH(s, &named_sems, ksem_list) {
		if (s->ksem_semid == semid)
			return (s->ksem_identity);
	}

	if ((sem = malloc(sizeof(*sem))) == NULL) {
		error = ENOSPC;
		goto bad;
	}
	if ((error = sem_alloc(value, semid, sem)) != 0)
		goto bad;

	LIST_INSERT_HEAD(&named_sems, *sem, ksem_list);
	(*sem)->ksem_identity = sem;

	return (sem);

 bad:
	_ksem_close(semid);
	if (sem != NULL) {
		if (*sem != NULL)
			sem_free(*sem);
		free(sem);
	}
	errno = error;
	return (SEM_FAILED);
}

int
sem_close(sem_t *sem)
{
	int error, save_errno;

#ifdef ERRORCHECK
	if (sem == NULL || *sem == NULL || (*sem)->ksem_magic != KSEM_MAGIC) {
		errno = EINVAL;
		return (-1);
	}
#endif

	error = _ksem_close((*sem)->ksem_semid);

	LIST_REMOVE((*sem), ksem_list);
	save_errno = errno;
	sem_free(*sem);
	free(sem);
	errno = save_errno;
	return error;
}

int
sem_unlink(const char *name)
{

	return (_ksem_unlink(name));
}

int
sem_wait(sem_t *sem)
{

#ifdef ERRORCHECK
	if (sem == NULL || *sem == NULL || (*sem)->ksem_magic != KSEM_MAGIC) {
		errno = EINVAL;
		return (-1);
	}
#endif

	return (_ksem_wait((*sem)->ksem_semid));
}

int
sem_timedwait(sem_t *sem, const struct timespec * __restrict abstime)
{

#ifdef ERRORCHECK
	if (sem == NULL || *sem == NULL || (*sem)->ksem_magic != KSEM_MAGIC) {
		errno = EINVAL;
		return (-1);
	}
#endif

	return (_ksem_timedwait((*sem)->ksem_semid, abstime));
}

int
sem_trywait(sem_t *sem)
{

#ifdef ERRORCHECK
	if (sem == NULL || *sem == NULL || (*sem)->ksem_magic != KSEM_MAGIC) {
		errno = EINVAL;
		return (-1);
	}
#endif

	return (_ksem_trywait((*sem)->ksem_semid));
}

int
sem_post(sem_t *sem)
{

#ifdef ERRORCHECK
	if (sem == NULL || *sem == NULL || (*sem)->ksem_magic != KSEM_MAGIC) {
		errno = EINVAL;
		return (-1);
	}
#endif

	return (_ksem_post((*sem)->ksem_semid));
}

int
sem_getvalue(sem_t * __restrict sem, int * __restrict sval)
{

#ifdef ERRORCHECK
	if (sem == NULL || *sem == NULL || (*sem)->ksem_magic != KSEM_MAGIC) {
		errno = EINVAL;
		return (-1);
	}
#endif
	return (_ksem_getvalue((*sem)->ksem_semid, sval));
}