Skip to content

Commit

Permalink
fix(lwip): Document that not all sock operations are thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak committed Nov 6, 2024
1 parent e8a6f41 commit e8c5992
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion docs/en/api-guides/lwip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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
^^^^^^^^^^
Expand Down Expand Up @@ -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`.

.. _lwip-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.

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.
Expand Down
6 changes: 5 additions & 1 deletion docs/zh_CN/api-guides/lwip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ BSD 套接字 API

BSD 套接字 API 是一种常见的跨平台 TCP/IP 套接字 API,最初源于 UNIX 操作系统的伯克利标准发行版,现已标准化为 POSIX 规范的一部分。BSD 套接字有时也称 POSIX 套接字,或伯克利套接字。

在 ESP-IDF 中,lwIP 支持 BSD 套接字 API 的所有常见用法。
在 ESP-IDF 中,lwIP 支持 BSD 套接字 API 的所有常见用法。然而,并非所有操作都完全线程安全,因此多个线程同时进行读写可能需要额外的同步机制。详情请参见 :ref:`lwip-limitations`。

参考
^^^^^^^^^^
Expand Down Expand Up @@ -461,9 +461,13 @@ NAPT 和端口转发

另一种方法是在头文件中定义函数式宏,该头文件将预先包含在 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 服务器,并在默认接口更改时重新配置。

通过网络数据库 API 返回的 IP 地址数量受限:``getaddrinfo()`` 和 ``gethostbyname()`` 受到宏 ``DNS_MAX_HOST_IP`` 的限制,宏的默认值为 1。
Expand Down

0 comments on commit e8c5992

Please sign in to comment.