pthread.h
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:11k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* ==== pthread.h ============================================================
  2.  * Copyright (c) 1993, 1994 by Chris Provenzano, proven@mit.edu
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *  This product includes software developed by Chris Provenzano.
  16.  * 4. The name of Chris Provenzano may not be used to endorse or promote 
  17.  *   products derived from this software without specific prior written
  18.  *   permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO ``AS IS'' AND
  21.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED.  IN NO EVENT SHALL CHRIS PROVENZANO BE LIABLE FOR ANY 
  24.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  26.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
  28.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
  30.  * SUCH DAMAGE.
  31.  *
  32.  * $Id$
  33.  *
  34.  * Description : Basic pthread header.
  35.  *
  36.  *  1.00 93/07/20 proven
  37.  *      -Started coding this file.
  38.  *
  39.  *  93/9/28 streepy - Added support for pthread cancel
  40.  *
  41.  */
  42.  
  43. #ifndef _PTHREAD_H_
  44. #define _PTHREAD_H_
  45. #include <pthread/types.h>
  46. #include <pthread/version.h>
  47. #include <pthread/machdep.h>
  48. #include <pthread/cleanup.h>
  49. #include <pthread/kernel.h>
  50. #include <pthread/prio_queue.h>
  51. #include <pthread/queue.h>
  52. #include <pthread/sleep.h>
  53. #include <pthread/mutex.h>
  54. #include <pthread/cond.h>
  55. #include <pthread/fd.h>
  56. #include <pthread/debug_out.h>
  57. /* Requires mutex.h */
  58. #include <pthread/specific.h>
  59. #include <pthread/util.h>
  60. /* More includes */
  61. #include <pthread/pthread_once.h>
  62. /* More includes, that need size_t */
  63. #include <pthread/pthread_attr.h>
  64. #include <signal.h> /* for sigset_t */ /* Moved by monty */
  65. /* Constants for use with pthread_setcancelstate and pthread_setcanceltype */
  66. #define PTHREAD_CANCEL_DISABLE 0
  67. #define PTHREAD_CANCEL_ENABLE 1
  68. #define PTHREAD_CANCEL_DEFERRED 0
  69. #define PTHREAD_CANCEL_ASYNCHRONOUS 1
  70. #define PTHREAD_CANCELLED (void *)1 /* Exit status of a cancelled thread */
  71. #ifdef PTHREAD_KERNEL
  72. enum pthread_state {
  73. #define __pthread_defstate(S,NAME) S,
  74. #include "pthread/state.def"
  75. #undef __pthread_defstate
  76.   /* enum lists aren't supposed to end with a comma, sigh */
  77.   PS_STATE_MAX
  78. };
  79. /* Put PANIC inside an expression that evaluates to non-void type, to
  80.    make it easier to combine it in expressions.  */
  81. #define DO_PANIC() (PANIC (), 0)
  82. #define PANICIF(x) ((x) ? DO_PANIC () : 0)
  83. /* In the thread flag field, we use a series of bit flags. Flags can
  84.  * organized into "groups" of mutually exclusive flags.  Other flags
  85.  * are unrelated and can be set and cleared with a single bit operation.
  86.  */
  87. #define PF_WAIT_EVENT 0x01
  88. #define PF_DONE_EVENT 0x02
  89. #define PF_EVENT_GROUP 0x03 /* All event bits */
  90. #define PF_CANCEL_STATE 0x04 /* cancellability state */
  91. #define PF_CANCEL_TYPE 0x08 /* cancellability type */
  92. #define PF_THREAD_CANCELLED 0x10 /* thread has been cancelled */
  93. #define PF_RUNNING_TO_CANCEL 0x20 /* Thread is running so it can cancel*/
  94. #define PF_AT_CANCEL_POINT 0x40 /* Thread is at a cancel point */
  95. /* Flag operations */
  96. #define SET_PF_FLAG(x,f) ( (x)->flags |= (f) )
  97. #define TEST_PF_FLAG(x,f) ( (x)->flags & (f) )
  98. #define CLEAR_PF_FLAG(x,f) ( (x)->flags &= ~(f) )
  99. #define CLEAR_PF_GROUP(x,g) ( (x)->flags &= ~(g) )
  100. #define SET_PF_FLAG_IN_GROUP(x,g,f) ( CLEAR_PF_GROUP(x,g),SET_PF_FLAG(x,f))
  101. #define TEST_PF_GROUP(x,g) ( (x)->flags & (g) )
  102. #define SET_PF_DONE_EVENT(x)
  103. ( !TEST_PF_FLAG(x,PF_DONE_EVENT)
  104.   ? ( TEST_PF_FLAG(x,PF_WAIT_EVENT)
  105.   ? (SET_PF_FLAG_IN_GROUP(x,PF_EVENT_GROUP,PF_DONE_EVENT), OK)
  106.   : DO_PANIC ())
  107.   : NOTOK )
  108. #define SET_PF_WAIT_EVENT(x)
  109. ( PANICIF (TEST_PF_GROUP(x,PF_EVENT_GROUP) ),
  110.   SET_PF_FLAG_IN_GROUP(x,PF_EVENT_GROUP,PF_WAIT_EVENT), 0)
  111. #define CLEAR_PF_DONE_EVENT(x)
  112. ( PANICIF (!TEST_PF_FLAG(x,PF_DONE_EVENT)),
  113.   CLEAR_PF_GROUP(x,PF_EVENT_GROUP) )
  114. #define SET_PF_CANCELLED(x) ( SET_PF_FLAG(x,PF_THREAD_CANCELLED) )
  115. #define TEST_PF_CANCELLED(x) ( TEST_PF_FLAG(x,PF_THREAD_CANCELLED) )
  116. #define SET_PF_RUNNING_TO_CANCEL(x) ( SET_PF_FLAG(x,PF_RUNNING_TO_CANCEL) )
  117. #define CLEAR_PF_RUNNING_TO_CANCEL(x)( CLEAR_PF_FLAG(x,PF_RUNNING_TO_CANCEL) )
  118. #define TEST_PF_RUNNING_TO_CANCEL(x)( TEST_PF_FLAG(x,PF_RUNNING_TO_CANCEL) )
  119. #define SET_PF_AT_CANCEL_POINT(x) ( SET_PF_FLAG(x,PF_AT_CANCEL_POINT) )
  120. #define CLEAR_PF_AT_CANCEL_POINT(x) ( CLEAR_PF_FLAG(x,PF_AT_CANCEL_POINT) )
  121. #define TEST_PF_AT_CANCEL_POINT(x) ( TEST_PF_FLAG(x,PF_AT_CANCEL_POINT) )
  122. #define SET_PF_CANCEL_STATE(x,f) 
  123. ( (f) ? SET_PF_FLAG(x,PF_CANCEL_STATE) : CLEAR_PF_FLAG(x,PF_CANCEL_STATE) )
  124. #define TEST_PF_CANCEL_STATE(x) 
  125. ( (TEST_PF_FLAG(x,PF_CANCEL_STATE)) ? PTHREAD_CANCEL_ENABLE 
  126. : PTHREAD_CANCEL_DISABLE )
  127. #define SET_PF_CANCEL_TYPE(x,f) 
  128. ( (f) ? SET_PF_FLAG(x,PF_CANCEL_TYPE) : CLEAR_PF_FLAG(x,PF_CANCEL_TYPE) )
  129. #define TEST_PF_CANCEL_TYPE(x) 
  130. ( (TEST_PF_FLAG(x,PF_CANCEL_TYPE)) ? PTHREAD_CANCEL_ASYNCHRONOUS 
  131.    : PTHREAD_CANCEL_DEFERRED )
  132. /* See if a thread is in a state that it can be cancelled */
  133. #define TEST_PTHREAD_IS_CANCELLABLE(x) 
  134. ( (TEST_PF_CANCEL_STATE(x) == PTHREAD_CANCEL_ENABLE && TEST_PF_CANCELLED(x)) 
  135.     ? ((TEST_PF_CANCEL_TYPE(x) == PTHREAD_CANCEL_ASYNCHRONOUS) 
  136.         ? 1 
  137.         : TEST_PF_AT_CANCEL_POINT(x)) 
  138. : 0 )
  139. struct pthread_select_data {
  140. int nfds;
  141. fd_set readfds;
  142. fd_set writefds;
  143. fd_set exceptfds;
  144. };
  145. union pthread_wait_data {
  146. pthread_mutex_t   * mutex;
  147. pthread_cond_t    * cond;
  148. const sigset_t   * sigwait; /* Waiting on a signal in sigwait */
  149. struct {
  150. short fd; /* Used when thread waiting on fd */
  151. short branch; /* line number, for debugging */
  152. } fd;
  153. struct pthread_select_data * select_data;
  154. };
  155. #define PTT_USER_THREAD 0x0001
  156. struct pthread {
  157. int thread_type;
  158. struct machdep_pthread machdep_data;
  159. pthread_attr_t attr;
  160. /* Signal interface */
  161. sigset_t sigmask;
  162. sigset_t sigpending;
  163. int sigcount; /* Number of signals pending */
  164. int sighandled; /* Set when signal has been handled */
  165. /* Timeout time */
  166. struct timespec wakeup_time;
  167. /* Join queue for waiting threads */
  168. struct pthread_queue join_queue;
  169. /*
  170.  * Thread implementations are just multiple queue type implemenations,
  171.  * Below are the various link lists currently necessary
  172.  * It is possible for a thread to be on multiple, or even all the
  173.  * queues at once, much care must be taken during queue manipulation.
  174.  *
  175.  * The pthread structure must be locked before you can even look at
  176.  * the link lists.
  177.  */ 
  178. /*
  179.  * ALL threads, in any state. 
  180.  * Must lock kernel lock before manipulating.
  181.  */
  182. struct pthread * pll;
  183. /*
  184.  * Standard link list for running threads, mutexes, etc ...
  185.  * It can't be on both a running link list and a wait queue.
  186.  * Must lock kernel lock before manipulating.
  187.  */
  188. struct pthread * next;
  189. union pthread_wait_data data;
  190. /*
  191.  * Actual queue state and priority of thread.
  192.  * (Note: "priority" is a reserved word in Concurrent C, please
  193.  * don't use it.  --KR)
  194.  */
  195. struct pthread_queue * queue;
  196. enum pthread_state   state;
  197. enum pthread_state   old_state; /* Used when cancelled */
  198. char   flags;
  199. char    pthread_priority;
  200. /*
  201.  * Sleep queue, this is different from the standard link list
  202.  * because it is possible to be on both (pthread_cond_timedwait();
  203.  * Must lock sleep mutex before manipulating
  204.  */
  205. struct pthread *sll; /* For sleeping threads */
  206. /*
  207.  * Data that doesn't need to be locked
  208.  * Mostly because only the thread owning the data can manipulate it
  209.  */
  210. void  * ret;
  211. int   error;
  212. int  * error_p;
  213. const void ** specific_data;
  214. int specific_data_count;
  215. /* Cleanup handlers Link List */
  216. struct pthread_cleanup *cleanup;
  217. };
  218. #else /* not PTHREAD_KERNEL */
  219. struct pthread;
  220. #endif
  221. typedef struct pthread *pthread_t;
  222. /*
  223.  * Globals
  224.  */
  225. #ifdef PTHREAD_KERNEL
  226. extern struct pthread  * pthread_run;
  227. extern struct pthread  * pthread_initial;
  228. extern struct pthread  * pthread_link_list;
  229. extern struct pthread_queue pthread_dead_queue;
  230. extern struct pthread_queue pthread_alloc_queue;
  231. extern pthread_attr_t pthread_attr_default;
  232. extern volatile int fork_lock;
  233. extern pthread_size_t pthread_pagesize;
  234. extern sigset_t * uthread_sigmask;
  235. /* Kernel global functions */
  236. extern void pthread_sched_prevent(void);
  237. extern void pthread_sched_resume(void);
  238. extern int __pthread_is_valid( pthread_t );
  239. extern void pthread_cancel_internal( int freelocks );
  240. #endif
  241. /*
  242.  * New functions
  243.  */
  244. __BEGIN_DECLS
  245. #if defined(DCE_COMPAT)
  246. typedef void * (*pthread_startroutine_t)(void *);
  247. typedef void * pthread_addr_t;
  248. int pthread_create __P_((pthread_t *, pthread_attr_t,
  249.     pthread_startroutine_t,
  250.     pthread_addr_t));
  251. void pthread_exit __P_((pthread_addr_t));
  252. int pthread_join __P_((pthread_t, pthread_addr_t *));
  253. #else
  254. void pthread_init __P_((void));
  255. int pthread_create __P_((pthread_t *,
  256.     const pthread_attr_t *,
  257.     void * (*start_routine)(void *),
  258.     void *));
  259. void pthread_exit __P_((void *));
  260. pthread_t pthread_self __P_((void));
  261. int pthread_equal __P_((pthread_t, pthread_t));
  262. int pthread_join __P_((pthread_t, void **));
  263. int pthread_detach __P_((pthread_t));
  264. void pthread_yield __P_((void));
  265. int pthread_setschedparam __P_((pthread_t pthread, int policy,
  266.    struct sched_param * param));
  267. int pthread_getschedparam __P_((pthread_t pthread, int * policy,
  268.    struct sched_param * param));
  269. int pthread_kill __P_((struct pthread *, int));
  270. void (*pthread_signal __P_((int, void (*)(int))))();
  271. int pthread_cancel __P_(( pthread_t pthread ));
  272. int pthread_setcancelstate __P_(( int state, int *oldstate ));
  273. int pthread_setcanceltype __P_(( int type, int *oldtype ));
  274. void pthread_testcancel __P_(( void ));
  275. int  pthread_sigmask __P_((int how, const sigset_t *set,
  276.      sigset_t * oset)); /* added by Monty */
  277. int sigwait __P_((const sigset_t * set, int * sig));
  278. int sigsetwait __P_((const sigset_t * set, int * sig));
  279. #endif
  280. #if defined(PTHREAD_KERNEL)
  281. /* Not valid, but I can't spell so this will be caught at compile time */
  282. #define pthread_yeild(notvalid)
  283. #endif
  284. __END_DECLS
  285. /*
  286.  * Static constructors
  287.  */
  288. #ifdef __cplusplus
  289. extern struct pthread    * pthread_initial;
  290. class __pthread_init_t {
  291. /* struct __pthread_init_t { */
  292. public:
  293. __pthread_init_t() {
  294. if (pthread_initial == NULL) {
  295. pthread_init();
  296. }
  297. }
  298. };
  299. static __pthread_init_t __pthread_init_this_file;
  300. #endif /* __cplusplus */
  301. #endif