| 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 |
|
|---|
| 5 | This file is part of GNU CC.
|
|---|
| 6 |
|
|---|
| 7 | GNU CC is free software; you can redistribute it and/or modify it under the
|
|---|
| 8 | terms of the GNU General Public License as published by the Free Software
|
|---|
| 9 | Foundation; either version 2, or (at your option) any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
|
|---|
| 12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|---|
| 13 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|---|
| 14 | details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License along with
|
|---|
| 17 | GNU CC; see the file COPYING. If not, write to the Free Software
|
|---|
| 18 | Foundation, 59 Temple Place - Suite 330,
|
|---|
| 19 | Boston, 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 */
|
|---|
| 36 | static DWORD __objc_data_tls = (DWORD)-1;
|
|---|
| 37 |
|
|---|
| 38 | /* Backend initialization functions */
|
|---|
| 39 |
|
|---|
| 40 | /* Initialize the threads subsystem. */
|
|---|
| 41 | int
|
|---|
| 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. */
|
|---|
| 52 | int
|
|---|
| 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. */
|
|---|
| 63 | objc_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. */
|
|---|
| 77 | int
|
|---|
| 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. */
|
|---|
| 104 | int
|
|---|
| 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. */
|
|---|
| 133 | void
|
|---|
| 134 | __objc_thread_yield(void)
|
|---|
| 135 | {
|
|---|
| 136 | Sleep(0);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /* Terminate the current thread. */
|
|---|
| 140 | int
|
|---|
| 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. */
|
|---|
| 151 | objc_thread_t
|
|---|
| 152 | __objc_thread_id(void)
|
|---|
| 153 | {
|
|---|
| 154 | return (objc_thread_t)GetCurrentThreadId();
|
|---|
|
|---|