/[libspopc]/mutex.c
ViewVC logotype

Diff of /mutex.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2 by ben, Thu Oct 8 15:37:49 2009 UTC revision 25 by ben, Wed Apr 11 14:06:48 2012 UTC
# Line 4  Line 4 
4   *   *
5   * libspopc offers simple API for a pop3 client.   * libspopc offers simple API for a pop3 client.
6   * See RFC 1725 for pop3 specifications.   * See RFC 1725 for pop3 specifications.
  * more information on http://herewe.servebeer.com/libspopc/  
7   *   *
8   *  This library is free software; you can redistribute it and/or   *  This library is free software; you can redistribute it and/or
9   *  modify it under the terms of the GNU Lesser General Public   *  modify it under the terms of the GNU Lesser General Public
# Line 20  Line 19 
19   *  License along with this library; if not, write to the Free Software   *  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   *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21   */   */
22    #ifdef USE_SEM
23     #include <semaphore.h>
24    #else
25     #ifdef USE_PMUTEX
26      #include <pthread.h>
27     #endif
28    #endif
29    
 #include <semaphore.h>  
30  #include "mutex.h"  #include "mutex.h"
31    
32  /* initializes a thread-wide mutex */  /* initializes a thread-wide mutex */
33  int mutex_init(mutex* id)  int mutex_init(mutex* id)
34  {  {
35    #ifdef USE_SEM
36          return sem_init(id, 0, 1);          return sem_init(id, 0, 1);
37    #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  }  }
47    
48  /* acquire the given mutex to enter in a critical section */  /* acquire the given mutex to enter in a critical section */
49  int mutex_in (mutex* id)  int mutex_in (mutex* id)
50  {  {
51    #ifdef USE_SEM
52          return sem_wait(id);          return sem_wait(id);
53          /* this is a blocking call */          /* this is a blocking call */
54    #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  }  }
64    
65  /* quit the critical section, by releasing the given mutex */  /* quit the critical section, by releasing the given mutex */
66  int mutex_out (mutex* id)  int mutex_out (mutex* id)
67  {  {
68    #ifdef USE_SEM
69          return sem_post(id);          return sem_post(id);
70    #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  }  }
80    
81  /* clean given mutex, thus re-initable by mutex_init() */  /* clean given mutex, thus re-initable by mutex_init() */
82  int mutex_clean(mutex* id)  int mutex_clean(mutex* id)
83  {  {
84    #ifdef USE_SEM
85          return sem_destroy(id);          return sem_destroy(id);
86    #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  }  }
96    

Legend:
Removed from v.2  
changed lines
  Added in v.25

  ViewVC Help
Powered by ViewVC 1.1.26