00001 /* 00002 * Copyright (C) 2009, Edmundo Albuquerque de Souza e Silva. 00003 * 00004 * This file may be distributed under the terms of the Q Public License 00005 * as defined by Trolltech AS of Norway and appearing in the file 00006 * LICENSE.QPL included in the packaging of this file. 00007 * 00008 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING 00009 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00010 * PURPOSE. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, 00011 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 00012 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00013 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00014 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00015 * 00016 * Thanks: Jose Renato Santos 00017 * 00018 */ 00019 00020 /////////////////////////////////////////////////////////////////////////////// 00021 // riofreelist.cpp -- free list primitives for pthreads 00022 /////////////////////////////////////////////////////////////////////////////// 00023 00024 // (Could write a cover class for type safety for a specific element 00025 // (type... 00026 00027 #include <riofreelist.h> 00028 00029 #include <stddef.h> // for offsetof macro 00030 00031 #include <td4list.h> 00032 00033 /////////////////////////////////////////////////////////////////////////////// 00034 CRioFreeList::CRioFreeList() 00035 { 00036 m_waitflag = 0; 00037 m_first = NULL; 00038 } 00039 00040 /////////////////////////////////////////////////////////////////////////////// 00041 CRioFreeList::~CRioFreeList() 00042 { 00043 } 00044 00045 /////////////////////////////////////////////////////////////////////////////// 00046 // Put -- add element to end of queue 00047 void CRioFreeList::Free(RioQueueEle *ep) 00048 { 00049 m_cond.Lock(); 00050 ep->linkf = m_first; 00051 m_first = ep; 00052 if(m_waitflag) 00053 { 00054 m_cond.Signal(); 00055 m_waitflag = 0; 00056 } 00057 m_cond.Unlock(); 00058 } 00059 00060 00061 /////////////////////////////////////////////////////////////////////////////// 00062 // Get -- Wait for element available on queue and get first one when available 00063 RioQueueEle *CRioFreeList::Get() 00064 { 00065 RioQueueEle *ep; 00066 00067 m_cond.Lock(); 00068 while(m_first == 0) 00069 { 00070 m_waitflag = 1; 00071 m_cond.Wait(); 00072 } 00073 00074 ep = m_first; 00075 m_first = ep->linkf; 00076 00077 m_waitflag = 0; 00078 00079 m_cond.Unlock(); 00080 00081 return ep; 00082 00083 } 00084 00085 /////////////////////////////////////////////////////////////////////////////// 00086 // GetNB -- Get first element from queue if available. 00087 // Otherrwise return null 00088 // ( Non Blocking) 00089 RioQueueEle *CRioFreeList::GetNB() 00090 { 00091 RioQueueEle *ep; 00092 00093 m_cond.Lock(); 00094 00095 ep = m_first; 00096 00097 if(m_first != 0) 00098 { 00099 m_first = ep->linkf; 00100 } 00101 00102 m_cond.Unlock(); 00103 00104 return ep; 00105 00106 } 00107