/[libspopc]/mutex.c
ViewVC logotype

Annotation of /mutex.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4 - (hide annotations)
Thu Oct 15 23:10:35 2009 UTC (11 years ago) by ben
File MIME type: text/plain
File size: 2293 byte(s)
added possiblilty to use POSIX mutexes instead of unix semaphores
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 ben 2 * more information on http://herewe.servebeer.com/libspopc/
8 ben 1 *
9     * This library is free software; you can redistribute it and/or
10     * modify it under the terms of the GNU Lesser General Public
11     * License as published by the Free Software Foundation; either
12     * version 2.1 of the License, or (at your option) any later version.
13     *
14     * This library is distributed in the hope that it will be useful,
15     * but WITHOUT ANY WARRANTY; without even the implied warranty of
16     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17     * Lesser General Public License for more details.
18     *
19     * You should have received a copy of the GNU Lesser General Public
20     * License along with this library; if not, write to the Free Software
21     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22     */
23 ben 4 #ifdef USE_SEM
24     #include <semaphore.h>
25     #else
26     #ifdef USE_PMUTEX
27     #include <pthread.h>
28     #endif
29     #endif
30 ben 1
31     #include "mutex.h"
32    
33     /* initializes a thread-wide mutex */
34     int mutex_init(mutex* id)
35     {
36 ben 4 #ifdef USE_SEM
37 ben 1 return sem_init(id, 0, 1);
38 ben 4 #else
39     #ifdef USE_PMUTEX
40     return pthread_mutex_init(id, NULL);
41     #else
42     /* dumb and pointelss code */
43     (*id) = 0;
44     return 0;
45     #endif
46     #endif
47 ben 1 }
48    
49     /* acquire the given mutex to enter in a critical section */
50     int mutex_in (mutex* id)
51     {
52 ben 4 #ifdef USE_SEM
53 ben 1 return sem_wait(id);
54     /* this is a blocking call */
55 ben 4 #else
56     #ifdef USE_PMUTEX
57     return pthread_mutex_lock(id);
58     #else
59     /* dumb and pointelss code */
60     ++(*id);
61     return 0;
62     #endif
63     #endif
64 ben 1 }
65    
66     /* quit the critical section, by releasing the given mutex */
67     int mutex_out (mutex* id)
68     {
69 ben 4 #ifdef USE_SEM
70 ben 1 return sem_post(id);
71 ben 4 #else
72     #ifdef USE_PMUTEX
73     return pthread_mutex_unlock(id);
74     #else
75     /* dumb and pointelss code */
76     --(*id);
77     return 0;
78     #endif
79     #endif
80 ben 1 }
81    
82     /* clean given mutex, thus re-initable by mutex_init() */
83     int mutex_clean(mutex* id)
84     {
85 ben 4 #ifdef USE_SEM
86 ben 1 return sem_destroy(id);
87 ben 4 #else
88     #ifdef USE_PMUTEX
89     return pthread_mutex_destroy(id);
90     #else
91     /* dumb and pointelss code */
92     (*id) = -1;
93     return 0;
94     #endif
95     #endif
96 ben 1 }
97    

  ViewVC Help
Powered by ViewVC 1.1.26