blob: ceafeb6329ffd4b78b334c66817740a5389d32cc [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Sebastien Decugis <sdecugis@freediameter.net> *
4* *
5* Copyright (c) 2012, WIDE Project and NICT *
6* All rights reserved. *
7* *
8* Redistribution and use of this software in source and binary forms, with or without modification, are *
9* permitted provided that the following conditions are met: *
10* *
11* * Redistributions of source code must retain the above *
12* copyright notice, this list of conditions and the *
13* following disclaimer. *
14* *
15* * Redistributions in binary form must reproduce the above *
16* copyright notice, this list of conditions and the *
17* following disclaimer in the documentation and/or other *
18* materials provided with the distribution. *
19* *
20* * Neither the name of the WIDE Project or NICT nor the *
21* names of its contributors may be used to endorse or *
22* promote products derived from this software without *
23* specific prior written permission of WIDE Project and *
24* NICT. *
25* *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34*********************************************************************************************************/
35
36/* Do not include this directly, use dbg_interactive.i instead */
37
38/****** POSIX wrappers around useful functions for fD *********/
39
40/**** MUTEX ****/
41typedef struct {
42} pthread_mutex_t;
43
44%extend pthread_mutex_t {
45 pthread_mutex_t() {
46 int ret = 0;
47 pthread_mutex_t * r = calloc(1, sizeof(pthread_mutex_t));
48 if (!r) {
49 DI_ERROR_MALLOC;
50 return NULL;
51 }
52 ret = pthread_mutex_init(r, NULL);
53 if (ret) {
54 DI_ERROR(ret, NULL, NULL);
55 free(r);
56 return NULL;
57 }
58 return r;
59 }
60 ~pthread_mutex_t() {
61 int ret = 0;
62 ret = pthread_mutex_destroy($self);
63 if (ret) {
64 DI_ERROR(ret, NULL, NULL);
65 return;
66 }
67 free($self);
68 return;
69 }
70 void lock() {
71 int ret = pthread_mutex_lock($self);
72 if (ret) {
73 DI_ERROR(ret, NULL, NULL);
74 }
75 }
76 void unlock() {
77 int ret = pthread_mutex_unlock($self);
78 if (ret) {
79 DI_ERROR(ret, NULL, NULL);
80 }
81 }
82}
83
84/**** CONDVAR ****/
85typedef struct {
86} pthread_cond_t;
87
88%extend pthread_cond_t {
89 pthread_cond_t() {
90 int ret = 0;
91 pthread_cond_t * r = calloc(1, sizeof(pthread_cond_t));
92 if (!r) {
93 DI_ERROR_MALLOC;
94 return NULL;
95 }
96 ret = pthread_cond_init(r, NULL);
97 if (ret) {
98 DI_ERROR(ret, NULL, NULL);
99 free(r);
100 return NULL;
101 }
102 return r;
103 }
104 ~pthread_cond_t() {
105 int ret = 0;
106 ret = pthread_cond_destroy($self);
107 if (ret) {
108 DI_ERROR(ret, NULL, NULL);
109 return;
110 }
111 free($self);
112 return;
113 }
114 void signal() {
115 int ret = pthread_cond_signal($self);
116 if (ret) {
117 DI_ERROR(ret, NULL, NULL);
118 }
119 }
120 void broadcast() {
121 int ret = pthread_cond_broadcast($self);
122 if (ret) {
123 DI_ERROR(ret, NULL, NULL);
124 }
125 }
126 void wait(pthread_mutex_t * mutex) {
127 int ret = pthread_cond_wait($self, mutex);
128 if (ret) {
129 DI_ERROR(ret, NULL, NULL);
130 }
131 }
132 void timedwait(pthread_mutex_t * mutex, long seconds) {
133 struct timespec ts;
134 int ret;
135
136 clock_gettime(CLOCK_REALTIME, &ts);
137 ts.tv_sec += seconds;
138
139 ret = pthread_cond_timedwait($self, mutex, &ts);
140 if (ret && (ret != ETIMEDOUT)) {
141 DI_ERROR(ret, NULL, NULL);
142 }
143 }
144}
145
146/**** RWLOCK ****/
147typedef struct {
148} pthread_rwlock_t;
149
150%extend pthread_rwlock_t {
151 pthread_rwlock_t() {
152 int ret = 0;
153 pthread_rwlock_t * r = calloc(1, sizeof(pthread_rwlock_t));
154 if (!r) {
155 DI_ERROR_MALLOC;
156 return NULL;
157 }
158 ret = pthread_rwlock_init(r, NULL);
159 if (ret) {
160 DI_ERROR(ret, NULL, NULL);
161 free(r);
162 return NULL;
163 }
164 return r;
165 }
166 ~pthread_rwlock_t() {
167 int ret = 0;
168 ret = pthread_rwlock_destroy($self);
169 if (ret) {
170 DI_ERROR(ret, NULL, NULL);
171 return;
172 }
173 free($self);
174 return;
175 }
176 void rdlock() {
177 int ret = pthread_rwlock_rdlock($self);
178 if (ret) {
179 DI_ERROR(ret, NULL, NULL);
180 }
181 }
182 void wrlock() {
183 int ret = pthread_rwlock_wrlock($self);
184 if (ret) {
185 DI_ERROR(ret, NULL, NULL);
186 }
187 }
188 void unlock() {
189 int ret = pthread_rwlock_unlock($self);
190 if (ret) {
191 DI_ERROR(ret, NULL, NULL);
192 }
193 }
194}