fix getting the current thread on linux & mac

This commit is contained in:
Ross Kinder
2015-12-23 12:07:04 -05:00
parent b390a3e30a
commit cccbc3e05b
3 changed files with 22 additions and 10 deletions

View File

@@ -1,16 +1,13 @@
package xmlsec
import (
"C"
"fmt"
"runtime"
"unsafe"
"github.com/crewjam/errset"
)
// #include <pthread.h>
import "C"
var globalErrors = map[uintptr]errset.ErrSet{}
type Error struct {
@@ -45,7 +42,7 @@ func onError(file *C.char, line C.int, funcName *C.char, errorObject *C.char, er
Subject: C.GoString(errorSubject),
Reason: int(reason),
Message: C.GoString(msg)}
threadID := uintptr(unsafe.Pointer(C.pthread_self()))
threadID := getThreadId()
globalErrors[threadID] = append(globalErrors[threadID], err)
}
@@ -54,22 +51,20 @@ func onError(file *C.char, line C.int, funcName *C.char, errorObject *C.char, er
// error object.
func startProcessingXML() {
runtime.LockOSThread()
threadID := uintptr(unsafe.Pointer(C.pthread_self()))
globalErrors[threadID] = errset.ErrSet{}
globalErrors[getThreadId()] = errset.ErrSet{}
}
// stopProcessingXML unlocks the goroutine-thread lock and deletes the current
// error stack.
func stopProcessingXML() {
runtime.UnlockOSThread()
threadID := uintptr(unsafe.Pointer(C.pthread_self()))
delete(globalErrors, threadID)
delete(globalErrors, getThreadId())
}
// popError returns the global error for the current thread and resets it to
// an empty error. Returns nil if no errors have occurred.
func popError() error {
threadID := uintptr(unsafe.Pointer(C.pthread_self()))
threadID := getThreadId()
rv := globalErrors[threadID].ReturnValue()
globalErrors[threadID] = errset.ErrSet{}
return rv

10
thread_darwin.go Normal file
View File

@@ -0,0 +1,10 @@
package xmlsec
import "unsafe"
// #include <pthread.h>
import "C"
func getThreadId() uintptr {
return uintptr(unsafe.Pointer(C.pthread_self()))
}

7
thread_linux.go Normal file
View File

@@ -0,0 +1,7 @@
package xmlsec
import "syscall"
func getThreadId() uintptr {
return uintptr(syscall.Gettid())
}