liblscp 0.5.6
|
00001 // thread.h 00002 // 00003 /**************************************************************************** 00004 liblscp - LinuxSampler Control Protocol API 00005 Copyright (C) 2004-2007, rncbc aka Rui Nuno Capela. All rights reserved. 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Lesser General Public 00009 License as published by the Free Software Foundation; either 00010 version 2.1 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License along 00018 with this program; if not, write to the Free Software Foundation, Inc., 00019 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00020 00021 *****************************************************************************/ 00022 00023 #ifndef __LSCP_THREAD_H 00024 #define __LSCP_THREAD_H 00025 00026 #include <stdio.h> 00027 #include <stdlib.h> 00028 #include <string.h> 00029 00030 #if (defined(_WIN32) || defined(__WIN32__)) 00031 #if (!defined(WIN32)) 00032 #define WIN32 00033 #endif 00034 #endif 00035 00036 #if defined(WIN32) 00037 #include <windows.h> 00038 #else 00039 #include <pthread.h> 00040 #endif 00041 00042 #include "lscp/version.h" 00043 00044 #if defined(__cplusplus) 00045 extern "C" { 00046 #endif 00047 00048 //------------------------------------------------------------------------- 00049 // Status. 00050 00051 typedef enum _lscp_status_t 00052 { 00053 LSCP_OK = 0, 00054 LSCP_FAILED = -1, 00055 LSCP_ERROR = -2, 00056 LSCP_WARNING = -3, 00057 LSCP_TIMEOUT = -4, 00058 LSCP_QUIT = -5 00059 00060 } lscp_status_t; 00061 00062 //------------------------------------------------------------------------- 00063 // Mutexes. 00064 00065 #if defined(WIN32) 00066 typedef HANDLE lscp_mutex_t; 00067 #define lscp_mutex_init(m) { (m) = CreateMutex(NULL, 0, NULL); } 00068 #define lscp_mutex_destroy(m) if (m) { CloseHandle(m); } 00069 #define lscp_mutex_lock(m) WaitForSingleObject((m), INFINITE) 00070 #define lscp_mutex_unlock(m) ReleaseMutex(m) 00071 #else 00072 typedef pthread_mutex_t lscp_mutex_t; 00073 #define lscp_mutex_init(m) pthread_mutex_init(&(m), NULL) 00074 #define lscp_mutex_destroy(m) pthread_mutex_destroy(&(m)) 00075 #define lscp_mutex_lock(m) pthread_mutex_lock(&(m)) 00076 #define lscp_mutex_unlock(m) pthread_mutex_unlock(&(m)) 00077 #endif 00078 00079 //------------------------------------------------------------------------- 00080 // Simple condition variables (FIXME: probably incorrect on WIN32). 00081 00082 #if defined(WIN32) 00083 typedef HANDLE lscp_cond_t; 00084 #define lscp_cond_init(c) { (c) = CreateEvent(NULL, FALSE, FALSE, NULL); } 00085 #define lscp_cond_destroy(c) if (c) { CloseHandle(c); } 00086 #define lscp_cond_wait(c, m) { lscp_mutex_unlock(m); WaitForSingleObject((c), INFINITE); lscp_mutex_lock(m); } 00087 #define lscp_cond_signal(c) SetEvent(c) 00088 #else 00089 typedef pthread_cond_t lscp_cond_t; 00090 #define lscp_cond_init(c) pthread_cond_init(&(c), NULL) 00091 #define lscp_cond_destroy(c) pthread_cond_destroy(&(c)) 00092 #define lscp_cond_wait(c, m) pthread_cond_wait(&(c), &(m)) 00093 #define lscp_cond_signal(c) pthread_cond_signal(&(c)) 00094 #endif 00095 00096 //------------------------------------------------------------------------- 00097 // Threads. 00098 00099 struct _lscp_thread_t; 00100 00101 typedef void (*lscp_thread_proc_t)(void *pvData); 00102 00103 typedef struct _lscp_thread_t lscp_thread_t; 00104 00105 lscp_thread_t *lscp_thread_create (lscp_thread_proc_t pfnProc, void *pvData, int iDetach); 00106 lscp_status_t lscp_thread_join (lscp_thread_t *pThread); 00107 lscp_status_t lscp_thread_cancel (lscp_thread_t *pThread); 00108 lscp_status_t lscp_thread_destroy (lscp_thread_t *pThread); 00109 00110 #if defined(WIN32) 00111 #define lscp_thread_exit() ExitThread(0) 00112 #else 00113 #define lscp_thread_exit() pthread_exit(NULL) 00114 #endif 00115 00116 #if defined(__cplusplus) 00117 } 00118 #endif 00119 00120 #endif // __LSCP_THREAD_H 00121 00122 // end of thread.h