/[libspopc]/mutex.c
ViewVC logotype

Contents of /mutex.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4 - (show 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 /* 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 * more information on http://herewe.servebeer.com/libspopc/
8 *
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 #ifdef USE_SEM
24 #include <semaphore.h>
25 #else
26 #ifdef USE_PMUTEX
27 #include <pthread.h>
28 #endif
29 #endif
30
31 #include "mutex.h"
32
33 /* initializes a thread-wide mutex */
34 int mutex_init(mutex* id)
35 {
36 #ifdef USE_SEM
37 return sem_init(id, 0, 1);
38 #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 }
48
49 /* acquire the given mutex to enter in a critical section */
50 int mutex_in (mutex* id)
51 {
52 #ifdef USE_SEM
53 return sem_wait(id);
54 /* this is a blocking call */
55 #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 }
65
66 /* quit the critical section, by releasing the given mutex */
67 int mutex_out (mutex* id)
68 {
69 #ifdef USE_SEM
70 return sem_post(id);
71 #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 }
81
82 /* clean given mutex, thus re-initable by mutex_init() */
83 int mutex_clean(mutex* id)
84 {
85 #ifdef USE_SEM
86 return sem_destroy(id);
87 #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 }
97

  ViewVC Help
Powered by ViewVC 1.1.26