]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/threads.c
Merge remote branch 'origin/mdb.master'
[openldap] / libraries / libldap_r / threads.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2012 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         ldap_pvt_thread_t tid;
43
44         /* we only get one shot at this */
45         if( init++ ) return -1;
46
47         rc = ldap_int_thread_initialize();
48         if( rc ) return rc;
49
50 #ifndef LDAP_THREAD_HAVE_TPOOL
51         rc = ldap_int_thread_pool_startup();
52         if( rc ) return rc;
53 #endif
54
55         /* kludge to pull symbol definitions in */
56         ldap_pvt_thread_rmutex_init( &rm );
57         tid = ldap_pvt_thread_self();
58         ldap_pvt_thread_rmutex_lock( &rm, tid );
59         ldap_pvt_thread_rmutex_trylock( &rm, tid );
60         ldap_pvt_thread_rmutex_unlock( &rm, tid );
61         ldap_pvt_thread_rmutex_unlock( &rm, tid );
62         ldap_pvt_thread_rmutex_destroy( &rm );
63
64         return 0;
65 }
66
67 int ldap_pvt_thread_destroy( void )
68 {
69 #ifndef LDAP_THREAD_HAVE_TPOOL
70         (void) ldap_int_thread_pool_shutdown();
71 #endif
72         return ldap_int_thread_destroy();
73 }
74
75
76 /*
77  * Default implementations of some LDAP thread routines
78  */
79
80 #define LDAP_THREAD_IMPLEMENTATION
81 #include "ldap_thr_debug.h"     /* May rename the symbols defined below */
82
83
84 #ifndef LDAP_THREAD_HAVE_GETCONCURRENCY
85 int
86 ldap_pvt_thread_get_concurrency ( void )
87 {
88         return 1;
89 }
90 #endif
91
92 #ifndef LDAP_THREAD_HAVE_SETCONCURRENCY
93 int
94 ldap_pvt_thread_set_concurrency ( int concurrency )
95 {
96         return 1;
97 }
98 #endif
99
100 #ifndef LDAP_THREAD_HAVE_SLEEP
101 /*
102  * Here we assume we have fully preemptive threads and that sleep()
103  * does the right thing.
104  */
105 unsigned int
106 ldap_pvt_thread_sleep(
107         unsigned int interval
108 )
109 {
110         sleep( interval );
111         return 0;
112 }
113 #endif