Subversion Repositories shark

Rev

Rev 3 | Rev 318 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/**
20
 ------------
271 giacomo 21
 CVS :        $Id: pthread.c,v 1.2 2003-10-07 09:25:33 giacomo Exp $
2 pj 22
 
23
 File:        $File$
271 giacomo 24
 Revision:    $Revision: 1.2 $
25
 Last update: $Date: 2003-10-07 09:25:33 $
2 pj 26
 ------------
27
 
28
 pthread.c
29
 
30
 This file contains the implementation of various posix primitives:
31
 
32
 refer to POSIX standard...
33
 
34
 maybe in the near future this functions will become inline functions...
35
 
36
**/
37
 
38
/*
39
 * Copyright (C) 2000 Paolo Gai
40
 *
41
 * This program is free software; you can redistribute it and/or modify
42
 * it under the terms of the GNU General Public License as published by
43
 * the Free Software Foundation; either version 2 of the License, or
44
 * (at your option) any later version.
45
 *
46
 * This program is distributed in the hope that it will be useful,
47
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
48
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
49
 * GNU General Public License for more details.
50
 *
51
 * You should have received a copy of the GNU General Public License
52
 * along with this program; if not, write to the Free Software
53
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
54
 *
55
 */
56
 
57
#include <kernel/const.h>
58
#include <kernel/func.h>
59
#include <pthread.h>
60
#include <errno.h>
61
#include <modules/pi.h>
62
#include <modules/pc.h>
63
#include <modules/posix.h>
64
 
65
// this fields are used to store the levels used in the pthread_functions...
66
static LEVEL level_sched;
67
static RLEVEL level_PI;
68
static RLEVEL level_PC;
69
 
70
// This function register the POSIX interface into the system...
71
void PTHREAD_register_module(LEVEL sched, RLEVEL pi, RLEVEL pc)
72
{
73
  level_sched = sched;
74
  level_PI    = pi;
75
  level_PC    = pc;
76
}
77
 
78
 
79
/*---------------------------------------------------------------------*/
80
/* 11.3.2 Initializing and Destroying a Mutex                          */
81
/*---------------------------------------------------------------------*/
82
 
83
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
84
{
85
  if (!attr || attr->protocol == PTHREAD_PRIO_NONE) {
86
    NOP_mutexattr_t m_nop;
87
 
88
    NOP_mutexattr_default(m_nop);
89
    return mutex_init(mutex,&m_nop);
90
  }
91
  else if (attr->protocol == PTHREAD_PRIO_INHERIT) {
92
    PI_mutexattr_t m_pi;
93
 
94
    PI_mutexattr_default(m_pi);
95
    return mutex_init(mutex,&m_pi);
96
  }
97
  else {   // PTHREAD_PRIO_PROTECT
98
    PC_mutexattr_t m_pc;
99
 
100
    PC_mutexattr_default(m_pc, sched_get_priority_max(SCHED_RR)-attr->prioceiling);
101
    return mutex_init(mutex,(mutexattr_t *)&m_pc);
102
  }
103
}
104
 
105
 
106
 
107
/*---------------------------------------------------------------------*/
108
/* 13.3.5 Yield processor                                              */
109
/*---------------------------------------------------------------------*/
110
 
111
int sched_yield(void)
112
{
113
  return POSIX_sched_yield(level_sched);
114
}
115
 
116
/*---------------------------------------------------------------------*/
117
/* 13.3.6 Get Scheduling Parameter Limits                              */
118
/*---------------------------------------------------------------------*/
119
 
120
int sched_get_priority_max(int policy)
121
{
122
  return POSIX_get_priority_max(level_sched);
123
}
124
 
125
int sched_rr_get_interval(pid_t pid, struct timespec *interval)
126
{
127
  return POSIX_rr_get_interval(level_sched);
128
}
129
 
130
/*---------------------------------------------------------------------*/
131
/* 13.5.2 Dynamic Thread Schedu ling Parameters Access                  */
132
/*---------------------------------------------------------------------*/
133
 
134
int pthread_getschedparam(pthread_t thread, int *policy,
135
                          struct sched_param *param)
136
{
137
  int returnvalue;
138
  kern_cli();
139
  returnvalue = POSIX_getschedparam(level_sched, thread, policy,
140
                                    &param->sched_priority);
141
  kern_sti();
142
  return returnvalue;
143
}
144
 
145
int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param)
146
{
147
  int returnvalue;
148
  kern_cli();
149
 
150
  returnvalue = POSIX_setschedparam(level_sched, thread, policy,
151
                                    param->sched_priority);
152
 
153
  if (!returnvalue)
154
    PC_set_task_ceiling(level_PC, thread,
155
                        sched_get_priority_max(SCHED_RR) -
156
                          param->sched_priority);
157
 
158
  kern_sti();
159
  return returnvalue;
160
}
161
 
162
 
163
/*---------------------------------------------------------------------*/
164
/* 13.6.2 Change the priority Ceiling of a Mutex                       */
165
/*---------------------------------------------------------------------*/
166
 
167
int pthread_mutex_setprioceiling(pthread_mutex_t *mutex, int prioceiling,
168
                                 int *old_ceiling)
169
{
170
  int returnvalue = 0;
171
  int max = sched_get_priority_max(SCHED_RR);
172
 
173
  if (prioceiling > max || prioceiling < sched_get_priority_min(SCHED_RR))
174
    return EINVAL;
175
 
176
  mutex_lock(mutex);
177
  if (PC_set_mutex_ceiling(mutex,
178
                           max - prioceiling,
179
                           (DWORD *)old_ceiling))
180
    returnvalue = EINVAL;
181
 
182
  if (old_ceiling)
183
    *old_ceiling = max - *old_ceiling;
184
 
185
  mutex_unlock(mutex);
186
 
187
  return returnvalue;
188
}
189
 
190
int pthread_mutex_getprioceiling(const pthread_mutex_t *mutex,
191
                                 int *prioceiling)
192
{
193
  PC_get_mutex_ceiling(mutex, (DWORD *)prioceiling);
194
  *prioceiling = sched_get_priority_max(SCHED_RR) - *prioceiling;
195
 
196
  return 0;
197
}
198
 
199
/*---------------------------------------------------------------------*/
200
/* 16.2.2 Thread Creation                                              */
201
/*---------------------------------------------------------------------*/
202
 
203
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
204
                   void *(*start_routine)(void *), void *arg)
205
{
206
  NRT_TASK_MODEL m;   // the scheduling model
207
  PC_RES_MODEL   pc;  // the resource model used for Priority Ceiling
208
  int olderrno;       // the pthread_create don't modify errno
209
 
210
  nrt_task_default_model(m);
211
  nrt_task_def_arg(m,arg);
212
  nrt_task_def_usemath(m);
213
  nrt_task_def_ctrl_jet(m);
214
  nrt_task_def_save_arrivals(m);
271 giacomo 215
  nrt_task_def_level(m, level_sched);
2 pj 216
 
217
  if (attr) {
218
    nrt_task_def_stack(m,attr->stacksize);
219
    nrt_task_def_stackaddr(m,attr->stackaddr);
220
    if (attr->detachstate == PTHREAD_CREATE_JOINABLE)
221
      nrt_task_def_joinable(m);
222
    else
223
      nrt_task_def_unjoinable(m);
224
 
225
    nrt_task_def_weight(m, attr->schedparam.sched_priority);
226
    PC_res_default_model(pc, sched_get_priority_max(SCHED_RR) - attr->schedparam.sched_priority);
227
 
228
    nrt_task_def_policy(m,attr->schedpolicy);
229
    nrt_task_def_inherit(m,attr->inheritsched);
230
  }
231
  else {
232
    nrt_task_def_stack(m,PTHREAD_STACK_MIN);
233
    nrt_task_def_stackaddr(m,NULL);
234
    nrt_task_def_joinable(m);
235
 
236
    nrt_task_def_weight(m, sched_get_priority_min(SCHED_RR));
237
    PC_res_default_model(pc, sched_get_priority_max(SCHED_RR));
238
 
239
    nrt_task_def_policy(m,SCHED_RR);
240
    nrt_task_def_inherit(m,PTHREAD_EXPLICIT_SCHED);
241
  }
242
 
243
  olderrno = errno;
244
  *thread = task_createn("Posix task", start_routine,
245
                                       (TASK_MODEL *)&m,
246
                                       &pc, NULL);
247
  if (*thread != NIL)
248
    task_activate(*thread);
249
 
250
  errno = olderrno;
251
 
252
  if (*thread == NIL)
253
    return EINVAL;
254
  else
255
    return 0;
256
}
257
/*---------------------------------------------------------------------*/
258
/* 16.2.8 Dynamic Package initialization                               */
259
/*---------------------------------------------------------------------*/
260
 
261
int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
262
{
263
  kern_cli();
264
  if (!(*once_control)) {
265
    *once_control = 1;
266
    kern_sti();
267
    init_routine();
268
  }
269
  else
270
    kern_sti();
271
  return 0;
272
}
273
 
274
 
275
 
276
/*---------------------------------------------------------------------*/
277
/* 18.2.1 Canceling Execution of a Thread                              */
278
/*---------------------------------------------------------------------*/
279
 
280
int pthread_cancel(pthread_t thread)
281
{
282
   int olderrno; // pthread_cancel doesn't modify errno
283
   int rvalue;   // the return value
284
 
285
   olderrno = errno;
286
   rvalue = task_kill(thread);
287
   errno = olderrno;
288
 
289
   if (rvalue)
290
     return (ESRCH);
291
   else
292
     return 0;
293
}
294
 
295
 
296