1 /*
2 * -------------------------------------------------------------
3 *
4 * Module: sem_getvalue.c
5 *
6 * Purpose:
7 * Semaphores aren't actually part of PThreads.
8 * They are defined by the POSIX Standard:
9 *
10 * POSIX 1003.1-2001
11 *
12 * -------------------------------------------------------------
13 *
14 * --------------------------------------------------------------------------
15 *
16 * Pthreads-win32 - POSIX Threads Library for Win32
17 * Copyright(C) 1998 John E. Bossom
18 * Copyright(C) 1999,2005 Pthreads-win32 contributors
19 *
20 * Contact Email: rpj@callisto.canberra.edu.au
21 *
22 * The current list of contributors is contained
23 * in the file CONTRIBUTORS included with the source
24 * code distribution. The list can also be seen at the
25 * following World Wide Web location:
26 * http://sources.redhat.com/pthreads-win32/contributors.html
27 *
28 * This library is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU Lesser General Public
30 * License as published by the Free Software Foundation; either
31 * version 2 of the License, or (at your option) any later version.
32 *
33 * This library is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 * Lesser General Public License for more details.
37 *
38 * You should have received a copy of the GNU Lesser General Public
39 * License along with this library in the file COPYING.LIB;
40 * if not, write to the Free Software Foundation, Inc.,
41 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
42 */
43
44 #include "pthread.h"
45 #include "semaphore.h"
46 #include "implement.h"
47
48
49 int
sem_getvalue(sem_t * sem,int * sval)50 sem_getvalue (sem_t * sem, int *sval)
51 /*
52 * ------------------------------------------------------
53 * DOCPUBLIC
54 * This function stores the current count value of the
55 * semaphore.
56 * RESULTS
57 *
58 * Return value
59 *
60 * 0 sval has been set.
61 * -1 failed, error in errno
62 *
63 * in global errno
64 *
65 * EINVAL 'sem' is not a valid semaphore,
66 * ENOSYS this function is not supported,
67 *
68 *
69 * PARAMETERS
70 *
71 * sem pointer to an instance of sem_t
72 *
73 * sval pointer to int.
74 *
75 * DESCRIPTION
76 * This function stores the current count value of the semaphore
77 * pointed to by sem in the int pointed to by sval.
78 */
79 {
80 if (sem == NULL || *sem == NULL || sval == NULL)
81 {
82 errno = EINVAL;
83 return -1;
84 }
85 else
86 {
87 long value;
88 register sem_t s = *sem;
89 int result = 0;
90
91 if ((result = pthread_mutex_lock(&s->lock)) == 0)
92 {
93 /* See sem_destroy.c
94 */
95 if (*sem == NULL)
96 {
97 (void) pthread_mutex_unlock (&s->lock);
98 errno = EINVAL;
99 return -1;
100 }
101
102 value = s->value;
103 (void) pthread_mutex_unlock(&s->lock);
104 *sval = value;
105 }
106
107 return result;
108 }
109
110 } /* sem_getvalue */
111