diff --git a/Greeter.cpp b/Greeter.cpp index 576fd54..415393d 100755 --- a/Greeter.cpp +++ b/Greeter.cpp @@ -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. diff --git a/pulsedThread.cpp b/pulsedThread.cpp index a438a42..17f9a89 100755 --- a/pulsedThread.cpp +++ b/pulsedThread.cpp @@ -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: @@ -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 */ diff --git a/pulsedThread.h b/pulsedThread.h index d362125..c62772c 100755 --- a/pulsedThread.h +++ b/pulsedThread.h @@ -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