1/* GNU Objective C Runtime Thread Interface - OS/2 emx Implementation
2   Copyright (C) 1996, 1997, 2009 Free Software Foundation, Inc.
3   Contributed by Thomas Baier (baier@ci.tuwien.ac.at)
4
5This file is part of GCC.
6
7GCC 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 3, or (at your option) any later version.
10
11GCC 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
16Under Section 7 of GPL version 3, you are granted additional
17permissions described in the GCC Runtime Library Exception, version
183.1, as published by the Free Software Foundation.
19
20You should have received a copy of the GNU General Public License and
21a copy of the GCC Runtime Library Exception along with this program;
22see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23<http://www.gnu.org/licenses/>.  */
24
25
26#include "objc/thr.h"
27#include "objc/runtime.h"
28
29#define INCL_DOSSEMAPHORES
30#define INCL_DOSPROCESS
31
32/*
33 * conflicts with objc.h:       SEL, BOOL, id
34 * solution:  prefixing those with _OS2_ before including <os2.h>
35 */
36#define SEL _OS2_SEL
37#define BOOL _OS2_BOOL
38#define id _OS2_id
39#include <os2.h>
40#undef id
41#undef SEL
42#undef BOOL
43
44#include <stdlib.h>
45
46/* Backend initialization functions */
47
48/* Initialize the threads subsystem. */
49int
50__objc_init_thread_system(void)
51{
52  return 0;
53}
54
55/* Close the threads subsystem. */
56int
57__objc_close_thread_system(void)
58{
59  return 0;
60}
61
62/* Backend thread functions */
63
64/* Create a new thread of execution. */
65objc_thread_t
66__objc_thread_detach(void (*func)(void *arg), void *arg)
67{
68  int thread_id = 0;
69
70  if ((thread_id = _beginthread (func,NULL,32768,arg)) < 0)
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  ULONG sys_class = 0;
81  ULONG sys_priority = 0;
82
83  /* OBJC_THREAD_INTERACTIVE_PRIORITY -> PRTYC_FOREGROUNDSERVER
84   * OBJC_THREAD_BACKGROUND_PRIORITY  -> PRTYC_REGULAR
85   * OBJC_THREAD_LOW_PRIORITY         -> PRTYC_IDLETIME */
86
87  switch (priority) {
88  case OBJC_THREAD_INTERACTIVE_PRIORITY:
89    sys_class = PRTYC_REGULAR;
90    sys_priority = 10;
91    break;
92  default:
93  case OBJC_THREAD_BACKGROUND_PRIORITY:
94    sys_class = PRTYC_IDLETIME;
95    sys_priority = 25;
96    break;
97  case OBJC_THREAD_LOW_PRIORITY:
98    sys_class = PRTYC_IDLETIME;
99    sys_priority = 0;
100    break;
101  }
102
103  /* Change priority */
104  if (!DosSetPriority (PRTYS_THREAD,sys_class,sys_priority,*_threadid))
105    return 0;
106  else
107    return -1;
108}
109
110/* Return the current thread's priority. */
111int
112__objc_thread_get_priority(void)
113{
114  PTIB ptib;
115  PPIB ppib;
116
117  /* get information about current thread */
118  DosGetInfoBlocks (&ptib,&ppib);
119
120  switch (ptib->tib_ptib2->tib2_ulpri)
121    {
122    case PRTYC_IDLETIME:
123    case PRTYC_REGULAR:
124    case PRTYC_TIMECRITICAL:
125    case PRTYC_FOREGROUNDSERVER:
126    default:
127      return OBJC_THREAD_INTERACTIVE_PRIORITY;
128    }
129
130  return -1;
131}
132
133/* Yield our process time to another thread. */
134void
135__objc_thread_yield(void)
136{
137  DosSleep (0);
138}
139
140/* Terminate the current thread. */
141int
142__objc_thread_exit(void)
143{
144  /* terminate the thread, NEVER use DosExit () */
145  _endthread ();
146
147  /* Failed if we reached here */
148  return -1;
149}
150
151/* Returns an integer value which uniquely describes a thread. */
152objc_thread_t
153__objc_thread_id(void)
154{
155  return (objc_thread_t) *_threadid;
156}
157
158/* Sets the thread's local storage pointer. */
159int
160__objc_thread_set_data(void *value)
161{
162  *_threadstore () = value;
163
164  return 0;
165}
166
167/* Returns the thread's local storage pointer. */
168void *
169__objc_thread_get_data(void)
170{
171  return *_threadstore ();
172}
173
174/* Backend mutex functions */
175
176/* Allocate a mutex. */
177int
178__objc_mutex_allocate(objc_mutex_t mutex)
179{
180  if (DosCreateMutexSem (NULL, (HMTX)(&(mutex->backend)),0L,0) > 0)
181    return -1;
182  else
183    return 0;
184}
185
186/* Deallocate a mutex. */
187int
188__objc_mutex_deallocate(objc_mutex_t mutex)
189{
190  DosCloseMutexSem ((HMTX)(mutex->backend));
191  return 0;
192}
193
194/* Grab a lock on a mutex. */
195int
196__objc_mutex_lock(objc_mutex_t mutex)
197{
198  if (DosRequestMutexSem ((HMTX)(mutex->backend),-1L) != 0)
199    return -1;
200  else
201    return 0;
202}
203
204/* Try to grab a lock on a mutex. */
205int
206__objc_mutex_trylock(objc_mutex_t mutex)
207{
208  if (DosRequestMutexSem ((HMTX)(mutex->backend),0L) != 0)
209    return -1;
210  else
211    return 0;
212}
213
214/* Unlock the mutex */
215int
216__objc_mutex_unlock(objc_mutex_t mutex)
217{
218  if (DosReleaseMutexSem((HMTX)(mutex->backend)) != 0)
219    return -1;
220  else
221    return 0;
222}
223
224/* Backend condition mutex functions */
225
226/* Allocate a condition. */
227int
228__objc_condition_allocate(objc_condition_t condition)
229{
230  /* Unimplemented. */
231  return -1;
232}
233
234/* Deallocate a condition. */
235int
236__objc_condition_deallocate(objc_condition_t condition)
237{
238  /* Unimplemented. */
239  return -1;
240}
241
242/* Wait on the condition */
243int
244__objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
245{
246  /* Unimplemented. */
247  return -1;
248}
249
250/* Wake up all threads waiting on this condition. */
251int
252__objc_condition_broadcast(objc_condition_t condition)
253{
254  /* Unimplemented. */
255  return -1;
256}
257
258/* Wake up one thread waiting on this condition. */
259int
260__objc_condition_signal(objc_condition_t condition)
261{
262  /* Unimplemented. */
263  return -1;
264}
265
266/* End of File */
267