Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members  

NativeLock.cpp

00001 /*
00002  * NOTE: "zlib/libpng" style License
00003  *
00004  *                 ----=== s t d n e t ===----
00005  *
00006  * Copyright (c) 2002 Warwick Molloy   w-molloy@users.sf.net
00007  *
00008  * Copyright (c) 2002 Stdnet Community
00009  * HTTP://Sourceforge.net/projects/stdnet
00010  *
00011  * All references to "software" refer to the stdnet.
00012  *
00013  * This software is provided 'as-is', without any express or
00014  * implied warranty. In no event will the authors be held liable
00015  * for any damages arising from the use of this software.
00016  *
00017  * Permission is granted to anyone to use this software for any
00018  * purpose, including commercial applications, and to alter it
00019  * and redistribute it freely, subject to the following
00020  * restrictions:
00021  *
00022  * 1.  The origin of this software must not be misrepresented;
00023  *     you must not claim that you wrote the original software.
00024  *     If you use this software in a product, an acknowledgment
00025  *     in the product documentation would be appreciated but
00026  *     is not required.
00027  *
00028  * 2.  Altered source versions must be plainly marked as such,
00029  *     and must not be misrepresented as being the original
00030  *     software.
00031  *
00032  * 3.  This notice may not be removed or altered from any source
00033  *     distribution.
00034  */
00035 
00036 #define __USE_GNU
00037 #include <pthread.h>
00038 #include <time.h>
00039 
00040 #include <stdnet/system/platform/posix/NativeLock>
00041 
00042 namespace stdbase {
00043     namespace system {
00044         namespace platform {
00045 
00046 Lock::
00047     Lock()
00048     :   mutex(),
00049         condition()
00050 {
00051     pthread_mutex_init( & mutex, NULL );
00052     pthread_cond_init( & condition, NULL );
00053     locked = false;
00054 }
00055 
00056 Lock::
00057     ~Lock()
00058 {
00059     pthread_cond_destroy( & condition );
00060     pthread_mutex_destroy( & mutex );
00061 }
00062 
00063 void Lock::
00064     seize()
00065 {
00066     int val = pthread_mutex_lock( & mutex );
00067     locked = true;
00068 }
00069 
00070 bool Lock::
00071     trySeize()
00072 {
00073     int val = pthread_mutex_trylock( & mutex );
00074     return val == 0;
00075 }
00076 
00077 void Lock::
00078     release()
00079 {
00080     int val;
00081     
00082     locked = false;
00083     val = pthread_mutex_unlock( & mutex );
00084 }
00085 
00086 void Lock::
00087     waitSignal()
00088 {
00089     int result = pthread_cond_wait( & condition, &mutex );
00090 }
00091 
00092 bool Lock::
00093     timedWaitSignal( int millisec )
00094 {
00095     bool    retval;
00096     int     result;
00097     struct timespec stop_time;
00098 
00099     stop_time.tv_sec = time( NULL ) + (millisec / 1000);
00100     stop_time.tv_nsec = (millisec % 1000) * 1000;
00101 
00102     result = pthread_cond_timedwait( &condition, &mutex, &stop_time);
00103     return retval == 0;
00104 }
00105 
00106 void Lock::
00107     signal()
00108 {
00109     int result;
00110     result = pthread_cond_signal( & condition );
00111 }
00112 
00113 void Lock::
00114     signalAll()
00115 {
00116     int result;
00117     result = pthread_cond_broadcast( & condition );
00118 }
00119 
00120         } // -- end platform --
00121     } // -- end system --
00122 } // -- end stdbase --
00123 

Generated at Tue Aug 13 14:19:39 2002 for stdnet2 by doxygen1.2.9.1 written by Dimitri van Heesch, © 1997-2001