Debian Codegraph @

lwip-contrib/ports/unix/sys_arch_pipe.c

raw symbols SCIP
1 /* This is part of LWIPv6
2 *
3 * Copyright 2004,2008,2011 Renzo Davoli University of Bologna - Italy
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20 /*
21 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
22 * All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without modification,
25 * are permitted provided that the following conditions are met:
26 *
27 * 1. Redistributions of source code must retain the above copyright notice,
28 * this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright notice,
30 * this list of conditions and the following disclaimer in the documentation
31 * and/or other materials provided with the distribution.
32 * 3. The name of the author may not be used to endorse or promote products
33 * derived from this software without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
38 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
43 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
44 * OF SUCH DAMAGE.
45 *
46 * This file is part of the lwIP TCP/IP stack.
47 *
48 * Author: Adam Dunkels <[email protected]>
49 *
50 */
51
52 /*
53 * Wed Apr 17 16:05:29 EDT 2002 (James Roth)
54 *
55 * - Fixed an unlikely sys_thread_new() race condition.
56 *
57 * - Made current_thread() work with threads which where
58 * not created with sys_thread_new(). This includes
59 * the main thread and threads made with pthread_create().
60 *
61 */
62 #include "lwip/debug.h"
63
64 #include <string.h>
65 #include <sys/time.h>
66 #include <sys/types.h>
67 #include <stdlib.h>
68 #include <unistd.h>
69 #include <pthread.h>
70 #include <stdlib.h>
71 #include <sys/select.h>
72
73 #include "lwip/sys.h"
74 #include "lwip/opt.h"
75 #include "lwip/stats.h"
76
77 #define UMAX(a, b) ((a) > (b) ? (a) : (b))
78
79 struct sys_mbox {
80 int pipe[2];
81 };
82
83 struct sys_sem {
84 unsigned int c;
85 pthread_cond_t cond;
86 pthread_mutex_t mutex;
87 };
88
89 static pthread_mutex_t lwprot_mutex = PTHREAD_MUTEX_INITIALIZER;
90 static pthread_t lwprot_thread = (pthread_t) 0xDEAD;
91 static int lwprot_count = 0;
92
93 static struct sys_sem *sys_sem_new_(u8_t count);
94 static void sys_sem_free_(struct sys_sem *sem);
95
96 static u32_t cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex,
97 u32_t timeout);
98
99 /*-----------------------------------------------------------------------------------*/
100 sys_thread_t
101 sys_thread_new(void (*function)(void *arg), void *arg, int prio)
102 {
103 int code;
104 pthread_t tmp;
105
106 code = pthread_create(&tmp,
107 NULL,
108 (void *(*)(void *))
109 function,
110 arg);
111
112 if (0 == code) {
113 return tmp;
114 } else
115 return -1;
116 }
117
118 /*-----------------------------------------------------------------------------------*/
119 struct sys_mbox *
120 sys_mbox_new()
121 {
122 struct sys_mbox *mbox;
123
124 mbox = mem_malloc(sizeof(struct sys_mbox));
125 pipe(mbox->pipe);
126
127 #if SYS_STATS
128 lwip_stats.sys.mbox.used++;
129 if (lwip_stats.sys.mbox.used > lwip_stats.sys.mbox.max) {
130 lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used;
131 }
132 #endif /* SYS_STATS */
133
134 return mbox;
135 }
136 /*-----------------------------------------------------------------------------------*/
137 void
138 sys_mbox_free(struct sys_mbox *mbox)
139 {
140 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_FREE: mbox %p \n", (void *)mbox ));
141 if (mbox != SYS_MBOX_NULL) {
142 #if SYS_STATS
143 lwip_stats.sys.mbox.used--;
144 #endif /* SYS_STATS */
145 close (mbox->pipe[0]);
146 close (mbox->pipe[1]);
147 /* LWIP_DEBUGF("sys_mbox_free: mbox 0x%lx\n", mbox); */
148 mem_free(mbox);
149 }
150 }
151
152 /*-----------------------------------------------------------------------------------*/
153 void
154 sys_mbox_post(struct sys_mbox *mbox, void *msg)
155 {
156 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_post: mbox %p msg %p\n", (void *)mbox, (void *)msg));
157
158 //fprintf(stderr,"sys_mbox_post %p %p %x\n",mbox,msg,(msg != NULL)?*((int *)msg):0);
159 write(mbox->pipe[1],&msg,sizeof(void *));
160 }
161 /*-----------------------------------------------------------------------------------*/
162 u32_t
163 sys_arch_mbox_fetch(struct sys_mbox *mbox, void **msg, u32_t timeout)
164 {
165 int fdn;
166 int time;
167 int n;
168
169 fd_set rds;
170 struct timeval tv;
171 FD_ZERO(&rds);
172 FD_SET(mbox->pipe[0],&rds);
173 //fprintf(stderr,"TIMEOUT %p %p ->%d\n",(void *)mbox,(void *) msg,timeout);
174
175 do {
176 if (timeout != 0) {
177 tv.tv_sec = timeout/1000;
178 tv.tv_usec = (timeout%1000) * 1000;
179 fdn=select(mbox->pipe[0]+1,&rds,NULL,NULL,&tv);
180 } else {
181 tv.tv_sec=tv.tv_usec=0;
182 fdn=select(mbox->pipe[0]+1,&rds,NULL,NULL,NULL);
183 }
184 } while (fdn < 0 && errno==EINTR);
185 //fprintf(stderr,"FDN %p %d %s %d\n",(void *)mbox,fdn,strerror(errno),FD_ISSET(mbox->pipe[0],&rds));
186
187 if (fdn > 0) {
188 if (msg != NULL)
189 n=read(mbox->pipe[0],msg,sizeof(void *));
190 else
191 n=read(mbox->pipe[0],&fdn,sizeof(void *));
192 //fprintf(stderr,"sys_mbox_read %p %p %x\n",mbox,(msg==NULL)?NULL:(void *)*msg, (msg==NULL || *msg==NULL)?0:**(int **)msg);
193 if (timeout != 0)
194 time=timeout - (tv.tv_sec * 1000+tv.tv_usec / 1000);
195 else time=0;
196 /***************************/
197 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_fetch: mbox %p msg %p timeout %d\n", (void *)mbox, (msg==NULL)?NULL:(void *)*msg,time));
198 } else {
199 time=SYS_ARCH_TIMEOUT;
200 if (msg != NULL)
201 *msg=NULL;
202 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_fetch: mbox %p msg %p TIMEOUT\n", (void *)mbox, (msg==NULL)?NULL:(void *)* msg));
203 }
204
205 return time;
206 }
207 /*-----------------------------------------------------------------------------------*/
208 struct sys_sem *
209 sys_sem_new(u8_t count)
210 {
211 #if SYS_STATS
212 lwip_stats.sys.sem.used++;
213 if (lwip_stats.sys.sem.used > lwip_stats.sys.sem.max) {
214 lwip_stats.sys.sem.max = lwip_stats.sys.sem.used;
215 }
216 #endif /* SYS_STATS */
217 return sys_sem_new_(count);
218 }
219
220 /*-----------------------------------------------------------------------------------*/
221 static struct sys_sem *
222 sys_sem_new_(u8_t count)
223 {
224 struct sys_sem *sem;
225
226 sem = mem_malloc(sizeof(struct sys_sem));
227 sem->c = count;
228
229 pthread_cond_init(&(sem->cond), NULL);
230 pthread_mutex_init(&(sem->mutex), NULL);
231
232 return sem;
233 }
234
235 /*-----------------------------------------------------------------------------------*/
236 static u32_t
237 cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex, u32_t timeout)
238 {
239 int tdiff;
240 unsigned long sec, usec;
241 struct timeval rtime1, rtime2;
242 struct timespec ts;
243 struct timezone tz;
244 int retval;
245
246 if (timeout > 0) {
247 /* Get a timestamp and add the timeout value. */
248 gettimeofday(&rtime1, &tz);
249 sec = rtime1.tv_sec;
250 usec = rtime1.tv_usec;
251 usec += timeout % 1000 * 1000;
252 sec += (int)(timeout / 1000) + (int)(usec / 1000000);
253 usec = usec % 1000000;
254 ts.tv_nsec = usec * 1000;
255 ts.tv_sec = sec;
256
257 retval = pthread_cond_timedwait(cond, mutex, &ts);
258
259 if (retval == ETIMEDOUT) {
260 return SYS_ARCH_TIMEOUT;
261 } else {
262 /* Calculate for how long we waited for the cond. */
263 gettimeofday(&rtime2, &tz);
264 tdiff = (rtime2.tv_sec - rtime1.tv_sec) * 1000 +
265 (rtime2.tv_usec - rtime1.tv_usec) / 1000;
266
267 if (tdiff <= 0) {
268 return 0;
269 }
270
271 return tdiff;
272 }
273 } else {
274 pthread_cond_wait(cond, mutex);
275 return SYS_ARCH_TIMEOUT;
276 }
277 }
278 /*-----------------------------------------------------------------------------------*/
279 u32_t
280 sys_arch_sem_wait(struct sys_sem *sem, u32_t timeout)
281 {
282 u32_t time = 0;
283
284 pthread_mutex_lock(&(sem->mutex));
285 while (sem->c <= 0) {
286 if (timeout > 0) {
287 time = cond_wait(&(sem->cond), &(sem->mutex), timeout);
288
289 if (time == SYS_ARCH_TIMEOUT) {
290 pthread_mutex_unlock(&(sem->mutex));
291 return SYS_ARCH_TIMEOUT;
292 }
293 /* pthread_mutex_unlock(&(sem->mutex));
294 return time; */
295 } else {
296 cond_wait(&(sem->cond), &(sem->mutex), 0);
297 }
298 }
299 sem->c--;
300 pthread_mutex_unlock(&(sem->mutex));
301 return time;
302 }
303 /*-----------------------------------------------------------------------------------*/
304 void
305 sys_sem_signal(struct sys_sem *sem)
306 {
307 pthread_mutex_lock(&(sem->mutex));
308 sem->c++;
309
310 if (sem->c > 1) {
311 sem->c = 1;
312 }
313
314 pthread_cond_broadcast(&(sem->cond));
315 pthread_mutex_unlock(&(sem->mutex));
316 }
317 /*-----------------------------------------------------------------------------------*/
318 void
319 sys_sem_free(struct sys_sem *sem)
320 {
321 if (sem != SYS_SEM_NULL) {
322 #if SYS_STATS
323 lwip_stats.sys.sem.used--;
324 #endif /* SYS_STATS */
325 sys_sem_free_(sem);
326 }
327 }
328
329 /*-----------------------------------------------------------------------------------*/
330 static void
331 sys_sem_free_(struct sys_sem *sem)
332 {
333 pthread_cond_destroy(&(sem->cond));
334 pthread_mutex_destroy(&(sem->mutex));
335 mem_free(sem);
336 }
337 /*-----------------------------------------------------------------------------------*/
338 unsigned long
339 time_now()
340 {
341 struct timeval tv;
342 struct timezone tz;
343 gettimeofday(&tv, &tz);
344
345 return tv.tv_sec;
346 }
347 /*-----------------------------------------------------------------------------------*/
348 void
349 sys_init()
350 {
351 }
352 /*-----------------------------------------------------------------------------------*/
353 static pthread_key_t key;
354 static pthread_once_t key_once = PTHREAD_ONCE_INIT;
355
356 static void del_key(void *ptr)
357 {
358 mem_free(ptr);
359 }
360
361 static void
362 make_key()
363 {
364 //fprintf(stderr,"new_key %p\n",pthread_self());
365 (void) pthread_key_create(&key, del_key);
366 }
367
368 struct sys_timeouts *
369 sys_arch_timeouts(void)
370 {
371 struct sys_timeouts *ptr;
372
373 (void) pthread_once(&key_once, make_key);
374 if ((ptr = pthread_getspecific(key)) == NULL) {
375 ptr = mem_malloc(sizeof(struct sys_timeouts));
376 ptr->next=NULL;
377 (void) pthread_setspecific(key, ptr);
378 }
379 //fprintf(stderr,"key %p %p %p\n",pthread_self(), ptr, ptr->next);
380
381 return ptr;
382 }
383 /*-----------------------------------------------------------------------------------*/
384 /** sys_prot_t sys_arch_protect(void)
385
386 This optional function does a "fast" critical region protection and returns
387 the previous protection level. This function is only called during very short
388 critical regions. An embedded system which supports ISR-based drivers might
389 want to implement this function by disabling interrupts. Task-based systems
390 might want to implement this by using a mutex or disabling tasking. This
391 function should support recursive calls from the same task or interrupt. In
392 other words, sys_arch_protect() could be called while already protected. In
393 that case the return value indicates that it is already protected.
394
395 sys_arch_protect() is only required if your port is supporting an operating
396 system.
397 */
398 sys_prot_t
399 sys_arch_protect(void)
400 {
401 /* Note that for the UNIX port, we are using a lightweight mutex, and our
402 * own counter (which is locked by the mutex). The return code is not actually
403 * used. */
404 if (lwprot_thread != pthread_self())
405 {
406 /* We are locking the mutex where it has not been locked before *
407 * or is being locked by another thread */
408 pthread_mutex_lock(&lwprot_mutex);
409 lwprot_thread = pthread_self();
410 lwprot_count = 1;
411 }
412 else
413 /* It is already locked by THIS thread */
414 lwprot_count++;
415 return 0;
416 }
417 /*-----------------------------------------------------------------------------------*/
418 /** void sys_arch_unprotect(sys_prot_t pval)
419
420 This optional function does a "fast" set of critical region protection to the
421 value specified by pval. See the documentation for sys_arch_protect() for
422 more information. This function is only required if your port is supporting
423 an operating system.
424 */
425 void
426 sys_arch_unprotect(sys_prot_t pval)
427 {
428 if (lwprot_thread == pthread_self())
429 {
430 if (--lwprot_count == 0)
431 {
432 lwprot_thread = (pthread_t) 0xDEAD;
433 pthread_mutex_unlock(&lwprot_mutex);
434 }
435 }
436 }
437
438 /*-----------------------------------------------------------------------------------*/
439
440 #ifndef MAX_JIFFY_OFFSET
441 #define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
442 #endif
443
444 #ifndef HZ
445 #define HZ 100
446 #endif
447
448 unsigned long
449 sys_jiffies(void)
450 {
451 struct timeval tv;
452 unsigned long sec = tv.tv_sec;
453 long usec = tv.tv_usec;
454
455 gettimeofday(&tv,NULL);
456
457 if (sec >= (MAX_JIFFY_OFFSET / HZ))
458 return MAX_JIFFY_OFFSET;
459 usec += 1000000L / HZ - 1;
460 usec /= 1000000L / HZ;
461 return HZ * sec + usec;
462 }
463
464 #if PPP_DEBUG
465
466 #include <stdarg.h>
467
468 void ppp_trace(int level, const char *format, ...)
469 {
470 va_list args;
471
472 (void)level;
473 va_start(args, format);
474 vprintf(format, args);
475 va_end(args);
476 }
477 #endif