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

MySQL数据库

开发平台:

Visual C++

  1. /* ==== pthread_atexit.c =====================================================
  2.  * Copyright (c) 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.20 94/02/13 proven
  35.  *      -Started coding this file.
  36.  */
  37. #ifndef lint
  38. static const char rcsid[] = "$Id$";
  39. #endif
  40. #define PTHREAD_KERNEL
  41. #include <errno.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include "pthreadutil.h"
  45. static int pthread_atexit_inited = 0;
  46. static pthread_key_t pthread_atexit_key;
  47. static pthread_mutex_t pthread_atexit_mutex = PTHREAD_MUTEX_INITIALIZER;
  48. /* ==========================================================================
  49.  * pthread_atexit_done()
  50.  */
  51. static void pthread_atexit_done(void * arg) 
  52. {
  53. pthread_atexit_t id, id_next;
  54. for (id = arg; id; id = id_next) {
  55. id_next = id->next;
  56. id->rtn(id->arg);
  57. free(id);
  58. }
  59. }
  60. /* ==========================================================================
  61.  * pthread_atexit_add()
  62.  */
  63. int pthread_atexit_add(pthread_atexit_t *id, void (*rtn)(void *), void * arg)
  64. {
  65. int ret;
  66. if (ret = pthread_mutex_lock(&pthread_atexit_mutex)) {
  67. return(ret);
  68. }
  69. if (!pthread_atexit_inited) {
  70. if (ret = pthread_key_create(&pthread_atexit_key, pthread_atexit_done)){
  71. pthread_mutex_unlock(&pthread_atexit_mutex);
  72. return(ret);
  73. }
  74. pthread_atexit_inited++;
  75. }
  76. pthread_mutex_unlock(&pthread_atexit_mutex);
  77. if ((*id) = (pthread_atexit_t)malloc(sizeof(struct pthread_atexit))) {
  78. if ((*id)->next = pthread_getspecific(pthread_atexit_key)) {
  79. (*id)->next->prev = (*id);
  80. }
  81. pthread_setspecific(pthread_atexit_key, (void *)*id);
  82. (*id)->prev = NULL;
  83. (*id)->rtn = rtn;
  84. (*id)->arg = arg;
  85. return(OK);
  86. }
  87. return(ENOMEM);
  88. }
  89. /* ==========================================================================
  90.  * pthread_atexit_remove()
  91.  */
  92. int pthread_atexit_remove(pthread_atexit_t * id, int execute)
  93. {
  94. pthread_atexit_t old;
  95. if (old = pthread_getspecific(pthread_atexit_key)) {
  96. if (old == *id) {
  97. old = old->next;
  98. old->prev = NULL;
  99. pthread_setspecific(pthread_atexit_key, old);
  100. } else {
  101. if ((*id)->next) {
  102. (*id)->next->prev = (*id)->prev;
  103. }
  104. (*id)->prev->next = (*id)->next;
  105. }
  106. if (execute) {
  107. (*id)->rtn((*id)->arg);
  108. }
  109. free((*id));
  110. return(OK);
  111. }
  112. return(EINVAL);
  113. }
  114. /* ==========================================================================
  115.  * A few non void functions that are often used as void functions
  116.  */
  117. void fflush_nrv(void * fp) { fflush((FILE *)fp); }
  118. void fclose_nrv(void * fp) { fclose((FILE *)fp); }
  119. void pthread_attr_destroy_nrv(void * attr) 
  120. pthread_attr_destroy((pthread_attr_t *)attr); 
  121. }