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

Contents of /trunk/3rdparty/w32pthreads/sem_destroy.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: 4671 byte(s)
committing r3113 initial commit again...
1 /*
2 * -------------------------------------------------------------
3 *
4 * Module: sem_destroy.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_destroy (sem_t * sem)
50 /*
51 * ------------------------------------------------------
52 * DOCPUBLIC
53 * This function destroys an unnamed semaphore.
54 *
55 * PARAMETERS
56 * sem
57 * pointer to an instance of sem_t
58 *
59 * DESCRIPTION
60 * This function destroys an unnamed semaphore.
61 *
62 * RESULTS
63 * 0 successfully destroyed semaphore,
64 * -1 failed, error in errno
65 * ERRNO
66 * EINVAL 'sem' is not a valid semaphore,
67 * ENOSYS semaphores are not supported,
68 * EBUSY threads (or processes) are currently
69 * blocked on 'sem'
70 *
71 * ------------------------------------------------------
72 */
73 {
74 int result = 0;
75 sem_t s = NULL;
76
77 if (sem == NULL || *sem == NULL)
78 {
79 result = EINVAL;
80 }
81 else
82 {
83 s = *sem;
84
85 if ((result = pthread_mutex_lock (&s->lock)) == 0)
86 {
87 if (s->value < 0)
88 {
89 (void) pthread_mutex_unlock (&s->lock);
90 result = EBUSY;
91 }
92 else
93 {
94 /* There are no threads currently blocked on this semaphore. */
95
96 if (!CloseHandle (s->sem))
97 {
98 (void) pthread_mutex_unlock (&s->lock);
99 result = EINVAL;
100 }
101 else
102 {
103 /*
104 * Invalidate the semaphore handle when we have the lock.
105 * Other sema operations should test this after acquiring the lock
106 * to check that the sema is still valid, i.e. before performing any
107 * operations. This may only be necessary before the sema op routine
108 * returns so that the routine can return EINVAL - e.g. if setting
109 * s->value to SEM_VALUE_MAX below does force a fall-through.
110 */
111 *sem = NULL;
112
113 /* Prevent anyone else actually waiting on or posting this sema.
114 */
115 s->value = SEM_VALUE_MAX;
116
117 (void) pthread_mutex_unlock (&s->lock);
118
119 do
120 {
121 /* Give other threads a chance to run and exit any sema op
122 * routines. Due to the SEM_VALUE_MAX value, if sem_post or
123 * sem_wait were blocked by us they should fall through.
124 */
125 Sleep(0);
126 }
127 while (pthread_mutex_destroy (&s->lock) == EBUSY);
128 }
129 }
130 }
131 }
132
133 if (result != 0)
134 {
135 errno = result;
136 return -1;
137 }
138
139 free (s);
140
141 return 0;
142
143 } /* sem_destroy */

  ViewVC Help
Powered by ViewVC 1.1.22