fix race condition on GetLastError() call

This commit is contained in:
Matthew Flatt 2012-03-02 11:03:57 -07:00
parent 5cb1a844fb
commit d74793a5f9
2 changed files with 8 additions and 2 deletions

View File

@ -65,6 +65,12 @@
(define-kernel32 GetLastError (_wfun -> _DWORD))
(define (failed who)
;; There's a race condition between this use of GetLastError()
;; and other Racket threads that may have run since
;; the call in this thread that we're reporting as failed.
;; In the rare case that we lose a race, though, it just
;; means a bad report for an error that shouldn't have happened
;;; anyway.
(error who "call failed (~s)"
(GetLastError)))

View File

@ -243,7 +243,7 @@
(cpointer-push-tag! p 'HBRUSH)
p))
(define-kernel32 GetModuleFileNameW (_wfun _pointer _pointer _DWORD -> _DWORD))
(define-kernel32 GetModuleFileNameW (_wfun #:save-errno 'windows _pointer _pointer _DWORD -> _DWORD))
(define ERROR_INSUFFICIENT_BUFFER 122)
(define-shell32 ExtractIconW (_wfun _HINSTANCE _string/utf-16 _UINT -> (r : _HICON)
-> (or r (failed 'ExtractIconW))))
@ -255,7 +255,7 @@
(let ([r (GetModuleFileNameW #f p size)])
(cond
[(and (or (zero? r) (= r size))
(= (GetLastError) ERROR_INSUFFICIENT_BUFFER))
(= (saved-errno) ERROR_INSUFFICIENT_BUFFER))
(loop (* size 2))]
[(zero? r) (failed 'GetModuleFileNameW)]
[else (cast p _gcpointer _string/utf-16)]))))])