seqlock

package module
v0.0.0-...-4ffdb79 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2021 License: BSD-2-Clause Imports: 4 Imported by: 0

README

Golang-SeqLock

A basice implementation of sequence lock in golang.

License

This project is licensed under the terms of the simplified BSD license.

How to use

go get -u github.com/MUSQQQ/Golang-SeqLock

I'm sorry for not the greatest example. I plan to prepare a better one inside this repo or make a new repo for showing the usage of this seqlock.


// func that shows the process of reading data
func ReadingData(seq *seqlock.SeqLock, wg *sync.WaitGroup) {
	tmp := uint32(0)
	for {
		tmp = seq.RdRead()
		/*

			reading data

		*/
		if !seq.RdAgain(tmp) {
			break
		}
	}
	defer wg.Done()
}

// func that shows the process of writing data
func WritingData(seq *seqlock.SeqLock, wg *sync.WaitGroup) {
	seq.WrLock()
	/*

		writing data

	*/
	seq.WrUnlock()
	defer wg.Done()
}

Why

It was a mini project for my Multithreaded Programming classes

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SeqLock

type SeqLock struct {
	Counter uint64
	sync.Mutex
}

SeqLock Counter increases by 1 everytime the lock gets taken or released by writer sequence number is other name for it inherited mutex is used only by writers

func NewSeqLock

func NewSeqLock() *SeqLock

func (*SeqLock) LiveLogger

func (seq *SeqLock) LiveLogger(ms int64)

LiveLogger Logs current counter value and for how long it has been running to the standard output

func (*SeqLock) RdAgain

func (seq *SeqLock) RdAgain(val uint64) bool

RdAgain checks if data is not being modified by writer or if it has not been modified since rdRead func

func (*SeqLock) RdRead

func (seq *SeqLock) RdRead() uint64

RdRead reads counter using atomic operations used before accessing data protected by the lock

func (*SeqLock) ResetCounter

func (seq *SeqLock) ResetCounter()

ResetCounter resets counter to zero

func (*SeqLock) TimeBlock

func (seq *SeqLock) TimeBlock(ms int64)

TimeBlock locks the data for both writers and readers for the given amount of miliseconds beware that it locks once all writers before it are finished

func (*SeqLock) TimeReadBenchmark

func (seq *SeqLock) TimeReadBenchmark() uint64

TimeReadBenchmark checks for how much time the reader has to keep repeating the reading process (in ms) better to use it multiple times and average results

func (*SeqLock) TimeWriteBenchmark

func (seq *SeqLock) TimeWriteBenchmark() uint64

TimeWriteBenchmark checks for how much time the lock is blocked until new writer gets access to it (in ms) better to use it multiple times and average results

func (*SeqLock) WrLock

func (seq *SeqLock) WrLock()

WrLock counter becomes odd when writer starts modifying data

func (*SeqLock) WrUnlock

func (seq *SeqLock) WrUnlock()

WrUnlock counter becomes even when writer starts modifying data

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL