Skip to content

Commit

Permalink
unDoTasks function
Browse files Browse the repository at this point in the history
  • Loading branch information
jamieboyd committed Nov 6, 2018
1 parent e126ba8 commit 0d99116
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Greeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ int main(int argc, char **argv){
return 0;
}
train1->DoTask ();// a train of 10 times should be enough, so just do it once
train2->DoTask ();
train2->DoOrUndoTasks (1);
// show that the train is running on its own
printf ("Trains were started and should be printing in a second\n");
// a loop to show we can do real work in the main thread while the pulsedThreads do their own things.
Expand Down
26 changes: 26 additions & 0 deletions pulsedThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ void pulsedThread::DoTasks(unsigned int nTasks){
pthread_mutex_unlock( &theTask.taskMutex );
}


/* ************ countermands currently requested tasks ***************************************
The thread will finish the current pulse or train of pulses then stop
Last Modified:
Expand All @@ -455,6 +456,31 @@ void pulsedThread::UnDoTasks (void){
}
}


/* ****************************************************************************************************
// gets the lock on the task and increments doTask to signal the thread to do task it is configured to do nTasks times
//Last Modified:
2018/11/05 by Jmie Boyd - added bounds checking
2018/10/25 by Jamie Boyd - initial version */
void pulsedThread::DoOrUndoTasks(int nTasks){
// for infinite task, this function is a NOP
if (theTask.nPulses != kINFINITETRAIN){
pthread_mutex_lock (&theTask.taskMutex);
// scrunch resulting value of doTask between 0 and kMODDELAY (first of signal bits)
unsigned int currentTasks = (theTask.doTask &~kMODANY);
if ((int)(currentTasks + nTasks) < 0){
theTask.doTask &= 1;
}else{
if ((int)currentTasks + nTasks >= kMODDELAY){
theTask.doTask = kMODDELAY -1;
}else{
theTask.doTask +=nTasks;
}
}
pthread_cond_signal(&theTask.taskVar);
pthread_mutex_unlock( &theTask.taskMutex );
}
}
/* ****************************************************************************************************
/returns 0 if a thread is not currently doing a task, else returns number of tasks still left to do
Last Modified 2015/09/28 by Jamie Boyd - initial verison */
Expand Down
1 change: 1 addition & 0 deletions pulsedThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ class pulsedThread{
void DoTask (void); // requests that the thread perform its task once, as currently configured, if not an infinite train, or will start an infinite train
void DoTasks (unsigned int nTasks); // requests that the thread perform its task nTasks times, as currently configured, or
void UnDoTasks (void); // removes requested tasks in doTask, save 1. Thread stops after current task
void DoOrUndoTasks(int nTasks); // signed integer, to add OR ubtract from the number of tasks left to do
int isBusy(void); // checks if a task is busy, returns how many tasks are left to do
int waitOnBusy(float timeOut); // doesn't return until a thread is no longer busy
// for infinite trains
Expand Down

0 comments on commit 0d99116

Please sign in to comment.