xdasd_thread.h

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------------
00002  * Copyright (c) 2006, Novell, Inc.
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions are 
00007  * met:
00008  * 
00009  *     * Redistributions of source code must retain the above copyright 
00010  *       notice, this list of conditions and the following disclaimer.
00011  *     * Redistributions in binary form must reproduce the above copyright 
00012  *       notice, this list of conditions and the following disclaimer in the 
00013  *       documentation and/or other materials provided with the distribution.
00014  *     * Neither the name of the Novell nor the names of its contributors 
00015  *       may be used to endorse or promote products derived from this 
00016  *       software without specific prior written permission.
00017  * 
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00019  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00020  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00021  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00022  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
00025  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
00026  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *--------------------------------------------------------------------------*/
00030 
00039 #ifndef XDASD_THREAD_H_INCLUDED
00040 #define XDASD_THREAD_H_INCLUDED
00041 
00047 #ifdef _WIN32
00048 # define WIN32_LEAN_AND_MEAN
00049 # define _WIN32_WINNT 0x0400
00050 # pragma warning(disable: 4201) /* non-standard extension used : nameless structure/union */
00051 # include <windows.h>
00052 
00053 /* mutex */
00054 # define mutex_t         CRITICAL_SECTION
00055 # define mutex_create    InitializeCriticalSection
00056 # define mutex_destroy   DeleteCriticalSection
00057 # define mutex_acquire   EnterCriticalSection
00058 # define mutex_release   LeaveCriticalSection
00059 
00060 /* semaphore */
00061 # define sem_t           HANDLE
00062 # define sem_create(y)   CreateSemaphore(0,(y),0x7FFFFFFFL,0)) == 0? -1: 0)
00063 # define sem_destroy     (void)CloseHandle
00064 # define sem_wait(x)     (void)WaitForSingleObject((x),INFINITE)
00065 # define sem_signal(x,y) (void)ReleaseSemaphore((x),(y),0)
00066 
00067 #else
00068 # include <pthread.h>
00069 
00070 /* mutex */
00071 # define mutex_t         pthread_mutex_t
00072 # define mutex_create(x) (void)pthread_mutex_init((x),0)
00073 # define mutex_destroy   (void)pthread_mutex_destroy
00074 # define mutex_acquire   (void)pthread_mutex_lock
00075 # define mutex_release   (void)pthread_mutex_unlock
00076 
00077 /* cv */
00078 # define cv_t            pthread_cond_t
00079 # define cv_create(x)    pthread_cond_init((x),0)
00080 # define cv_destroy      pthread_cond_destroy
00081 # define cv_signal       pthread_cond_signal
00082 # define cv_wait         pthread_cond_wait
00083 
00084 #endif
00085 
00086 typedef void * thread_t;
00087 
00088 thread_t thread_create(void *(*thread_proc)(void *), void * arg);
00089 void * thread_wait(thread_t th);
00090 
00091 #ifdef _WIN32
00092 
00093 typedef struct cv_t
00094 {
00095    long waitersBlocked;             /* Number of threads blocked */
00096    long waitersGone;                /* Number of threads timed out */
00097    long waitersToUnblock;           /* Number of threads to unblock */
00098    HANDLE semBlockQueue;            /* Queue up waiting threads */
00099    HANDLE semBlockLock;             /* Guards access to waiters queue */
00100    CRITICAL_SECTION csUnblockLock;  /* Guards access to waiters to unblock */
00101 } cv_t;
00102 
00103 int cv_create(cv_t * cvp);
00104 void cv_destroy(cv_t * cvp);
00105 int cv_wait(cv_t * cvp, mutex_t * mp);
00106 int cv_signal(cv_t * cvp);
00107 int cv_broadcast(cv_t * cvp);
00108 
00109 #else
00110 
00111 typedef struct sem_t
00112 {
00113    pthread_mutex_t lock;
00114    pthread_cond_t cond;
00115    int count;
00116 } sem_t;
00117 
00118 int sem_create(sem_t * sp, int count);
00119 void sem_destroy(sem_t * sp);
00120 void sem_wait(sem_t * sp);
00121 void sem_signal(sem_t * sp, int count);
00122 
00123 #endif
00124 
00128 #endif   /* XDASD_THREAD_H_INCLUDED */
00129 

Generated on Thu Aug 20 22:33:06 2009 for OpenXDAS by  doxygen 1.5.6