deadlock

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2024 License: GPL-3.0 Imports: 6 Imported by: 1

README

Deadlock Detection RWMutex

This Go package provides a custom implementation of RWMutex with built-in deadlock detection and timeout handling. It aims to enhance the standard sync.RWMutex by preventing deadlocks and providing useful debugging information when a deadlock situation is detected.

Features

  • Deadlock Detection: Automatically detects and prevents double locking scenarios within the same goroutine, including both Lock and RLock operations.
  • Timeout Handling: Configurable lock timeout with customizable timeout handlers to notify or take action when a lock exceeds the specified duration.
  • Detailed Debugging Information: Captures and reports the file and line number where the lock was last held, providing valuable insights for debugging.
  • Global and Instance-Specific Configuration: Supports both global and per-instance lock timeout settings and handlers.

Usage

  1. Import the Package:

    import "github.com/goupdate/deadlock"
    
  2. Initialize RWMutex:

    var mutex deadlock.RWMutex
    
  3. Lock and Unlock:

    go func() {
        mutex.Lock()
        defer mutex.Unlock()
        // Critical section
    }()
    
  4. Read Lock and Unlock:

    go func() {
        mutex.RLock()
        defer mutex.RUnlock()
        // Read-only section
    }()
    
  5. Set Global Lock Timeout and Handler:

    deadlock.SetGlobalLockTimeout(time.Second*5, func(dur time.Duration, file string, line int) {
        fmt.Printf("Global lock timeout: %s at %s:%d\n", dur, file, line)
    })
    
  6. Set Instance-Specific Lock Timeout and Handler:

    mutex.SetLockTimeout(time.Second*2, func(dur time.Duration, file string, line int) {
        fmt.Printf("Instance lock timeout: %s at %s:%d\n", dur, file, line)
    })
    

Example

Here's a simple example demonstrating the usage of RWMutex with deadlock detection and timeout handling:

package main

import (
    "fmt"
    "time"

    "github.com/goupdate/deadlock"
)

func main() {
    var mutex deadlock.RWMutex

    deadlock.SetGlobalLockTimeout(time.Second*5, func(dur time.Duration, file string, line int) {
        fmt.Printf("Global lock timeout: %s at %s:%d\n", dur, file, line)
    })

    go func() {
        mutex.Lock()
        defer mutex.Unlock()
        time.Sleep(time.Second * 6) // Simulate long operation
    }()

    time.Sleep(time.Second * 7) // Wait for timeout to trigger
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResetGlobalTimers

func ResetGlobalTimers()

func SetGlobalLockTimeout

func SetGlobalLockTimeout(duration time.Duration, handler func(dur time.Duration, file string, line int))

SetGlobalLockTimeout sets the global lock timeout and handler if handler == nil or duration == 0, checking is turned off

Types

type RWMutex

type RWMutex struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func (*RWMutex) LastLocker

func (m *RWMutex) LastLocker() (string, int, time.Duration)

file and line of last Lock() position in code (if locked!)

func (*RWMutex) Lock

func (m *RWMutex) Lock()

Lock method with deadlock detection and timeout handling

func (*RWMutex) RLock

func (m *RWMutex) RLock()

RLock method with deadlock detection and timeout handling

func (*RWMutex) RUnlock

func (m *RWMutex) RUnlock()

RUnlock method

func (*RWMutex) SetLockTimeout

func (m *RWMutex) SetLockTimeout(duration time.Duration, handler func(dur time.Duration, file string, line int))

SetLockTimeout sets the lock timeout and handler for an instance of RWMutex if handler == nil or duration == 0, checking is turned off

func (*RWMutex) Unlock

func (m *RWMutex) Unlock()

Unlock method

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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