github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/pkg/sentry/uniqueid/context.go (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package uniqueid defines context.Context keys for obtaining system-wide 16 // unique identifiers. 17 package uniqueid 18 19 import ( 20 "github.com/MerlinKodo/gvisor/pkg/context" 21 ) 22 23 // contextID is the kernel package's type for context.Context.Value keys. 24 type contextID int 25 26 const ( 27 // CtxGlobalUniqueID is a Context.Value key for a system-wide 28 // unique identifier. 29 CtxGlobalUniqueID contextID = iota 30 31 // CtxGlobalUniqueIDProvider is a Context.Value key for a 32 // system-wide unique identifier generator. 33 CtxGlobalUniqueIDProvider 34 35 // CtxInotifyCookie is a Context.Value key for a unique inotify 36 // event cookie. 37 CtxInotifyCookie 38 ) 39 40 // Provider generates a sequence of unique identifiers useful for, 41 // among other things, lock ordering. 42 type Provider interface { 43 // UniqueID returns a new unique identifier. 44 UniqueID() uint64 45 } 46 47 // GlobalFromContext returns a system-wide unique identifier from ctx. 48 func GlobalFromContext(ctx context.Context) uint64 { 49 return ctx.Value(CtxGlobalUniqueID).(uint64) 50 } 51 52 // GlobalProviderFromContext returns a system-wide unique identifier from ctx. 53 func GlobalProviderFromContext(ctx context.Context) Provider { 54 return ctx.Value(CtxGlobalUniqueIDProvider).(Provider) 55 } 56 57 // InotifyCookie generates a unique inotify event cookie from ctx. 58 func InotifyCookie(ctx context.Context) uint32 { 59 return ctx.Value(CtxInotifyCookie).(uint32) 60 }