timer_xsb.h

00001 /* File:      timer_xsb.h
00002 ** Author(s): Songmei Yu, kifer
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 **
00005 ** Copyright (C) The Research Foundation of SUNY, 1999
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 ** $Id: timer_xsb.h,v 1.15 2006/03/24 16:40:29 tswift Exp $
00022 ** 
00023 */
00024 
00025 #ifndef _TIMER_XSB_H_
00026 #define _TIMER_XSB_H_
00027 
00028 #ifndef CONFIG_INCLUDED
00029 #error "File xsb_config.h must be included before this file"
00030 #endif
00031 
00032 #ifndef CELL_DEFS_INCLUDED
00033 #error "File cell_xsb.h must be included before this file"
00034 #endif
00035 
00036 #ifndef SYSTEM_FLAGS
00037 #include "flags_xsb.h"
00038 #endif
00039 
00040 #include "timer_defs_xsb.h"
00041 #include "context.h"
00042 #include "setjmp_xsb.h" 
00043 #include "thread_xsb.h"
00044 
00045 #ifndef MULTI_THREAD
00046 #ifdef WIN_NT
00047 #include <windows.h>
00048 #endif
00049 #endif
00050 
00051 #define NORMAL_TERMINATION  -1
00052 #define TIMED_OUT            1
00053 #define STILL_WAITING        0
00054 #if !defined(WIN_NT) && !defined(MULTI_THREAD) 
00055 extern sigjmp_buf xsb_timer_env;
00056 #endif
00057 
00058 /* TLS: changed xsb_timeout_info so to be non-empty when MULTI_THREAD
00059    is not defined.  Some compilers complain about empty structures. */
00060 struct xsb_timeout_info {
00061   int             exitFlag;
00062 #ifdef MULTI_THREAD
00063   pthread_t_p     timedThread;
00064   pthread_cond_t  condition;
00065   pthread_mutex_t mutex;
00066   CTXTdecl;
00067 #else /* not multithreaded */
00068 #ifdef WIN_NT
00069   long parent_thread;
00070 #else /* UNIX */
00071   /* Nothing currently needed here */
00072 #endif /* WIN_NT else */
00073 #endif /* MULTI_THREAD else */
00074 };
00075 
00076 typedef struct xsb_timeout_info xsbTimeoutInfo;
00077 
00078 struct xsb_timeout {
00079   xsbTimeoutInfo timeout_info;
00080 };
00081 
00082 
00083 typedef struct xsb_timeout xsbTimeout;
00084 
00085 /* generic function to control the timeout */
00086 
00087 int make_timed_call(CTXTdeclc xsbTimeout*, void (*) (xsbTimeout*));
00088 
00089 // #define NEW_TIMEOUT_OBJECT  (xsbTimeout *)mem_alloc(sizeof(xsbTimeout))
00090 
00091 
00092 
00093 #ifdef MULTI_THREAD /* PTHREADS */
00094 
00095 #define SETALARM            ;  /* no-op */
00096 #define OP_TIMED_OUT(timeout)        (op_timed_out(CTXTc timeout))
00097 
00098 #define TURNOFFALARM        /* no-op */
00099 #define CHECK_TIMER_SET     ((int)pflags[SYS_TIMER] > 0)
00100 #define NOTIFY_PARENT_THREAD(timeout)                          \
00101    if (CHECK_TIMER_SET) {                                      \
00102       /* Send msg to the timer thread immediately,             \
00103          so it won't wait to time out */                       \
00104       pthread_mutex_lock(&timeout->timeout_info.mutex);         \
00105       timeout->timeout_info.exitFlag = NORMAL_TERMINATION;     \
00106       pthread_cond_signal(&timeout->timeout_info.condition); \
00107       pthread_mutex_unlock(&timeout->timeout_info.mutex);       \
00108    }
00109 
00110 #else /* NOT PTHREADS */
00111 
00112 #ifdef WIN_NT
00113 
00114 VOID CALLBACK xsb_timer_handler(HWND wind, UINT msg, UINT eventid, DWORD time);
00115 unsigned long _beginthread(void(_cdecl *start_address) (void *),
00116                            unsigned stack_size,
00117                            void *arglist);
00118 int message_pump();
00119 UINT xsb_timer_id; 
00120 
00121 #define TURNOFFALARM        KillTimer(NULL, xsb_timer_id);
00122 #define CHECK_TIMER_SET     ((int)pflags[SYS_TIMER] > 0)
00123 #define NOTIFY_PARENT_THREAD(timeout)       \
00124    if (CHECK_TIMER_SET) {\
00125       /* Send msg to the timer thread immediately, \
00126          so it won't wait to time out */ \
00127       PostThreadMessage((DWORD)(timeout->timeout_info.parent_thread), \
00128                                 WM_COMMAND,NORMAL_TERMINATION,0);\
00129    }
00130 #define OP_TIMED_OUT(timeout)        (message_pump())
00131 
00132 #else  /* Unix */
00133 
00134 void xsb_timer_handler(int signo);
00135 
00136 #define SETALARM                      (signal(SIGALRM, xsb_timer_handler))
00137 #define TURNOFFALARM                  alarm(0);
00138 #define CHECK_TIMER_SET               ((int)pflags[SYS_TIMER] > 0)
00139 #define NOTIFY_PARENT_THREAD(timeout)  ;  /* no-op */
00140 #define SET_TIMER                     alarm((int)pflags[SYS_TIMER])
00141 #define OP_TIMED_OUT                  (sigsetjmp(xsb_timer_env,1) != 0)
00142 
00143 #endif /* WIN_NT (else part) */
00144 
00145 #endif /* MULTI_THREAD (else part) */
00146 
00147 #endif /* _TIMER_XSB_H_ */

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