1195972f6Sopenharmony_ci/**
2195972f6Sopenharmony_ci * @file
3195972f6Sopenharmony_ci * lwIP Operating System abstraction
4195972f6Sopenharmony_ci *
5195972f6Sopenharmony_ci */
6195972f6Sopenharmony_ci
7195972f6Sopenharmony_ci/*
8195972f6Sopenharmony_ci * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9195972f6Sopenharmony_ci * All rights reserved.
10195972f6Sopenharmony_ci *
11195972f6Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
12195972f6Sopenharmony_ci * are permitted provided that the following conditions are met:
13195972f6Sopenharmony_ci *
14195972f6Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice,
15195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer.
16195972f6Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice,
17195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer in the documentation
18195972f6Sopenharmony_ci *    and/or other materials provided with the distribution.
19195972f6Sopenharmony_ci * 3. The name of the author may not be used to endorse or promote products
20195972f6Sopenharmony_ci *    derived from this software without specific prior written permission.
21195972f6Sopenharmony_ci *
22195972f6Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23195972f6Sopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24195972f6Sopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25195972f6Sopenharmony_ci * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26195972f6Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27195972f6Sopenharmony_ci * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28195972f6Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29195972f6Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30195972f6Sopenharmony_ci * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31195972f6Sopenharmony_ci * OF SUCH DAMAGE.
32195972f6Sopenharmony_ci *
33195972f6Sopenharmony_ci * This file is part of the lwIP TCP/IP stack.
34195972f6Sopenharmony_ci *
35195972f6Sopenharmony_ci * Author: Adam Dunkels <adam@sics.se>
36195972f6Sopenharmony_ci *
37195972f6Sopenharmony_ci */
38195972f6Sopenharmony_ci
39195972f6Sopenharmony_ci/**
40195972f6Sopenharmony_ci * @defgroup sys_layer Porting (system abstraction layer)
41195972f6Sopenharmony_ci * @ingroup lwip
42195972f6Sopenharmony_ci *
43195972f6Sopenharmony_ci * @defgroup sys_os OS abstraction layer
44195972f6Sopenharmony_ci * @ingroup sys_layer
45195972f6Sopenharmony_ci * No need to implement functions in this section in NO_SYS mode.
46195972f6Sopenharmony_ci * The OS-specific code should be implemented in arch/sys_arch.h
47195972f6Sopenharmony_ci * and sys_arch.c of your port.
48195972f6Sopenharmony_ci *
49195972f6Sopenharmony_ci * The operating system emulation layer provides a common interface
50195972f6Sopenharmony_ci * between the lwIP code and the underlying operating system kernel. The
51195972f6Sopenharmony_ci * general idea is that porting lwIP to new architectures requires only
52195972f6Sopenharmony_ci * small changes to a few header files and a new sys_arch
53195972f6Sopenharmony_ci * implementation. It is also possible to do a sys_arch implementation
54195972f6Sopenharmony_ci * that does not rely on any underlying operating system.
55195972f6Sopenharmony_ci *
56195972f6Sopenharmony_ci * The sys_arch provides semaphores, mailboxes and mutexes to lwIP. For the full
57195972f6Sopenharmony_ci * lwIP functionality, multiple threads support can be implemented in the
58195972f6Sopenharmony_ci * sys_arch, but this is not required for the basic lwIP
59195972f6Sopenharmony_ci * functionality. Timer scheduling is implemented in lwIP, but can be implemented
60195972f6Sopenharmony_ci * by the sys_arch port (LWIP_TIMERS_CUSTOM==1).
61195972f6Sopenharmony_ci *
62195972f6Sopenharmony_ci * In addition to the source file providing the functionality of sys_arch,
63195972f6Sopenharmony_ci * the OS emulation layer must provide several header files defining
64195972f6Sopenharmony_ci * macros used throughout lwip.  The files required and the macros they
65195972f6Sopenharmony_ci * must define are listed below the sys_arch description.
66195972f6Sopenharmony_ci *
67195972f6Sopenharmony_ci * Since lwIP 1.4.0, semaphore, mutexes and mailbox functions are prototyped in a way that
68195972f6Sopenharmony_ci * allows both using pointers or actual OS structures to be used. This way, memory
69195972f6Sopenharmony_ci * required for such types can be either allocated in place (globally or on the
70195972f6Sopenharmony_ci * stack) or on the heap (allocated internally in the "*_new()" functions).
71195972f6Sopenharmony_ci *
72195972f6Sopenharmony_ci * Note:
73195972f6Sopenharmony_ci * -----
74195972f6Sopenharmony_ci * Be careful with using mem_malloc() in sys_arch. When malloc() refers to
75195972f6Sopenharmony_ci * mem_malloc() you can run into a circular function call problem. In mem.c
76195972f6Sopenharmony_ci * mem_init() tries to allocate a semaphore using mem_malloc, which of course
77195972f6Sopenharmony_ci * can't be performed when sys_arch uses mem_malloc.
78195972f6Sopenharmony_ci *
79195972f6Sopenharmony_ci * @defgroup sys_sem Semaphores
80195972f6Sopenharmony_ci * @ingroup sys_os
81195972f6Sopenharmony_ci * Semaphores can be either counting or binary - lwIP works with both
82195972f6Sopenharmony_ci * kinds.
83195972f6Sopenharmony_ci * Semaphores are represented by the type "sys_sem_t" which is typedef'd
84195972f6Sopenharmony_ci * in the sys_arch.h file. Mailboxes are equivalently represented by the
85195972f6Sopenharmony_ci * type "sys_mbox_t". Mutexes are represented by the type "sys_mutex_t".
86195972f6Sopenharmony_ci * lwIP does not place any restrictions on how these types are represented
87195972f6Sopenharmony_ci * internally.
88195972f6Sopenharmony_ci *
89195972f6Sopenharmony_ci * @defgroup sys_mutex Mutexes
90195972f6Sopenharmony_ci * @ingroup sys_os
91195972f6Sopenharmony_ci * Mutexes are recommended to correctly handle priority inversion,
92195972f6Sopenharmony_ci * especially if you use LWIP_CORE_LOCKING .
93195972f6Sopenharmony_ci *
94195972f6Sopenharmony_ci * @defgroup sys_mbox Mailboxes
95195972f6Sopenharmony_ci * @ingroup sys_os
96195972f6Sopenharmony_ci * Mailboxes should be implemented as a queue which allows multiple messages
97195972f6Sopenharmony_ci * to be posted (implementing as a rendez-vous point where only one message can be
98195972f6Sopenharmony_ci * posted at a time can have a highly negative impact on performance). A message
99195972f6Sopenharmony_ci * in a mailbox is just a pointer, nothing more.
100195972f6Sopenharmony_ci *
101195972f6Sopenharmony_ci * @defgroup sys_time Time
102195972f6Sopenharmony_ci * @ingroup sys_layer
103195972f6Sopenharmony_ci *
104195972f6Sopenharmony_ci * @defgroup sys_prot Critical sections
105195972f6Sopenharmony_ci * @ingroup sys_layer
106195972f6Sopenharmony_ci * Used to protect short regions of code against concurrent access.
107195972f6Sopenharmony_ci * - Your system is a bare-metal system (probably with an RTOS)
108195972f6Sopenharmony_ci *   and interrupts are under your control:
109195972f6Sopenharmony_ci *   Implement this as LockInterrupts() / UnlockInterrupts()
110195972f6Sopenharmony_ci * - Your system uses an RTOS with deferred interrupt handling from a
111195972f6Sopenharmony_ci *   worker thread: Implement as a global mutex or lock/unlock scheduler
112195972f6Sopenharmony_ci * - Your system uses a high-level OS with e.g. POSIX signals:
113195972f6Sopenharmony_ci *   Implement as a global mutex
114195972f6Sopenharmony_ci *
115195972f6Sopenharmony_ci * @defgroup sys_misc Misc
116195972f6Sopenharmony_ci * @ingroup sys_os
117195972f6Sopenharmony_ci */
118195972f6Sopenharmony_ci
119195972f6Sopenharmony_ci#include "lwip/opt.h"
120195972f6Sopenharmony_ci
121195972f6Sopenharmony_ci#include "lwip/sys.h"
122195972f6Sopenharmony_ci
123195972f6Sopenharmony_ci/* Most of the functions defined in sys.h must be implemented in the
124195972f6Sopenharmony_ci * architecture-dependent file sys_arch.c */
125195972f6Sopenharmony_ci
126195972f6Sopenharmony_ci#if !NO_SYS
127195972f6Sopenharmony_ci
128195972f6Sopenharmony_ci#ifndef sys_msleep
129195972f6Sopenharmony_ci/**
130195972f6Sopenharmony_ci * Sleep for some ms. Timeouts are NOT processed while sleeping.
131195972f6Sopenharmony_ci *
132195972f6Sopenharmony_ci * @param ms number of milliseconds to sleep
133195972f6Sopenharmony_ci */
134195972f6Sopenharmony_civoid
135195972f6Sopenharmony_cisys_msleep(u32_t ms)
136195972f6Sopenharmony_ci{
137195972f6Sopenharmony_ci  if (ms > 0) {
138195972f6Sopenharmony_ci    sys_sem_t delaysem;
139195972f6Sopenharmony_ci    err_t err = sys_sem_new(&delaysem, 0);
140195972f6Sopenharmony_ci    if (err == ERR_OK) {
141195972f6Sopenharmony_ci      sys_arch_sem_wait(&delaysem, ms);
142195972f6Sopenharmony_ci      sys_sem_free(&delaysem);
143195972f6Sopenharmony_ci    }
144195972f6Sopenharmony_ci  }
145195972f6Sopenharmony_ci}
146195972f6Sopenharmony_ci#endif /* sys_msleep */
147195972f6Sopenharmony_ci
148195972f6Sopenharmony_ci#endif /* !NO_SYS */
149