Mercurial > almixer_isolated
comparison Isolated/UNUSED/SimpleSemaphorePosix.c @ 38:71b465ff0622
Added support files.
author | Eric Wing <ewing@anscamobile.com> |
---|---|
date | Thu, 28 Apr 2011 16:22:30 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
37:b346b6608eab | 38:71b465ff0622 |
---|---|
1 #include "SimpleSemaphore.h" | |
2 #include <semaphore.h> | |
3 #include <stdlib.h> | |
4 | |
5 #if defined(DEBUG) | |
6 #include <stdio.h> | |
7 #include <string.h> | |
8 #include <errno.h> | |
9 #define SEMDBG(x) printf x | |
10 #else | |
11 #define SEMDBG(x) | |
12 #endif | |
13 | |
14 | |
15 | |
16 struct SimpleSemaphore | |
17 { | |
18 sem_t theSemaphore; | |
19 }; | |
20 | |
21 SimpleSemaphore* SimpleSemaphore_CreateSemaphore(int initial_value) | |
22 { | |
23 int ret_val; | |
24 SimpleSemaphore* simple_semaphore = (SimpleSemaphore*)malloc(sizeof(SimpleSemaphore)); | |
25 if(NULL == simple_semaphore) | |
26 { | |
27 SEMDBG(("Out of memory.\n")); | |
28 return NULL; | |
29 } | |
30 /* Drat: sem_init isn't available on OS X */ | |
31 #ifdef __APPLE__ | |
32 #warning "sem_init (unnamed semaphores) do not work on OS X. This code is broken." | |
33 #endif | |
34 ret_val = sem_init(&simple_semaphore->theSemaphore, 0, initial_value); | |
35 if(0 != ret_val) | |
36 { | |
37 /* failed */ | |
38 SEMDBG(("sem_init failed with: %d\n", ret_val)); | |
39 free(simple_semaphore); | |
40 return 0; | |
41 } | |
42 | |
43 return simple_semaphore; | |
44 } | |
45 | |
46 void SimpleSemaphore_DestroySemaphore(SimpleSemaphore* simple_semaphore) | |
47 { | |
48 if(NULL == simple_semaphore) | |
49 { | |
50 return; | |
51 } | |
52 sem_destroy(&simple_semaphore->theSemaphore); | |
53 free(simple_semaphore); | |
54 } | |
55 | |
56 int SimpleSemaphore_SemaphoreTryWait(SimpleSemaphore* simple_semaphore) | |
57 { | |
58 int ret_val; | |
59 if(NULL == simple_semaphore) | |
60 { | |
61 SEMDBG(("SimpleSemaphore_SemTryWait was passed a NULL semaphore\n")); | |
62 return 0; | |
63 } | |
64 ret_val = sem_trywait(&simple_semaphore->theSemaphore); | |
65 if(0 == ret_val) | |
66 { | |
67 return 1; | |
68 } | |
69 else | |
70 { | |
71 return 0; | |
72 } | |
73 } | |
74 | |
75 int SimpleSemaphore_SemaphoreWait(SimpleSemaphore* simple_semaphore) | |
76 { | |
77 int ret_val; | |
78 if(NULL == simple_semaphore) | |
79 { | |
80 SEMDBG(("SimpleSemaphore_SemaphoreWait was passed a NULL semaphore\n")); | |
81 return 0; | |
82 } | |
83 ret_val = sem_wait(&simple_semaphore->theSemaphore); | |
84 if(0 == ret_val) | |
85 { | |
86 return 1; | |
87 } | |
88 else | |
89 { | |
90 return 0; | |
91 } | |
92 } | |
93 | |
94 int SimpleSemaphore_SemaphoreGetValue(SimpleSemaphore* simple_semaphore) | |
95 { | |
96 int ret_val = 0; | |
97 if(NULL == simple_semaphore) | |
98 { | |
99 SEMDBG(("SimpleSemaphore_SemaphoreGetValue was passed a NULL semaphore\n")); | |
100 return 0; | |
101 } | |
102 sem_getvalue(&simple_semaphore->theSemaphore, &ret_val); | |
103 return ret_val; | |
104 } | |
105 | |
106 int SimpleSemaphore_SemaphorePost(SimpleSemaphore* simple_semaphore) | |
107 { | |
108 int ret_val; | |
109 if(NULL == simple_semaphore) | |
110 { | |
111 SEMDBG(("SimpleSemaphore_SemaphorePost was passed a NULL semaphore\n")); | |
112 return 0; | |
113 } | |
114 | |
115 ret_val = sem_post(&simple_semaphore->theSemaphore); | |
116 if(-1 == ret_val) | |
117 { | |
118 SEMDBG(("sem_post failed with: %s\n", strerror(errno))); | |
119 } | |
120 return ret_val; | |
121 } | |
122 |