Merge branch 'fix/lwip_docs_thread_safe_socks' into 'master'

fix(lwip): Document that not all sock operations are thread safe

See merge request espressif/esp-idf!34333
This commit is contained in:
David Čermák
2024-11-07 15:41:12 +08:00
2 changed files with 10 additions and 2 deletions

View File

@ -43,7 +43,7 @@ BSD Sockets API
The BSD Sockets API is a common cross-platform TCP/IP sockets API that originated in the Berkeley Standard Distribution of UNIX but is now standardized in a section of the POSIX specification. BSD Sockets are sometimes called POSIX Sockets or Berkeley Sockets. The BSD Sockets API is a common cross-platform TCP/IP sockets API that originated in the Berkeley Standard Distribution of UNIX but is now standardized in a section of the POSIX specification. BSD Sockets are sometimes called POSIX Sockets or Berkeley Sockets.
As implemented in ESP-IDF, lwIP supports all of the common usages of the BSD Sockets API. As implemented in ESP-IDF, lwIP supports all of the common usages of the BSD Sockets API. However, not all operations are fully thread-safe, and simultaneous reads and writes from multiple threads may require additional synchronization mechanisms, see :ref:`lwip-limitations` for more details.
References References
^^^^^^^^^^ ^^^^^^^^^^
@ -461,9 +461,13 @@ This approach may not work for function-like macros, as there is no guarantee th
Alternatively, you can define your function-like macro in a header file which will be pre-included as an lwIP hook file, see :ref:`lwip-custom-hooks`. Alternatively, you can define your function-like macro in a header file which will be pre-included as an lwIP hook file, see :ref:`lwip-custom-hooks`.
.. _lwip-limitations:
Limitations Limitations
^^^^^^^^^^^ ^^^^^^^^^^^
lwIP in ESP-IDF supports thread safety in certain scenarios, but with limitations. It is possible to perform read, write, and close operations from different threads on the same socket simultaneously. However, performing multiple reads or multiple writes from more than one thread on the same socket at the same time is not supported. Applications that require simultaneous reads or writes from multiple threads on the same socket must implement additional synchronization mechanisms, such as locking around socket operations.
ESP-IDF additions to lwIP still suffer from the global DNS limitation, described in :ref:`lwip-dns-limitation`. To address this limitation from application code, the ``FALLBACK_DNS_SERVER_ADDRESS()`` macro can be utilized to define a global DNS fallback server accessible from all interfaces. Alternatively, you have the option to maintain per-interface DNS servers and reconfigure them whenever the default interface changes. ESP-IDF additions to lwIP still suffer from the global DNS limitation, described in :ref:`lwip-dns-limitation`. To address this limitation from application code, the ``FALLBACK_DNS_SERVER_ADDRESS()`` macro can be utilized to define a global DNS fallback server accessible from all interfaces. Alternatively, you have the option to maintain per-interface DNS servers and reconfigure them whenever the default interface changes.
The number of IP addresses returned by network database APIs such as ``getaddrinfo()`` and ``gethostbyname()`` is restricted by the macro ``DNS_MAX_HOST_IP``. By default, the value of this macro is set to 1. The number of IP addresses returned by network database APIs such as ``getaddrinfo()`` and ``gethostbyname()`` is restricted by the macro ``DNS_MAX_HOST_IP``. By default, the value of this macro is set to 1.

View File

@ -43,7 +43,7 @@ BSD 套接字 API
BSD 套接字 API 是一种常见的跨平台 TCP/IP 套接字 API最初源于 UNIX 操作系统的伯克利标准发行版,现已标准化为 POSIX 规范的一部分。BSD 套接字有时也称 POSIX 套接字,或伯克利套接字。 BSD 套接字 API 是一种常见的跨平台 TCP/IP 套接字 API最初源于 UNIX 操作系统的伯克利标准发行版,现已标准化为 POSIX 规范的一部分。BSD 套接字有时也称 POSIX 套接字,或伯克利套接字。
在 ESP-IDF 中lwIP 支持 BSD 套接字 API 的所有常见用法。 在 ESP-IDF 中lwIP 支持 BSD 套接字 API 的所有常见用法。然而,并非所有操作都完全线程安全,因此多个线程同时进行读写可能需要额外的同步机制。详情请参见 :ref:`lwip-limitations`
参考 参考
^^^^^^^^^^ ^^^^^^^^^^
@ -461,9 +461,13 @@ NAPT 和端口转发
另一种方法是在头文件中定义函数式宏,该头文件将预先包含在 lwIP 钩子文件中,请参考 :ref:`lwip-custom-hooks` 另一种方法是在头文件中定义函数式宏,该头文件将预先包含在 lwIP 钩子文件中,请参考 :ref:`lwip-custom-hooks`
.. _lwip-limitations:
限制 限制
^^^^^^^^^^^ ^^^^^^^^^^^
在 ESP-IDF 中lwIP 在某些场景下线程安全,但存在一定的限制。在 lwIP 中,可以在同一套接字上由多个线程同时分别执行读、写和关闭操作,但不支持在同一套接字上由多个线程同时执行多个读操作或多个写操作。如果应用程序需要在多个线程中同时对同一套接字进行读、写操作,就需要额外的同步机制来确保线程安全。例如,在套接字操作周围加锁。
如 :ref:`lwip-dns-limitation` 所述ESP-IDF 中的 lwIP 扩展功能仍然受到全局 DNS 限制的影响。为了在应用程序代码中解决这一限制,可以使用 ``FALLBACK_DNS_SERVER_ADDRESS()`` 宏定义所有接口能够访问的全局 DNS 备用服务器,或者单独维护每个接口的 DNS 服务器,并在默认接口更改时重新配置。 如 :ref:`lwip-dns-limitation` 所述ESP-IDF 中的 lwIP 扩展功能仍然受到全局 DNS 限制的影响。为了在应用程序代码中解决这一限制,可以使用 ``FALLBACK_DNS_SERVER_ADDRESS()`` 宏定义所有接口能够访问的全局 DNS 备用服务器,或者单独维护每个接口的 DNS 服务器,并在默认接口更改时重新配置。
通过网络数据库 API 返回的 IP 地址数量受限:``getaddrinfo()````gethostbyname()`` 受到宏 ``DNS_MAX_HOST_IP`` 的限制,宏的默认值为 1。 通过网络数据库 API 返回的 IP 地址数量受限:``getaddrinfo()````gethostbyname()`` 受到宏 ``DNS_MAX_HOST_IP`` 的限制,宏的默认值为 1。