source: branches/libc-0.6/src/gcc/libobjc/thr-win32.c

Last change on this file was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 5.9 KB
Line 
1/* GNU Objective C Runtime Thread Interface - Win32 Implementation
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Galen C. Hunt ([email protected])
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 2, or (at your option) any later version.
10
11GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14details.
15
16You should have received a copy of the GNU General Public License along with
17GNU CC; see the file COPYING. If not, write to the Free Software
18Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
20
21/* As a special exception, if you link this library with files compiled with
22 GCC to produce an executable, this does not cause the resulting executable
23 to be covered by the GNU General Public License. This exception does not
24 however invalidate any other reasons why the executable file might be
25 covered by the GNU General Public License. */
26
27#include <objc/thr.h>
28#include "runtime.h"
29
30#ifndef __OBJC__
31#define __OBJC__
32#endif
33#include <windows.h>
34
35/* Key structure for maintaining thread specific storage */
36static DWORD __objc_data_tls = (DWORD)-1;
37
38/* Backend initialization functions */
39
40/* Initialize the threads subsystem. */
41int
42__objc_init_thread_system(void)
43{
44 /* Initialize the thread storage key */
45 if ((__objc_data_tls = TlsAlloc()) != (DWORD)-1)
46 return 0;
47 else
48 return -1;
49}
50
51/* Close the threads subsystem. */
52int
53__objc_close_thread_system(void)
54{
55 if (__objc_data_tls != (DWORD)-1)
56 TlsFree(__objc_data_tls);
57 return 0;
58}
59
60/* Backend thread functions */
61
62/* Create a new thread of execution. */
63objc_thread_t
64__objc_thread_detach(void (*func)(void *arg), void *arg)
65{
66 DWORD thread_id = 0;
67 HANDLE win32_handle;
68
69 if (!(win32_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func,
70 arg, 0, &thread_id)))
71 thread_id = 0;
72
73 return (objc_thread_t)thread_id;
74}
75
76/* Set the current thread's priority. */
77int
78__objc_thread_set_priority(int priority)
79{
80 int sys_priority = 0;
81
82 switch (priority)
83 {
84 case OBJC_THREAD_INTERACTIVE_PRIORITY:
85 sys_priority = THREAD_PRIORITY_NORMAL;
86 break;
87 default:
88 case OBJC_THREAD_BACKGROUND_PRIORITY:
89 sys_priority = THREAD_PRIORITY_BELOW_NORMAL;
90 break;
91 case OBJC_THREAD_LOW_PRIORITY:
92 sys_priority = THREAD_PRIORITY_LOWEST;
93 break;
94 }
95
96 /* Change priority */
97 if (SetThreadPriority(GetCurrentThread(), sys_priority))
98 return 0;
99 else
100 return -1;
101}
102
103/* Return the current thread's priority. */
104int
105__objc_thread_get_priority(void)
106{
107 int sys_priority;
108
109 sys_priority = GetThreadPriority(GetCurrentThread());
110
111 switch (sys_priority)
112 {
113 case THREAD_PRIORITY_HIGHEST:
114 case THREAD_PRIORITY_TIME_CRITICAL:
115 case THREAD_PRIORITY_ABOVE_NORMAL:
116 case THREAD_PRIORITY_NORMAL:
117 return OBJC_THREAD_INTERACTIVE_PRIORITY;
118
119 default:
120 case THREAD_PRIORITY_BELOW_NORMAL:
121 return OBJC_THREAD_BACKGROUND_PRIORITY;
122
123 case THREAD_PRIORITY_IDLE:
124 case THREAD_PRIORITY_LOWEST:
125 return OBJC_THREAD_LOW_PRIORITY;
126 }
127
128 /* Couldn't get priority. */
129 return -1;
130}
131
132/* Yield our process time to another thread. */
133void
134__objc_thread_yield(void)
135{
136 Sleep(0);
137}
138
139/* Terminate the current thread. */
140int
141__objc_thread_exit(void)
142{
143 /* exit the thread */
144 ExitThread(__objc_thread_exit_status);
145
146 /* Failed if we reached here */
147 return -1;
148}
149
150/* Returns an integer value which uniquely describes a thread. */
151objc_thread_t
152__objc_thread_id(void)
153{
154 return (objc_thread_t)GetCurrentThreadId();