/[libspopc]/mutex.c
ViewVC logotype

Annotation of /mutex.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 25 - (hide annotations)
Wed Apr 11 14:06:48 2012 UTC (8 years, 1 month ago) by ben
File MIME type: text/plain
File size: 2232 byte(s)
Removed old homepage reference in docs and headers

1 ben 1 /* this is mutex.c file, part of the libspopc library sources
2     * copyright © 2002- Benoit Rouits <brouits@free.fr>
3     * released under the terms of the GNU Lesser General Public Licence.
4     *
5     * libspopc offers simple API for a pop3 client.
6     * See RFC 1725 for pop3 specifications.
7     *
8     * This library is free software; you can redistribute it and/or
9     * modify it under the terms of the GNU Lesser General Public
10     * License as published by the Free Software Foundation; either
11     * version 2.1 of the License, or (at your option) any later version.
12     *
13     * This library is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16     * Lesser General Public License for more details.
17     *
18     * You should have received a copy of the GNU Lesser General Public
19     * License along with this library; if not, write to the Free Software
20     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21     */
22 ben 4 #ifdef USE_SEM
23     #include <semaphore.h>
24     #else
25     #ifdef USE_PMUTEX
26     #include <pthread.h>
27     #endif
28     #endif
29 ben 1
30     #include "mutex.h"
31    
32     /* initializes a thread-wide mutex */
33     int mutex_init(mutex* id)
34     {
35 ben 4 #ifdef USE_SEM
36 ben 1 return sem_init(id, 0, 1);
37 ben 4 #else
38     #ifdef USE_PMUTEX
39     return pthread_mutex_init(id, NULL);
40     #else
41     /* dumb and pointelss code */
42     (*id) = 0;
43     return 0;
44     #endif
45     #endif
46 ben 1 }
47    
48     /* acquire the given mutex to enter in a critical section */
49     int mutex_in (mutex* id)
50     {
51 ben 4 #ifdef USE_SEM
52 ben 1 return sem_wait(id);
53     /* this is a blocking call */
54 ben 4 #else
55     #ifdef USE_PMUTEX
56     return pthread_mutex_lock(id);
57     #else
58     /* dumb and pointelss code */
59     ++(*id);
60     return 0;
61     #endif
62     #endif
63 ben 1 }
64    
65     /* quit the critical section, by releasing the given mutex */
66     int mutex_out (mutex* id)
67     {
68 ben 4 #ifdef USE_SEM
69 ben 1 return sem_post(id);
70 ben 4 #else
71     #ifdef USE_PMUTEX
72     return pthread_mutex_unlock(id);
73     #else
74     /* dumb and pointelss code */
75     --(*id);
76     return 0;
77     #endif
78     #endif
79 ben 1 }
80    
81     /* clean given mutex, thus re-initable by mutex_init() */
82     int mutex_clean(mutex* id)
83     {
84 ben 4 #ifdef USE_SEM
85 ben 1 return sem_destroy(id);
86 ben 4 #else
87     #ifdef USE_PMUTEX
88     return pthread_mutex_destroy(id);
89     #else
90     /* dumb and pointelss code */
91     (*id) = -1;
92     return 0;
93     #endif
94     #endif
95 ben 1 }
96    

  ViewVC Help
Powered by ViewVC 1.1.26