Giter Club home page Giter Club logo

Comments (6)

xhd2015 avatar xhd2015 commented on June 5, 2024

Solution:

var lock sync.Mutex

func demo(){
    __xgo_lock1 := &lock
    __xgo_lock1.Lock()
    __xgo_lock2 := &lock
     defer __xgo_lock2.Unlock()
     ...
}

However if lock itself is a pointer, then we are wrong again.

var lock *sync.Mutex

func demo(){
    __xgo_lock1 := &lock
    __xgo_lock1.Lock()
    __xgo_lock2 := &lock
     defer __xgo_lock2.Unlock()
     ...
}

Another simple solution is to turn off rewritting selector expr. i.e. only direct references are intercepted.

var lock sync.Mutex

func demo(){
     lock1:=&lock // intercepted
     lock1.Lock()

     lock.Unock() // not intercepted
}

from xgo.

xhd2015 avatar xhd2015 commented on June 5, 2024

Another simple solution is to turn off rewritting selector expr. i.e. only direct references are intercepted.

var lock sync.Mutex

func demo(){
     lock1:=&lock // intercepted
     lock1.Lock()

     lock.Unock() // not intercepted
}

After this, some valid case may not be intercepted:

type Config struct {
     On bool
}

var cfg = &Config{}

func demo(){
    if cfg.On { // cfg is not captured
        // ...
    }
}

from xgo.

xhd2015 avatar xhd2015 commented on June 5, 2024

var lock sync.Mutex
...
Will be transformed to:
Is obviously wrong.

Other types include: sync.Map.

Actually this does not happen, because func names are always skipped:

func (ctx *BlockContext) traverseCallExpr(node *syntax.CallExpr, globaleNames map[string]*DeclInfo, imports map[string]string) *syntax.CallExpr {
	if node == nil {
		return nil
	}
	if ctx.ArgCallExprParent == nil {
		ctx.ArgCallExprParent = make(map[syntax.Node]*syntax.CallExpr, len(node.ArgList))
		for _, arg := range node.ArgList {
			ctx.ArgCallExprParent[arg] = node
		}
	}

	// NOTE: we skip capturing a name as a function
	// node.Fun = ctx.traverseExpr(node.Fun, globaleNames, imports)
	for i, arg := range node.ArgList {
		node.ArgList[i] = ctx.traverseExpr(arg, globaleNames, imports)
	}
	return node
}

from xgo.

xhd2015 avatar xhd2015 commented on June 5, 2024

However, this causes panic:

func incrLocked_Fn() {
	f := lock.Lock
	f()
	count = count + 1
	lock.Unlock()
}

fatal error: sync: unlock of unlocked mutex

from xgo.

xhd2015 avatar xhd2015 commented on June 5, 2024

After this, some valid case may not be intercepted:

type Config struct {
     On bool
}

var cfg = &Config{}

func demo(){
    if cfg.On { // cfg is not captured
        // ...
    }
}

For cases where copy is allowed, we can add a comment to the definition to tell xgo allow it:

// xgo:trap
var cfg = &Config{}

from xgo.

xhd2015 avatar xhd2015 commented on June 5, 2024

After this, some valid case may not be intercepted:

type Config struct {
     On bool
}

var cfg = &Config{}

func demo(){
    if cfg.On { // cfg is not captured
        // ...
    }
}

For cases where copy is allowed, we can add a comment to the definition to tell xgo allow it:

// xgo:trap
var cfg = &Config{}

But since current syntax does not include comments, we cannot tell what the user want to do.

So we instead of comment ,use this:

const __xgo_trap_cfg = 1
var cfg = &Config{}

from xgo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.