Subversion Repositories shark

Rev

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