From a40bdfd4202db244bfc5da348a0f0c528ef122cd Mon Sep 17 00:00:00 2001 From: Carlos Martín Nieto Date: Wed, 18 Sep 2013 09:23:47 +0200 Subject: Lock the OS thread when acessing errors The library stores error information in thread-local storage, which means we need to make sure that the Go runtime doesn't switch OS threads between the time we call a function and th time we attempt to retrieve the error information. --- git.go | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'git.go') diff --git a/git.go b/git.go index 6f81293..28196c8 100644 --- a/git.go +++ b/git.go @@ -9,6 +9,7 @@ import "C" import ( "bytes" "errors" + "runtime" "unsafe" "strings" ) @@ -57,6 +58,9 @@ func NewOidFromString(s string) (*Oid, error) { cs := C.CString(s) defer C.free(unsafe.Pointer(cs)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + if C.git_oid_fromstr(o.toC(), cs) < 0 { return nil, LastError() } @@ -109,6 +113,10 @@ func ShortenOids(ids []*Oid, minlen int) (int, error) { defer C.git_oid_shorten_free(shorten) var ret C.int + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + for _, id := range ids { buf := make([]byte, 41) C.git_oid_fmt((*C.char)(unsafe.Pointer(&buf[0])), id.toC()) @@ -162,6 +170,9 @@ func Discover(start string, across_fs bool, ceiling_dirs []string) (string, erro retpath := (*C.char)(C.malloc(C.GIT_PATH_MAX)) defer C.free(unsafe.Pointer(retpath)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + r := C.git_repository_discover(retpath, C.GIT_PATH_MAX, cstart, cbool(across_fs), ceildirs) if r == 0 { -- cgit v1.2.3