deadlock

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2025 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/2, func(dur time.Duration, file string, line int) {
	panic(fmt.Sprintf("Detected deadlock via lock timeout! Locked for %s at %s:%d\n", dur, file, line))
    })

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

    mutex.Lock() // << alert happens here
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetGlobalLockTimeout added in v0.5.3

func GetGlobalLockTimeout() time.Duration

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 GoroutineID added in v1.0.0

type GoroutineID int64

func GetGoroutineId added in v1.0.0

func GetGoroutineId() GoroutineID

type RWMutex

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

func (*RWMutex) GetLockTimeout added in v0.5.3

func (m *RWMutex) GetLockTimeout() time.Duration

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(goid GoroutineID)

Lock method with deadlock detection and timeout handling goid from GetGoroutineId() !

func (*RWMutex) RLock

func (m *RWMutex) RLock(goid GoroutineID)

RLock method with deadlock detection and timeout handling goid from GetGoroutineId() !

func (*RWMutex) RUnlock

func (m *RWMutex) RUnlock(goid GoroutineID)

RUnlock method goid from GetGoroutineId() !

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(goid GoroutineID)

Unlock method goid from GetGoroutineId() !

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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