home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gnat-2.06-src.tgz / tar.out / fsf / gnat / ada / threads / include / pthread.h
C/C++ Source or Header  |  1996-09-28  |  18KB  |  493 lines

  1. /* Copyright (C) 1992, the Florida State University
  2.    Distributed by the Florida State University under the terms of the
  3.    GNU Library General Public License.
  4.  
  5. This file is part of Pthreads.
  6.  
  7. Pthreads is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public
  9. License as published by the Free Software Foundation (version 2).
  10.  
  11. Pthreads is distributed "AS IS" in the hope that it will be
  12. useful, but WITHOUT ANY WARRANTY; without even the implied
  13. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. See the GNU Library General Public License for more details.
  15.  
  16. You should have received a copy of the GNU Library General Public
  17. License along with Pthreads; see the file COPYING.  If not, write
  18. to the Free Software Foundation, 675 Mass Ave, Cambridge,
  19. MA 02139, USA.
  20.  
  21. Report problems and direct all questions to:
  22.  
  23.   pthreads-bugs@ada.cs.fsu.edu
  24.  
  25.   @(#)pthread.h    2.5 4/12/95
  26.  
  27. */
  28.  
  29. #ifndef _pthread_pthread_h
  30. #define _pthread_pthread_h
  31.  
  32. /*
  33.  * Pthreads interface definition
  34.  */
  35.  
  36. #ifndef    _POSIX_THREADS
  37.  
  38. #include <pthread/config.h>
  39. #include <pthread/unistd.h>
  40. #include <pthread/signal.h>
  41. #include <stdio.h>
  42. #include <pthread/limits.h>
  43. #include <sys/time.h>            /* needed for struct timeval */
  44. #include <pthread/errno.h>
  45. #include <pthread/pthread_asm.h>
  46. #include <setjmp.h>
  47. #ifdef IO_NP
  48. #include <sys/asynch.h>
  49. #include <sys/types.h>
  50. #endif IO_NP
  51. #ifdef MALLOC_NP
  52. #include <malloc.h>
  53. #endif MALLOC_NP
  54.  
  55. /* contentionscope attribute values */
  56. #define PTHREAD_SCOPE_GLOBAL 0
  57. #define PTHREAD_SCOPE_LOCAL  1
  58.  
  59. /* delivery modes */
  60. #define PTHREAD_PROCSYNC     0
  61. #define PTHREAD_PROCASYNC    1
  62.  
  63. /* inheritsched attribute values */
  64. #define PTHREAD_INHERIT_SCHED 0
  65. #define PTHREAD_DEFAULT_SCHED 1
  66.  
  67. /* Allow variable stack sizes */
  68. #ifndef _POSIX_THREAD_ATTR_STACKSIZE
  69. #define _POSIX_THREAD_ATTR_STACKSIZE
  70. #endif
  71.  
  72. #if defined (__GNUC__) || defined(__STDC__) || defined(__cplusplus) || defined(c_plusplus)
  73.  
  74. #ifndef    _C_PROTOTYPE
  75. #define    _C_PROTOTYPE(arglist)    arglist
  76. #endif !_C_PROTOTYPE
  77.  
  78. typedef void *any_t;
  79.  
  80. #else !(defined (__GNUC__) || defined(__STDC__) || defined(__cplusplus) || defined(c_plusplus))
  81.  
  82. #ifndef    _C_PROTOTYPE
  83. #define    _C_PROTOTYPE(arglist)    ()
  84. #endif    not _C_PROTOTYPE
  85.  
  86. typedef char *any_t;
  87.  
  88. #ifndef const
  89. #define const
  90. #endif
  91.  
  92. #ifndef volatile
  93. #define volatile
  94. #endif
  95.  
  96. #endif defined(__GNUC__) || defined(__STDC__) || defined(__cplusplus) || defined(c_plusplus)
  97.  
  98. #ifdef STAND_ALONE_NP
  99. typedef char *malloc_t;
  100. extern malloc_t malloc _C_PROTOTYPE((unsigned __size));
  101. #endif STAND_ALONE_NP
  102.  
  103. #ifndef    TRUE
  104. #define    TRUE    1
  105. #define    FALSE    0
  106. #endif
  107.  
  108. #if !defined(__sys_stdtypes_h) && !defined(_SIZE_T)
  109. typedef    unsigned int    size_t;
  110. #endif
  111.  
  112. /*
  113.  * Mutex objects.
  114.  */
  115.  
  116. typedef int pthread_protocol_t;
  117.  
  118. typedef struct pthread_queue {
  119.           struct pthread *head;
  120.           struct pthread *tail;
  121. } *pthread_queue_t;
  122.  
  123. typedef struct mutex {
  124.     struct pthread_queue queue;
  125.     char lock;
  126.         struct pthread *owner;
  127.         int flags;
  128. #ifdef _POSIX_THREADS_PRIO_PROTECT
  129.         int prio_ceiling;
  130.         pthread_protocol_t protocol;
  131.     int prev_max_ceiling_prio;
  132. #endif
  133. } pthread_mutex_t;
  134.  
  135. typedef struct pthread_mutexattr {
  136.         int flags;
  137. #ifdef _POSIX_THREADS_PRIO_PROTECT
  138.         int prio_ceiling;
  139.         pthread_protocol_t protocol;
  140. #endif
  141. } pthread_mutexattr_t;
  142.  
  143. /******************************/
  144. /* Mutex Attributes Functions */
  145. /*       Mutex Functions      */
  146. /******************************/
  147.  
  148. #ifdef __cplusplus
  149. extern "C" {
  150. #endif
  151.  
  152. extern int pthread_mutex_lock     _C_PROTOTYPE((pthread_mutex_t *__mutex));
  153. extern int pthread_mutex_trylock  _C_PROTOTYPE((pthread_mutex_t *__mutex));
  154. extern int pthread_mutex_unlock   _C_PROTOTYPE((pthread_mutex_t *__mutex));
  155. extern int pthread_mutex_init     _C_PROTOTYPE((pthread_mutex_t *__mutex,
  156.                         pthread_mutexattr_t *__attr));
  157. extern int pthread_mutex_destroy  _C_PROTOTYPE((pthread_mutex_t *__mutex));
  158. extern int pthread_mutex_setprio_ceiling
  159.                                   _C_PROTOTYPE((pthread_mutex_t *__mutex,
  160.                         int __prio_ceiling));
  161. extern int pthread_mutex_getprio_ceiling
  162.                                   _C_PROTOTYPE((pthread_mutex_t __mutex));
  163. extern int pthread_mutexattr_init _C_PROTOTYPE((pthread_mutexattr_t *__attr));
  164. extern int pthread_mutexattr_destroy
  165.                                   _C_PROTOTYPE((pthread_mutexattr_t *__attr));
  166. extern int pthread_mutexattr_setprotocol
  167.                                   _C_PROTOTYPE((pthread_mutexattr_t *__attr,
  168.                         pthread_protocol_t __protocol));
  169. extern pthread_protocol_t pthread_mutexattr_getprotocol
  170.                   _C_PROTOTYPE((pthread_mutexattr_t __attr));
  171.  
  172. extern int pthread_mutexattr_setprio_ceiling
  173.                                   _C_PROTOTYPE((pthread_mutexattr_t *__attr,
  174.                         int __prio_ceiling));
  175. extern int pthread_mutexattr_getprio_ceiling
  176.                                   _C_PROTOTYPE((pthread_mutexattr_t __attr));
  177. extern int pthread_mutexattr_getpshared
  178.                                   _C_PROTOTYPE((pthread_mutexattr_t *__attr,
  179.                         int *__pshared));
  180. extern int pthread_mutexattr_setpshared
  181.                                   _C_PROTOTYPE((pthread_mutexattr_t *__attr,
  182.                         int __pshared));
  183.  
  184. #ifdef __cplusplus
  185. } /* extern "C" */
  186. #endif
  187.  
  188. /*
  189.  * Once Objects.
  190.  */
  191.  
  192. typedef    struct {
  193.     short init;
  194.     short exec;
  195.     pthread_mutex_t mutex;
  196. } pthread_once_t;
  197.  
  198. #define PTHREAD_ONCE_INIT    {FALSE,FALSE,0, };
  199.  
  200. /*
  201.  * Condition variables.
  202.  */
  203. typedef struct condition {
  204.     struct pthread_queue queue;
  205.         int flags;
  206.         int waiters;
  207.         pthread_mutex_t *mutex;
  208. } pthread_cond_t;
  209.  
  210. typedef struct condattr {
  211.         int flags;
  212. } pthread_condattr_t;
  213.  
  214. /*******************************/
  215. /*      Condition Functions    */
  216. /*Condition Attribute Functions*/
  217. /*******************************/
  218.  
  219. #ifdef __cplusplus
  220. extern "C" {
  221. #endif
  222.  
  223. extern int pthread_cond_destroy     _C_PROTOTYPE((pthread_cond_t *__cond));
  224. extern int pthread_cond_init        _C_PROTOTYPE((pthread_cond_t *__cond,
  225.                           pthread_condattr_t *__attr));
  226. extern int pthread_condattr_init    _C_PROTOTYPE((pthread_condattr_t *__attr));
  227. extern int pthread_condattr_destroy _C_PROTOTYPE((pthread_condattr_t *__attr));
  228. extern int pthread_cond_wait        _C_PROTOTYPE((pthread_cond_t *__cond,
  229.                           pthread_mutex_t *__mutex));
  230. extern int pthread_cond_timedwait   _C_PROTOTYPE((pthread_cond_t *__cond,
  231.                           pthread_mutex_t *__mutex,
  232.                           struct timespec *__timeout));
  233. extern int pthread_cond_signal      _C_PROTOTYPE((pthread_cond_t *__cond));
  234. extern int pthread_cond_broadcast   _C_PROTOTYPE((pthread_cond_t *__cond));
  235. extern int pthread_condattr_getpshared
  236.                                     _C_PROTOTYPE((pthread_condattr_t *__attr,
  237.                           int *__pshared));
  238. extern int pthread_condattr_setpshared
  239.                                     _C_PROTOTYPE((pthread_condattr_t *__attr,
  240.                           int __pshared));
  241.  
  242. #ifdef __cplusplus
  243. } /* extern "C" */
  244. #endif
  245.  
  246. /*
  247.  * Threads.
  248.  */
  249.  
  250. typedef any_t (*pthread_func_t) _C_PROTOTYPE((any_t __arg));
  251.  
  252. typedef struct pthread_attr {
  253.   int flags;
  254. #ifdef _POSIX_THREAD_ATTR_STACKSIZE
  255.   int stacksize;
  256. #endif
  257.   int contentionscope;
  258.   int inheritsched;
  259.   int detachstate;
  260.   int sched;
  261. #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
  262.   int prio;                      /* (Active) priority of Thread        */
  263. #endif
  264.   struct timespec starttime, deadline, period;
  265. } pthread_attr_t;
  266.  
  267. /*
  268.  * Queue indices
  269.  */
  270. #define PRIMARY_QUEUE 0
  271. #define ALL_QUEUE     1
  272. /* this slot may be used by the TIMER_QUEUE with index 2 */
  273. #define NUM_QUEUES    3
  274.  
  275. typedef int pthread_key_t;
  276.  
  277. typedef struct cleanup *cleanup_t;
  278.  
  279. typedef struct pthread {
  280.     sigjmp_buf context;                /* save area for context switch   */
  281.     sigjmp_buf body;                   /* save area for pthread_body     */
  282.     volatile int errno;                /* thread-specific errno          */
  283.     volatile int ret;                  /* return value (EINTR --> -1)    */
  284.     char *stack_base;                  /* bottom of run-time stack       */
  285.     int state;                         /* thread state, -> pthread_asm.h */
  286.         struct pthread *next[NUM_QUEUES];  /* links for queues               */
  287.     int num_timers;               /* number of timers for thread    */
  288.     struct timeval interval;       /* time left for SCHED_RR          */
  289.         struct p_siginfo sig_info[NNSIG];  /* info for user handlers         */
  290.     int sig;                           /* latest received signal         */
  291.     int code;                          /* latest received signal code    */
  292.     int osp, opc;                      /* save area for old context sw   */
  293.         struct context_t *nscp;            /* UNIX signal context (new sig)  */
  294.         struct context_t *scp;             /* UNIX signal context (current)  */
  295.         struct pthread_queue joinq;        /* queue to await termination     */
  296.         pthread_cond_t *cond;              /* cond var. if in cond_wait      */
  297.         pthread_queue_t queue;/* primary queue thread is contained in        */
  298.         sigset_t mask;        /* set of signals thread has masked out        */
  299.         sigset_t pending;     /* set of signals pending on thread            */
  300.         sigset_t sigwaitset;  /* set of signals thread is waiting for        */
  301.     pthread_func_t func;  /* actual function to call upon activation     */
  302.     any_t arg;            /* argument list to above function             */
  303.         any_t result;         /* return value of above function              */
  304.     any_t key[_POSIX_DATAKEYS_MAX];    /* thread specific data           */
  305.         cleanup_t cleanup_top;             /* stack of cleanup handlers      */
  306.         struct pthread_attr attr;          /* attributes                     */
  307.         int base_prio;                     /* Base priority of thread        */
  308.     int max_ceiling_prio; /* Max of ceiling prio among locked mutexes    */
  309.         int new_prio;         /* New Priority                                */
  310. #ifdef IO_NP
  311.     volatile struct aio_result_t resultp;/*result of asynchronous I/O ops*/
  312.         int wait_on_select;                /* more information in fds        */
  313.     int width;                         /* pertinent file desc. set width */
  314.     int how_many;                      /* how many amoung  0 .. width -1 */
  315.     fd_set readfds;                    /* read file descriptor set       */
  316.     fd_set writefds;                   /* write file descriptor set      */
  317.     fd_set exceptfds;                  /* except. file descriptor set    */
  318. #endif !IO_NP
  319. #ifdef STAND_ALONE_NP
  320.     pthread_func_t timer_func;      /* function to be called on timeout*/
  321.     any_t timer_arg;                  /* arg of above function         */
  322.         struct timespec tp;               /* wake-up time                    */
  323.     int dummy;              /* filler for stack alignment      */
  324. #else !STAND_ALONE_NP
  325.         struct timeval tp;                /* wake-up time                    */
  326. #endif !STAND_ALONE_NP
  327. } *pthread_t;
  328.  
  329.  
  330. #if defined(__GNUC__) && defined(_PARAMS)
  331. #define IO_SIZE_T __SIZE_TYPE__
  332. /*
  333.  * for gcc 2.5.8 or before use:
  334.  * #define IO_SIZE_T unsigned int
  335.  */
  336. #define SIGSUSPEND_CONST
  337. #else
  338. #define IO_SIZE_T size_t
  339. #define SIGSUSPEND_CONST const
  340. #endif
  341.  
  342. /******************************/
  343. /*      Thread Functions      */
  344. /* Thread Attribute Functions */
  345. /******************************/
  346.  
  347. #ifdef __cplusplus
  348. extern "C" {
  349. #endif
  350.  
  351. extern pthread_t pthread_self           _C_PROTOTYPE ((void));
  352. extern void pthread_init                _C_PROTOTYPE ((void));
  353. extern int pthread_create               _C_PROTOTYPE((pthread_t *__thread,
  354.                               pthread_attr_t *__attr,
  355.                               pthread_func_t __func,
  356.                               any_t __arg));
  357. extern int pthread_equal                _C_PROTOTYPE((pthread_t __t1,
  358.                               pthread_t __t2));
  359. extern int pthread_detach               _C_PROTOTYPE((pthread_t *__thread));
  360. extern int pthread_join                 _C_PROTOTYPE((pthread_t __thread,
  361.                               any_t *__status));
  362. extern void pthread_yield               _C_PROTOTYPE((any_t __arg));
  363. extern void pthread_exit                _C_PROTOTYPE((any_t __status));
  364. extern int pthread_attr_init            _C_PROTOTYPE((pthread_attr_t *__attr));
  365. extern int pthread_attr_destroy         _C_PROTOTYPE((pthread_attr_t *__attr));
  366. extern int pthread_getschedattr         _C_PROTOTYPE((pthread_t __thread,
  367.                               pthread_attr_t *__attrs));
  368. extern int pthread_setschedattr         _C_PROTOTYPE((pthread_t __thread,
  369.                               pthread_attr_t __attrs));
  370. extern int pthread_attr_setstacksize    _C_PROTOTYPE((pthread_attr_t *__attr,
  371.                               size_t __stacksize));
  372. extern int pthread_attr_getstacksize    _C_PROTOTYPE((pthread_attr_t *__attr,
  373.                               size_t *__stacksize));
  374. extern int pthread_attr_setscope        _C_PROTOTYPE((pthread_attr_t *__attr,
  375.                               int __contentionscope));
  376. extern int pthread_attr_setinheritsched _C_PROTOTYPE((pthread_attr_t *__attr,
  377.                               int __inherit));
  378. extern int pthread_attr_setsched        _C_PROTOTYPE((pthread_attr_t *__attr,
  379.                               int __scheduler));
  380. extern int pthread_attr_setprio         _C_PROTOTYPE((pthread_attr_t *__attr,
  381.                               int __priority));
  382. extern int pthread_attr_getscope        _C_PROTOTYPE((pthread_attr_t __attr));
  383. extern int pthread_attr_getinheritsched _C_PROTOTYPE((pthread_attr_t *__attr));
  384. extern int pthread_attr_getsched        _C_PROTOTYPE((pthread_attr_t *__attr));
  385. extern int pthread_attr_getprio         _C_PROTOTYPE((pthread_attr_t *__attr));
  386.  
  387. extern int pthread_attr_getstarttime_np _C_PROTOTYPE((pthread_attr_t *__attr,
  388.                               struct timespec *__tp));
  389. extern int pthread_attr_setstarttime_np _C_PROTOTYPE((pthread_attr_t *__attr,
  390.                               struct timespec *__tp));
  391. extern int pthread_attr_getdeadline_np  _C_PROTOTYPE((pthread_attr_t *__attr,
  392.                               struct timespec *__tp));
  393. extern int pthread_attr_setdeadline_np  _C_PROTOTYPE((pthread_attr_t *__attr,
  394.                               struct timespec *__tp,
  395.                               pthread_func_t func));
  396. extern int pthread_attr_getperiod_np    _C_PROTOTYPE((pthread_attr_t *__attr,
  397.                               struct timespec *__tp));
  398. extern int pthread_attr_setperiod_np    _C_PROTOTYPE((pthread_attr_t *__attr,
  399.                               struct timespec *__tp,
  400.                               pthread_func_t func));
  401.  
  402. extern int pthread_key_create           _C_PROTOTYPE((pthread_key_t *__key,
  403.                               void (*__func)
  404.                               (any_t __value)));
  405. extern int pthread_setspecific          _C_PROTOTYPE((pthread_key_t __key,
  406.                               any_t __value));
  407. extern int pthread_getspecific          _C_PROTOTYPE((pthread_key_t __key,
  408.                               any_t *__value));
  409. extern int pthread_cleanup_push         _C_PROTOTYPE((void (*__func)
  410.                               (any_t __value),
  411.                               any_t __arg));
  412. extern int pthread_cleanup_pop          _C_PROTOTYPE((int __execute));
  413. extern int sched_get_priority_max       _C_PROTOTYPE((int __policy));
  414. extern int sched_get_priority_min       _C_PROTOTYPE((int __policy));
  415. extern int pthread_attr_setdetachstate  _C_PROTOTYPE((pthread_attr_t *__attr,
  416.                               int *__detachstate));
  417. extern int pthread_attr_getdetachstate  _C_PROTOTYPE((pthread_attr_t *__attr));
  418. extern int pthread_once                 _C_PROTOTYPE((pthread_once_t *__once_c,
  419.                               void (*__func)
  420.                               (any_t __value)));
  421.  
  422. /*
  423.  * implementation-defined extensions
  424.  */
  425. extern int pthread_setprio_np        _C_PROTOTYPE((pthread_t __thread,
  426.                               int __prio));
  427. extern void pthread_setsigcontext_np    _C_PROTOTYPE((struct context_t *__scp,
  428.                               jmp_buf __env,
  429.                               int __val));
  430. extern int pthread_lock_stack_np    _C_PROTOTYPE((pthread_t __p));
  431.  
  432.  
  433. /******************************/
  434. /*      Signal Functions      */
  435. /******************************/
  436.  
  437. extern int sigwait             _C_PROTOTYPE((sigset_t *__set));
  438. extern int sigprocmask         _C_PROTOTYPE((int __how,
  439.                          const sigset_t *__set,
  440.                          sigset_t *__oset));
  441. extern int sigpending          _C_PROTOTYPE((sigset_t *__set));
  442. extern int sigsuspend          _C_PROTOTYPE((SIGSUSPEND_CONST sigset_t *__set));
  443. extern int pause               _C_PROTOTYPE((void));
  444. extern int raise               _C_PROTOTYPE((int __sig));
  445. extern int pthread_kill        _C_PROTOTYPE((pthread_t __thread,
  446.                          int __sig));
  447. extern int pthread_cancel      _C_PROTOTYPE((pthread_t __thread));
  448. extern int pthread_setintr     _C_PROTOTYPE((int __state));
  449. extern int pthread_setintrtype _C_PROTOTYPE((int __type));
  450. extern void pthread_testintr   _C_PROTOTYPE((void));
  451. extern int sigaction           _C_PROTOTYPE((int __sig,
  452.                          const struct sigaction *__act,
  453.                          struct sigaction *__oact));
  454. /* yet to come...
  455. extern unsigned int alarm      _C_PROTOTYPE((unsigned int __seconds));
  456. */
  457. extern int nanosleep           _C_PROTOTYPE((const struct timespec *__rqtp,
  458.                          struct timespec *__rmtp));
  459. extern unsigned int sleep      _C_PROTOTYPE((unsigned int __seconds));
  460. extern int clock_gettime       _C_PROTOTYPE((int __clock_id,
  461.                          struct timespec *__tp));
  462.  
  463. /******************************/
  464. /*    Low-Level Functions     */
  465. /******************************/
  466.  
  467. extern int setjmp         _C_PROTOTYPE((jmp_buf __env));
  468. extern void longjmp       _C_PROTOTYPE((jmp_buf __env,
  469.                     int __val));
  470. extern int sigsetjmp      _C_PROTOTYPE((sigjmp_buf __env,
  471.                     int __savemask));
  472. extern void siglongjmp    _C_PROTOTYPE((sigjmp_buf __env,
  473.                     int __val));
  474.  
  475. /******************************/
  476. /*       I/O Functions        */
  477. /******************************/
  478.  
  479. extern int read  _C_PROTOTYPE((int __fd,
  480.                    void *__buf,
  481.                    IO_SIZE_T __nbytes));
  482. extern int write _C_PROTOTYPE((int __fd,
  483.                    const void *__buf,
  484.                    IO_SIZE_T __nbytes));
  485.  
  486. #ifdef __cplusplus
  487. } /* extern "C" */
  488. #endif
  489.  
  490. #endif    _POSIX_THREADS
  491.  
  492. #endif /*!_pthread_pthread_h*/
  493.