thr_once.c (bb535300dd871731b2542594a917bc2892479ca0) | thr_once.c (a091d823ad89a36935ba6e5568c4720a35f9a99b) |
---|---|
1/* 2 * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>. 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 --- 17 unchanged lines hidden (view full) --- 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER 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 * $FreeBSD$ 33 */ | 1/* 2 * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>. 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 --- 17 unchanged lines hidden (view full) --- 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER 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 * $FreeBSD$ 33 */ |
34 35#include "namespace.h" |
|
34#include <pthread.h> | 36#include <pthread.h> |
37#include "un-namespace.h" 38 |
|
35#include "thr_private.h" 36 37__weak_reference(_pthread_once, pthread_once); 38 | 39#include "thr_private.h" 40 41__weak_reference(_pthread_once, pthread_once); 42 |
43#define ONCE_NEVER_DONE PTHREAD_NEEDS_INIT 44#define ONCE_DONE PTHREAD_DONE_INIT 45#define ONCE_IN_PROGRESS 0x02 46#define ONCE_MASK 0x03 47 48static pthread_mutex_t once_lock = PTHREAD_MUTEX_INITIALIZER; 49static pthread_cond_t once_cv = PTHREAD_COND_INITIALIZER; 50 51/* 52 * POSIX: 53 * The pthread_once() function is not a cancellation point. However, 54 * if init_routine is a cancellation point and is canceled, the effect 55 * on once_control shall be as if pthread_once() was never called. 56 */ 57 58static void 59once_cancel_handler(void *arg) 60{ 61 pthread_once_t *once_control = arg; 62 63 _pthread_mutex_lock(&once_lock); 64 once_control->state = ONCE_NEVER_DONE; 65 _pthread_mutex_unlock(&once_lock); 66 _pthread_cond_broadcast(&once_cv); 67} 68 |
|
39int | 69int |
40_pthread_once(pthread_once_t * once_control, void (*init_routine) (void)) | 70_pthread_once(pthread_once_t *once_control, void (*init_routine) (void)) |
41{ | 71{ |
42 if (once_control->state == PTHREAD_NEEDS_INIT) { 43 if (_thread_initial == NULL) 44 _thread_init(); 45 pthread_mutex_lock(&(once_control->mutex)); 46 if (once_control->state == PTHREAD_NEEDS_INIT) { 47 init_routine(); 48 once_control->state = PTHREAD_DONE_INIT; 49 } 50 pthread_mutex_unlock(&(once_control->mutex)); | 72 int wakeup = 0; 73 74 if (once_control->state == ONCE_DONE) 75 return (0); 76 _pthread_mutex_lock(&once_lock); 77 while (*(volatile int *)&(once_control->state) == ONCE_IN_PROGRESS) 78 _pthread_cond_wait(&once_cv, &once_lock); 79 /* 80 * If previous thread was canceled, then the state still 81 * could be ONCE_NEVER_DONE, we need to check it again. 82 */ 83 if (*(volatile int *)&(once_control->state) == ONCE_NEVER_DONE) { 84 once_control->state = ONCE_IN_PROGRESS; 85 _pthread_mutex_unlock(&once_lock); 86 _pthread_cleanup_push(once_cancel_handler, once_control); 87 init_routine(); 88 _pthread_cleanup_pop(0); 89 _pthread_mutex_lock(&once_lock); 90 once_control->state = ONCE_DONE; 91 wakeup = 1; |
51 } | 92 } |
93 _pthread_mutex_unlock(&once_lock); 94 if (wakeup) 95 _pthread_cond_broadcast(&once_cv); |
|
52 return (0); 53} | 96 return (0); 97} |
98 |
|