diff --git a/error.go b/error.go index d47c1d9..df83fef 100644 --- a/error.go +++ b/error.go @@ -1,16 +1,13 @@ package xmlsec import ( + "C" "fmt" "runtime" - "unsafe" "github.com/crewjam/errset" ) -// #include -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 diff --git a/thread_darwin.go b/thread_darwin.go new file mode 100644 index 0000000..0cf1a42 --- /dev/null +++ b/thread_darwin.go @@ -0,0 +1,10 @@ +package xmlsec + +import "unsafe" + +// #include +import "C" + +func getThreadId() uintptr { + return uintptr(unsafe.Pointer(C.pthread_self())) +} diff --git a/thread_linux.go b/thread_linux.go new file mode 100644 index 0000000..dd6f99b --- /dev/null +++ b/thread_linux.go @@ -0,0 +1,7 @@ +package xmlsec + +import "syscall" + +func getThreadId() uintptr { + return uintptr(syscall.Gettid()) +}