| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 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 |
| 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 |
| 145 |
close (mbox->pipe[0]); |
| 146 |
close (mbox->pipe[1]); |
| 147 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
| 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 |
|
| 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 |
|
| 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 |
|
| 294 |
|
| 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 |
| 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 |
|
| 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 |
|
| 380 |
|
| 381 |
return ptr; |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
sys_prot_t |
| 399 |
sys_arch_protect(void) |
| 400 |
{ |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
if (lwprot_thread != pthread_self()) |
| 405 |
{ |
| 406 |
|
| 407 |
|
| 408 |
pthread_mutex_lock(&lwprot_mutex); |
| 409 |
lwprot_thread = pthread_self(); |
| 410 |
lwprot_count = 1; |
| 411 |
} |
| 412 |
else |
| 413 |
|
| 414 |
lwprot_count++; |
| 415 |
return 0; |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 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 |