{"id":16011316,"url":"https://github.com/sof3/rwlock.php","last_synced_at":"2025-03-17T20:31:26.212Z","repository":{"id":43366067,"uuid":"361443844","full_name":"SOF3/rwlock.php","owner":"SOF3","description":"Exclusive and Read-write locks for PHP.","archived":false,"fork":false,"pushed_at":"2024-09-28T03:25:31.000Z","size":35,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-28T04:08:01.020Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SOF3.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":"2021-04-25T14:00:27.000Z","updated_at":"2024-09-28T03:25:35.000Z","dependencies_parsed_at":"2024-10-27T16:25:15.830Z","dependency_job_id":null,"html_url":"https://github.com/SOF3/rwlock.php","commit_stats":{"total_commits":14,"total_committers":3,"mean_commits":4.666666666666667,"dds":0.2142857142857143,"last_synced_commit":"7f9b59e488326619e9b24a39620ce747618362b9"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SOF3%2Frwlock.php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SOF3%2Frwlock.php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SOF3%2Frwlock.php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SOF3%2Frwlock.php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SOF3","download_url":"https://codeload.github.com/SOF3/rwlock.php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243882244,"owners_count":20363110,"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":[],"created_at":"2024-10-08T13:41:43.658Z","updated_at":"2025-03-17T20:31:25.932Z","avatar_url":"https://github.com/SOF3.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rwlock.php\n\nExclusive and Read-write locks for PHP.\n\nThis is a quick port of [my javascript library `rwlock-promise`](https://github.com/SOF3/rwlock-promise) to PHP.\nSome parts may happen to be less idiomatic because of this.\n\nThis library uses the [`await-generator`](https://github.com/SOF3/await-generator) framework\nto expose asynchronous function call API.\n\n## Usage\n```php\n$mutex = new Mutex;\n\n$epoch = time();\n\nyield $mutex-\u003erun(function() use($epoch) {\n\techo \"Start 1: \", time() - $epoch, \"\\n\";\n\tyield $this-\u003ewaitSeconds(2);\n\techo \"End 1: \", time() - $epoch, \"\\n\";\n});\n\nyield $mutex-\u003erun(function() use($epoch) {\n\techo \"Start 2: \", time() - $epoch, \"\\n\";\n\tyield $this-\u003ewaitSeconds(1);\n\techo \"End 2: \", time() - $epoch, \"\\n\";\n});\n```\n\nThe above should output\n\n```\nStart 1: 0\nEnd 1: 2\nStart 2: 2\nEnd 2: 3\n```\n\nThe second generator is only run after the first generator returns.\n\n```php\n$mutex = new RwLock;\n\n$epoch = time();\n\nAwait::g2c($mutex-\u003ereadClosure(function() use($epoch) {\n\techo \"Start 1: \", time() - $epoch, \"\\n\";\n\tyield $this-\u003ewaitSeconds(2);\n\techo \"End 1: \", time() - $epoch, \"\\n\";\n}));\n\nAwait::g2c($mutex-\u003ereadClosure(function() use($epoch, $mutex) {\n\techo \"Start 2: \", time() - $epoch, \"\\n\";\n\tyield $this-\u003ewaitSeconds(1);\n\techo \"End 2: \", time() - $epoch, \"\\n\";\n\n\tAwait::g2c($mutex-\u003ereadClosure(function() use($epoch) {\n\t\techo \"Start 3: \", time() - $epoch, \"\\n\";\n\t\tyield $this-\u003ewaitSeconds(2);\n\t\techo \"End 3: \", time() - $epoch, \"\\n\";\n\t}));\n\n\tAwait::f2c($mutex-\u003ewriteClosure(function() use($epoch) {\n\t\techo \"Start 4: \", time() - $epoch, \"\\n\";\n\t\tyield $this-\u003ewaitSeconds(2);\n\t\techo \"End 4: \", time() - $epoch, \"\\n\";\n\t}));\n\n\tAwait::f2c($mutex-\u003ereadClosure(function() use($epoch) {\n\t\techo \"Start 5: \", time() - $epoch, \"\\n\";\n\t\tyield $this-\u003ewaitSeconds(2);\n\t\techo \"End 5: \", time() - $epoch, \"\\n\";\n\t}));\n}));\n```\n\nThe above should output\n\n```\nStart 1: 0\nStart 2: 0\nEnd 2: 1\nStart 3: 1\nEnd 1: 2\nEnd 3: 3\nStart 4: 3\nEnd 4: 5\nStart 5: 5\nEnd 5: 7\n```\n\nIt does not make sense to `yield` (await) a read/write inside a read/write block;\nin particular, yielding another write inside a read/write block would lead to a deadlock.\nUse `Await::g2c`, as shown above, to schedule a read/write without blocking on it.\nHowever, you must always either `yield` or `f2c`/`g2c` a read/write,\nbecause it only returns a generator,\nwhich does nothing if you don't call anything on it.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsof3%2Frwlock.php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsof3%2Frwlock.php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsof3%2Frwlock.php/lists"}