Here is my network handler:
https://gist.github.com/anonymous/22fc110ad126ef3a2c5f
The problem is that when data is received it blocks my animiation (I have a wheel spinning when data has been requested) so it looks like the app have crashed and then after 1 second or 2, when the data has been received, it works like a charm.
This line seems to make sure that when waiting for an answer the app doesn't freeze:
local input,output = socket.select( { self.sock }, nil, 0 ) -- this is a way not to block runtime while reading socket. zero timeout does the trick
Here is another timeout setting:
self.sock:settimeout(0)
I have tried to change them to 0.01 and 0.001 but with no luck. I'm not really sure how to make it so the animation doesn't freeze.
Maybe I should change to one of Coronas built in async network handler (with callback functions) or is it possible to modify this network handler so that the animation doesn't freeze?
If you can use Corona's network.* api, it's async so you won't have any blocking calls.
Documentation link: http://docs.coronalabs.com/daily/api/library/network/index.html
Related
The deprecated UpdateSystemActivity() works by "notifying the Power Manager that activity has taken place and the timers used to measure idle time should be updated to the time of this call."
The documentation recommends using IOPMAssertionCreateWithName(...) but this method doesn't reset the timer for the idle time; it prevents the machine from sleeping forever until IOPMAssertionRelease(...) is called. Using IOPMAssertionCreateWithName(...) is unsuitable for my current codebase.
Are there any non-deprecated methods that reset the timer for measuring idle time?
Thanks!
"it prevents the machine from sleeping forever until IOPMAssertionRelease(...) is called"
I don't observe this behavior with macOS 10.15, not for IOPMAssertionCreateWithName(...) . In contrast to documentation IOPMAssertionDeclareUserActivity(...) seems to behave like UpdateSystemActivity() and needs to be called periodically.
See snippet of Screen Saver timer event
examples/common/ScreenSaver.cpp
I ran into this same problem and discovered this closely related question which points to the IOPMAssertionDeclareUserActivity function.
You can use this:
IOPMAssertionID assertionID;
IOPMAssertionDeclareUserActivity(CFSTR(""), kIOPMUserActiveLocal, &assertionID);
Here is the answer in Swift 3.x:
var assertionID : IOPMAssertionID = 0
_ = IOPMAssertionDeclareUserActivity("TeslaWatcher" as CFString, kIOPMUserActiveLocal, &assertionID)
I have a specific task routine which performs some operations in a specific order, and these operations handle few volatile variables. There is a specific interrupt which updates these volatile variables asynchronously. Hence, the task routine should restart if such an interrupt occurs. Normally FreeRTOS will resume the task, but this will result in wrong derived values, hence the requirement for restarting the routine. I also cannot keep the task routine under critical section, because I should not be missing any interrupts.
Is there a way in FreeRTOS with which I can achieve this? Like a vtaskRestart API. I could have deleted the task and re-created it, but this adds a lot of memory management complications, which I would like to avoid. Currently my only option is to add checks in the routine on a flag to see if a context switch have occured and if yes, restart, else continue.
Googling did not fetch any clue on this. Seems like people never faced such a problem or may be its that this design is poor. In FreeRTOS forum, few who asked for a task-restart didn't seem to have this same problem. stackOverflow didn't have a result on freertos + task + restart. So, this could be the first post with this tag combination ;)
Can someone please tell me if this is directly possible in FreeRTOS?
You can use semaphore for this purpose. If you decide using semaphore, you should do the steps below.
Firstly, you should create a binary semaphore.
The semaphore must be given in the interrupt routine with
xSemaphoreGiveFromISR( Example_xSemaphore, &xHigherPriorityTaskWoken
);
And, you must check taking semaphore in the task.
void vExample_Task( void * pvParameters )
{
for( ;; )
{
if (xSemaphoreTake( Example_xSemaphore, Example_PROCESS_TIME)==pdTRUE)
{
}
}
}
For this purpose you should use a queue and use the queue peek function to yield at your volatile data.
I'm using it as I have a real time timer and this way I make the time available to all my task, without any blocking.
Here it how it goes:
Declare the queue:
xQueueHandle RTC_Time_Queue;
Create the queue of 1 element:
RTC_Time_Queue = xQueueCreate( 1, sizeof(your volatile struct) );
Overwrite the queue everytime your interrupt occurs:
xQueueOverwriteFromISR(RTC_Time_Queue, (void*) &time);
And from other task peek the queue:
xQueuePeek(RTC_GetReadQueue(), (void*) &TheTime, 0);
The 0 at the end of xQueuePeek means you don't want to wait if the queue is empty. The queue peek won't delete the value in the queue so it will be present every time you peek and the code will never stop.
Also you should avoid having variable being accessed from ISR and the RTOS code as you may get unexpected corruption.
NB: The entire code base for this project is so large that posting any meaningful amount wold render this question too localised, I have tried to distil any code down to the bare-essentials. I'm not expecting anyone to solve my problems directly but I will up vote those answers I find helpful or intriguing.
This project uses a modified version of AudioStreamer to playback audio files that are saved to locally to the device (iPhone).
The stream is set up and scheduled on the current loop using this code (unaltered from the standard AudioStreamer project as far as I know):
CFStreamClientContext context = {0, self, NULL, NULL, NULL};
CFReadStreamSetClient(
stream,
kCFStreamEventHasBytesAvailable | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered,
ASReadStreamCallBack,
&context);
CFReadStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
The ASReadStreamCallBack calls:
- (void)handleReadFromStream:(CFReadStreamRef)aStream
eventType:(CFStreamEventType)eventType
On the AudioStreamer object, this all works fine until the stream is read using this code:
BOOL hasBytes = NO; //Added for debugging
hasBytes = CFReadStreamHasBytesAvailable(stream);
length = CFReadStreamRead(stream, bytes, kAQDefaultBufSize);
hasBytes is YES but when CFReadStreamRead is called execution stops, the App does not crash it just stops exciting, any break points below the CFReadStreamRead call are not hit and ASReadStreamCallBack is not called again.
I am at a loss to what might cause this, my best guess is the thread is being terminated? But the hows and whys is why I'm asking SO.
Has anyone seen this behaviour before? How can I track it down and ideas on how I might solve it will be very much welcome!
Additional Info Requested via Comments
This is 100% repeatable
CFReadStreamHasBytesAvailable was added by me for debugging but removing it has no effect
First, I assume that CFReadStreamScheduleWithRunLoop() is running on the same thread as CFReadStreamRead()?
Is this thread processing its runloop? Failure to do this is my main suspicion. Do you have a call like CFRunLoopRun() or equivalent on this thread?
Typically there is no reason to spawn a separate thread for reading streams asynchronously, so I'm a little confused about your threading design. Is there really a background thread involved here? Also, typically CFReadStreamRead() would be in your client callback (when you receive the kCFStreamEventHasBytesAvailable event (which it appears to be in the linked code), but you're suggesting ASReadStreamCallBack is never called. How have you modified AudioStreamer?
It is possible that the stream pointer is just corrupt in some way. CFReadStreamRead should certainly not block if bytes are available (it certainly would never block for more than a few milliseconds for local files). Can you provide the code you use to create the stream?
Alternatively, CFReadStreams send messages asynchronously but it is possible (but not likely) that it's blocking because the runloop isn't being processed.
If you prefer, I've uploaded my AudioPlayer inspired by Matt's AudioStreamer hosted at https://code.google.com/p/audjustable/. It supports local files (as well as HTTP). I think it does what you wanted (stream files from more than just HTTP).
I have a task to wrap a 3rd party video decoder library in a direct show transform filter.
I believe this is going to be a problem as the 3rd party library uses an asynchronous callback when a full frame is ready i.e.
// on main thread
lib->RegisterCallback(callback function)
lib->write(raw data bytes)
void callback(frame)
{
// here is your frame (on a worker thread)
}
When I look at the pure virtual CTransformFilter.Transform function it expects the transform to be synchronous. Now I could set this up so it blocks on a event that is Set in the callback but what happens if the data coming in to the Transform function is not sufficient to generate a full new frame? I would deadlock the function.
Is my only option to go back to the library developer and ask for a synchronous decoder?
I've just been looking at the CTransformFilter Receive function. This is what calls the (overridden) Transform function. It then calls m_pOutput->m_pInputPin->Receive(pOutSample) to pass the sample onwards. Can I call this Receive function from the worker thread in the callback or do I have to keep everything on the same thread?
Thanks
You can still achieve what you want. Note that a filter is not required to output a media sample on the same thread and/or within the call it receives an input media sample. CTransformFilter is however made with this assumption in mind.
So basically your straightforward choices are:
step back from CTransformFilter and use its ancestor to inherit from to implement output media sample delivery from [a worker thread's] callback call
wait within Transform function for asynchronous completion (makes sense if the inner library still decodes 1 output frame for 1 input frame) and catch up then delivering the output; you will also have to wait for pause requests there and abort your wait in order to not block the execution
I have been doing overlapped serial port communication in Delphi lately and there is one problem I'm not sure how to solve.
I communicate with a modem. I write a request frame (an AT command) to the modem's COM port and then wait for the modem to respond. The event mask of the port is set to EV_RXCHAR, so when I write a request, I call WaitCommEvent() and start waiting for data to appear in the input queue. When overlapped waiting for event finishes, I immediately start reading data from the queue and read all that the device sends at once:
1) write a request
2) call WaitCommEvent() and wait until waiting finishes
3) read all the data that the device sends (not only the data being in the input queue at that moment)
4) do something and then goto 1
Waiting for event finishes after first byte appears in the input queue. During my read operation, however, more bytes appear in the queue and each of them causes an internal event flag to be set. This means that when I read all the data from the queue and then call WaitCommEvent() for the second time, it will immediately return with EV_RXCHAR mask, even though there is no data to be read.
How should I handle reading and waiting for event to be sure that the event mask returned by WaitCommEvent() is always valid? Is it possible to reset the flags of the serial port so that when I read all data from the queue and call WaitCommEvent() after then, it will not return immediately with a mask that was valid before I read the data?
The only solution that comes to my mind is this:
1) write a request
2) call WaitCommEvent() and wait until waiting finishes
3) read all the data that the device sends (not only the data being in the input queue at that moment)
4) call WaitCommEvent() which should return true immediately at the same time resetting the event flag set internally
5) do something and goto 1
Is it a good idea or is it stupid? Of course I know that the modem almost always finishes its answers with CRLF characters so I could set the comm mask to EV_RXFLAG and wait for the #10 character to appear, but there are many other devices with which I communicate and they do not always send frame end characters.
Your help will be appreciated. Thanks in advance!
Mariusz.
Your solution does sound workable. I just use a state machine to handle the transitions.
(psuedocode)
ioState := ioIdle;
while (ioState <> ioFinished) and (not aborted) do
Case ioState of
ioIdle : if there is data to read then set state to ioMidFrame
ioMidframe : if data to read then read, if end of frame set to ioEndFrame
ioEndFrame : process the data and set to ioFinished
ioFinished : // don't do anything, for doc purposes only.
end;