root/src/utils/noit_sem.c

Revision 4bd337d1bd56e5e4bc7fb3615cffd102e2cd1639, 3.0 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

invert the logic, no functional change at all. Don't ask

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2005-2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *    * Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *    * Redistributions in binary form must reproduce the above
12  *      copyright notice, this list of conditions and the following
13  *      disclaimer in the documentation and/or other materials provided
14  *      with the distribution.
15  *    * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *      of its contributors may be used to endorse or promote products
17  *      derived from this software without specific prior written
18  *      permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 /* This implementation is based directly on:
34  * http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
35  * Many thanks for his thorough explanation of the problem.
36  */
37
38 #include "noit_defines.h"
39 #include "utils/noit_sem.h"
40
41 #ifndef WORKING_SEM_INIT
42 #include <pthread.h>
43 #include <errno.h>
44
45 int
46 noit_sem_init(noit_sem_t *s, int unused, int value) {
47   pthread_mutexattr_t mutexattr;
48   if(pthread_mutexattr_init(&mutexattr) != 0) return -1;
49   if(pthread_mutex_init(&s->lock, &mutexattr) != 0) return -1;
50   if(pthread_cond_init(&s->cond, NULL) != 0) return -1;
51   s->value = value;
52   return 0;
53 }
54
55 void noit_sem_post(noit_sem_t *s) {
56   pthread_mutex_lock(&s->lock);
57   pthread_cond_signal(&s->cond);
58   s->value++;
59   pthread_mutex_unlock(&s->lock);
60 }
61
62 int
63 noit_sem_wait(noit_sem_t *s) {
64   pthread_mutex_lock(&s->lock);
65  reattempt:
66   if(s->value > 0) {
67     s->value--;
68     pthread_mutex_unlock(&s->lock);
69   } else {
70     pthread_cond_wait(&s->cond, &s->lock);
71     goto reattempt;
72   }
73   return 0;
74 }
75
76 int
77 noit_sem_trywait(noit_sem_t *s) {
78   pthread_mutex_lock(&s->lock);
79   if(s->value > 0) {
80     s->value--;
81     pthread_mutex_unlock(&s->lock);
82     return 0;
83   }
84   pthread_mutex_unlock(&s->lock);
85   errno = EAGAIN;
86   return -1;
87 }
88
89 int
90 noit_sem_getvalue(noit_sem_t *s, int *value) {
91   *value = s->value;
92   return 0;
93 }
94
95 void
96 noit_sem_destroy(noit_sem_t *s)
97 {
98   pthread_mutex_destroy(&s->lock);
99   pthread_cond_destroy(&s->cond);
100 }
101
102 #endif
Note: See TracBrowser for help on using the browser.