/[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 4 by ben, Thu Oct 15 23:10:35 2009 UTC
# Line 20  Line 20 
20   *  License along with this library; if not, write to the Free Software   *  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   *  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    
 #include <semaphore.h>  
31  #include "mutex.h"  #include "mutex.h"
32    
33  /* initializes a thread-wide mutex */  /* initializes a thread-wide mutex */
34  int mutex_init(mutex* id)  int mutex_init(mutex* id)
35  {  {
36    #ifdef USE_SEM
37          return sem_init(id, 0, 1);          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 */  /* acquire the given mutex to enter in a critical section */
50  int mutex_in (mutex* id)  int mutex_in (mutex* id)
51  {  {
52    #ifdef USE_SEM
53          return sem_wait(id);          return sem_wait(id);
54          /* this is a blocking call */          /* 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 */  /* quit the critical section, by releasing the given mutex */
67  int mutex_out (mutex* id)  int mutex_out (mutex* id)
68  {  {
69    #ifdef USE_SEM
70          return sem_post(id);          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() */  /* clean given mutex, thus re-initable by mutex_init() */
83  int mutex_clean(mutex* id)  int mutex_clean(mutex* id)
84  {  {
85    #ifdef USE_SEM
86          return sem_destroy(id);          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    

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

  ViewVC Help
Powered by ViewVC 1.1.26