Skip to content

Commit e862cf1

Browse files
committed
tweaks to taskqueue, alphawheel orientation, formatting tweaks
1 parent cf957e1 commit e862cf1

File tree

4 files changed

+49
-36
lines changed

4 files changed

+49
-36
lines changed

blocks/BloomTasks/src/TaskQueue.h

+44-30
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class TaskQueue {
2323

2424
static void pushTask( VoidFunc f )
2525
{
26-
// TODO: later, use a single thread and queue the tasks
26+
// TODO: later, optionally use a single thread and queue the tasks
2727
// e.g. http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html
2828
// for now, a thread per task will help spec out the right interface:
2929
std::thread taskThread( &TaskQueue::doWork, f );
@@ -67,7 +67,7 @@ class UiTaskQueue {
6767
return mCompletedTasks > taskId;
6868
}
6969

70-
// only call from main thread, no canceling from background threads mmmkay?
70+
// only call from main thread, no canceling from background threads yet :(
7171
static void cancelTask( uint64_t taskId )
7272
{
7373
if (!isTaskComplete(taskId)) {
@@ -76,44 +76,58 @@ class UiTaskQueue {
7676
}
7777

7878
// call this from the UI thread, or pain will occur
79-
static void popTask( )
79+
// default ~5ms budget (but will always do at least one call)
80+
static void update( const float &budgetSeconds = 0.005f )
8081
{
81-
float t = ci::app::getElapsedSeconds();
82-
do {
83-
// TODO: within a certain time limit, keep popping tasks
84-
if (mFunctionsMutex.try_lock()) {
85-
if (mFunctions.empty()) {
86-
mFunctionsMutex.unlock();
87-
break; // quit the do/while
82+
// empty while loop *shiver*
83+
const float t = ci::app::getElapsedSeconds();
84+
while ( popTask() && ci::app::getElapsedSeconds() - t < budgetSeconds );
85+
}
86+
87+
private:
88+
89+
// returns true if it got a lock and has more tasks, false otherwise
90+
static bool popTask( )
91+
{
92+
if (mFunctionsMutex.try_lock()) {
93+
if (mFunctions.empty()) {
94+
mFunctionsMutex.unlock();
95+
return false;
96+
}
97+
else {
98+
// remember this id and increment too;
99+
// (it's about to be gone from mFunctions so cancelTask won't work anyway)
100+
uint64_t thisTaskId = mCompletedTasks++;
101+
102+
// grab the next task and remove it; there is no undo
103+
VoidFunc f = mFunctions.front();
104+
mFunctions.pop();
105+
106+
// let callers know if it's worth calling again
107+
bool hasMoreTasks = !mFunctions.empty();
108+
109+
// unlock first so that tasks can queue other tasks
110+
mFunctionsMutex.unlock();
111+
112+
// if it's not canceled, do it (otherwise just clean up the canceled marker)
113+
std::set<uint64_t>::iterator iter = mCanceledIds.find( thisTaskId );
114+
if ( iter == mCanceledIds.end() ) {
115+
f();
88116
}
89117
else {
90-
uint64_t thisTaskId = mCompletedTasks++; // remember this id and increment too;
91-
// (it's about to be gone from mFunctions so cancelTask won't work anyway)
92-
VoidFunc f = mFunctions.front(); // grab the next task
93-
mFunctions.pop(); // remove it; there is no undo
94-
mFunctionsMutex.unlock(); // unlock first so that tasks can queue other tasks
95-
std::set<uint64_t>::iterator iter = mCanceledIds.find( thisTaskId );
96-
if ( iter == mCanceledIds.end() ) {
97-
// if it's not canceled, do it
98-
std::cout << "performing task " << thisTaskId << std::endl;
99-
f();
100-
}
101-
else {
102-
std::cout << "skipping task " << thisTaskId << std::endl;
103-
// otherwise remove it
104-
mCanceledIds.erase( iter );
105-
}
118+
mCanceledIds.erase( iter );
106119
}
120+
121+
return hasMoreTasks;
107122
}
108-
} while ( ci::app::getElapsedSeconds() - t < 0.005 ); // ~5ms budget (but always do at least one call)
123+
}
124+
return false;
109125
}
110126

111-
private:
112-
113127
static std::mutex mFunctionsMutex;
114128
static std::queue<VoidFunc> mFunctions;
115129
static uint64_t mCompletedTasks;
116130
static uint64_t mTotalTasks;
117-
static std::set<uint64_t> mCanceledIds;
131+
static std::set<uint64_t> mCanceledIds; // FIXME: no mutex for canceling so not thread safe :(
118132

119133
};

src/AlphaChooser.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ bool AlphaChooser::touchBegan( TouchEvent::Touch touch )
7070
{
7171
if (!mWheelOverlay->getShowWheel()) return false;
7272
Vec2f pos = globalToLocal( touch.getPos() );
73-
// TODO: make a padded hitrect
7473
return mFullRect.contains( pos );
7574
}
7675

@@ -104,13 +103,13 @@ bool AlphaChooser::touchEnded( TouchEvent::Touch touch )
104103
if( mAlphaChar != mAlphaString[i] ){
105104
mAlphaChar = mAlphaString[i];
106105
mCallbacksAlphaCharSelected.call( mAlphaChar );
107-
mWheelOverlay->setShowWheel(false);
108106
}
107+
mWheelOverlay->setShowWheel(false);
109108
return true;
110109
}
111110
}
112111

113-
return false;
112+
return mFullRect.contains( pos );
114113
}
115114

116115
void AlphaChooser::setNumberAlphaPerChar( float *numberAlphaPerChar )

src/KeplerApp.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,7 @@ void KeplerApp::update()
16181618
}
16191619

16201620
// do UI thread things...
1621-
UiTaskQueue::popTask();
1621+
UiTaskQueue::update();
16221622
}
16231623

16241624
void KeplerApp::updateArcball()

src/OrbitRing.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void OrbitRing::setup()
4343
glGenBuffers(1, &mLowResVBO);
4444
glBindBuffer(GL_ARRAY_BUFFER, mLowResVBO);
4545
glBufferData(GL_ARRAY_BUFFER, sizeof(VertexData) * G_RING_LOW_RES, mVertsLowRes, GL_STATIC_DRAW);
46-
glBindBuffer(GL_ARRAY_BUFFER,0); // Leave no VBO bound.
46+
glBindBuffer(GL_ARRAY_BUFFER, 0); // Leave no VBO bound.
4747

4848
delete[] mVertsLowRes;
4949

@@ -59,7 +59,7 @@ void OrbitRing::setup()
5959
glGenBuffers(1, &mHighResVBO);
6060
glBindBuffer(GL_ARRAY_BUFFER, mHighResVBO);
6161
glBufferData(GL_ARRAY_BUFFER, sizeof(VertexData) * G_RING_HIGH_RES, mVertsHighRes, GL_STATIC_DRAW);
62-
glBindBuffer(GL_ARRAY_BUFFER,0); // Leave no VBO bound.
62+
glBindBuffer(GL_ARRAY_BUFFER, 0); // Leave no VBO bound.
6363

6464
delete[] mVertsHighRes;
6565
}

0 commit comments

Comments
 (0)