Skip to content

alexyer/taggedptr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Coverage Status

Description

This module contains methods to tag the pointer. Tagged pointer is a pointer with additional data associate with it. It's possible because data must be word aligned, hence least significant bits could be used to store some data. It's useful in lock-free programming, to store some data in pointer atomically using CAS instructions.

Examples

type TestStruct struct {
	Field int
}

s := &TestStruct{42}

// Tag pointer and assign new value
newPtr, _ := Tag(unsafe.Pointer(s), 3)
s = (*TestStruct)(newPtr)

// Get clear pointer
initialPtr := GetPointer(unsafe.Pointer(s))

// Get tag
tag := GetTag(unsafe.Pointer(s))

// Tag, compare and swap pointer
casPtr := unsafe.Pointer(s)
CompareAndSwap(&casPtr, casPtr, casPtr, 0, 1)

// Atomically tag pointer
AttemptTag(&casPtr, casPtr, 1)

// Get both pointer and tag values
ptr, tag := Get(unsafe.Pointer(s))

About

Golang implementation of tagged pointers.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages