thread_xsb.h

00001 /* File:      thread_xsb.h
00002 ** Author(s): Marques
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 ** 
00005 ** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
00006 ** 
00007 ** XSB is free software; you can redistribute it and/or modify it under the
00008 ** terms of the GNU Library General Public License as published by the Free
00009 ** Software Foundation; either version 2 of the License, or (at your option)
00010 ** any later version.
00011 ** 
00012 ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
00013 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 ** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
00015 ** more details.
00016 ** 
00017 ** You should have received a copy of the GNU Library General Public License
00018 ** along with XSB; if not, write to the Free Software Foundation,
00019 ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020 **
00021 */
00022 
00023 
00024 #ifndef __THREAD_XSB_H__
00025 
00026 #define __THREAD_XSB_H__
00027 
00028 #include "context.h"
00029 #include "thread_defs_xsb.h"
00030 #include "basictypes.h"
00031 
00032 xsbBool xsb_thread_request( CTXTdecl ) ;
00033 xsbBool mt_random_request( CTXTdecl ) ;
00034 int xsb_thread_self() ;
00035 
00036 
00037 #ifdef MULTI_THREAD
00038 
00039 #include <pthread.h>
00040 
00041 #ifdef WIN_NT
00042 typedef pthread_t* pthread_t_p;
00043 #define PTHREAD_CREATE(a,b,c,d) pthread_create(&a,b,c,d);
00044 #define PTHREAD_DETACH(a) pthread_detach(*a);
00045 #define PTHREAD_CANCEL(a) pthread_cancel(*a);
00046 #else
00047 typedef pthread_t pthread_t_p;
00048 #define PTHREAD_CREATE(a,b,c,d) pthread_create(a,b,c,d);
00049 #define PTHREAD_DETACH(a) pthread_detach(a);
00050 #define PTHREAD_CANCEL(a) pthread_cancel(a);
00051 #endif
00052 
00053 typedef struct Mutex_Frame {
00054   pthread_mutex_t th_mutex; 
00055   int num_locks;
00056   int owner;
00057 } MutexFrame;
00058 
00059 MutexFrame sys_mut[MAX_SYS_MUTEXES];
00060 
00061 #define MUTARRAY_MUTEX(i) &(sys_mut[(i)].th_mutex)
00062 #define MUTARRAY_NUMLOCKS(i) sys_mut[(i)].num_locks
00063 #define MUTARRAY_OWNER(i) sys_mut[(i)].owner
00064 
00065 extern void print_mutex_use(void);
00066 extern void release_held_mutexes(CTXTdecl);
00067 
00068 extern pthread_mutex_t completing_mut;
00069 extern pthread_cond_t completing_cond;
00070 
00071 #define PROFILE_MUTEXES 1
00072 #ifdef PROFILE_MUTEXES
00073 
00074 #define SYS_MUTEX_LOCK( M )   {pthread_mutex_lock(MUTARRAY_MUTEX(M));         \
00075                                MUTARRAY_OWNER(M) = xsb_thread_id;             \
00076                                MUTARRAY_NUMLOCKS(M)++; }
00077 
00078 #define SYS_MUTEX_LOCK_NOERROR( M )   {pthread_mutex_lock(MUTARRAY_MUTEX(M));  \
00079                                        MUTARRAY_NUMLOCKS(M)++; }
00080 #else
00081 
00082 #define SYS_MUTEX_LOCK( M )   {pthread_mutex_lock( MUTARRAY_MUTEX(M));        \
00083                                MUTARRAY_OWNER(M) = xsb_thread_id; }
00084 
00085 #define SYS_MUTEX_LOCK_NOERROR( M )   {pthread_mutex_lock(MUTARRAY_MUTEX(M)); }
00086 
00087 #endif /* PROFILE_MUTEXES */
00088 
00089 #define SYS_MUTEX_UNLOCK( M )   {pthread_mutex_unlock( MUTARRAY_MUTEX(M) );        \
00090                                  MUTARRAY_OWNER(M) = -1; }
00091 
00092 #define SYS_MUTEX_UNLOCK_NOERROR( M )   {pthread_mutex_unlock( MUTARRAY_MUTEX(M) );        \
00093                                          MUTARRAY_OWNER(M) = -1; }
00094 #else
00095 #define SYS_MUTEX_LOCK( M ) 
00096 #define SYS_MUTEX_LOCK_NOERROR( M ) 
00097 #define SYS_MUTEX_UNLOCK( M )
00098 #define SYS_MUTEX_UNLOCK_NOERROR( M )
00099 #endif /* MULTI_THREAD */
00100 
00101 #ifdef MULTI_THREAD
00102 void init_system_mutexes( void ) ;
00103 void init_system_threads( th_context * ctxt ) ;
00104 
00105 th_context *find_context( int tid );
00106 #endif
00107 
00108 #define ENSURE_ONE_THREAD()                                             \
00109   { if( flags[NUM_THREADS] > 1 )                                        \
00110       xsb_abort( "Operation is permitted only when a single thread is active" ) ; \
00111   }
00112 
00113 
00114 /*
00115   TLS: the mt engine does not yet work for enable no cygwin, but this
00116    allows random and srandom to be used.  They're both defined in
00117    stdlib.h as they should be, but Windows calls them rand and srand.
00118 */
00119 
00120 #if defined(WIN_NT)
00121 
00122 #define RANDOM_CALL rand
00123 #define SRANDOM_CALL srand
00124 
00125 #else
00126 
00127 #define RANDOM_CALL random
00128 #define SRANDOM_CALL srandom
00129 
00130 #endif
00131 
00132 
00133 /* TLS: for Cygwin, these constants must be re-defined */
00134 
00135 #if defined(DARWIN) || defined(FREEBSD) || defined(SOLARIS)
00136 
00137 #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE
00138 #define PTHREAD_MUTEX_ERRORCHECK_NP PTHREAD_MUTEX_ERRORCHECK
00139 #define PTHREAD_MUTEX_FAST_NP PTHREAD_MUTEX_NORMAL
00140 
00141 #endif
00142 
00143 #endif
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151 
00152 
00153 
00154 
00155 
00156 
00157 
00158 

Generated on Wed Jul 26 13:30:42 2006 for XSB by  doxygen 1.4.5