/[pcsx2_0.9.7]/trunk/3rdparty/w32pthreads/sem_post_multiple.c
ViewVC logotype

Contents of /trunk/3rdparty/w32pthreads/sem_post_multiple.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Tue Sep 7 03:24:11 2010 UTC (9 years, 11 months ago) by william
File MIME type: text/plain
File size: 3931 byte(s)
committing r3113 initial commit again...
1 /*
2 * -------------------------------------------------------------
3 *
4 * Module: sem_post_multiple.c
5 *
6 * Purpose:
7 * Semaphores aren't actually part of the PThreads standard.
8 * They are defined by the POSIX Standard:
9 *
10 * POSIX 1003.1b-1993 (POSIX.1b)
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
42 */
43
44 #include "ptw32pch.h"
45 #include "semaphore.h"
46
47
48 int
49 sem_post_multiple (sem_t * sem, int count)
50 /*
51 * ------------------------------------------------------
52 * DOCPUBLIC
53 * This function posts multiple wakeups to a semaphore.
54 *
55 * PARAMETERS
56 * sem
57 * pointer to an instance of sem_t
58 *
59 * count
60 * counter, must be greater than zero.
61 *
62 * DESCRIPTION
63 * This function posts multiple wakeups to a semaphore. If there
64 * are waiting threads (or processes), n <= count are awakened;
65 * the semaphore value is incremented by count - n.
66 *
67 * RESULTS
68 * 0 successfully posted semaphore,
69 * -1 failed, error in errno
70 * ERRNO
71 * EINVAL 'sem' is not a valid semaphore
72 * or count is less than or equal to zero.
73 * ERANGE semaphore count is too big
74 *
75 * ------------------------------------------------------
76 */
77 {
78 int result = 0;
79 long waiters;
80 sem_t s = *sem;
81
82 if (s == NULL || count <= 0)
83 {
84 result = EINVAL;
85 }
86 else if ((result = pthread_mutex_lock (&s->lock)) == 0)
87 {
88 /* See sem_destroy.c
89 */
90 if (*sem == NULL)
91 {
92 (void) pthread_mutex_unlock (&s->lock);
93 result = EINVAL;
94 return -1;
95 }
96
97 if (s->value <= (SEM_VALUE_MAX - count))
98 {
99 waiters = -s->value;
100 s->value += count;
101 if (waiters > 0)
102 {
103 #ifdef NEED_SEM
104 if (SetEvent(s->sem))
105 {
106 waiters--;
107 s->leftToUnblock += count - 1;
108 if (s->leftToUnblock > waiters)
109 {
110 s->leftToUnblock = waiters;
111 }
112 }
113 #else
114 if (ReleaseSemaphore (s->sem, (waiters<=count)?waiters:count, 0))
115 {
116 /* No action */
117 }
118 #endif
119 else
120 {
121 s->value -= count;
122 result = EINVAL;
123 }
124 }
125 }
126 else
127 {
128 result = ERANGE;
129 }
130 (void) pthread_mutex_unlock (&s->lock);
131 }
132
133 if (result != 0)
134 {
135 errno = result;
136 return -1;
137 }
138
139 return 0;
140
141 } /* sem_post_multiple */

  ViewVC Help
Powered by ViewVC 1.1.22