Skip to content

Commit

Permalink
GSoC 2016: Charles Cui: add SEM_NSEMS_MAX
Browse files Browse the repository at this point in the history
  • Loading branch information
zoulasc committed Jun 10, 2016
1 parent f1937da commit 2b763de
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
3 changes: 2 additions & 1 deletion include/limits.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: limits.h,v 1.36 2016/03/08 05:02:55 christos Exp $ */
/* $NetBSD: limits.h,v 1.37 2016/06/10 23:24:33 christos Exp $ */

/*
* Copyright (c) 1988, 1993
Expand Down Expand Up @@ -93,6 +93,7 @@
#define PTHREAD_THREADS_MAX _POSIX_THREAD_THREADS_MAX

#define _POSIX_TIMER_MAX 32
#define _POSIX_SEM_NSEMS_MAX 256
#define _POSIX_TTY_NAME_MAX 9
#define _POSIX_TZNAME_MAX 6

Expand Down
5 changes: 4 additions & 1 deletion lib/libc/gen/sysconf.3
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.\" $NetBSD: sysconf.3,v 1.45 2016/02/26 17:13:01 christos Exp $
.\" $NetBSD: sysconf.3,v 1.46 2016/06/10 23:24:33 christos Exp $
.\"
.\" Copyright (c) 1993
.\" The Regents of the University of California. All rights reserved.
Expand Down Expand Up @@ -149,6 +149,9 @@ and its
Semaphores
option to which the system attempts to conform,
otherwise \-1.
.It Li _SC_SEM_NSEMS_MAX
The maximum number of semaphores that one process can have open at a time,
otherwise \-1.
.It Li _SC_SHELL
Return 1 if
.Tn POSIX
Expand Down
6 changes: 4 additions & 2 deletions lib/libc/gen/sysconf.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: sysconf.c,v 1.37 2016/02/26 17:13:01 christos Exp $ */
/* $NetBSD: sysconf.c,v 1.38 2016/06/10 23:24:33 christos Exp $ */

/*-
* Copyright (c) 1993
Expand Down Expand Up @@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)sysconf.c 8.2 (Berkeley) 3/20/94";
#else
__RCSID("$NetBSD: sysconf.c,v 1.37 2016/02/26 17:13:01 christos Exp $");
__RCSID("$NetBSD: sysconf.c,v 1.38 2016/06/10 23:24:33 christos Exp $");
#endif
#endif /* LIBC_SCCS and not lint */

Expand Down Expand Up @@ -420,6 +420,8 @@ yesno: if (sysctl(mib, mib_len, &value, &len, NULL, 0) == -1)
return pathconf(_PATH_DEV, _PC_NAME_MAX);
case _SC_TIMER_MAX:
return _POSIX_TIMER_MAX;
case _SC_SEM_NSEMS_MAX:
return _POSIX_SEM_NSEMS_MAX;
default:
errno = EINVAL;
return (-1);
Expand Down
11 changes: 9 additions & 2 deletions sys/kern/uipc_sem.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: uipc_sem.c,v 1.45 2016/04/24 19:48:29 dholland Exp $ */
/* $NetBSD: uipc_sem.c,v 1.46 2016/06/10 23:24:33 christos Exp $ */

/*-
* Copyright (c) 2011 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -60,7 +60,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uipc_sem.c,v 1.45 2016/04/24 19:48:29 dholland Exp $");
__KERNEL_RCSID(0, "$NetBSD: uipc_sem.c,v 1.46 2016/06/10 23:24:33 christos Exp $");

#include <sys/param.h>
#include <sys/kernel.h>
Expand All @@ -87,6 +87,7 @@ MODULE(MODULE_CLASS_MISC, ksem, NULL);

#define SEM_MAX_NAMELEN 14

#define SEM_NSEMS_MAX 256
#define KS_UNLINKED 0x01

static kmutex_t ksem_lock __cacheline_aligned;
Expand Down Expand Up @@ -333,6 +334,11 @@ ksem_create(lwp_t *l, const char *name, ksem_t **ksret, mode_t mode, u_int val)
len = 0;
}

if (atomic_inc_uint_nv(&l->l_proc->p_nsems) > SEM_NSEMS_MAX) {
atomic_dec_uint(&l->l_proc->p_nsems);
return -1;
}

ks = kmem_zalloc(sizeof(ksem_t), KM_SLEEP);
mutex_init(&ks->ks_lock, MUTEX_DEFAULT, IPL_NONE);
cv_init(&ks->ks_cv, "psem");
Expand Down Expand Up @@ -366,6 +372,7 @@ ksem_free(ksem_t *ks)
kmem_free(ks, sizeof(ksem_t));

atomic_dec_uint(&nsems_total);
atomic_dec_uint(&curproc->p_nsems);
}

int
Expand Down
3 changes: 2 additions & 1 deletion sys/sys/proc.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: proc.h,v 1.330 2016/04/27 21:15:40 christos Exp $ */
/* $NetBSD: proc.h,v 1.331 2016/06/10 23:24:33 christos Exp $ */

/*-
* Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -305,6 +305,7 @@ struct proc {
struct lcproc *p_lwpctl; /* p, a: _lwp_ctl() information */
pid_t p_ppid; /* :: cached parent pid */
pid_t p_fpid; /* :: forked pid */
u_int p_nsems; /* Count of semaphores */

/*
* End area that is zeroed on creation
Expand Down
4 changes: 2 additions & 2 deletions sys/sys/unistd.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: unistd.h,v 1.56 2016/02/26 17:10:41 christos Exp $ */
/* $NetBSD: unistd.h,v 1.57 2016/06/10 23:24:33 christos Exp $ */

/*
* Copyright (c) 1989, 1993
Expand Down Expand Up @@ -308,7 +308,7 @@
#define _SC_SHARED_MEMORY_OBJECTS 87

#define _SC_TIMER_MAX 88

#define _SC_SEM_NSEMS_MAX 89

/* Extensions found in Solaris and Linux. */
#define _SC_PHYS_PAGES 121
Expand Down

0 comments on commit 2b763de

Please sign in to comment.