{"id":15018664,"url":"https://github.com/pmmp/snooze","last_synced_at":"2026-03-12T08:10:27.294Z","repository":{"id":32677764,"uuid":"132746487","full_name":"pmmp/Snooze","owner":"pmmp","description":"Event loop for handling notifications from multiple threads at once","archived":false,"fork":false,"pushed_at":"2024-12-03T15:01:42.000Z","size":88,"stargazers_count":18,"open_issues_count":2,"forks_count":3,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-09T15:09:57.796Z","etag":null,"topics":["github-actions-enabled","on-packagist","php8","php81","php82","phpstan-l9","phpstan-strict"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pmmp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-05-09T11:25:19.000Z","updated_at":"2024-12-15T04:13:49.000Z","dependencies_parsed_at":"2024-02-28T14:29:33.382Z","dependency_job_id":"dd43e391-f3c5-4f5e-9570-0004025fb035","html_url":"https://github.com/pmmp/Snooze","commit_stats":{"total_commits":99,"total_committers":4,"mean_commits":24.75,"dds":0.6868686868686869,"last_synced_commit":"d54dcf41a09e61a40c89f2d0ed9938787a83bc75"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmmp%2FSnooze","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmmp%2FSnooze/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmmp%2FSnooze/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmmp%2FSnooze/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pmmp","download_url":"https://codeload.github.com/pmmp/Snooze/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248055282,"owners_count":21040157,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["github-actions-enabled","on-packagist","php8","php81","php82","phpstan-l9","phpstan-strict"],"created_at":"2024-09-24T19:52:15.154Z","updated_at":"2026-03-12T08:10:27.244Z","avatar_url":"https://github.com/pmmp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Snooze\nEvent-driven thread notification management library for code using the pthreads extension\n\n## Use cases\next-pthreads currently doesn't make it conveniently possible to `wait()` for notifications from multiple threads simultaneously.\nThis library allows you to do that using a `SleeperHandler`.\n\nEvery thread must receive its own `SleeperNotifier` (since they are thread-safe, you can share notifiers between threads, but it's recommended not to).\nThe thread should call `wakeupSleeper()` on its `SleeperNotifier`, which will cause the thread waiting on `SleeperHandler` to wake up and process whatever notification was delivered.\n\nIt's similar to using the `select()` system call on an array of sockets or file descriptors, but with threads instead.\n\n## Example\n\n```php\nclass NotifyingThread extends \\pmmp\\thread\\Thread{\n\tpublic function __construct(\n\t    private \\pocketmine\\snooze\\SleeperHandlerEntry $sleeperEntry,\n\t    private \\pmmp\\thread\\ThreadSafeArray $buffer\n\t){}\n\n\tpublic function run() : void{\n\t\t$stdin = fopen('php://stdin', 'r');\n\t\t$notifer = $this-\u003esleeperEntry-\u003ecreateNotifier();\n\t\twhile(true){\n\t\t\techo \"Type something and press ENTER:\\n\";\n\t\t\t//do whatever you're doing\n\t\t\t$line = fgets($stdin); //blocks until the user enters something\n\n\t\t\t//add the line to the buffer\n\t\t\t$this-\u003ebuffer[] = $line;\n\n\t\t\t//send a notification to the main thread to tell it that we read a line\n\t\t\t//the parent thread doesn't have to be sleeping to receive this, it'll process it next time it tries to go\n\t\t\t//back to sleep\n\t\t\t//if the parent thread is sleeping, it'll be woken up to process notifications immediately.\n\t\t\t$notifer-\u003ewakeupSleeper();\n\t\t}\n\t}\n}\n\n$sleeper = new \\pocketmine\\snooze\\SleeperHandler();\n\n$buffer = new \\pmmp\\thread\\ThreadSafeArray();\n$sleeperEntry = $sleeper-\u003eaddNotifier(function() use($buffer) : void{\n\t//do some things when this notifier sends a notification\n\techo \"Main thread got line: \" . $buffer-\u003eshift();\n});\n\n$thread = new NotifyingThread($sleeperEntry, $buffer);\n$thread-\u003estart();\n\nwhile(true){\n\t$start = microtime(true);\n\t//do some work that you do every tick\n\n\t//process any pending notifications, then try to sleep 50ms until the next tick\n\t//this may wakeup at any time to process received notifications\n\t//if it wakes up and there is still time left to sleep before the specified time, it will go back to sleep again\n\t//until that time, guaranteeing a delay of at least this amount\n\t//if there are notifications waiting when this is called, they'll be processed before going to sleep\n\t$sleeper-\u003esleepUntil($start + 0.05);\n}\n\nwhile(true){\n\t//alternatively, if you want to only wait for notifications and not tick:\n\t//but from the pthreads rulebook, only ever wait FOR something!\n\t//this will wait indefinitely until something wakes it up, and then return immediately\n\t$sleeper-\u003esleepUntilNotification();\n}\n\n$thread-\u003ejoin();\n//Unregister the notifier when you're done with it\n$sleeper-\u003eremoveNotifier($sleeperEntry-\u003egetNotifierId());\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpmmp%2Fsnooze","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpmmp%2Fsnooze","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpmmp%2Fsnooze/lists"}