]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | \r |
2 | /* This code implemented by Mark Hammond (MHammond@skippinet.com.au) */\r | |
3 | \r | |
4 | #include <windows.h>\r | |
5 | #include <limits.h>\r | |
6 | #include <pydebug.h>\r | |
7 | \r | |
8 | long PyThread_get_thread_ident(void);\r | |
9 | \r | |
10 | /*\r | |
11 | * Change all headers to pure ANSI as no one will use K&R style on an\r | |
12 | * NT\r | |
13 | */\r | |
14 | \r | |
15 | /*\r | |
16 | * Initialization of the C package, should not be needed.\r | |
17 | */\r | |
18 | static void PyThread__init_thread(void)\r | |
19 | {\r | |
20 | }\r | |
21 | \r | |
22 | /*\r | |
23 | * Thread support.\r | |
24 | */\r | |
25 | long PyThread_start_new_thread(void (*func)(void *), void *arg)\r | |
26 | {\r | |
27 | long rv;\r | |
28 | int success = -1;\r | |
29 | \r | |
30 | dprintf(("%ld: PyThread_start_new_thread called\n", PyThread_get_thread_ident()));\r | |
31 | if (!initialized)\r | |
32 | PyThread_init_thread();\r | |
33 | \r | |
34 | rv = _beginthread(func, 0, arg); /* use default stack size */\r | |
35 | \r | |
36 | if (rv != -1) {\r | |
37 | success = 0;\r | |
38 | dprintf(("%ld: PyThread_start_new_thread succeeded:\n", PyThread_get_thread_ident()));\r | |
39 | }\r | |
40 | \r | |
41 | return success;\r | |
42 | }\r | |
43 | \r | |
44 | /*\r | |
45 | * Return the thread Id instead of an handle. The Id is said to uniquely identify the\r | |
46 | * thread in the system\r | |
47 | */\r | |
48 | long PyThread_get_thread_ident(void)\r | |
49 | {\r | |
50 | if (!initialized)\r | |
51 | PyThread_init_thread();\r | |
52 | \r | |
53 | return GetCurrentThreadId();\r | |
54 | }\r | |
55 | \r | |
56 | void PyThread_exit_thread(void)\r | |
57 | {\r | |
58 | dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));\r | |
59 | if (!initialized)\r | |
60 | exit(0);\r | |
61 | _endthread();\r | |
62 | }\r | |
63 | \r | |
64 | /*\r | |
65 | * Lock support. It has to be implemented using Mutexes, as\r | |
66 | * CE doesnt support semaphores. Therefore we use some hacks to\r | |
67 | * simulate the non reentrant requirements of Python locks\r | |
68 | */\r | |
69 | PyThread_type_lock PyThread_allocate_lock(void)\r | |
70 | {\r | |
71 | HANDLE aLock;\r | |
72 | \r | |
73 | dprintf(("PyThread_allocate_lock called\n"));\r | |
74 | if (!initialized)\r | |
75 | PyThread_init_thread();\r | |
76 | \r | |
77 | aLock = CreateEvent(NULL, /* Security attributes */\r | |
78 | 0, /* Manual-Reset */\r | |
79 | 1, /* Is initially signalled */\r | |
80 | NULL); /* Name of event */\r | |
81 | \r | |
82 | dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));\r | |
83 | \r | |
84 | return (PyThread_type_lock) aLock;\r | |
85 | }\r | |
86 | \r | |
87 | void PyThread_free_lock(PyThread_type_lock aLock)\r | |
88 | {\r | |
89 | dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));\r | |
90 | \r | |
91 | CloseHandle(aLock);\r | |
92 | }\r | |
93 | \r | |
94 | /*\r | |
95 | * Return 1 on success if the lock was acquired\r | |
96 | *\r | |
97 | * and 0 if the lock was not acquired. This means a 0 is returned\r | |
98 | * if the lock has already been acquired by this thread!\r | |
99 | */\r | |
100 | int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)\r | |
101 | {\r | |
102 | int success = 1;\r | |
103 | DWORD waitResult;\r | |
104 | \r | |
105 | dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),aLock, waitflag));\r | |
106 | \r | |
107 | #ifndef DEBUG\r | |
108 | waitResult = WaitForSingleObject(aLock, (waitflag ? INFINITE : 0));\r | |
109 | #else\r | |
110 | /* To aid in debugging, we regularly wake up. This allows us to\r | |
111 | break into the debugger */\r | |
112 | while (TRUE) {\r | |
113 | waitResult = WaitForSingleObject(aLock, waitflag ? 3000 : 0);\r | |
114 | if (waitflag==0 || (waitflag && waitResult == WAIT_OBJECT_0))\r | |
115 | break;\r | |
116 | }\r | |
117 | #endif\r | |
118 | \r | |
119 | if (waitResult != WAIT_OBJECT_0) {\r | |
120 | success = 0; /* We failed */\r | |
121 | }\r | |
122 | \r | |
123 | dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n", PyThread_get_thread_ident(),aLock, waitflag, success));\r | |
124 | \r | |
125 | return success;\r | |
126 | }\r | |
127 | \r | |
128 | void PyThread_release_lock(PyThread_type_lock aLock)\r | |
129 | {\r | |
130 | dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));\r | |
131 | \r | |
132 | if (!SetEvent(aLock))\r | |
133 | dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", PyThread_get_thread_ident(), aLock, GetLastError()));\r | |
134 | }\r | |
135 | \r | |
136 | \r |