Index: src/OFThread.h ================================================================== --- src/OFThread.h +++ src/OFThread.h @@ -17,10 +17,12 @@ #import "OFObject.h" #import "OFList.h" #import "threading.h" +@class OFDate; + /** * \brief A class for Thread Local Storage keys. */ @interface OFTLSKey: OFObject { @@ -114,10 +116,15 @@ /** * Suspends execution of the current thread for N milliseconds. */ + (void)sleepForNMilliseconds: (unsigned int)msecs; +/** + * Suspends execution of the current thread until the specified date. + */ ++ (void)sleepUntilDate: (OFDate*)date; + /** * Yields a processor voluntarily and moves the thread at the end of the queue * for its priority. */ + (void)yield; Index: src/OFThread.m ================================================================== --- src/OFThread.m +++ src/OFThread.m @@ -23,10 +23,11 @@ # include #endif #import "OFThread.h" #import "OFList.h" +#import "OFDate.h" #import "OFAutoreleasePool.h" #import "OFExceptions.h" #import "threading.h" @@ -101,10 +102,31 @@ usleep(msecs * 1000); #else Sleep(msecs); #endif } + ++ (void)sleepUntilDate: (OFDate*)date +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFDate *now = [OFDate date]; + int64_t sec; + uint32_t usec; + + if ((sec = [date timeIntervalSinceDate: now]) < 0) + @throw [OFOutOfRangeException newWithClass: self]; + + usec = [date microsecondsOfTimeIntervalSinceDate: now]; + + [pool release]; + +#ifndef _WIN32 + usleep(sec * 1000000 + usec); +#else + Sleep(sec * 1000 + usec / 1000); +#endif +} + (void)yield { #ifndef _WIN32 sched_yield();