[GH-ISSUE #35] rand() realtime safety on macOS and Linux #28

Open
opened 2026-05-05 22:02:35 -06:00 by gitea-mirror · 3 comments
Owner

Originally created by @x42 on GitHub (Dec 7, 2021).
Original GitHub issue: https://github.com/airwindows/airwindows/issues/35

On unices (Linux and macOS) the call to rand() takes a lock (preserve global random seed) and is hence not realtime-safe.
This also results in a significant DSP load increase, if multiple airwindows plugins are used on those systems.

On Windows this is less of an issue since rand() uses a thread-local variable by default.

I suggest to have a look at https://www.pcg-random.org/

You may also prefer this to the 6 calls on Windows to work around their 15 bits limitation. PCG provides excellent 32bit randomness and is even more efficient compared to rand().

A proof-of-concept patching one plugin is shown at #31 - this will have to be copied to other plugins as well.

Direct diff of that pull-request is available via
https://patch-diff.githubusercontent.com/raw/airwindows/airwindows/pull/31.patch
https://patch-diff.githubusercontent.com/raw/airwindows/airwindows/pull/31.diff

Originally created by @x42 on GitHub (Dec 7, 2021). Original GitHub issue: https://github.com/airwindows/airwindows/issues/35 On unices (Linux and macOS) the call to `rand()` takes a lock (preserve global random seed) and is hence not realtime-safe. This also results in a significant DSP load increase, if multiple airwindows plugins are used on those systems. On Windows this is less of an issue since `rand()` uses a thread-local variable by default. I suggest to have a look at https://www.pcg-random.org/ You may also prefer this to the 6 calls on Windows to work around their 15 bits limitation. PCG provides excellent 32bit randomness and is even more efficient compared to rand(). A proof-of-concept patching one plugin is shown at #31 - this will have to be copied to other plugins as well. Direct diff of that pull-request is available via https://patch-diff.githubusercontent.com/raw/airwindows/airwindows/pull/31.patch https://patch-diff.githubusercontent.com/raw/airwindows/airwindows/pull/31.diff
Author
Owner

@mxmilkiib commented on GitHub (Dec 7, 2021):

How does one access .patch and .diff files via the github interface?

<!-- gh-comment-id:988165616 --> @mxmilkiib commented on GitHub (Dec 7, 2021): How does one access .patch and .diff files via the github interface?
Author
Owner

@x42 commented on GitHub (Dec 7, 2021):

Go to a pull-request e.g. https://github.com/airwindows/airwindows/pull/31 and then add .patch or .diff suffix to the URL. e.g. https://github.com/airwindows/airwindows/pull/31.patch or https://github.com/airwindows/airwindows/pull/31.diff

The former is a complete git patch series (individual commits with commit messages), while the latter produces a combined flat change-set. Both can be applied with the patch commandline util. The former can also be used with git am or git apply.

PS. you can also directly apply it:

curl -L https://github.com/airwindows/airwindows/pull/31.diff | patch -p1 # --dry-run
<!-- gh-comment-id:988170292 --> @x42 commented on GitHub (Dec 7, 2021): Go to a pull-request e.g. `https://github.com/airwindows/airwindows/pull/31` and then add `.patch` or `.diff` suffix to the URL. e.g. `https://github.com/airwindows/airwindows/pull/31.patch` or `https://github.com/airwindows/airwindows/pull/31.diff` The former is a complete git patch series (individual commits with commit messages), while the latter produces a combined flat change-set. Both can be applied with the `patch` commandline util. The former can also be used with `git am` or `git apply`. PS. you can also directly apply it: ```bash curl -L https://github.com/airwindows/airwindows/pull/31.diff | patch -p1 # --dry-run ```
Author
Owner

@faithanalog commented on GitHub (Nov 4, 2024):

I believe this is fixed since 54fd8c7a18.

> grep -rl 'rand(' | grep Proc
TapeDust/TapeDustProc.cpp

It looks like TapeDust is the only plugin that calls rand() in its process function today.

<!-- gh-comment-id:2455733904 --> @faithanalog commented on GitHub (Nov 4, 2024): I believe this is fixed since 54fd8c7a18f0faa3489f8a430e06f2f4f98b3936. ``` > grep -rl 'rand(' | grep Proc TapeDust/TapeDustProc.cpp ``` It looks like TapeDust is the only plugin that calls `rand()` in its process function today.
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/airwindows#28
No description provided.