Subversion Repositories shark

Rev

Details | 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
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
11
 *   Luca Abeni          <luca@gandalf.sssup.it>
12
 *   (see the web pages for full authors list)
13
 *
14
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
15
 *
16
 * http://www.sssup.it
17
 * http://retis.sssup.it
18
 * http://shark.sssup.it
19
 */
20
 
21
/**
22
 ------------
23
 CVS :        $Id: pthread.h,v 1.1.1.1 2002-03-29 14:12:51 pj Exp $
24
 
25
 File:        $File$
26
 Revision:    $Revision: 1.1.1.1 $
27
 Last update: $Date: 2002-03-29 14:12:51 $
28
 ------------
29
 
30
 pthread.h
31
 
32
 the commented functions are not implemented yet...
33
 
34
**/
35
 
36
/*
37
 * Copyright (C) 2000 Paolo Gai
38
 *
39
 * This program is free software; you can redistribute it and/or modify
40
 * it under the terms of the GNU General Public License as published by
41
 * the Free Software Foundation; either version 2 of the License, or
42
 * (at your option) any later version.
43
 *
44
 * This program is distributed in the hope that it will be useful,
45
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
47
 * GNU General Public License for more details.
48
 *
49
 * You should have received a copy of the GNU General Public License
50
 * along with this program; if not, write to the Free Software
51
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
52
 *
53
 */
54
 
55
#ifndef __PTHREAD_H__
56
#define __PTHREAD_H__
57
 
58
#include <kernel/const.h>
59
#include <sys/types.h>
60
#include <signal.h>
61
#include <errno.h>
62
#include <kernel/func.h>
63
 
64
void PTHREAD_register_module(LEVEL sched, RLEVEL pi, RLEVEL pc);
65
 
66
#define PTHREAD_CANCELED                TASK_CANCELED
67
#define PTHREAD_CANCEL_ASYNCHRONOUS     TASK_CANCEL_ASYNCHRONOUS 
68
#define PTHREAD_CANCEL_DEFERRED         TASK_CANCEL_DEFERRED     
69
#define PTHREAD_CANCEL_DISABLE          TASK_CANCEL_DISABLE 
70
#define PTHREAD_CANCEL_ENABLE           TASK_CANCEL_ENABLE
71
#define PTHREAD_COND_INITIALIZER  {NIL,NULL}
72
#define PTHREAD_CREATE_DETACHED     0
73
#define PTHREAD_CREATE_JOINABLE     1
74
#define PTHREAD_EXPLICIT_SCHED      0
75
#define PTHREAD_INHERIT_SCHED       1
76
#define PTHREAD_MUTEX_INITIALIZER {0,(void *)NULL}
77
#define PTHREAD_ONCE_INIT           0
78
#define PTHREAD_PRIO_INHERIT        1
79
#define PTHREAD_PRIO_NONE           0
80
#define PTHREAD_PRIO_PROTECT        2
81
// NRQ for PSE52 #define PTHREAD_PROCESS_PRIVATE
82
// NRQ for PSE52 #define PTHREAD_PROCESS_SHARED
83
 
84
/* there is no difference in PSE52!!! (only one process...) */
85
#define PTHREAD_SCOPE_PROCESS 0
86
#define PTHREAD_SCOPE_SYSTEM  0
87
 
88
// NRQ for PSE52 pthread_atfork
89
int pthread_attr_destroy(pthread_attr_t *attr);
90
int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
91
int pthread_attr_getinheritsched(pthread_attr_t *attr, int *inheritsched);
92
int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param);
93
int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);
94
int pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope);
95
int pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr);
96
int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
97
int pthread_attr_init(pthread_attr_t *attr);
98
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
99
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);
100
int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param);
101
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policyt);
102
int pthread_attr_setscope(pthread_attr_t *attr, int *contentionscope);
103
int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr);
104
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
105
int pthread_cancel(pthread_t thread);
106
void pthread_cleanup_push(void (*routine)(void *), void *arg);
107
void pthread_cleanup_pop(int execute);
108
int pthread_cond_broadcast(pthread_cond_t *cond);
109
int pthread_cond_destroy(pthread_cond_t *cond);
110
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
111
int pthread_cond_signal(pthread_cond_t *cond);
112
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
113
                           const struct timespec *abstime);
114
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
115
int pthread_condattr_destroy(pthread_condattr_t *attr);
116
// NRQ for PSE52 pthread_condattr_getpshared
117
int pthread_condattr_init(pthread_condattr_t *attr);
118
// NRQ for PSE52 pthread_condattr_setpshared
119
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
120
                   void *(*start_routine)(void *), void *arg);
121
int pthread_detach(pthread_t thread);
122
int pthread_equal(pthread_t t1, pthread_t t2);
123
void pthread_exit(void *value_ptr);
124
void *pthread_getspecific(pthread_key_t key);
125
int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param);
126
int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param);
127
int pthread_join(pthread_t thread, void **value_ptr);
128
int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));
129
int pthread_key_delete(pthread_key_t key);
130
int pthread_kill(PID p, int signo);
131
int pthread_mutex_destroy(pthread_mutex_t *mutex);
132
int pthread_mutex_getprioceiling(const pthread_mutex_t *mutex, int *prioceiling);
133
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
134
int pthread_mutex_lock(pthread_mutex_t *mutex);
135
int pthread_mutex_setprioceiling(pthread_mutex_t *mutex, int prioceiling, int *old_ceiling);
136
int pthread_mutex_trylock(pthread_mutex_t *mutex);
137
int pthread_mutex_unlock(pthread_mutex_t *mutex);
138
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
139
int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *prioceiling);
140
int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int *protocol);
141
// NRQ for PSE52 pthread_mutexattr_getpshared
142
int pthread_mutexattr_init(pthread_mutexattr_t *attr);
143
int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling);
144
int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol);
145
// NRQ for PSE52 pthread_mutexattr_setpshared
146
int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));
147
pthread_t pthread_self(void);
148
int pthread_setcancelstate(int state, int *oldstate);
149
int pthread_setcanceltype(int type, int *oldtype);
150
int pthread_setspecific(pthread_key_t key, const void *value);
151
int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset);
152
void pthread_testcancel(void);
153
 
154
 
155
/* Some of the pthread functions are implemented inline */
156
 
157
/*---------------------------------------------------------------------*/
158
/* 3.3.5 Examine and Change Blocked Signals                            */
159
/*---------------------------------------------------------------------*/
160
extern __inline__ int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
161
{
162
  return task_sigmask(how,set,oset);
163
}
164
 
165
/*---------------------------------------------------------------------*/
166
/* 3.3.10 Send a signal to a thread                                    */
167
/*---------------------------------------------------------------------*/
168
extern __inline__ int pthread_kill(PID p, int signo)
169
{
170
  return task_signal(p,signo);
171
}
172
 
173
/*---------------------------------------------------------------------*/
174
/* 11.3.1 Mutex Initialization Attributes                              */
175
/*---------------------------------------------------------------------*/
176
extern __inline__ int pthread_mutexattr_init(pthread_mutexattr_t *attr)
177
{
178
  attr->protocol    = PTHREAD_PRIO_NONE;
179
  attr->prioceiling = sched_get_priority_min(SCHED_RR);
180
 
181
  return 0;
182
}
183
 
184
extern __inline__ int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
185
{
186
  // nothing!!!
187
  return 0;
188
}
189
 
190
 
191
/*---------------------------------------------------------------------*/
192
/* 11.3.2 Initializing and Destroying a Mutex                          */
193
/*---------------------------------------------------------------------*/
194
 
195
extern __inline__ int pthread_mutex_destroy(pthread_mutex_t *mutex)
196
{
197
  return mutex_destroy(mutex);
198
}
199
 
200
 
201
/*---------------------------------------------------------------------*/
202
/* 11.3.2 Locking and Unlocking a mutex                                */
203
/*---------------------------------------------------------------------*/
204
 
205
extern __inline__ int pthread_mutex_lock(pthread_mutex_t *mutex)
206
{
207
  return mutex_lock(mutex);
208
}
209
 
210
extern __inline__ int pthread_mutex_trylock(pthread_mutex_t *mutex)
211
{
212
  return mutex_trylock(mutex);
213
}
214
 
215
extern __inline__ int pthread_mutex_unlock(pthread_mutex_t *mutex)
216
{
217
  return mutex_unlock(mutex);
218
}
219
 
220
/*---------------------------------------------------------------------*/
221
/* 11.4.1 Condition Variable initialization Attributes                 */
222
/*---------------------------------------------------------------------*/
223
 
224
extern __inline__ int pthread_condattr_init(pthread_condattr_t *attr)
225
{
226
  return 0;
227
}
228
 
229
extern __inline__ int pthread_condattr_destroy(pthread_condattr_t *attr)
230
{
231
  return 0;
232
}
233
 
234
/*---------------------------------------------------------------------*/
235
/* 11.4.2 Initializing and Destroying Condition Variables              */
236
/*---------------------------------------------------------------------*/
237
 
238
extern __inline__ int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
239
{
240
  return cond_init(cond);
241
}
242
 
243
extern __inline__ int pthread_cond_destroy(pthread_cond_t *cond)
244
{
245
  return cond_destroy(cond);
246
}
247
 
248
/*---------------------------------------------------------------------*/
249
/* 11.4.3 Broadcasting and Signaling a Condition                       */
250
/*---------------------------------------------------------------------*/
251
 
252
extern __inline__ int pthread_cond_broadcast(pthread_cond_t *cond)
253
{
254
  return cond_broadcast(cond);
255
}
256
 
257
extern __inline__ int pthread_cond_signal(pthread_cond_t *cond)
258
{
259
  return cond_signal(cond);
260
}
261
 
262
/*---------------------------------------------------------------------*/
263
/* 11.4.4 Waiting on a Condition                                       */
264
/*---------------------------------------------------------------------*/
265
 
266
extern __inline__ int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
267
{
268
  return cond_wait(cond, mutex);
269
}
270
 
271
extern __inline__ int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
272
                           const struct timespec *abstime)
273
{
274
  return cond_timedwait(cond, mutex, abstime);
275
}
276
 
277
 
278
/*---------------------------------------------------------------------*/
279
/* 13.5.1 Thread Creation Scheduling Attributes                        */
280
/*---------------------------------------------------------------------*/
281
extern __inline__ int pthread_attr_getinheritsched(pthread_attr_t *attr, int *inheritsched)
282
{
283
  *inheritsched = attr->inheritsched;
284
  return 0;
285
}
286
 
287
extern __inline__ int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
288
{
289
  param->sched_priority = attr->schedparam.sched_priority;
290
  return 0;
291
}
292
 
293
extern __inline__ int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
294
{
295
  *policy = attr->schedpolicy;
296
  return 0;
297
}
298
 
299
extern __inline__ int pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope)
300
{
301
  *contentionscope = attr->contentionscope;
302
  return 0;
303
}
304
 
305
extern __inline__ int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched)
306
{
307
  if (inheritsched != PTHREAD_INHERIT_SCHED &&
308
      inheritsched != PTHREAD_EXPLICIT_SCHED   ) return (EINVAL);
309
 
310
  attr->inheritsched = inheritsched;
311
  return 0;
312
}
313
 
314
extern __inline__ int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
315
{
316
  // ehm... no controls :-)
317
  attr->schedparam = *param;
318
  return 0;
319
}
320
 
321
extern __inline__ int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
322
{
323
  if (policy != SCHED_RR && policy != SCHED_FIFO && policy != SCHED_OTHER)
324
    return (EINVAL);
325
 
326
  attr->schedpolicy = policy;
327
  return 0;
328
}
329
 
330
extern __inline__ int pthread_attr_setscope(pthread_attr_t *attr, int *contentionscope)
331
{
332
  if (contentionscope != PTHREAD_SCOPE_SYSTEM &&
333
      contentionscope != PTHREAD_SCOPE_PROCESS   ) return (EINVAL);
334
 
335
  attr->contentionscope = *contentionscope;
336
  return 0;
337
}
338
 
339
/*---------------------------------------------------------------------*/
340
/* 13.6.1 Mutex Initialization Scheduling Attributes                   */
341
/*---------------------------------------------------------------------*/
342
 
343
extern __inline__ int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *prioceiling)
344
{
345
  *prioceiling = attr->prioceiling;
346
  return 0;
347
}
348
 
349
extern __inline__ int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int *protocol)
350
{
351
  *protocol = attr->protocol;
352
  return 0;
353
}
354
 
355
extern __inline__ int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling)
356
{
357
  if (prioceiling < sched_get_priority_min(SCHED_RR) ||
358
      prioceiling > sched_get_priority_max(SCHED_RR)   ) {
359
    return EINVAL;
360
  }
361
 
362
  attr->prioceiling = prioceiling;
363
  return 0;
364
}
365
 
366
extern __inline__ int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol)
367
{
368
  if (protocol != PTHREAD_PRIO_NONE &&
369
      protocol != PTHREAD_PRIO_INHERIT &&
370
      protocol != PTHREAD_PRIO_PROTECT) {
371
    return EINVAL;
372
  }
373
 
374
  attr->protocol = protocol;
375
  return 0;
376
}
377
 
378
/*---------------------------------------------------------------------*/
379
/* 16.2.1 Thread Creation attributes                                   */
380
/*---------------------------------------------------------------------*/
381
 
382
extern __inline__ int pthread_attr_destroy(pthread_attr_t *attr)
383
{
384
  // nothing!!!
385
  return 0;
386
}
387
 
388
extern __inline__ int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
389
{
390
  *detachstate = attr->detachstate;
391
  return 0;
392
}
393
 
394
extern __inline__ int pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
395
{
396
  *stackaddr = attr->stackaddr;
397
  return 0;
398
}
399
 
400
extern __inline__ int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
401
{
402
  *stacksize = attr->stacksize;
403
  return 0;
404
}
405
 
406
extern __inline__ int pthread_attr_init(pthread_attr_t *attr)
407
{
408
  // note that the contention scope is not used, because the behaviour
409
  // is the same...
410
  attr->contentionscope           = PTHREAD_SCOPE_PROCESS;
411
  attr->inheritsched              = PTHREAD_EXPLICIT_SCHED;
412
  attr->schedpolicy               = SCHED_RR;
413
  attr->schedparam.sched_priority = sched_get_priority_min(SCHED_RR);
414
  attr->detachstate               = PTHREAD_CREATE_JOINABLE;
415
 
416
  attr->stacksize                 = STACK_SIZE;
417
  attr->stackaddr                 = NULL;
418
 
419
  return 0;
420
}
421
 
422
extern __inline__ int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
423
{
424
  if (detachstate != PTHREAD_CREATE_JOINABLE &&
425
      detachstate != PTHREAD_CREATE_DETACHED   ) return (EINVAL);
426
 
427
  attr->detachstate = detachstate;
428
  return 0;
429
}
430
 
431
extern __inline__ int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
432
{
433
  attr->stackaddr = stackaddr;
434
  return 0;
435
}
436
 
437
extern __inline__ int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
438
{
439
  if (stacksize < STACK_SIZE) return (EINVAL);
440
 
441
  attr->stacksize = stacksize;
442
  return 0;
443
}
444
 
445
/*---------------------------------------------------------------------*/
446
/* 16.2.3 Wait for thread termination                                  */
447
/*---------------------------------------------------------------------*/
448
 
449
extern __inline__ int pthread_join(pthread_t thread, void **value_ptr)
450
{
451
  return task_join(thread, value_ptr);
452
}
453
 
454
/*---------------------------------------------------------------------*/
455
/* 16.2.4 Detaching a thread                                           */
456
/*---------------------------------------------------------------------*/
457
 
458
extern __inline__ int pthread_detach(pthread_t thread)
459
{
460
  return task_unjoinable(thread);
461
}
462
 
463
/*---------------------------------------------------------------------*/
464
/* 16.2.5 Thread Termination                                           */
465
/*---------------------------------------------------------------------*/
466
 
467
extern __inline__ void pthread_exit(void *value_ptr)
468
{
469
  task_abort(value_ptr);
470
}
471
 
472
/*---------------------------------------------------------------------*/
473
/* 16.2.6 Get thread ID                                                */
474
/*---------------------------------------------------------------------*/
475
 
476
extern __inline__ pthread_t pthread_self(void)
477
{
478
  return exec_shadow;
479
}
480
 
481
/*---------------------------------------------------------------------*/
482
/* 16.2.7 Compare Threads ID                                           */
483
/*---------------------------------------------------------------------*/
484
 
485
extern __inline__ int pthread_equal(pthread_t t1, pthread_t t2)
486
{
487
  return t1 == t2;
488
}
489
 
490
 
491
/*---------------------------------------------------------------------*/
492
/* 17.1.1 Thread specific data key creation                            */
493
/*---------------------------------------------------------------------*/
494
 
495
extern __inline__ int pthread_key_create(pthread_key_t *key, void (*destructor)(void *))
496
{
497
  return task_key_create(key,destructor);
498
}
499
 
500
/*---------------------------------------------------------------------*/
501
/* 17.1.2 Thread Specific Data Management                              */
502
/*---------------------------------------------------------------------*/
503
 
504
extern __inline__ void *pthread_getspecific(pthread_key_t key)
505
{
506
  return task_getspecific(key);
507
}
508
 
509
extern __inline__ int pthread_setspecific(pthread_key_t key, const void *value)
510
{
511
  return task_setspecific(key,value);
512
}
513
 
514
/*---------------------------------------------------------------------*/
515
/* 17.1.3 Thread-Specific Data key deletion                            */
516
/*---------------------------------------------------------------------*/
517
 
518
extern __inline__ int pthread_key_delete(pthread_key_t key)
519
{
520
  return task_key_delete(key);
521
}
522
 
523
/*---------------------------------------------------------------------*/
524
/* 18.2.2 Setting Cancelability State                                  */
525
/*---------------------------------------------------------------------*/
526
 
527
extern __inline__ int pthread_setcancelstate(int state, int *oldstate)
528
{
529
  return task_setcancelstate(state, oldstate);
530
}
531
 
532
extern __inline__ int pthread_setcanceltype(int type, int *oldtype)
533
{
534
  return task_setcanceltype(type, oldtype);
535
}
536
 
537
extern __inline__ void pthread_testcancel(void)
538
{
539
  task_testcancel();
540
}
541
 
542
/*---------------------------------------------------------------------*/
543
/* 18.2.2 Setting Cancelability State                                  */
544
/*---------------------------------------------------------------------*/
545
 
546
#define pthread_cleanup_push task_cleanup_push
547
#define pthread_cleanup_pop  task_cleanup_pop
548
 
549
 
550
#endif
551