]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/threads.c
Added recursive mutex implementation
[openldap] / libraries / libldap_r / threads.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2006 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19
20 #include <ac/stdarg.h>
21 #include <ac/stdlib.h>
22 #include <ac/string.h>
23 #include <ac/unistd.h>
24
25 #include "ldap_pvt_thread.h" /* Get the thread interface */
26 #include "ldap_thr_debug.h"  /* May redirect thread initialize/destroy calls */
27
28
29 /*
30  * Common LDAP thread routines
31  *      see thr_*.c for implementation specific routines
32  *      see rdwr.c for generic reader/writer lock implementation
33  *      see tpool.c for generic thread pool implementation
34  */
35
36
37 int ldap_pvt_thread_initialize( void )
38 {
39         int rc;
40         static int init = 0;
41         ldap_pvt_thread_rmutex_t rm;
42
43         /* we only get one shot at this */
44         if( init++ ) return -1;
45
46         rc = ldap_int_thread_initialize();
47         if( rc ) return rc;
48
49 #ifndef LDAP_THREAD_HAVE_TPOOL
50         rc = ldap_int_thread_pool_startup();
51         if( rc ) return rc;
52 #endif
53
54         /* kludge to pull symbol definitions in */
55         ldap_pvt_thread_rmutex_init( &rm );
56         ldap_pvt_thread_rmutex_lock( &rm );
57         ldap_pvt_thread_rmutex_trylock( &rm );
58         ldap_pvt_thread_rmutex_unlock( &rm );
59         ldap_pvt_thread_rmutex_unlock( &rm );
60         ldap_pvt_thread_rmutex_destroy( &rm );
61
62         return 0;
63 }
64
65 int ldap_pvt_thread_destroy( void )
66 {
67 #ifndef LDAP_THREAD_HAVE_TPOOL
68         (void) ldap_int_thread_pool_shutdown();
69 #endif
70         return ldap_int_thread_destroy();
71 }
72
73
74 /*
75  * Default implementations of some LDAP thread routines
76  */
77
78 #define LDAP_THREAD_IMPLEMENTATION
79 #include "ldap_thr_debug.h"     /* May rename the symbols defined below */
80
81
82 #ifndef LDAP_THREAD_HAVE_GETCONCURRENCY
83 int
84 ldap_pvt_thread_get_concurrency ( void )
85 {
86         return 1;
87 }
88 #endif
89
90 #ifndef LDAP_THREAD_HAVE_SETCONCURRENCY
91 int
92 ldap_pvt_thread_set_concurrency ( int concurrency )
93 {
94         return 1;
95 }
96 #endif
97
98 #ifndef LDAP_THREAD_HAVE_SLEEP
99 /*
100  * Here we assume we have fully preemptive threads and that sleep()
101  * does the right thing.
102  */
103 unsigned int
104 ldap_pvt_thread_sleep(
105         unsigned int interval
106 )
107 {
108         sleep( interval );
109         return 0;
110 }
111 #endif