~ubuntu-branches/ubuntu/oneiric/clamav/oneiric-security

« back to all changes in this revision

Viewing changes to win32/3rdparty/pthreads/pthread_cond_destroy.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2011-06-18 11:56:34 UTC
  • mfrom: (0.35.21 sid)
  • Revision ID: james.westby@ubuntu.com-20110618115634-u2lovivet0qx34d0
Tags: 0.97.1+dfsg-1ubuntu1
* Merge from debian unstable.  Remaining changes:
  - Drop build-dep on electric-fence (in Universe)
  - Add apparmor profiles for clamd and freshclam along with maintainer
    script changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * pthread_cond_destroy.c
3
 
 *
4
 
 * Description:
5
 
 * This translation unit implements condition variables and their primitives.
6
 
 *
7
 
 *
8
 
 * --------------------------------------------------------------------------
9
 
 *
10
 
 *      Pthreads-win32 - POSIX Threads Library for Win32
11
 
 *      Copyright(C) 1998 John E. Bossom
12
 
 *      Copyright(C) 1999,2005 Pthreads-win32 contributors
13
 
 * 
14
 
 *      Contact Email: rpj@callisto.canberra.edu.au
15
 
 * 
16
 
 *      The current list of contributors is contained
17
 
 *      in the file CONTRIBUTORS included with the source
18
 
 *      code distribution. The list can also be seen at the
19
 
 *      following World Wide Web location:
20
 
 *      http://sources.redhat.com/pthreads-win32/contributors.html
21
 
 * 
22
 
 *      This library is free software; you can redistribute it and/or
23
 
 *      modify it under the terms of the GNU Lesser General Public
24
 
 *      License as published by the Free Software Foundation; either
25
 
 *      version 2 of the License, or (at your option) any later version.
26
 
 * 
27
 
 *      This library is distributed in the hope that it will be useful,
28
 
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
30
 
 *      Lesser General Public License for more details.
31
 
 * 
32
 
 *      You should have received a copy of the GNU Lesser General Public
33
 
 *      License along with this library in the file COPYING.LIB;
34
 
 *      if not, write to the Free Software Foundation, Inc.,
35
 
 *      59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
36
 
 */
37
 
 
38
 
#include "pthread.h"
39
 
#include "implement.h"
40
 
 
41
 
int
42
 
pthread_cond_destroy (pthread_cond_t * cond)
43
 
     /*
44
 
      * ------------------------------------------------------
45
 
      * DOCPUBLIC
46
 
      *      This function destroys a condition variable
47
 
      *
48
 
      *
49
 
      * PARAMETERS
50
 
      *      cond
51
 
      *              pointer to an instance of pthread_cond_t
52
 
      *
53
 
      *
54
 
      * DESCRIPTION
55
 
      *      This function destroys a condition variable.
56
 
      *
57
 
      *      NOTES:
58
 
      *              1)      A condition variable can be destroyed
59
 
      *                      immediately after all the threads that
60
 
      *                      are blocked on it are awakened. e.g.
61
 
      *
62
 
      *                      struct list {
63
 
      *                        pthread_mutex_t lm;
64
 
      *                        ...
65
 
      *                      }
66
 
      *
67
 
      *                      struct elt {
68
 
      *                        key k;
69
 
      *                        int busy;
70
 
      *                        pthread_cond_t notbusy;
71
 
      *                        ...
72
 
      *                      }
73
 
      *
74
 
      *                      
75
 
      *                      struct elt *
76
 
      *                      list_find(struct list *lp, key k)
77
 
      *                      {
78
 
      *                        struct elt *ep;
79
 
      *
80
 
      *                        pthread_mutex_lock(&lp->lm);
81
 
      *                        while ((ep = find_elt(l,k) != NULL) && ep->busy)
82
 
      *                          pthread_cond_wait(&ep->notbusy, &lp->lm);
83
 
      *                        if (ep != NULL)
84
 
      *                          ep->busy = 1;
85
 
      *                        pthread_mutex_unlock(&lp->lm);
86
 
      *                        return(ep);
87
 
      *                      }
88
 
      *
89
 
      *                      delete_elt(struct list *lp, struct elt *ep)
90
 
      *                      {
91
 
      *                        pthread_mutex_lock(&lp->lm);
92
 
      *                        assert(ep->busy);
93
 
      *                        ... remove ep from list ...
94
 
      *                        ep->busy = 0;
95
 
      *                    (A) pthread_cond_broadcast(&ep->notbusy);
96
 
      *                        pthread_mutex_unlock(&lp->lm);
97
 
      *                    (B) pthread_cond_destroy(&rp->notbusy);
98
 
      *                        free(ep);
99
 
      *                      }
100
 
      *
101
 
      *                      In this example, the condition variable
102
 
      *                      and its list element may be freed (line B)
103
 
      *                      immediately after all threads waiting for
104
 
      *                      it are awakened (line A), since the mutex
105
 
      *                      and the code ensure that no other thread
106
 
      *                      can touch the element to be deleted.
107
 
      *
108
 
      * RESULTS
109
 
      *              0               successfully released condition variable,
110
 
      *              EINVAL          'cond' is invalid,
111
 
      *              EBUSY           'cond' is in use,
112
 
      *
113
 
      * ------------------------------------------------------
114
 
      */
115
 
{
116
 
  pthread_cond_t cv;
117
 
  int result = 0, result1 = 0, result2 = 0;
118
 
 
119
 
  /*
120
 
   * Assuming any race condition here is harmless.
121
 
   */
122
 
  if (cond == NULL || *cond == NULL)
123
 
    {
124
 
      return EINVAL;
125
 
    }
126
 
 
127
 
  if (*cond != PTHREAD_COND_INITIALIZER)
128
 
    {
129
 
      EnterCriticalSection (&ptw32_cond_list_lock);
130
 
 
131
 
      cv = *cond;
132
 
 
133
 
      /*
134
 
       * Close the gate; this will synchronize this thread with
135
 
       * all already signaled waiters to let them retract their
136
 
       * waiter status - SEE NOTE 1 ABOVE!!!
137
 
       */
138
 
      if (ptw32_semwait (&(cv->semBlockLock)) != 0) /* Non-cancelable */
139
 
        {
140
 
          result = errno;
141
 
        }
142
 
      else
143
 
        {
144
 
          /*
145
 
           * !TRY! lock mtxUnblockLock; try will detect busy condition
146
 
           * and will not cause a deadlock with respect to concurrent
147
 
           * signal/broadcast.
148
 
           */
149
 
          if ((result = pthread_mutex_trylock (&(cv->mtxUnblockLock))) != 0)
150
 
            {
151
 
              (void) sem_post (&(cv->semBlockLock));
152
 
            }
153
 
        }
154
 
        
155
 
      if (result != 0)
156
 
        {
157
 
          LeaveCriticalSection (&ptw32_cond_list_lock);
158
 
          return result;
159
 
        }
160
 
 
161
 
      /*
162
 
       * Check whether cv is still busy (still has waiters)
163
 
       */
164
 
      if (cv->nWaitersBlocked > cv->nWaitersGone)
165
 
        {
166
 
          if (sem_post (&(cv->semBlockLock)) != 0)
167
 
            {
168
 
              result = errno;
169
 
            }
170
 
          result1 = pthread_mutex_unlock (&(cv->mtxUnblockLock));
171
 
          result2 = EBUSY;
172
 
        }
173
 
      else
174
 
        {
175
 
          /*
176
 
           * Now it is safe to destroy
177
 
           */
178
 
          *cond = NULL;
179
 
 
180
 
          if (sem_destroy (&(cv->semBlockLock)) != 0)
181
 
            {
182
 
              result = errno;
183
 
            }
184
 
          if (sem_destroy (&(cv->semBlockQueue)) != 0)
185
 
            {
186
 
              result1 = errno;
187
 
            }
188
 
          if ((result2 = pthread_mutex_unlock (&(cv->mtxUnblockLock))) == 0)
189
 
            {
190
 
              result2 = pthread_mutex_destroy (&(cv->mtxUnblockLock));
191
 
            }
192
 
 
193
 
          /* Unlink the CV from the list */
194
 
 
195
 
          if (ptw32_cond_list_head == cv)
196
 
            {
197
 
              ptw32_cond_list_head = cv->next;
198
 
            }
199
 
          else
200
 
            {
201
 
              cv->prev->next = cv->next;
202
 
            }
203
 
 
204
 
          if (ptw32_cond_list_tail == cv)
205
 
            {
206
 
              ptw32_cond_list_tail = cv->prev;
207
 
            }
208
 
          else
209
 
            {
210
 
              cv->next->prev = cv->prev;
211
 
            }
212
 
 
213
 
          (void) free (cv);
214
 
        }
215
 
 
216
 
      LeaveCriticalSection (&ptw32_cond_list_lock);
217
 
    }
218
 
  else
219
 
    {
220
 
      /*
221
 
       * See notes in ptw32_cond_check_need_init() above also.
222
 
       */
223
 
      EnterCriticalSection (&ptw32_cond_test_init_lock);
224
 
 
225
 
      /*
226
 
       * Check again.
227
 
       */
228
 
      if (*cond == PTHREAD_COND_INITIALIZER)
229
 
        {
230
 
          /*
231
 
           * This is all we need to do to destroy a statically
232
 
           * initialised cond that has not yet been used (initialised).
233
 
           * If we get to here, another thread waiting to initialise
234
 
           * this cond will get an EINVAL. That's OK.
235
 
           */
236
 
          *cond = NULL;
237
 
        }
238
 
      else
239
 
        {
240
 
          /*
241
 
           * The cv has been initialised while we were waiting
242
 
           * so assume it's in use.
243
 
           */
244
 
          result = EBUSY;
245
 
        }
246
 
 
247
 
      LeaveCriticalSection (&ptw32_cond_test_init_lock);
248
 
    }
249
 
 
250
 
  return ((result != 0) ? result : ((result1 != 0) ? result1 : result2));
251
 
}