Mercurial > projects > hoofbaby
comparison deps/Platinum/ThirdParty/Neptune/Source/System/PSP/NptPSPQueue.cpp @ 0:3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
author | fraserofthenight |
---|---|
date | Mon, 06 Jul 2009 08:06:28 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3425707ddbf6 |
---|---|
1 /***************************************************************** | |
2 | | |
3 | Neptune - Queue :: PSP Implementation | |
4 | | |
5 | (c) 2001-2002 Gilles Boccon-Gibod | |
6 | Author: Sylvain Rebaud | |
7 | | |
8 ****************************************************************/ | |
9 | |
10 /*---------------------------------------------------------------------- | |
11 | includes | |
12 +---------------------------------------------------------------------*/ | |
13 #include "NptConfig.h" | |
14 #include "NptTypes.h" | |
15 #include "NptQueue.h" | |
16 //#include "NptThreads.h" | |
17 #include "NptList.h" | |
18 #include "NptDebug.h" | |
19 | |
20 /*---------------------------------------------------------------------- | |
21 | NPT_PSPQueue | |
22 +---------------------------------------------------------------------*/ | |
23 class NPT_PSPQueue : public NPT_GenericQueue | |
24 { | |
25 public: | |
26 // methods | |
27 NPT_PSPQueue(NPT_Cardinal max_items); | |
28 ~NPT_PSPQueue(); | |
29 NPT_Result Push(NPT_QueueItem* item); | |
30 NPT_Result Pop(NPT_QueueItem*& item, NPT_Boolean blocking); | |
31 | |
32 | |
33 private: | |
34 // members | |
35 NPT_Cardinal m_MaxItems; | |
36 //NPT_Mutex m_Mutex; | |
37 //pthread_cond_t m_CanPushOrPopCondition; | |
38 NPT_List<NPT_QueueItem*> m_Items; | |
39 }; | |
40 | |
41 /*---------------------------------------------------------------------- | |
42 | NPT_PSPQueue::NPT_PSPQueue | |
43 +---------------------------------------------------------------------*/ | |
44 NPT_PSPQueue::NPT_PSPQueue(NPT_Cardinal max_items) : | |
45 m_MaxItems(max_items) | |
46 { | |
47 NPT_Debug(":: NPT_PSPQueue::NPT_PSPQueue\n"); | |
48 | |
49 //pthread_cond_init(&m_CanPushOrPopCondition, NULL); | |
50 } | |
51 | |
52 /*---------------------------------------------------------------------- | |
53 | NPT_PSPQueue::~NPT_PSPQueue() | |
54 +---------------------------------------------------------------------*/ | |
55 NPT_PSPQueue::~NPT_PSPQueue() | |
56 { | |
57 // destroy resources | |
58 //pthread_cond_destroy(&m_CanPushOrPopCondition); | |
59 } | |
60 | |
61 /*---------------------------------------------------------------------- | |
62 | NPT_PSPQueue::Push | |
63 +---------------------------------------------------------------------*/ | |
64 NPT_Result | |
65 NPT_PSPQueue::Push(NPT_QueueItem* item) | |
66 { | |
67 // lock the mutex that protects the list | |
68 m_Items.Lock(); | |
69 | |
70 // check that we have not exceeded the max | |
71 //if (m_MaxItems) { | |
72 // while (m_Items.GetItemCount() >= m_MaxItems) { | |
73 // // wait until some items have been removed | |
74 // //NPT_Debug(":: NPT_PSPQueue::Push - waiting for queue to empty\n"); | |
75 // pthread_cond_wait(&m_CanPushOrPopCondition, &m_Mutex); | |
76 // } | |
77 //} | |
78 | |
79 // add the item to the list | |
80 m_Items.Add(item); | |
81 | |
82 // if the list was previously empty, signal the condition | |
83 // to wake up the waiting thread | |
84 //if (m_Items.GetItemCount() == 1) { | |
85 // pthread_cond_signal(&m_CanPushOrPopCondition); | |
86 //} | |
87 | |
88 // unlock the mutex | |
89 m_Items.Unlock(); | |
90 | |
91 return NPT_SUCCESS; | |
92 } | |
93 | |
94 /*---------------------------------------------------------------------- | |
95 | NPT_PSPQueue::Pop | |
96 +---------------------------------------------------------------------*/ | |
97 NPT_Result | |
98 NPT_PSPQueue::Pop(NPT_QueueItem*& item, NPT_Boolean blocking) | |
99 { | |
100 // lock the mutex that protects the list | |
101 m_Items.Lock(); | |
102 | |
103 NPT_Result result; | |
104 //if (blocking) { | |
105 // while ((result = m_Items.PopHead(item)) == NPT_ERROR_LIST_EMPTY) { | |
106 // // no item in the list, wait for one | |
107 // //NPT_Debug(":: NPT_PSPQueue::Pop - waiting for queue to fill up\n"); | |
108 // pthread_cond_wait(&m_CanPushOrPopCondition, &m_Mutex); | |
109 // } | |
110 //} else { | |
111 result = m_Items.PopHead(item); | |
112 //} | |
113 | |
114 // if the list was previously full, signal the condition | |
115 // to wake up the waiting thread | |
116 //if (m_MaxItems && (result == NPT_SUCCESS)) { | |
117 // if (m_Items.GetItemCount() == m_MaxItems-1) { | |
118 // pthread_cond_signal(&m_CanPushOrPopCondition); | |
119 // } | |
120 //} | |
121 | |
122 // unlock the mutex | |
123 m_Items.Unlock(); | |
124 | |
125 return result; | |
126 } | |
127 | |
128 /*---------------------------------------------------------------------- | |
129 | NPT_GenericQueue::CreateInstance | |
130 +---------------------------------------------------------------------*/ | |
131 NPT_GenericQueue* | |
132 NPT_GenericQueue::CreateInstance(NPT_Cardinal max_items) | |
133 { | |
134 NPT_Debug(":: NPT_GenericQueue::CreateInstance - queue max_items = %ld\n", | |
135 max_items); | |
136 return new NPT_PSPQueue(max_items); | |
137 } | |
138 | |
139 | |
140 | |
141 |