Skip to content
d3x0r edited this page Nov 20, 2016 · 3 revisions

#idle.h

Sometimes threads in callbacks need to be dispatched back to their thread start. (A windows mouse callback that generates a show/hide for instance). This allows registering thread procs for idle. These idle routines should check they are for the correct thread; but otherwise can be useful to yield in a callback and allow other work to be done.

// return -1 if not the correct thread
// return 0 if no events processed
// return 1 if events were processed
typedef int (CPROC *IdleProc)(uintptr_t);

IDLE_PROC( void, AddIdleProc )( IdleProc Proc, uintptr_t psvUser );
IDLE_PROC( int, RemoveIdleProc )( IdleProc Proc );

IDLE_PROC( int, Idle )( void );
IDLE_PROC( int, IdleFor )( uint32_t dwMilliseconds );

//----------------

int myIdleCallback( uintptr_t userData ) {
    if( MakeThread() == /* some valid thread */ ) {
       //do soemthing
       return 1;
    }
    return -1;  // will no longer be called on this thread.
}

void g(){
   AddIdleProc( myIdleProc, (uintptr_t)"some data; can be a pointer" );
}

later when doing something, Idle()... or IdleFor()


void f() {
   Idle(); // calls all idle procs.
}

This calls all idle procs for 2000 millliseconds, if non were on this thread, and this one is really just waiting, this turns into a WakeableSleep()... so this thread can be woken with WakeThread();

void f() {
   IdleFor(2000); 
}

Clone this wiki locally