{"id":13438505,"url":"https://github.com/aymanmadkour/glock","last_synced_at":"2025-03-20T06:30:33.547Z","repository":{"id":62439566,"uuid":"163623225","full_name":"aymanmadkour/glock","owner":"aymanmadkour","description":"Granular locking crate for Rust","archived":false,"fork":false,"pushed_at":"2019-01-03T23:10:14.000Z","size":30,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-04T09:39:01.258Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aymanmadkour.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}},"created_at":"2018-12-30T22:58:22.000Z","updated_at":"2024-04-22T21:00:37.000Z","dependencies_parsed_at":"2022-11-01T21:16:29.492Z","dependency_job_id":null,"html_url":"https://github.com/aymanmadkour/glock","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aymanmadkour%2Fglock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aymanmadkour%2Fglock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aymanmadkour%2Fglock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aymanmadkour%2Fglock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aymanmadkour","download_url":"https://codeload.github.com/aymanmadkour/glock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244564844,"owners_count":20473143,"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-07-31T03:01:06.077Z","updated_at":"2025-03-20T06:30:28.537Z","avatar_url":"https://github.com/aymanmadkour.png","language":"Rust","funding_links":[],"categories":["Libraries","库","库 Libraries"],"sub_categories":["Concurrency","并发","并发 Concurrency"],"readme":"# glock\nGranular locking crate for Rust. Instead of using coarse-grained `Mutex` or `RwLock` which can be used to lock an entire structure, glock provides more granular locking.\n\n`GLock` can be either nested or non-nested. The same locking rules apply in both cases, but the way they are constructed is different in each case.\n\n## Nested GLocks\nWhen `GLock`s are nested (i.e. child `GLock`s are protected inside parent `GLock`), a `GLockBuilder` must be used to construct the parent `GLock`.\n\n### Example\n```\nextern crate glock;\n\nuse glock::*;\n\nstruct Parent {\n    a: GLock\u003cChild\u003e,\n    b: GLock\u003cChild\u003e,\n}\n\nstruct Child {\n    x: GLock\u003ci32\u003e,\n    y: GLock\u003ci32\u003e,\n}\n\nfn main() {\n    // Construct parent GLocks and all of its nested GLocks\n    let parent_lock = {\n        // Prepare parent builder\n        let parent_builder = GLock::\u003cParent\u003e::new_root_builder();\n\n        // Build child GLock protecting first Child struct\n        let child1_lock = {\n            let child1_builder = parent_builder.new_child_builder().unwrap();\n\n            let child1 = Child {\n                x: child1_builder.new_child(0i32).unwrap(),\n                y: child1_builder.new_child(0i32).unwrap(),\n            };\n\n            child1_builder.build(child1).unwrap()\n        };\n\n        // Build child GLock protecting second Child struct\n        let child2_lock = {\n            let child2_builder = parent_builder.new_child_builder().unwrap();\n\n            let child2 = Child {\n                x: child2_builder.new_child(0i32).unwrap(),\n                y: child2_builder.new_child(0i32).unwrap(),\n            };\n\n            child2_builder.build(child2).unwrap()\n        };\n\n        // Create parent struct\n        let parent = Parent {\n            a: child1_lock,\n            b: child2_lock,\n        };\n\n        // Build parent GLock protecting parent struct\n        parent_builder.build(parent).unwrap()\n    };\n\n    // Lock parent\n    let p_guard = parent_lock.lock(LockType::IntentionExclusive).unwrap();\n\n    // Lock first child\n    let a_guard = p_guard.a.lock_using_parent(LockType::IntentionExclusive, \u0026p_guard).unwrap();\n\n    // Lock and modify field x inside first child\n    let mut a_x_guard = a_guard.x.lock_exclusive_using_parent(\u0026a_guard).unwrap();\n    *a_x_guard = 10;\n\n    // Lock and modify field y inside first child\n    let mut a_y_guard = a_guard.y.lock_exclusive_using_parent(\u0026a_guard).unwrap();\n    *a_y_guard = 20;\n\n    // This one will fail with LockError::LockBusy\n    let p_guard_2 = parent_lock.try_lock(LockType::Shared).unwrap();\n}\n```\n\n# Non-Nested GLocks\nWhen `GLock`s are not nested, you can access child locks directly. If you try to acquire a child `GLock` without locking its parent first, an implicit lock will be acquired on its parent and released when the child `GLockGuard` is dropped.\n\n## Example\n\n```\nextern crate glock;\n\nuse glock::*;\n\nfn main() {\n    // Create parent lock\n    let parent_lock = GLock::new_root(0u32).unwrap();\n\n    // Create child locks\n    let child1_lock = parent_lock.new_child(0u32).unwrap();\n    let child2_lock = parent_lock.new_child(0u32).unwrap();\n\n    // An implicit IntentionExclusive lock is acquired on parent_lock,\n    // Because child1_lock is a child of parent_lock.\n    let mut child1_guard = child1_lock.lock_exclusive().unwrap();\n    *child1_guard = 10;\n\n    // This will fail with LockError::LockBusy because an IntentionExclusive lock is held\n    // on parent_lock\n    let parent_guard2 = parent_lock.try_lock(LockType::Shared).unwrap();\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faymanmadkour%2Fglock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faymanmadkour%2Fglock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faymanmadkour%2Fglock/lists"}