1*1c60b9acSAndroid Build Coastguard Worker# `lws_sul` scheduler api 2*1c60b9acSAndroid Build Coastguard Worker 3*1c60b9acSAndroid Build Coastguard WorkerSince v3.2 lws no longer requires periodic checking for timeouts and 4*1c60b9acSAndroid Build Coastguard Workerother events. A new system was refactored in where future events are 5*1c60b9acSAndroid Build Coastguard Workerscheduled on to a single, unified, sorted linked-list in time order, 6*1c60b9acSAndroid Build Coastguard Workerwith everything at us resolution. 7*1c60b9acSAndroid Build Coastguard Worker 8*1c60b9acSAndroid Build Coastguard WorkerThis makes it very cheap to know when the next scheduled event is 9*1c60b9acSAndroid Build Coastguard Workercoming and restrict the poll wait to match, or for event libraries 10*1c60b9acSAndroid Build Coastguard Workerset a timer to wake at the earliest event when returning to the 11*1c60b9acSAndroid Build Coastguard Workerevent loop. 12*1c60b9acSAndroid Build Coastguard Worker 13*1c60b9acSAndroid Build Coastguard WorkerEverything that was checked periodically was converted to use `lws_sul` 14*1c60b9acSAndroid Build Coastguard Workerand schedule its own later event. The end result is when lws is idle, 15*1c60b9acSAndroid Build Coastguard Workerit will stay asleep in the poll wait until a network event or the next 16*1c60b9acSAndroid Build Coastguard Workerscheduled `lws_sul` event happens, which is optimal for power. 17*1c60b9acSAndroid Build Coastguard Worker 18*1c60b9acSAndroid Build Coastguard Worker# Side effect for older code 19*1c60b9acSAndroid Build Coastguard Worker 20*1c60b9acSAndroid Build Coastguard WorkerIf your older code uses `lws_service_fd()`, it used to be necessary 21*1c60b9acSAndroid Build Coastguard Workerto call this with a NULL pollfd periodically to indicate you wanted 22*1c60b9acSAndroid Build Coastguard Workerto let the background checks happen. `lws_sul` eliminates the whole 23*1c60b9acSAndroid Build Coastguard Workerconcept of periodic checking and NULL is no longer a valid pollfd 24*1c60b9acSAndroid Build Coastguard Workervalue for this and related apis. 25*1c60b9acSAndroid Build Coastguard Worker 26*1c60b9acSAndroid Build Coastguard Worker# Using `lws_sul` in user code 27*1c60b9acSAndroid Build Coastguard Worker 28*1c60b9acSAndroid Build Coastguard WorkerSee `minimal-http-client-multi` for an example of using the `lws_sul` 29*1c60b9acSAndroid Build Coastguard Workerscheduler from your own code; it uses it to spread out connection 30*1c60b9acSAndroid Build Coastguard Workerattempts so they are staggered in time. You must create an 31*1c60b9acSAndroid Build Coastguard Worker`lws_sorted_usec_list_t` object somewhere, eg, in you own existing object. 32*1c60b9acSAndroid Build Coastguard Worker 33*1c60b9acSAndroid Build Coastguard Worker``` 34*1c60b9acSAndroid Build Coastguard Workerstatic lws_sorted_usec_list_t sul_stagger; 35*1c60b9acSAndroid Build Coastguard Worker``` 36*1c60b9acSAndroid Build Coastguard Worker 37*1c60b9acSAndroid Build Coastguard WorkerCreate your own callback for the event... the argument points to the sul object 38*1c60b9acSAndroid Build Coastguard Workerused when the callback was scheduled. You can use pointer arithmetic to translate 39*1c60b9acSAndroid Build Coastguard Workerthat to your own struct when the `lws_sorted_usec_list_t` was a member of the 40*1c60b9acSAndroid Build Coastguard Workersame struct. 41*1c60b9acSAndroid Build Coastguard Worker 42*1c60b9acSAndroid Build Coastguard Worker``` 43*1c60b9acSAndroid Build Coastguard Workerstatic void 44*1c60b9acSAndroid Build Coastguard Workerstagger_cb(lws_sorted_usec_list_t *sul) 45*1c60b9acSAndroid Build Coastguard Worker{ 46*1c60b9acSAndroid Build Coastguard Worker... 47*1c60b9acSAndroid Build Coastguard Worker} 48*1c60b9acSAndroid Build Coastguard Worker``` 49*1c60b9acSAndroid Build Coastguard Worker 50*1c60b9acSAndroid Build Coastguard WorkerWhen you want to schedule the callback, use `lws_sul_schedule()`... this will call 51*1c60b9acSAndroid Build Coastguard Workerit 10ms in the future 52*1c60b9acSAndroid Build Coastguard Worker 53*1c60b9acSAndroid Build Coastguard Worker``` 54*1c60b9acSAndroid Build Coastguard Worker lws_sul_schedule(context, 0, &sul_stagger, stagger_cb, 10 * LWS_US_PER_MS); 55*1c60b9acSAndroid Build Coastguard Worker``` 56*1c60b9acSAndroid Build Coastguard Worker 57*1c60b9acSAndroid Build Coastguard WorkerIn the case you destroy your object and need to cancel the scheduled callback, use 58*1c60b9acSAndroid Build Coastguard Worker 59*1c60b9acSAndroid Build Coastguard Worker``` 60*1c60b9acSAndroid Build Coastguard Worker lws_sul_schedule(context, 0, &sul_stagger, NULL, LWS_SET_TIMER_USEC_CANCEL); 61*1c60b9acSAndroid Build Coastguard Worker``` 62*1c60b9acSAndroid Build Coastguard Worker 63*1c60b9acSAndroid Build Coastguard Worker# lws_sul2 and system suspend 64*1c60b9acSAndroid Build Coastguard Worker 65*1c60b9acSAndroid Build Coastguard WorkerIn v4.1, alongside the existing `lws_sul` apis there is a refactor and additional 66*1c60b9acSAndroid Build Coastguard Workerfunctionality aimed at negotiating system suspend, while remaining completely 67*1c60b9acSAndroid Build Coastguard Workerbackwards-compatible with v3.2+ lws_sul apis. 68*1c60b9acSAndroid Build Coastguard Worker 69*1c60b9acSAndroid Build Coastguard WorkerDevicewide suspend is basically the withdrawal of CPU availability for an unbounded 70*1c60b9acSAndroid Build Coastguard Workeramount of time, so what may have been scheduled by the user code may miss its time 71*1c60b9acSAndroid Build Coastguard Workerslot because the cpu was down and nothing is getting serviced. Whether that is 72*1c60b9acSAndroid Build Coastguard Workeractively desirable, OK, a big disaster, or a failure that will be corrected at other 73*1c60b9acSAndroid Build Coastguard Workerlayers at the cost of, eg, some additional latency, depends on the required device 74*1c60b9acSAndroid Build Coastguard Workerbehaviours and the function of the user code that was scheduled, and its meaning to 75*1c60b9acSAndroid Build Coastguard Workerthe system. 76*1c60b9acSAndroid Build Coastguard Worker 77*1c60b9acSAndroid Build Coastguard WorkerBefore v4.1, lws just offers the same scheduling service for everything both internal 78*1c60b9acSAndroid Build Coastguard Workerand arranged by user code, and has no way to know what is critical for the device to 79*1c60b9acSAndroid Build Coastguard Workeroperate as intended, and so must force wake from suspend, or if for that scheduled 80*1c60b9acSAndroid Build Coastguard Workerevent 'failure [to get the event] is an option'. 81*1c60b9acSAndroid Build Coastguard Worker 82*1c60b9acSAndroid Build Coastguard WorkerFor example locally-initiated periodic keepalive pings not happening may allow 83*1c60b9acSAndroid Build Coastguard Workerpersistently dead (ie, no longer passing data) connections to remain unrenewed, but 84*1c60b9acSAndroid Build Coastguard Workereventually when suspend ends for another reason, the locally-initiated PING probes 85*1c60b9acSAndroid Build Coastguard Workerwill resume and it will be discovered and if the connectivity allows, corrected. 86*1c60b9acSAndroid Build Coastguard Worker 87*1c60b9acSAndroid Build Coastguard WorkerIf the device's function can handle the latency of there being no connectivity in 88*1c60b9acSAndroid Build Coastguard Workersuspend under those conditions until it wakes for another reason, it's OK for these 89*1c60b9acSAndroid Build Coastguard Workerkind of timeouts to be suppressed during suspend and basically take the power saving 90*1c60b9acSAndroid Build Coastguard Workerinstead. If for a particular device it's intolerable to ever have a silently dead 91*1c60b9acSAndroid Build Coastguard Workerconnection for more than a very short time compared to suspend durations, then these 92*1c60b9acSAndroid Build Coastguard Workerkind of timeouts must have the priority to wake the whole device from suspend so 93*1c60b9acSAndroid Build Coastguard Workerthey continue to operate unimpeded. 94*1c60b9acSAndroid Build Coastguard Worker 95*1c60b9acSAndroid Build Coastguard WorkerThat is just one example, lws offers generic scheduler services the user code can 96*1c60b9acSAndroid Build Coastguard Workerexploit for any purpose, including mission-critical ones. The changes give the user 97*1c60b9acSAndroid Build Coastguard Workercode a way to tell lws if a particular scheduled event is important enough to the 98*1c60b9acSAndroid Build Coastguard Workersystem operation to wake the system from devicewide suspend. 99*1c60b9acSAndroid Build Coastguard Worker 100