00001 /* ==================================================================== 00002 * The Kannel Software License, Version 1.0 00003 * 00004 * Copyright (c) 2001-2008 Kannel Group 00005 * Copyright (c) 1998-2001 WapIT Ltd. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * 1. Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * 00015 * 2. Redistributions in binary form must reproduce the above copyright 00016 * notice, this list of conditions and the following disclaimer in 00017 * the documentation and/or other materials provided with the 00018 * distribution. 00019 * 00020 * 3. The end-user documentation included with the redistribution, 00021 * if any, must include the following acknowledgment: 00022 * "This product includes software developed by the 00023 * Kannel Group (http://www.kannel.org/)." 00024 * Alternately, this acknowledgment may appear in the software itself, 00025 * if and wherever such third-party acknowledgments normally appear. 00026 * 00027 * 4. The names "Kannel" and "Kannel Group" must not be used to 00028 * endorse or promote products derived from this software without 00029 * prior written permission. For written permission, please 00030 * contact org@kannel.org. 00031 * 00032 * 5. Products derived from this software may not be called "Kannel", 00033 * nor may "Kannel" appear in their name, without prior written 00034 * permission of the Kannel Group. 00035 * 00036 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 00037 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00038 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00039 * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 00040 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 00041 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00042 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 00043 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00044 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 00045 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00046 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00047 * ==================================================================== 00048 * 00049 * This software consists of voluntary contributions made by many 00050 * individuals on behalf of the Kannel Group. For more information on 00051 * the Kannel Group, please see <http://www.kannel.org/>. 00052 * 00053 * Portions of this software are based upon software originally written at 00054 * WapIT Ltd., Helsinki, Finland for the Kannel project. 00055 */ 00056 00057 /* 00058 * gwthread.h - threads wrapper with interruptible sleep and poll operations. 00059 * 00060 * This is a (partial) encapsulation of threads. It provides functions 00061 * to create new threads and to manipulate threads. It will eventually 00062 * be extended to encapsulate all pthread functions we use, so that 00063 * non-POSIX platforms can plug in their own versions. 00064 * 00065 * Richard Braakman 00066 */ 00067 00068 #ifndef GWTHREAD_H 00069 #define GWTHREAD_H 00070 00071 #include "gw-config.h" 00072 #include <sys/poll.h> 00073 00074 /* gwthread_self() must return this value for the main thread. */ 00075 #define MAIN_THREAD_ID 0 00076 00077 typedef void gwthread_func_t(void *arg); 00078 00079 /* Called by the gwlib init code */ 00080 void gwthread_init(void); 00081 void gwthread_shutdown(void); 00082 00083 /* Start a new thread, running func(arg). Return the new thread ID 00084 * on success, or -1 on failure. Thread IDs are unique during the lifetime 00085 * of the entire process, unless you use more than LONG_MAX threads. */ 00086 long gwthread_create_real(gwthread_func_t *func, const char *funcname, 00087 void *arg); 00088 #define gwthread_create(func, arg) \ 00089 (gwthread_create_real(func, __FILE__ ":" #func, arg)) 00090 00091 /* Wait for the other thread to terminate. Return immediately if it 00092 * has already terminated. */ 00093 void gwthread_join(long thread); 00094 00095 /* Wait for all threads whose main function is `func' to terminate. 00096 * Return immediately if none are running. */ 00097 void gwthread_join_every(gwthread_func_t *func); 00098 00099 /* Wait for all threads to terminate. Return immediately if none 00100 * are running. This function is not intended to be called if new 00101 * threads are still being created, and it may not notice such threads. */ 00102 void gwthread_join_all(void); 00103 00104 /* Return the thread id of this thread. Note that it may be called for 00105 * the main thread even before the gwthread library has been initialized 00106 * and after it had been shut down. */ 00107 long gwthread_self(void); 00108 00109 /* Same as above, but returns the process id (pid) of the thread. */ 00110 long gwthread_self_pid(void); 00111 00112 /* Same as gwthread_self() and gwthread_self_pid() combined to one void 00113 * call. Returns the internal thread id and the process id. */ 00114 void gwthread_self_ids(long *tid, long *pid); 00115 00116 /* If the other thread is currently in gwthread_pollfd or gwthread_sleep, 00117 * make it return immediately. Otherwise, make it return immediately, the 00118 * next time it calls one of those functions. */ 00119 void gwthread_wakeup(long thread); 00120 00121 /* Wake up all threads */ 00122 void gwthread_wakeup_all(void); 00123 00124 /* Wrapper around the poll() system call, for one file descriptor. 00125 * "events" is a set of the flags defined in <sys/poll.h>, usually 00126 * POLLIN, POLLOUT, or (POLLIN|POLLOUT). Return when one of the 00127 * events is true, or when another thread calls gwthread_wakeup on us, or 00128 * when the timeout expires. The timeout is specified in seconds, 00129 * and a negative value means do not time out. Return the revents 00130 * structure filled in by poll() for this fd. Return -1 if something 00131 * went wrong. */ 00132 int gwthread_pollfd(int fd, int events, double timeout); 00133 00134 /* Wrapper around the poll() system call, for an array of file 00135 * descriptors. The difference with normal poll is that the 00136 * thread can be woken up with gwthread_wakeup. timeout is in seconds. */ 00137 /* NOTE: This interface will probably change in the future, because currently 00138 * it is hard to implement efficiently. */ 00139 int gwthread_poll(struct pollfd *fds, long numfds, double timeout); 00140 00141 /* Sleep until "seconds" seconds have elapsed, or until another thread 00142 * calls gwthread_wakeup on us. Fractional seconds are allowed. */ 00143 void gwthread_sleep(double seconds); 00144 00145 /* Sleep until "seconds" seconds have elapsed, or until another thread 00146 * calls gwthread_wakeup on us. Fractional seconds are allowed. */ 00147 void gwthread_sleep_micro(double dseconds); 00148 00149 /* Force a specific thread to terminate. Returns 0 on success, -1 if the 00150 * thread has been terminated while calling and non-zero for the pthread 00151 * specific error code. 00152 */ 00153 int gwthread_cancel(long thread); 00154 00155 /* 00156 * Check wheather this thread should handle the given signal. 00157 * Since signals are thread specific, this needs to be handled 00158 * by the thread code here. This is mostly to cope with 00159 * "interesting" implementations of "pthreads" 00160 */ 00161 int gwthread_shouldhandlesignal(int signal); 00162 00163 /* Dump the current signal mask for this thread. This will print out a 00164 * set of debug messages that state the signal handling status for the 00165 * first 32 signals on the current system. Return -1 if something goes 00166 * wrong. 00167 * 00168 * Debugging purposes mostly so it can be ignored on platforms 00169 * where this isn't applicable. 00170 */ 00171 int gwthread_dumpsigmask(void); 00172 00173 00174 #endif