thr_once.c (155739) | thr_once.c (172695) |
---|---|
1/* 2 * Copyright (c) 2005, David Xu <davidxu@freebsd.org> 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 unchanged lines hidden (view full) --- 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * | 1/* 2 * Copyright (c) 2005, David Xu <davidxu@freebsd.org> 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 unchanged lines hidden (view full) --- 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * |
26 * $FreeBSD: head/lib/libthr/thread/thr_once.c 155739 2006-02-15 23:05:03Z davidxu $ | 26 * $FreeBSD: head/lib/libthr/thread/thr_once.c 172695 2007-10-16 07:46:15Z davidxu $ |
27 * 28 */ 29 30#include "namespace.h" 31#include <pthread.h> 32#include "un-namespace.h" 33 34#include "thr_private.h" --- 24 unchanged lines hidden (view full) --- 59 once_control->state = ONCE_NEVER_DONE; 60 _pthread_mutex_unlock(&_thr_once_lock); 61 _pthread_cond_broadcast(&_thr_once_cv); 62} 63 64int 65_pthread_once(pthread_once_t *once_control, void (*init_routine) (void)) 66{ | 27 * 28 */ 29 30#include "namespace.h" 31#include <pthread.h> 32#include "un-namespace.h" 33 34#include "thr_private.h" --- 24 unchanged lines hidden (view full) --- 59 once_control->state = ONCE_NEVER_DONE; 60 _pthread_mutex_unlock(&_thr_once_lock); 61 _pthread_cond_broadcast(&_thr_once_cv); 62} 63 64int 65_pthread_once(pthread_once_t *once_control, void (*init_routine) (void)) 66{ |
67 struct pthread *curthread; |
|
67 int wakeup = 0; 68 69 if (once_control->state == ONCE_DONE) 70 return (0); 71 _pthread_mutex_lock(&_thr_once_lock); 72 while (*(volatile int *)&(once_control->state) == ONCE_IN_PROGRESS) 73 _pthread_cond_wait(&_thr_once_cv, &_thr_once_lock); 74 /* 75 * If previous thread was canceled, then the state still 76 * could be ONCE_NEVER_DONE, we need to check it again. 77 */ 78 if (*(volatile int *)&(once_control->state) == ONCE_NEVER_DONE) { 79 once_control->state = ONCE_IN_PROGRESS; 80 _pthread_mutex_unlock(&_thr_once_lock); | 68 int wakeup = 0; 69 70 if (once_control->state == ONCE_DONE) 71 return (0); 72 _pthread_mutex_lock(&_thr_once_lock); 73 while (*(volatile int *)&(once_control->state) == ONCE_IN_PROGRESS) 74 _pthread_cond_wait(&_thr_once_cv, &_thr_once_lock); 75 /* 76 * If previous thread was canceled, then the state still 77 * could be ONCE_NEVER_DONE, we need to check it again. 78 */ 79 if (*(volatile int *)&(once_control->state) == ONCE_NEVER_DONE) { 80 once_control->state = ONCE_IN_PROGRESS; 81 _pthread_mutex_unlock(&_thr_once_lock); |
81 _pthread_cleanup_push(once_cancel_handler, once_control); | 82 curthread = _get_curthread(); 83 THR_CLEANUP_PUSH(curthread, once_cancel_handler, once_control); |
82 init_routine(); | 84 init_routine(); |
83 _pthread_cleanup_pop(0); | 85 THR_CLEANUP_POP(curthread, 0); |
84 _pthread_mutex_lock(&_thr_once_lock); 85 once_control->state = ONCE_DONE; 86 wakeup = 1; 87 } 88 _pthread_mutex_unlock(&_thr_once_lock); 89 if (wakeup) 90 _pthread_cond_broadcast(&_thr_once_cv); 91 return (0); 92} 93 94void 95_thr_once_init() 96{ 97 _thr_once_lock = PTHREAD_MUTEX_INITIALIZER; 98 _thr_once_cv = PTHREAD_COND_INITIALIZER; 99} | 86 _pthread_mutex_lock(&_thr_once_lock); 87 once_control->state = ONCE_DONE; 88 wakeup = 1; 89 } 90 _pthread_mutex_unlock(&_thr_once_lock); 91 if (wakeup) 92 _pthread_cond_broadcast(&_thr_once_cv); 93 return (0); 94} 95 96void 97_thr_once_init() 98{ 99 _thr_once_lock = PTHREAD_MUTEX_INITIALIZER; 100 _thr_once_cv = PTHREAD_COND_INITIALIZER; 101} |