From 4ed16a935a7846bb2c6e40d5bb80605ba6aaa2a5 Mon Sep 17 00:00:00 2001 From: Ihor Dutchak Date: Sun, 15 Nov 2026 23:09:19 +0200 Subject: [PATCH] docs: clarify HIDAPI lifecycle concurrency rules Require initialization before worker threads and completed calls and closed devices before shutdown. Scope serialization to the listed global functions instead of prohibiting every API call alongside repeated initialization. Assisted-by: Codex:GPT-6 --- AGENTS.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 3f7d88128..f60059ab0 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -56,17 +56,16 @@ generate example/test code that respects it. The authoritative reference is ### Library lifecycle -- Call `hid_init()` before any other HIDAPI function. Implicit lazy initialization exists, but - must not be relied upon when more than one thread may make the first call. -- Call `hid_exit()` last, after every device has been closed. `hid_exit()` deinitializes the - whole library; no HIDAPI function other than a new `hid_init()` may be called after it, and - device handles obtained before it are invalid. -- `hid_init()`/`hid_exit()` must never run concurrently with any other HIDAPI function. +- Call `hid_init()` before starting threads that use HIDAPI. Implicit lazy initialization + exists, but must not be relied upon when more than one thread may make the first call. +- Call `hid_exit()` last, after all other calls have returned and every device has been closed. + `hid_exit()` deinitializes the whole library; no HIDAPI function other than a new `hid_init()` + may be called after it, and device handles obtained before it are invalid. ### Concurrency -- HIDAPI v0.x.x is **not thread-safe**. The following must not be called concurrently from - different threads: `hid_init`, `hid_exit`, `hid_enumerate`, `hid_open`, `hid_open_path`, +- HIDAPI v0.x.x is **not thread-safe**. The following functions must be serialized with one + another: `hid_init`, `hid_exit`, `hid_enumerate`, `hid_open`, `hid_open_path`, `hid_close`, `hid_error(NULL)` — the global error string is one reason. - Functions taking a `hid_device *` are not individually thread-safe, but different devices may be used from different threads — i.e. a dedicated thread per device.