sys.h 15.4 KB
Newer Older
1 2 3 4 5
/**
 * @file
 * OS abstraction layer
 */

H
hduffddybz 已提交
6 7
/*
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8 9 10
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
H
hduffddybz 已提交
11 12 13 14 15 16 17 18
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
19
 *    derived from this software without specific prior written permission.
H
hduffddybz 已提交
20
 *
21 22 23 24 25 26 27 28 29
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
H
hduffddybz 已提交
30 31 32
 * OF SUCH DAMAGE.
 *
 * This file is part of the lwIP TCP/IP stack.
33
 *
H
hduffddybz 已提交
34
 * Author: Adam Dunkels <adam@sics.se>
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 63 64 65 66 67 68 69
 */

/**
 * @defgroup sys_layer System abstraction layer
 * @ingroup infrastructure
 * @verbinclude "sys_arch.txt"
 *
 * @defgroup sys_os OS abstraction layer
 * @ingroup sys_layer
 * No need to implement functions in this section in NO_SYS mode.
 *
 * @defgroup sys_sem Semaphores
 * @ingroup sys_os
 *
 * @defgroup sys_mutex Mutexes
 * @ingroup sys_os
 * Mutexes are recommended to correctly handle priority inversion,
 * especially if you use LWIP_CORE_LOCKING .
 *
 * @defgroup sys_mbox Mailboxes
 * @ingroup sys_os
 *
 * @defgroup sys_time Time
 * @ingroup sys_layer
 *
 * @defgroup sys_prot Critical sections
 * @ingroup sys_layer
 * Used to protect short regions of code against concurrent access.
 * - Your system is a bare-metal system (probably with an RTOS)
 *   and interrupts are under your control:
 *   Implement this as LockInterrupts() / UnlockInterrupts()
 * - Your system uses an RTOS with deferred interrupt handling from a
 *   worker thread: Implement as a global mutex or lock/unlock scheduler
 * - Your system uses a high-level OS with e.g. POSIX signals:
 *   Implement as a global mutex
H
hduffddybz 已提交
70
 *
71 72
 * @defgroup sys_misc Misc
 * @ingroup sys_os
H
hduffddybz 已提交
73
 */
74

H
hduffddybz 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#ifndef LWIP_HDR_SYS_H
#define LWIP_HDR_SYS_H

#include "lwip/opt.h"

#ifdef __cplusplus
extern "C" {
#endif

#if NO_SYS

/* For a totally minimal and standalone system, we provide null
   definitions of the sys_ functions. */
typedef u8_t sys_sem_t;
typedef u8_t sys_mutex_t;
typedef u8_t sys_mbox_t;

#define sys_sem_new(s, c) ERR_OK
#define sys_sem_signal(s)
#define sys_sem_wait(s)
#define sys_arch_sem_wait(s,t)
#define sys_sem_free(s)
#define sys_sem_valid(s) 0
98
#define sys_sem_valid_val(s) 0
H
hduffddybz 已提交
99
#define sys_sem_set_invalid(s)
100
#define sys_sem_set_invalid_val(s)
H
hduffddybz 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113
#define sys_mutex_new(mu) ERR_OK
#define sys_mutex_lock(mu)
#define sys_mutex_unlock(mu)
#define sys_mutex_free(mu)
#define sys_mutex_valid(mu) 0
#define sys_mutex_set_invalid(mu)
#define sys_mbox_new(m, s) ERR_OK
#define sys_mbox_fetch(m,d)
#define sys_mbox_tryfetch(m,d)
#define sys_mbox_post(m,d)
#define sys_mbox_trypost(m,d)
#define sys_mbox_free(m)
#define sys_mbox_valid(m)
114
#define sys_mbox_valid_val(m)
H
hduffddybz 已提交
115
#define sys_mbox_set_invalid(m)
116
#define sys_mbox_set_invalid_val(m)
H
hduffddybz 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129

#define sys_thread_new(n,t,a,s,p)

#define sys_msleep(t)

#else /* NO_SYS */

/** Return code for timeouts from sys_arch_mbox_fetch and sys_arch_sem_wait */
#define SYS_ARCH_TIMEOUT 0xffffffffUL

/** sys_mbox_tryfetch() returns SYS_MBOX_EMPTY if appropriate.
 * For now we use the same magic value, but we allow this to change in future.
 */
130
#define SYS_MBOX_EMPTY SYS_ARCH_TIMEOUT
H
hduffddybz 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144

#include "lwip/err.h"
#include "arch/sys_arch.h"

/** Function prototype for thread functions */
typedef void (*lwip_thread_fn)(void *arg);

/* Function prototypes for functions to be implemented by platform ports
   (in sys_arch.c) */

/* Mutex functions: */

/** Define LWIP_COMPAT_MUTEX if the port has no mutexes and binary semaphores
    should be used instead */
145 146 147 148
#ifndef LWIP_COMPAT_MUTEX
#define LWIP_COMPAT_MUTEX 0
#endif

H
hduffddybz 已提交
149 150 151 152 153 154 155 156 157 158 159 160
#if LWIP_COMPAT_MUTEX
/* for old ports that don't have mutexes: define them to binary semaphores */
#define sys_mutex_t                   sys_sem_t
#define sys_mutex_new(mutex)          sys_sem_new(mutex, 1)
#define sys_mutex_lock(mutex)         sys_sem_wait(mutex)
#define sys_mutex_unlock(mutex)       sys_sem_signal(mutex)
#define sys_mutex_free(mutex)         sys_sem_free(mutex)
#define sys_mutex_valid(mutex)        sys_sem_valid(mutex)
#define sys_mutex_set_invalid(mutex)  sys_sem_set_invalid(mutex)

#else /* LWIP_COMPAT_MUTEX */

161 162 163 164 165
/**
 * @ingroup sys_mutex
 * Create a new mutex.
 * Note that mutexes are expected to not be taken recursively by the lwIP code,
 * so both implementation types (recursive or non-recursive) should work.
H
hduffddybz 已提交
166
 * @param mutex pointer to the mutex to create
167 168
 * @return ERR_OK if successful, another err_t otherwise
 */
H
hduffddybz 已提交
169
err_t sys_mutex_new(sys_mutex_t *mutex);
170 171 172 173 174
/**
 * @ingroup sys_mutex
 * Lock a mutex
 * @param mutex the mutex to lock
 */
H
hduffddybz 已提交
175
void sys_mutex_lock(sys_mutex_t *mutex);
176 177 178 179 180
/**
 * @ingroup sys_mutex
 * Unlock a mutex
 * @param mutex the mutex to unlock
 */
H
hduffddybz 已提交
181
void sys_mutex_unlock(sys_mutex_t *mutex);
182 183 184 185 186 187
/**
 * @ingroup sys_mutex
 * Delete a semaphore
 * @param mutex the mutex to delete
 */
void sys_mutex_free(sys_mutex_t *mutex);
H
hduffddybz 已提交
188
#ifndef sys_mutex_valid
189 190 191 192
/**
 * @ingroup sys_mutex
 * Check if a mutex is valid/allocated: return 1 for valid, 0 for invalid
 */
H
hduffddybz 已提交
193 194 195
int sys_mutex_valid(sys_mutex_t *mutex);
#endif
#ifndef sys_mutex_set_invalid
196 197 198 199
/**
 * @ingroup sys_mutex
 * Set a mutex invalid so that sys_mutex_valid returns 0
 */
H
hduffddybz 已提交
200 201 202 203 204 205
void sys_mutex_set_invalid(sys_mutex_t *mutex);
#endif
#endif /* LWIP_COMPAT_MUTEX */

/* Semaphore functions: */

206 207 208
/**
 * @ingroup sys_sem
 * Create a new semaphore
H
hduffddybz 已提交
209 210
 * @param sem pointer to the semaphore to create
 * @param count initial count of the semaphore
211 212
 * @return ERR_OK if successful, another err_t otherwise
 */
H
hduffddybz 已提交
213
err_t sys_sem_new(sys_sem_t *sem, u8_t count);
214 215 216 217 218
/**
 * @ingroup sys_sem
 * Signals a semaphore
 * @param sem the semaphore to signal
 */
H
hduffddybz 已提交
219
void sys_sem_signal(sys_sem_t *sem);
220 221 222
/**
 * @ingroup sys_sem
 * Wait for a semaphore for the specified timeout
H
hduffddybz 已提交
223 224 225
 * @param sem the semaphore to wait for
 * @param timeout timeout in milliseconds to wait (0 = wait forever)
 * @return time (in milliseconds) waited for the semaphore
226 227
 *         or SYS_ARCH_TIMEOUT on timeout
 */
H
hduffddybz 已提交
228
u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout);
229 230 231 232 233
/**
 * @ingroup sys_sem
 * Delete a semaphore
 * @param sem semaphore to delete
 */
H
hduffddybz 已提交
234 235 236 237
void sys_sem_free(sys_sem_t *sem);
/** Wait for a semaphore - forever/no timeout */
#define sys_sem_wait(sem)                  sys_arch_sem_wait(sem, 0)
#ifndef sys_sem_valid
238 239 240 241
/**
 * @ingroup sys_sem
 * Check if a semaphore is valid/allocated: return 1 for valid, 0 for invalid
 */
H
hduffddybz 已提交
242 243 244
int sys_sem_valid(sys_sem_t *sem);
#endif
#ifndef sys_sem_set_invalid
245 246 247 248
/**
 * @ingroup sys_sem
 * Set a semaphore invalid so that sys_sem_valid returns 0
 */
H
hduffddybz 已提交
249 250
void sys_sem_set_invalid(sys_sem_t *sem);
#endif
251 252 253 254 255 256 257 258 259 260 261 262
#ifndef sys_sem_valid_val
/**
 * Same as sys_sem_valid() but taking a value, not a pointer
 */
#define sys_sem_valid_val(sem)       sys_sem_valid(&(sem))
#endif
#ifndef sys_sem_set_invalid_val
/**
 * Same as sys_sem_set_invalid() but taking a value, not a pointer
 */
#define sys_sem_set_invalid_val(sem) sys_sem_set_invalid(&(sem))
#endif
H
hduffddybz 已提交
263 264

#ifndef sys_msleep
265 266 267 268 269
/**
 * @ingroup sys_misc
 * Sleep for specified number of ms
 */
void sys_msleep(u32_t ms); /* only has a (close to) 1 ms resolution. */
H
hduffddybz 已提交
270 271 272 273
#endif

/* Mailbox functions. */

274 275 276
/**
 * @ingroup sys_mbox
 * Create a new mbox of specified size
H
hduffddybz 已提交
277
 * @param mbox pointer to the mbox to create
278 279 280
 * @param size (minimum) number of messages in this mbox
 * @return ERR_OK if successful, another err_t otherwise
 */
H
hduffddybz 已提交
281
err_t sys_mbox_new(sys_mbox_t *mbox, int size);
282 283 284
/**
 * @ingroup sys_mbox
 * Post a message to an mbox - may not fail
H
hduffddybz 已提交
285 286
 * -> blocks if full, only used from tasks not from ISR
 * @param mbox mbox to posts the message
287 288
 * @param msg message to post (ATTENTION: can be NULL)
 */
H
hduffddybz 已提交
289
void sys_mbox_post(sys_mbox_t *mbox, void *msg);
290 291 292
/**
 * @ingroup sys_mbox
 * Try to post a message to an mbox - may fail if full or ISR
H
hduffddybz 已提交
293
 * @param mbox mbox to posts the message
294 295
 * @param msg message to post (ATTENTION: can be NULL)
 */
H
hduffddybz 已提交
296
err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg);
297 298 299
/**
 * @ingroup sys_mbox
 * Wait for a new message to arrive in the mbox
H
hduffddybz 已提交
300 301 302 303 304
 * @param mbox mbox to get a message from
 * @param msg pointer where the message is stored
 * @param timeout maximum time (in milliseconds) to wait for a message (0 = wait forever)
 * @return time (in milliseconds) waited for a message, may be 0 if not waited
           or SYS_ARCH_TIMEOUT on timeout
305 306
 *         The returned time has to be accurate to prevent timer jitter!
 */
H
hduffddybz 已提交
307
u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout);
308
/* Allow port to override with a macro, e.g. special timeout for sys_arch_mbox_fetch() */
H
hduffddybz 已提交
309
#ifndef sys_arch_mbox_tryfetch
310 311 312
/**
 * @ingroup sys_mbox
 * Wait for a new message to arrive in the mbox
H
hduffddybz 已提交
313 314 315
 * @param mbox mbox to get a message from
 * @param msg pointer where the message is stored
 * @return 0 (milliseconds) if a message has been received
316 317
 *         or SYS_MBOX_EMPTY if the mailbox is empty
 */
H
hduffddybz 已提交
318 319
u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg);
#endif
320 321 322
/**
 * For now, we map straight to sys_arch implementation.
 */
H
hduffddybz 已提交
323
#define sys_mbox_tryfetch(mbox, msg) sys_arch_mbox_tryfetch(mbox, msg)
324 325 326 327 328
/**
 * @ingroup sys_mbox
 * Delete an mbox
 * @param mbox mbox to delete
 */
H
hduffddybz 已提交
329 330 331
void sys_mbox_free(sys_mbox_t *mbox);
#define sys_mbox_fetch(mbox, msg) sys_arch_mbox_fetch(mbox, msg, 0)
#ifndef sys_mbox_valid
332 333 334 335
/**
 * @ingroup sys_mbox
 * Check if an mbox is valid/allocated: return 1 for valid, 0 for invalid
 */
H
hduffddybz 已提交
336 337 338
int sys_mbox_valid(sys_mbox_t *mbox);
#endif
#ifndef sys_mbox_set_invalid
339 340 341 342
/**
 * @ingroup sys_mbox
 * Set an mbox invalid so that sys_mbox_valid returns 0
 */
H
hduffddybz 已提交
343 344
void sys_mbox_set_invalid(sys_mbox_t *mbox);
#endif
345 346 347 348 349 350 351 352 353 354 355 356
#ifndef sys_mbox_valid_val
/**
 * Same as sys_mbox_valid() but taking a value, not a pointer
 */
#define sys_mbox_valid_val(mbox)       sys_mbox_valid(&(mbox))
#endif
#ifndef sys_mbox_set_invalid_val
/**
 * Same as sys_mbox_set_invalid() but taking a value, not a pointer
 */
#define sys_mbox_set_invalid_val(mbox) sys_mbox_set_invalid(&(mbox))
#endif
H
hduffddybz 已提交
357

358 359 360 361

/**
 * @ingroup sys_misc
 * The only thread function:
H
hduffddybz 已提交
362
 * Creates a new thread
363
 * ATTENTION: although this function returns a value, it MUST NOT FAIL (ports have to assert this!)
H
hduffddybz 已提交
364 365 366 367 368 369 370 371 372
 * @param name human-readable name for the thread (used for debugging purposes)
 * @param thread thread-function
 * @param arg parameter passed to 'thread'
 * @param stacksize stack size in bytes for the new thread (may be ignored by ports)
 * @param prio priority of the new thread (may be ignored by ports) */
sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio);

#endif /* NO_SYS */

373
/* sys_init() must be called before anything else. */
H
hduffddybz 已提交
374 375 376
void sys_init(void);

#ifndef sys_jiffies
377 378 379
/**
 * Ticks/jiffies since power up.
 */
H
hduffddybz 已提交
380 381 382
u32_t sys_jiffies(void);
#endif

383 384 385 386 387
/**
 * @ingroup sys_time
 * Returns the current time in milliseconds,
 * may be the same as sys_jiffies or at least based on it.
 */
H
hduffddybz 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
u32_t sys_now(void);

/* Critical Region Protection */
/* These functions must be implemented in the sys_arch.c file.
   In some implementations they can provide a more light-weight protection
   mechanism than using semaphores. Otherwise semaphores can be used for
   implementation */
#ifndef SYS_ARCH_PROTECT
/** SYS_LIGHTWEIGHT_PROT
 * define SYS_LIGHTWEIGHT_PROT in lwipopts.h if you want inter-task protection
 * for certain critical regions during buffer allocation, deallocation and memory
 * allocation and deallocation.
 */
#if SYS_LIGHTWEIGHT_PROT

403 404 405
/**
 * @ingroup sys_prot
 * SYS_ARCH_DECL_PROTECT
H
hduffddybz 已提交
406 407 408 409 410
 * declare a protection variable. This macro will default to defining a variable of
 * type sys_prot_t. If a particular port needs a different implementation, then
 * this macro may be defined in sys_arch.h.
 */
#define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev
411 412 413
/**
 * @ingroup sys_prot
 * SYS_ARCH_PROTECT
H
hduffddybz 已提交
414 415 416 417 418 419 420 421 422
 * Perform a "fast" protect. This could be implemented by
 * disabling interrupts for an embedded system or by using a semaphore or
 * mutex. The implementation should allow calling SYS_ARCH_PROTECT when
 * already protected. The old protection level is returned in the variable
 * "lev". This macro will default to calling the sys_arch_protect() function
 * which should be implemented in sys_arch.c. If a particular port needs a
 * different implementation, then this macro may be defined in sys_arch.h
 */
#define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect()
423 424 425
/**
 * @ingroup sys_prot
 * SYS_ARCH_UNPROTECT
H
hduffddybz 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
 * Perform a "fast" set of the protection level to "lev". This could be
 * implemented by setting the interrupt level to "lev" within the MACRO or by
 * using a semaphore or mutex.  This macro will default to calling the
 * sys_arch_unprotect() function which should be implemented in
 * sys_arch.c. If a particular port needs a different implementation, then
 * this macro may be defined in sys_arch.h
 */
#define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect(lev)
sys_prot_t sys_arch_protect(void);
void sys_arch_unprotect(sys_prot_t pval);

#else

#define SYS_ARCH_DECL_PROTECT(lev)
#define SYS_ARCH_PROTECT(lev)
#define SYS_ARCH_UNPROTECT(lev)

#endif /* SYS_LIGHTWEIGHT_PROT */

#endif /* SYS_ARCH_PROTECT */

/*
 * Macros to set/get and increase/decrease variables in a thread-safe way.
 * Use these for accessing variable that are used from more than one thread.
 */

#ifndef SYS_ARCH_INC
#define SYS_ARCH_INC(var, val) do { \
                                SYS_ARCH_DECL_PROTECT(old_level); \
                                SYS_ARCH_PROTECT(old_level); \
                                var += val; \
                                SYS_ARCH_UNPROTECT(old_level); \
                              } while(0)
#endif /* SYS_ARCH_INC */

#ifndef SYS_ARCH_DEC
#define SYS_ARCH_DEC(var, val) do { \
                                SYS_ARCH_DECL_PROTECT(old_level); \
                                SYS_ARCH_PROTECT(old_level); \
                                var -= val; \
                                SYS_ARCH_UNPROTECT(old_level); \
                              } while(0)
#endif /* SYS_ARCH_DEC */

#ifndef SYS_ARCH_GET
#define SYS_ARCH_GET(var, ret) do { \
                                SYS_ARCH_DECL_PROTECT(old_level); \
                                SYS_ARCH_PROTECT(old_level); \
                                ret = var; \
                                SYS_ARCH_UNPROTECT(old_level); \
                              } while(0)
#endif /* SYS_ARCH_GET */

#ifndef SYS_ARCH_SET
#define SYS_ARCH_SET(var, val) do { \
                                SYS_ARCH_DECL_PROTECT(old_level); \
                                SYS_ARCH_PROTECT(old_level); \
                                var = val; \
                                SYS_ARCH_UNPROTECT(old_level); \
                              } while(0)
#endif /* SYS_ARCH_SET */


#ifdef __cplusplus
}
#endif

#endif /* LWIP_HDR_SYS_H */