19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "runtime/atomic.hpp"
27 #include "runtime/thread.inline.hpp"
28 #include "runtime/threadCritical.hpp"
29
30 // OS-includes here
31 # include <windows.h>
32 # include <winbase.h>
33
34 //
35 // See threadCritical.hpp for details of this class.
36 //
37
38 static bool initialized = false;
39 static volatile jint lock_count = -1;
40 static HANDLE lock_event;
41 static DWORD lock_owner = -1;
42
43 //
44 // Note that Microsoft's critical region code contains a race
45 // condition, and is not suitable for use. A thread holding the
46 // critical section cannot safely suspend a thread attempting
47 // to enter the critical region. The failure mode is that both
48 // threads are permanently suspended.
49 //
50 // I experiemented with the use of ordinary windows mutex objects
51 // and found them ~30 times slower than the critical region code.
52 //
53
54 ThreadCritical::ThreadCritical() {
55 DWORD current_thread = GetCurrentThreadId();
56
57 if (lock_owner != current_thread) {
58 // Grab the lock before doing anything.
59 while (Atomic::cmpxchg(0, &lock_count, -1) != -1) {
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "runtime/atomic.hpp"
27 #include "runtime/thread.inline.hpp"
28 #include "runtime/threadCritical.hpp"
29
30 // OS-includes here
31 # include <windows.h>
32 # include <winbase.h>
33
34 //
35 // See threadCritical.hpp for details of this class.
36 //
37
38 static bool initialized = false;
39 static volatile int lock_count = -1;
40 static HANDLE lock_event;
41 static DWORD lock_owner = -1;
42
43 //
44 // Note that Microsoft's critical region code contains a race
45 // condition, and is not suitable for use. A thread holding the
46 // critical section cannot safely suspend a thread attempting
47 // to enter the critical region. The failure mode is that both
48 // threads are permanently suspended.
49 //
50 // I experiemented with the use of ordinary windows mutex objects
51 // and found them ~30 times slower than the critical region code.
52 //
53
54 ThreadCritical::ThreadCritical() {
55 DWORD current_thread = GetCurrentThreadId();
56
57 if (lock_owner != current_thread) {
58 // Grab the lock before doing anything.
59 while (Atomic::cmpxchg(0, &lock_count, -1) != -1) {
|