source: branches/libc-0.6/src/gcc/libobjc/thr-posix.c@ 2777

Last change on this file since 2777 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: 7.5 KB
Line 
1/* GNU Objective C Runtime Thread Interface for POSIX compliant threads
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Galen C. Hunt ([email protected])
4 Modified for Linux/Pthreads by Kai-Uwe Sattler ([email protected])
5 Modified for posix compliance by Chris Ball ([email protected])
6
7This file is part of GNU CC.
8
9GNU CC is free software; you can redistribute it and/or modify it under the
10terms of the GNU General Public License as published by the Free Software
11Foundation; either version 2, or (at your option) any later version.
12
13GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING. If not, write to
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
22
23/* As a special exception, if you link this library with files compiled with
24 GCC to produce an executable, this does not cause the resulting executable
25 to be covered by the GNU General Public License. This exception does not
26 however invalidate any other reasons why the executable file might be
27 covered by the GNU General Public License. */
28
29#include <objc/thr.h>
30#include "runtime.h"
31#include <pthread.h>
32
33/* Key structure for maintaining thread specific storage */
34static pthread_key_t _objc_thread_storage;
35static pthread_attr_t _objc_thread_attribs;
36
37/* Backend initialization functions */
38
39/* Initialize the threads subsystem. */
40int
41__objc_init_thread_system(void)
42{
43 /* Initialize the thread storage key */
44 if (pthread_key_create(&_objc_thread_storage, NULL) == 0)
45 {
46 /*
47 * The normal default detach state for threads is PTHREAD_CREATE_JOINABLE
48 * which causes threads to not die when you think they should.
49 */
50 if (pthread_attr_init(&_objc_thread_attribs) == 0)
51 {
52 if (pthread_attr_setdetachstate(&_objc_thread_attribs,
53 PTHREAD_CREATE_DETACHED) == 0)
54 return 0;
55 }
56 }
57
58 return -1;
59}
60
61/* Close the threads subsystem. */
62int
63__objc_close_thread_system(void)
64{
65 if (pthread_key_delete(_objc_thread_storage) == 0)
66 {
67 if (pthread_attr_destroy(&_objc_thread_attribs) == 0)
68 return 0;
69 }
70
71 return -1;
72}
73
74/* Backend thread functions */
75
76/* Create a new thread of execution. */
77objc_thread_t
78__objc_thread_detach(void (*func)(void *arg), void *arg)
79{
80 objc_thread_t thread_id;
81 pthread_t new_thread_handle;
82
83 if (!(pthread_create(&new_thread_handle, &_objc_thread_attribs,
84 (void *)func, arg)))
85 thread_id = *(objc_thread_t *)&new_thread_handle;
86 else
87 thread_id = NULL;
88
89 return thread_id;
90}
91
92/* Set the current thread's priority.
93 *
94 * Be aware that the default schedpolicy often disallows thread priorities.
95 */
96int
97__objc_thread_set_priority(int priority)
98{
99 pthread_t thread_id = pthread_self();
100 int policy;
101 struct sched_param params;
102 int priority_min, priority_max;
103
104 if (pthread_getschedparam(thread_id, &policy, &params) == 0)
105 {
106 if ((priority_max = sched_get_priority_max(policy)) != 0)
107 return -1;
108
109 if ((priority_min = sched_get_priority_min(policy)) != 0)
110 return -1;
111
112 if (priority > priority_max)
113 priority = priority_max;
114 else if (priority < priority_min)
115 priority = priority_min;
116 params.sched_priority = priority;
117
118 /*
119 * The solaris 7 and several other man pages incorrectly state that
120 * this should be a pointer to policy but pthread.h is universally
121 * at odds with this.
122 */
123 if (pthread_setschedparam(thread_id, policy, &params) == 0)
124 return 0;
125 }