Back to index

openldap  2.4.31
threads.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 
00016 #include "portable.h"
00017 
00018 #include <stdio.h>
00019 
00020 #include <ac/stdarg.h>
00021 #include <ac/stdlib.h>
00022 #include <ac/string.h>
00023 #include <ac/unistd.h>
00024 
00025 #include "ldap_pvt_thread.h" /* Get the thread interface */
00026 #include "ldap_thr_debug.h"  /* May redirect thread initialize/destroy calls */
00027 
00028 
00029 /*
00030  * Common LDAP thread routines
00031  *     see thr_*.c for implementation specific routines
00032  *     see rdwr.c for generic reader/writer lock implementation
00033  *     see tpool.c for generic thread pool implementation
00034  */
00035 
00036 
00037 int ldap_pvt_thread_initialize( void )
00038 {
00039        int rc;
00040        static int init = 0;
00041        ldap_pvt_thread_rmutex_t rm;
00042        ldap_pvt_thread_t tid;
00043 
00044        /* we only get one shot at this */
00045        if( init++ ) return -1;
00046 
00047        rc = ldap_int_thread_initialize();
00048        if( rc ) return rc;
00049 
00050 #ifndef LDAP_THREAD_HAVE_TPOOL
00051        rc = ldap_int_thread_pool_startup();
00052        if( rc ) return rc;
00053 #endif
00054 
00055        /* kludge to pull symbol definitions in */
00056        ldap_pvt_thread_rmutex_init( &rm );
00057        tid = ldap_pvt_thread_self();
00058        ldap_pvt_thread_rmutex_lock( &rm, tid );
00059        ldap_pvt_thread_rmutex_trylock( &rm, tid );
00060        ldap_pvt_thread_rmutex_unlock( &rm, tid );
00061        ldap_pvt_thread_rmutex_unlock( &rm, tid );
00062        ldap_pvt_thread_rmutex_destroy( &rm );
00063 
00064        return 0;
00065 }
00066 
00067 int ldap_pvt_thread_destroy( void )
00068 {
00069 #ifndef LDAP_THREAD_HAVE_TPOOL
00070        (void) ldap_int_thread_pool_shutdown();
00071 #endif
00072        return ldap_int_thread_destroy();
00073 }
00074 
00075 
00076 /*
00077  * Default implementations of some LDAP thread routines
00078  */
00079 
00080 #define LDAP_THREAD_IMPLEMENTATION
00081 #include "ldap_thr_debug.h" /* May rename the symbols defined below */
00082 
00083 
00084 #ifndef LDAP_THREAD_HAVE_GETCONCURRENCY
00085 int
00086 ldap_pvt_thread_get_concurrency ( void )
00087 {
00088        return 1;
00089 }
00090 #endif
00091 
00092 #ifndef LDAP_THREAD_HAVE_SETCONCURRENCY
00093 int
00094 ldap_pvt_thread_set_concurrency ( int concurrency )
00095 {
00096        return 1;
00097 }
00098 #endif
00099 
00100 #ifndef LDAP_THREAD_HAVE_SLEEP
00101 /*
00102  * Here we assume we have fully preemptive threads and that sleep()
00103  * does the right thing.
00104  */
00105 unsigned int
00106 ldap_pvt_thread_sleep(
00107        unsigned int interval
00108 )
00109 {
00110        sleep( interval );
00111        return 0;
00112 }
00113 #endif