{"action":"created","comment":{"url":"https://api.github.com/repos/docker/docker/pulls/comments/44083805","id":44083805,"diff_hunk":"@@ -0,0 +1,111 @@\n+/*\n+Package locker provides a mechanism for creating finer-grained locking to help\n+free up more global locks to handle other tasks.\n+\n+The implementation looks close to a sync.Mutex, however the user must provide a\n+reference to use to refer to the underlying lock when locking and unlocking,\n+and unlock may generate an error.\n+\n+If a lock with a given name does not exist when `Lock` is called, one is\n+created.\n+Lock references are automatically cleaned up on `Unlock` if nothing else is\n+waiting for the lock.\n+*/\n+package locker\n+\n+import (\n+\t\"errors\"\n+\t\"sync\"\n+\t\"sync/atomic\"\n+)\n+\n+// ErrNoSuchLock is returned when the requested lock does not exist\n+var ErrNoSuchLock = errors.New(\"no such lock\")\n+\n+// Locker provides a locking mechanism based on the passed in reference name\n+type Locker struct {\n+\tmu    sync.Mutex\n+\tlocks map[string]*lockCtr\n+}\n+\n+// lockCtr is used by Locker to represent a lock with a given name.\n+type lockCtr struct {\n+\tmu sync.Mutex\n+\t// waiters is the number of waiters waiting to acquire the lock\n+\twaiters uint32\n+}\n+\n+// inc increments the number of waiters waiting for the lock\n+func (l *lockCtr) inc() {\n+\tatomic.AddUint32(&l.waiters, 1)\n+}\n+\n+// dec decrements the number of waiters wating on the lock\n+func (l *lockCtr) dec() {\n+\tatomic.AddUint32(&l.waiters, ^uint32(l.waiters-1))\n+}\n+\n+// count gets the current number of waiters\n+func (l *lockCtr) count() uint32 {\n+\treturn atomic.LoadUint32(&l.waiters)\n+}\n+\n+// Lock locks the mutex\n+func (l *lockCtr) Lock() {\n+\tl.mu.Lock()\n+}\n+\n+// Unlock unlocks the mutex\n+func (l *lockCtr) Unlock() {\n+\tl.mu.Unlock()\n+}\n+\n+// New creates a new Locker\n+func New() *Locker {\n+\treturn &Locker{\n+\t\tlocks: make(map[string]*lockCtr),\n+\t}\n+}\n+\n+// Lock locks a mutex with the given name. If it doesn't exist, one is created\n+func (l *Locker) Lock(name string) {\n+\tl.mu.Lock()\n+\tif l.locks == nil {\n+\t\tl.locks = make(map[string]*lockCtr)\n+\t}\n+\n+\tnameLock, exists := l.locks[name]\n+\tif !exists {\n+\t\tnameLock = &lockCtr{}\n+\t\tl.locks[name] = nameLock\n+\t}\n+\n+\t// increment the nameLock waiters while inside the main mutex\n+\t// this makes sure that the lock isn't deleted if `Lock` and `Unlock` are called concurrently\n+\tnameLock.inc()\n+\tl.mu.Unlock()\n+\n+\t// Lock the nameLock outside the main mutex so we don't block other operations\n+\t// once locked then we can decrement the number of waiters for this lock\n+\tnameLock.Lock()\n+\tnameLock.dec()\n+}\n+\n+// Unlock unlocks the mutex with the given name\n+// If the given lock is not being waited on by any other callers, it is deleted\n+func (l *Locker) Unlock(name string) error {\n+\tl.mu.Lock()\n+\tnameLock, exists := l.locks[name]\n+\tif !exists {\n+\t\tl.mu.Unlock()\n+\t\treturn ErrNoSuchLock","path":"pkg/locker/locker.go","position":101,"original_position":101,"commit_id":"fe66fdd18c0307cc02e6aeff8625c179452b8436","original_commit_id":"fe66fdd18c0307cc02e6aeff8625c179452b8436","user":{"login":"LK4D4","id":101445,"avatar_url":"https://avatars.githubusercontent.com/u/101445?v=3","gravatar_id":"","url":"https://api.github.com/users/LK4D4","html_url":"https://github.com/LK4D4","followers_url":"https://api.github.com/users/LK4D4/followers","following_url":"https://api.github.com/users/LK4D4/following{/other_user}","gists_url":"https://api.github.com/users/LK4D4/gists{/gist_id}","starred_url":"https://api.github.com/users/LK4D4/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LK4D4/subscriptions","organizations_url":"https://api.github.com/users/LK4D4/orgs","repos_url":"https://api.github.com/users/LK4D4/repos","events_url":"https://api.github.com/users/LK4D4/events{/privacy}","received_events_url":"https://api.github.com/users/LK4D4/received_events","type":"User","site_admin":false},"body":"So, you got Lock, now inc() is zero. You started Unlock, at same time another goroutine did Lock(), you removed lock from map, another goroutine will have error on Unlock. I think it's better to exclude error from interface maybe.","created_at":"2015-11-05T23:11:14Z","updated_at":"2015-11-05T23:11:14Z","html_url":"https://github.com/docker/docker/pull/17185#discussion_r44083805","pull_request_url":"https://api.github.com/repos/docker/docker/pulls/17185","_links":{"self":{"href":"https://api.github.com/repos/docker/docker/pulls/comments/44083805"},"html":{"href":"https://github.com/docker/docker/pull/17185#discussion_r44083805"},"pull_request":{"href":"https://api.github.com/repos/docker/docker/pulls/17185"}}},"pull_request":{"url":"https://api.github.com/repos/docker/docker/pulls/17185","id":48123911,"html_url":"https://github.com/docker/docker/pull/17185","diff_url":"https://github.com/docker/docker/pull/17185.diff","patch_url":"https://github.com/docker/docker/pull/17185.patch","issue_url":"https://api.github.com/repos/docker/docker/issues/17185","number":17185,"state":"open","locked":false,"title":"Fix potential races in the volume store","user":{"login":"cpuguy83","id":799078,"avatar_url":"https://avatars.githubusercontent.com/u/799078?v=3","gravatar_id":"","url":"https://api.github.com/users/cpuguy83","html_url":"https://github.com/cpuguy83","followers_url":"https://api.github.com/users/cpuguy83/followers","following_url":"https://api.github.com/users/cpuguy83/following{/other_user}","gists_url":"https://api.github.com/users/cpuguy83/gists{/gist_id}","starred_url":"https://api.github.com/users/cpuguy83/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cpuguy83/subscriptions","organizations_url":"https://api.github.com/users/cpuguy83/orgs","repos_url":"https://api.github.com/users/cpuguy83/repos","events_url":"https://api.github.com/users/cpuguy83/events{/privacy}","received_events_url":"https://api.github.com/users/cpuguy83/received_events","type":"User","site_admin":false},"body":"Uses finer grained locking so that each volume name gets its own lock\r\nrather than only being protected by the global lock, which itself needs\r\nto be unlocked during cetain operations (`create` especially`)\r\n\r\nExtracts locking package/store changes for locking out from #16534\r\n\r\nCloses #16473","created_at":"2015-10-19T20:46:27Z","updated_at":"2015-11-05T23:11:14Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6dba20777f395c061dfdf586f9aff9978af46e06","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/docker/docker/pulls/17185/commits","review_comments_url":"https://api.github.com/repos/docker/docker/pulls/17185/comments","review_comment_url":"https://api.github.com/repos/docker/docker/pulls/comments{/number}","comments_url":"https://api.github.com/repos/docker/docker/issues/17185/comments","statuses_url":"https://api.github.com/repos/docker/docker/statuses/fe66fdd18c0307cc02e6aeff8625c179452b8436","head":{"label":"cpuguy83:use_finer_locking_for_volume_store","ref":"use_finer_locking_for_volume_store","sha":"fe66fdd18c0307cc02e6aeff8625c179452b8436","user":{"login":"cpuguy83","id":799078,"avatar_url":"https://avatars.githubusercontent.com/u/799078?v=3","gravatar_id":"","url":"https://api.github.com/users/cpuguy83","html_url":"https://github.com/cpuguy83","followers_url":"https://api.github.com/users/cpuguy83/followers","following_url":"https://api.github.com/users/cpuguy83/following{/other_user}","gists_url":"https://api.github.com/users/cpuguy83/gists{/gist_id}","starred_url":"https://api.github.com/users/cpuguy83/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cpuguy83/subscriptions","organizations_url":"https://api.github.com/users/cpuguy83/orgs","repos_url":"https://api.github.com/users/cpuguy83/repos","events_url":"https://api.github.com/users/cpuguy83/events{/privacy}","received_events_url":"https://api.github.com/users/cpuguy83/received_events","type":"User","site_admin":false},"repo":{"id":15300899,"name":"docker","full_name":"cpuguy83/docker","owner":{"login":"cpuguy83","id":799078,"avatar_url":"https://avatars.githubusercontent.com/u/799078?v=3","gravatar_id":"","url":"https://api.github.com/users/cpuguy83","html_url":"https://github.com/cpuguy83","followers_url":"https://api.github.com/users/cpuguy83/followers","following_url":"https://api.github.com/users/cpuguy83/following{/other_user}","gists_url":"https://api.github.com/users/cpuguy83/gists{/gist_id}","starred_url":"https://api.github.com/users/cpuguy83/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cpuguy83/subscriptions","organizations_url":"https://api.github.com/users/cpuguy83/orgs","repos_url":"https://api.github.com/users/cpuguy83/repos","events_url":"https://api.github.com/users/cpuguy83/events{/privacy}","received_events_url":"https://api.github.com/users/cpuguy83/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/cpuguy83/docker","description":"Docker - the open-source application container engine","fork":true,"url":"https://api.github.com/repos/cpuguy83/docker","forks_url":"https://api.github.com/repos/cpuguy83/docker/forks","keys_url":"https://api.github.com/repos/cpuguy83/docker/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cpuguy83/docker/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cpuguy83/docker/teams","hooks_url":"https://api.github.com/repos/cpuguy83/docker/hooks","issue_events_url":"https://api.github.com/repos/cpuguy83/docker/issues/events{/number}","events_url":"https://api.github.com/repos/cpuguy83/docker/events","assignees_url":"https://api.github.com/repos/cpuguy83/docker/assignees{/user}","branches_url":"https://api.github.com/repos/cpuguy83/docker/branches{/branch}","tags_url":"https://api.github.com/repos/cpuguy83/docker/tags","blobs_url":"https://api.github.com/repos/cpuguy83/docker/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cpuguy83/docker/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cpuguy83/docker/git/refs{/sha}","trees_url":"https://api.github.com/repos/cpuguy83/docker/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cpuguy83/docker/statuses/{sha}","languages_url":"https://api.github.com/repos/cpuguy83/docker/languages","stargazers_url":"https://api.github.com/repos/cpuguy83/docker/stargazers","contributors_url":"https://api.github.com/repos/cpuguy83/docker/contributors","subscribers_url":"https://api.github.com/repos/cpuguy83/docker/subscribers","subscription_url":"https://api.github.com/repos/cpuguy83/docker/subscription","commits_url":"https://api.github.com/repos/cpuguy83/docker/commits{/sha}","git_commits_url":"https://api.github.com/repos/cpuguy83/docker/git/commits{/sha}","comments_url":"https://api.github.com/repos/cpuguy83/docker/comments{/number}","issue_comment_url":"https://api.github.com/repos/cpuguy83/docker/issues/comments{/number}","contents_url":"https://api.github.com/repos/cpuguy83/docker/contents/{+path}","compare_url":"https://api.github.com/repos/cpuguy83/docker/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cpuguy83/docker/merges","archive_url":"https://api.github.com/repos/cpuguy83/docker/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cpuguy83/docker/downloads","issues_url":"https://api.github.com/repos/cpuguy83/docker/issues{/number}","pulls_url":"https://api.github.com/repos/cpuguy83/docker/pulls{/number}","milestones_url":"https://api.github.com/repos/cpuguy83/docker/milestones{/number}","notifications_url":"https://api.github.com/repos/cpuguy83/docker/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cpuguy83/docker/labels{/name}","releases_url":"https://api.github.com/repos/cpuguy83/docker/releases{/id}","created_at":"2013-12-19T02:58:40Z","updated_at":"2015-04-27T13:11:12Z","pushed_at":"2015-11-05T15:05:17Z","git_url":"git://github.com/cpuguy83/docker.git","ssh_url":"git@github.com:cpuguy83/docker.git","clone_url":"https://github.com/cpuguy83/docker.git","svn_url":"https://github.com/cpuguy83/docker","homepage":"http://www.docker.io","size":76919,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":1,"forks":2,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"docker:master","ref":"master","sha":"b2f14f9bec0c4c8f14e9ac5446fbb49d88310bae","user":{"login":"docker","id":5429470,"avatar_url":"https://avatars.githubusercontent.com/u/5429470?v=3","gravatar_id":"","url":"https://api.github.com/users/docker","html_url":"https://github.com/docker","followers_url":"https://api.github.com/users/docker/followers","following_url":"https://api.github.com/users/docker/following{/other_user}","gists_url":"https://api.github.com/users/docker/gists{/gist_id}","starred_url":"https://api.github.com/users/docker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/docker/subscriptions","organizations_url":"https://api.github.com/users/docker/orgs","repos_url":"https://api.github.com/users/docker/repos","events_url":"https://api.github.com/users/docker/events{/privacy}","received_events_url":"https://api.github.com/users/docker/received_events","type":"Organization","site_admin":false},"repo":{"id":7691631,"name":"docker","full_name":"docker/docker","owner":{"login":"docker","id":5429470,"avatar_url":"https://avatars.githubusercontent.com/u/5429470?v=3","gravatar_id":"","url":"https://api.github.com/users/docker","html_url":"https://github.com/docker","followers_url":"https://api.github.com/users/docker/followers","following_url":"https://api.github.com/users/docker/following{/other_user}","gists_url":"https://api.github.com/users/docker/gists{/gist_id}","starred_url":"https://api.github.com/users/docker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/docker/subscriptions","organizations_url":"https://api.github.com/users/docker/orgs","repos_url":"https://api.github.com/users/docker/repos","events_url":"https://api.github.com/users/docker/events{/privacy}","received_events_url":"https://api.github.com/users/docker/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/docker/docker","description":"Docker - the open-source application container engine","fork":false,"url":"https://api.github.com/repos/docker/docker","forks_url":"https://api.github.com/repos/docker/docker/forks","keys_url":"https://api.github.com/repos/docker/docker/keys{/key_id}","collaborators_url":"https://api.github.com/repos/docker/docker/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/docker/docker/teams","hooks_url":"https://api.github.com/repos/docker/docker/hooks","issue_events_url":"https://api.github.com/repos/docker/docker/issues/events{/number}","events_url":"https://api.github.com/repos/docker/docker/events","assignees_url":"https://api.github.com/repos/docker/docker/assignees{/user}","branches_url":"https://api.github.com/repos/docker/docker/branches{/branch}","tags_url":"https://api.github.com/repos/docker/docker/tags","blobs_url":"https://api.github.com/repos/docker/docker/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/docker/docker/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/docker/docker/git/refs{/sha}","trees_url":"https://api.github.com/repos/docker/docker/git/trees{/sha}","statuses_url":"https://api.github.com/repos/docker/docker/statuses/{sha}","languages_url":"https://api.github.com/repos/docker/docker/languages","stargazers_url":"https://api.github.com/repos/docker/docker/stargazers","contributors_url":"https://api.github.com/repos/docker/docker/contributors","subscribers_url":"https://api.github.com/repos/docker/docker/subscribers","subscription_url":"https://api.github.com/repos/docker/docker/subscription","commits_url":"https://api.github.com/repos/docker/docker/commits{/sha}","git_commits_url":"https://api.github.com/repos/docker/docker/git/commits{/sha}","comments_url":"https://api.github.com/repos/docker/docker/comments{/number}","issue_comment_url":"https://api.github.com/repos/docker/docker/issues/comments{/number}","contents_url":"https://api.github.com/repos/docker/docker/contents/{+path}","compare_url":"https://api.github.com/repos/docker/docker/compare/{base}...{head}","merges_url":"https://api.github.com/repos/docker/docker/merges","archive_url":"https://api.github.com/repos/docker/docker/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/docker/docker/downloads","issues_url":"https://api.github.com/repos/docker/docker/issues{/number}","pulls_url":"https://api.github.com/repos/docker/docker/pulls{/number}","milestones_url":"https://api.github.com/repos/docker/docker/milestones{/number}","notifications_url":"https://api.github.com/repos/docker/docker/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/docker/docker/labels{/name}","releases_url":"https://api.github.com/repos/docker/docker/releases{/id}","created_at":"2013-01-18T18:10:57Z","updated_at":"2015-11-05T22:25:44Z","pushed_at":"2015-11-05T23:11:02Z","git_url":"git://github.com/docker/docker.git","ssh_url":"git@github.com:docker/docker.git","clone_url":"https://github.com/docker/docker.git","svn_url":"https://github.com/docker/docker","homepage":"http://www.docker.com","size":281247,"stargazers_count":25955,"watchers_count":25955,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":6937,"mirror_url":null,"open_issues_count":1200,"forks":6937,"open_issues":1200,"watchers":25955,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/docker/docker/pulls/17185"},"html":{"href":"https://github.com/docker/docker/pull/17185"},"issue":{"href":"https://api.github.com/repos/docker/docker/issues/17185"},"comments":{"href":"https://api.github.com/repos/docker/docker/issues/17185/comments"},"review_comments":{"href":"https://api.github.com/repos/docker/docker/pulls/17185/comments"},"review_comment":{"href":"https://api.github.com/repos/docker/docker/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/docker/docker/pulls/17185/commits"},"statuses":{"href":"https://api.github.com/repos/docker/docker/statuses/fe66fdd18c0307cc02e6aeff8625c179452b8436"}}}}