1 |
/* PEAK Library |
2 |
* |
3 |
* Copyright (c) 2003 |
4 |
* Stephane Thiell <mbuna@bugged.org>. All rights reserved. |
5 |
* |
6 |
* Redistribution and use in source and binary forms, with or without |
7 |
* modification, are permitted provided that the following conditions |
8 |
* are met: |
9 |
* |
10 |
* 1. Redistributions of source code must retain the above copyright |
11 |
* notice, this list of conditions and the following disclaimer. |
12 |
* |
13 |
* 2. Redistributions in binary form must reproduce the above copyright |
14 |
* notice, this list of conditions and the following disclaimer in the |
15 |
* documentation and/or other materials provided with the distribution. |
16 |
* |
17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
19 |
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
20 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
24 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
25 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
26 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
27 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 |
* |
29 |
*/ |
30 |
#define RCSID "$Id: init.c,v 1.1.1.1 2003/12/30 02:29:17 mbuna Exp $" |
31 |
|
32 |
#ifdef HAVE_CONFIG_H |
33 |
#include "config.h" |
34 |
#endif |
35 |
|
36 |
#include "init_private.h" |
37 |
#include <pthread.h> |
38 |
|
39 |
/* Globals |
40 |
*/ |
41 |
int _peak_is_threaded; |
42 |
|
43 |
|
44 |
static int __peak_initialized = 0; |
45 |
static pthread_key_t __task_key, __task_runloop_key; |
46 |
|
47 |
|
48 |
static void |
49 |
__peak_init() |
50 |
{ |
51 |
__peak_initialized = 1; |
52 |
_peak_is_threaded = 0; |
53 |
|
54 |
if (pthread_key_create(&__task_key, NULL) || |
55 |
pthread_key_create(&__task_runloop_key, NULL)) |
56 |
PEAK_HALT; |
57 |
} |
58 |
|
59 |
__private_extern__ void |
60 |
_peak_init_thread_task(peak_task task) |
61 |
{ |
62 |
pthread_setspecific(__task_key, (void *)task); |
63 |
} |
64 |
|
65 |
__private_extern__ void |
66 |
_peak_init_thread_runloop(peak_task_runloop rl) |
67 |
{ |
68 |
pthread_setspecific(__task_runloop_key, (void *)rl); |
69 |
} |
70 |
|
71 |
peak_task |
72 |
peak_task_self() |
73 |
{ |
74 |
void *obj; |
75 |
|
76 |
if (__peak_initialized && (obj = pthread_getspecific(__task_key)) != NULL) |
77 |
return (peak_task)obj; /* Normal prediction */ |
78 |
else if (__peak_initialized == 0) |
79 |
__peak_init(); /* Once */ |
80 |
return _peak_task_create(); /* Once per task */ |
81 |
} |
82 |
|
83 |
__private_extern__ peak_task_runloop |
84 |
_peak_task_runloop_self() |
85 |
{ |
86 |
return (peak_task_runloop)pthread_getspecific(__task_runloop_key); |
87 |
} |