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

MySQL数据库

开发平台:

Visual C++

  1. /* ==== pthread_attr.c =======================================================
  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.  * Description : Pthread attribute functions.
  33.  *
  34.  *  1.00 93/11/04 proven
  35.  *      -Started coding this file.
  36.  */
  37. #ifndef lint
  38. static const char rcsid[] = "$Id$";
  39. #endif
  40. #include <pthread.h>
  41. #include <errno.h>
  42. #include <string.h>
  43. /* Currently we do no locking, should we just to be safe? CAP */
  44. /* ==========================================================================
  45.  * pthread_attr_init()
  46.  */
  47. int pthread_attr_init(pthread_attr_t *attr)
  48. {
  49. memcpy(attr, &pthread_attr_default, sizeof(pthread_attr_t));
  50. return(OK);
  51. }
  52. /* ==========================================================================
  53.  * pthread_attr_destroy()
  54.  */
  55. int pthread_attr_destroy(pthread_attr_t *attr)
  56. {
  57. return(OK);
  58. }
  59. /* ==========================================================================
  60.  * pthread_attr_getstacksize()
  61.  */
  62. int pthread_attr_getstacksize(pthread_attr_t *attr, size_t * stacksize)
  63. {
  64. *stacksize = attr->stacksize_attr;
  65. return(OK);
  66. }
  67. /* ==========================================================================
  68.  * pthread_attr_setstacksize()
  69.  */
  70. int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
  71. {
  72. if (stacksize >= PTHREAD_STACK_MIN) {
  73. attr->stacksize_attr = stacksize;
  74. return(OK);
  75. }
  76. return(EINVAL);
  77. }
  78. /* ==========================================================================
  79.  * pthread_attr_getstackaddr()
  80.  */
  81. int pthread_attr_getstackaddr(pthread_attr_t *attr, void ** stackaddr)
  82. {
  83. *stackaddr = attr->stackaddr_attr;
  84. return(OK);
  85. }
  86. /* ==========================================================================
  87.  * pthread_attr_setstackaddr()
  88.  */
  89. int pthread_attr_setstackaddr(pthread_attr_t *attr, void * stackaddr)
  90. {
  91. attr->stackaddr_attr = stackaddr;
  92. return(OK);
  93. }
  94. /* ==========================================================================
  95.  * pthread_attr_setcleanup()
  96.  */
  97. int pthread_attr_setcleanup(pthread_attr_t *attr, void (*routine)(void *),
  98.   void * arg)
  99. {
  100. attr->cleanup_attr = routine;
  101. attr->arg_attr = arg;
  102. return(OK);
  103. }
  104. /* ==========================================================================
  105.  * pthread_attr_getdetachstate()
  106.  */
  107. int pthread_attr_getdetachstate(pthread_attr_t *attr, int * detachstate)
  108. {
  109. *detachstate = attr->flags & PTHREAD_DETACHED;
  110. return(OK);
  111. }
  112. /* ==========================================================================
  113.  * pthread_attr_setdetachstate()
  114.  */
  115. int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
  116. {
  117. attr->flags = (attr->flags & ~(PTHREAD_DETACHED)) |
  118.   (detachstate & PTHREAD_DETACHED);
  119. return(OK);
  120. }
  121. /* ==========================================================================
  122.  * pthread_attr_getfloatstate()
  123.  */
  124. int pthread_attr_getfloatstate(pthread_attr_t *attr, int * floatstate)
  125. {
  126. *floatstate = attr->flags & PTHREAD_NOFLOAT;
  127. return(OK);
  128. }
  129. /* ==========================================================================
  130.  * pthread_attr_setfloatstate()
  131.  */
  132. int pthread_attr_setfloatstate(pthread_attr_t *attr, int floatstate)
  133. {
  134. attr->flags = (attr->flags & ~(PTHREAD_NOFLOAT)) |
  135.   (floatstate & PTHREAD_NOFLOAT);
  136. return(OK);
  137. }
  138. /* ==========================================================================
  139.  * pthread_attr_getscope()
  140.  */
  141. int pthread_attr_getscope(pthread_attr_t *attr, int * contentionscope)
  142. {
  143. *contentionscope = attr->flags & PTHREAD_SCOPE_SYSTEM;
  144. return(OK);
  145. }
  146. /* ==========================================================================
  147.  * pthread_attr_setscope()
  148.  */
  149. int pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)
  150. {
  151. int ret;
  152. switch (contentionscope) {
  153. case PTHREAD_SCOPE_PROCESS:
  154. attr->flags = (attr->flags & ~(PTHREAD_SCOPE_PROCESS)) 
  155.   | PTHREAD_SCOPE_PROCESS;
  156. ret = OK;
  157. break;
  158. case PTHREAD_SCOPE_SYSTEM:
  159. ret = ENOSYS;
  160. break;
  161. default:
  162. ret = EINVAL;
  163. break;
  164. }
  165. return(ret);
  166. }
  167. /* ==========================================================================
  168.  * pthread_attr_getinheritsched()
  169.  */
  170. int pthread_attr_getinheritsched(pthread_attr_t *attr, int * inheritsched)
  171. {
  172. *inheritsched = attr->flags & PTHREAD_INHERIT_SCHED;
  173. return(OK);
  174. }
  175. /* ==========================================================================
  176.  * pthread_attr_setinheritsched()
  177.  */
  178. int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched)
  179. {
  180. attr->flags = (attr->flags & ~(PTHREAD_INHERIT_SCHED)) |
  181.   (inheritsched & PTHREAD_INHERIT_SCHED);
  182. return(OK);
  183. }
  184. /* ==========================================================================
  185.  * pthread_attr_getschedpolicy()
  186.  */
  187. int pthread_attr_getschedpolicy(pthread_attr_t *attr, int * schedpolicy)
  188. {
  189. *schedpolicy = (int)attr->schedparam_policy;
  190. return(OK);
  191. }
  192. /* ==========================================================================
  193.  * pthread_attr_setschedpolicy()
  194.  */
  195. int pthread_attr_setschedpolicy(pthread_attr_t *attr, int schedpolicy)
  196. {
  197. int ret;
  198. switch(schedpolicy) {
  199. case SCHED_FIFO:
  200. case SCHED_IO:
  201. case SCHED_RR:
  202. attr->schedparam_policy = schedpolicy;
  203. ret = OK;
  204. break;
  205. default:
  206. ret = EINVAL;
  207. break;
  208. }
  209. return(ret);
  210. }
  211. /* ==========================================================================
  212.  * pthread_attr_getschedparam()
  213.  */
  214. int pthread_attr_getschedparam(pthread_attr_t *attr, struct sched_param * param)
  215. {
  216. param->sched_priority = attr->sched_priority;
  217. return(OK);
  218. }
  219. /* ==========================================================================
  220.  * pthread_attr_setschedparam()
  221.  */
  222. int pthread_attr_setschedparam(pthread_attr_t *attr, struct sched_param * param)
  223. {
  224. if ((param->sched_priority >= PTHREAD_MIN_PRIORITY) &&
  225.   (param->sched_priority <= PTHREAD_MAX_PRIORITY)) {
  226. attr->sched_priority = param->sched_priority;
  227. return(OK);
  228. }
  229. return(EINVAL);
  230. }