mirror of
https://github.com/gnustep/libs-performance.git
synced 2025-02-24 12:21:23 +00:00
make it clear we don't message items in FIFO
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/performance/trunk@33232 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
ef1806dff0
commit
7a937cd635
3 changed files with 52 additions and 45 deletions
|
@ -1,3 +1,10 @@
|
||||||
|
2011-06-02 Richard Frith-Macdonald <rfm@gnu.org>
|
||||||
|
|
||||||
|
* GSFIFO.h:
|
||||||
|
* GSFIFO.m:
|
||||||
|
Make FIFO items be void* rather than id since we never send them
|
||||||
|
any messages anyway.
|
||||||
|
|
||||||
2011-05-19 Richard Frith-Macdonald <rfm@gnu.org>
|
2011-05-19 Richard Frith-Macdonald <rfm@gnu.org>
|
||||||
|
|
||||||
* GNUmakefile: Add FIFO
|
* GNUmakefile: Add FIFO
|
||||||
|
|
54
GSFIFO.h
54
GSFIFO.h
|
@ -29,7 +29,7 @@
|
||||||
|
|
||||||
|
|
||||||
/** GSFIFO manages a first-in-first-out queue of items.<br />
|
/** GSFIFO manages a first-in-first-out queue of items.<br />
|
||||||
* Items in the queue ae <em>NOT</em> retained ... memory management
|
* Items in the queue ae <em>NOT</em> retained objects ... memory management
|
||||||
* is not the job of this class and it is not intended to be treated
|
* is not the job of this class and it is not intended to be treated
|
||||||
* as a 'collection', rather its role is intended to be that of an
|
* as a 'collection', rather its role is intended to be that of an
|
||||||
* inter-thread coordination mechanism.<br />
|
* inter-thread coordination mechanism.<br />
|
||||||
|
@ -69,7 +69,7 @@
|
||||||
*/
|
*/
|
||||||
volatile uint64_t _head;
|
volatile uint64_t _head;
|
||||||
volatile uint64_t _tail;
|
volatile uint64_t _tail;
|
||||||
id *_items;
|
void **_items;
|
||||||
uint32_t _capacity;
|
uint32_t _capacity;
|
||||||
@private
|
@private
|
||||||
uint16_t granularity;
|
uint16_t granularity;
|
||||||
|
@ -81,15 +81,15 @@
|
||||||
NSString *name;
|
NSString *name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the approximate number of objects in the FIFO.
|
/** Returns the approximate number of items in the FIFO.
|
||||||
*/
|
*/
|
||||||
- (NSUInteger) count;
|
- (NSUInteger) count;
|
||||||
|
|
||||||
/** Gets the next object from the FIFO, blocking if necessary until an
|
/** Gets the next item from the FIFO, blocking if necessary until an
|
||||||
* object is available. Raises an exception if the FIFO is configured
|
* item is available. Raises an exception if the FIFO is configured
|
||||||
* with a timeout and it is exceeded.
|
* with a timeout and it is exceeded.
|
||||||
*/
|
*/
|
||||||
- (id) get;
|
- (void*) get;
|
||||||
|
|
||||||
/** Initialises the receiver with the specified capacity (buffer size).<br />
|
/** Initialises the receiver with the specified capacity (buffer size).<br />
|
||||||
* If the granularity value is non-zero, it is treated as the maximum time
|
* If the granularity value is non-zero, it is treated as the maximum time
|
||||||
|
@ -98,10 +98,10 @@
|
||||||
* If the timeout value is non-zero, it is treated as the total time in
|
* If the timeout value is non-zero, it is treated as the total time in
|
||||||
* milliseconds for which a -get or -put: operation may block, and a
|
* milliseconds for which a -get or -put: operation may block, and a
|
||||||
* longer delay will cause those methods to raise an exception.<br />
|
* longer delay will cause those methods to raise an exception.<br />
|
||||||
* If the multiProducer flag is YES, the FIFI is configured to support
|
* If the multiProducer flag is YES, the FIFO is configured to support
|
||||||
* multiple producer threads (ie more than one thread using the -put:
|
* multiple producer threads (ie more than one thread using the -put:
|
||||||
* method) by using of locking while adding items to the FIFO.<br />
|
* method) by using of locking while adding items to the FIFO.<br />
|
||||||
* If the multiConsumer flag is YES, the FIFI is configured to support
|
* If the multiConsumer flag is YES, the FIFO is configured to support
|
||||||
* multiple consumer threads (ie more than one thread using the -get
|
* multiple consumer threads (ie more than one thread using the -get
|
||||||
* method) by using of locking while removng items from the FIFO.<br />
|
* method) by using of locking while removng items from the FIFO.<br />
|
||||||
* The name string is simply used to identify the receiver when printing
|
* The name string is simply used to identify the receiver when printing
|
||||||
|
@ -114,57 +114,57 @@
|
||||||
multiConsumer: (BOOL)mc
|
multiConsumer: (BOOL)mc
|
||||||
name: (NSString*)n;
|
name: (NSString*)n;
|
||||||
|
|
||||||
/** Adds an object to the FIFO, blocking if necessary until there is
|
/** Adds an item to the FIFO, blocking if necessary until there is
|
||||||
* space in the buffer. Raises an exception if the FIFO is configured
|
* space in the buffer. Raises an exception if the FIFO is configured
|
||||||
* with a timeout and it is exceeded.
|
* with a timeout and it is exceeded.
|
||||||
*/
|
*/
|
||||||
- (void) put: (id)item;
|
- (void) put: (void*)item;
|
||||||
|
|
||||||
/** Checks the FIFO and returns the first available object or nil if the
|
/** Checks the FIFO and returns the first available item or NULL if the
|
||||||
* FIFO is empty.
|
* FIFO is empty.
|
||||||
*/
|
*/
|
||||||
- (id) tryGet;
|
- (void*) tryGet;
|
||||||
|
|
||||||
/** Attempts to put an item into the FIFO, returning YES on success or NO
|
/** Attempts to put an item into the FIFO, returning YES on success or NO
|
||||||
* if the FIFO is full.
|
* if the FIFO is full.
|
||||||
*/
|
*/
|
||||||
- (BOOL) tryPut: (id)item;
|
- (BOOL) tryPut: (void*)item;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
/** Function to efficiently get an item from a fast FIFO.<br />
|
/** Function to efficiently get an item from a fast FIFO.<br />
|
||||||
* Returns nil if the FIFO is empty.<br />
|
* Returns NULL if the FIFO is empty.<br />
|
||||||
* Warning ... only for use if the FIFO is NOT configured for multiple
|
* Warning ... only for use if the FIFO is NOT configured for multiple
|
||||||
* consumers.
|
* consumers.
|
||||||
*/
|
*/
|
||||||
static inline id
|
static inline void*
|
||||||
GSGetFastNonBlockingFIFO(GSFIFO *receiver)
|
GSGetFastNonBlockingFIFO(GSFIFO *receiver)
|
||||||
{
|
{
|
||||||
if (receiver->_head > receiver->_tail)
|
if (receiver->_head > receiver->_tail)
|
||||||
{
|
{
|
||||||
id object;
|
void *item;
|
||||||
|
|
||||||
object = receiver->_items[receiver->_tail % receiver->_capacity];
|
item = receiver->_items[receiver->_tail % receiver->_capacity];
|
||||||
receiver->_tail++;
|
receiver->_tail++;
|
||||||
return object;
|
return item;
|
||||||
}
|
}
|
||||||
return nil;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Function to efficiently get an item from a fast FIFO, blocking if
|
/** Function to efficiently get an item from a fast FIFO, blocking if
|
||||||
* necessary until an object is available or the timeout occurs.<br />
|
* necessary until an item is available or the timeout occurs.<br />
|
||||||
* Warning ... only for use if the FIFO is NOT configured for multiple
|
* Warning ... only for use if the FIFO is NOT configured for multiple
|
||||||
* consumers.
|
* consumers.
|
||||||
*/
|
*/
|
||||||
static inline id
|
static inline void*
|
||||||
GSGetFastFIFO(GSFIFO *receiver)
|
GSGetFastFIFO(GSFIFO *receiver)
|
||||||
{
|
{
|
||||||
id object = GSGetFastNonBlockingFIFO(receiver);
|
void *item = GSGetFastNonBlockingFIFO(receiver);
|
||||||
|
|
||||||
if (nil == object)
|
if (0 == item)
|
||||||
{
|
{
|
||||||
object = [receiver get];
|
item = [receiver get];
|
||||||
}
|
}
|
||||||
return object;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Function to efficiently put an item to a fast FIFO.<br />
|
/** Function to efficiently put an item to a fast FIFO.<br />
|
||||||
|
@ -173,7 +173,7 @@ GSGetFastFIFO(GSFIFO *receiver)
|
||||||
* producers.
|
* producers.
|
||||||
*/
|
*/
|
||||||
static inline BOOL
|
static inline BOOL
|
||||||
GSPutFastNonBlockingFIFO(GSFIFO *receiver, id item)
|
GSPutFastNonBlockingFIFO(GSFIFO *receiver, void *item)
|
||||||
{
|
{
|
||||||
if (receiver->_head - receiver->_tail < receiver->_capacity)
|
if (receiver->_head - receiver->_tail < receiver->_capacity)
|
||||||
{
|
{
|
||||||
|
@ -191,7 +191,7 @@ GSPutFastNonBlockingFIFO(GSFIFO *receiver, id item)
|
||||||
* producers.
|
* producers.
|
||||||
*/
|
*/
|
||||||
static inline void
|
static inline void
|
||||||
GSPutFastFIFO(GSFIFO *receiver, id item)
|
GSPutFastFIFO(GSFIFO *receiver, void *item)
|
||||||
{
|
{
|
||||||
if (NO == GSPutFastNonBlockingFIFO(receiver, item))
|
if (NO == GSPutFastNonBlockingFIFO(receiver, item))
|
||||||
{
|
{
|
||||||
|
|
36
GSFIFO.m
36
GSFIFO.m
|
@ -57,9 +57,9 @@
|
||||||
(unsigned long long)fullCount];
|
(unsigned long long)fullCount];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id) get
|
- (void*) get
|
||||||
{
|
{
|
||||||
id obj;
|
void *item;
|
||||||
NSTimeInterval sum;
|
NSTimeInterval sum;
|
||||||
uint32_t old;
|
uint32_t old;
|
||||||
uint32_t fib;
|
uint32_t fib;
|
||||||
|
@ -68,9 +68,9 @@
|
||||||
{
|
{
|
||||||
if (_head > _tail)
|
if (_head > _tail)
|
||||||
{
|
{
|
||||||
obj = _items[_tail % _capacity];
|
item = _items[_tail % _capacity];
|
||||||
_tail++;
|
_tail++;
|
||||||
return obj;
|
return item;
|
||||||
}
|
}
|
||||||
emptyCount++;
|
emptyCount++;
|
||||||
}
|
}
|
||||||
|
@ -79,10 +79,10 @@
|
||||||
[getLock lock];
|
[getLock lock];
|
||||||
if (_head > _tail)
|
if (_head > _tail)
|
||||||
{
|
{
|
||||||
obj = _items[_tail % _capacity];
|
item = _items[_tail % _capacity];
|
||||||
_tail++;
|
_tail++;
|
||||||
[getLock unlock];
|
[getLock unlock];
|
||||||
return obj;
|
return item;
|
||||||
}
|
}
|
||||||
emptyCount++;
|
emptyCount++;
|
||||||
}
|
}
|
||||||
|
@ -112,10 +112,10 @@
|
||||||
[NSThread sleepForTimeInterval: dly];
|
[NSThread sleepForTimeInterval: dly];
|
||||||
sum += dly;
|
sum += dly;
|
||||||
}
|
}
|
||||||
obj = _items[_tail % _capacity];
|
item = _items[_tail % _capacity];
|
||||||
_tail++;
|
_tail++;
|
||||||
[getLock unlock];
|
[getLock unlock];
|
||||||
return obj;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id) initWithCapacity: (uint32_t)c
|
- (id) initWithCapacity: (uint32_t)c
|
||||||
|
@ -133,14 +133,14 @@
|
||||||
_capacity = c;
|
_capacity = c;
|
||||||
granularity = g;
|
granularity = g;
|
||||||
timeout = t;
|
timeout = t;
|
||||||
_items = (id*)NSZoneMalloc(NSDefaultMallocZone(), sizeof(id) * c);
|
_items = (void*)NSZoneMalloc(NSDefaultMallocZone(), sizeof(void*) * c);
|
||||||
if (YES == mp) putLock = [NSLock new];
|
if (YES == mp) putLock = [NSLock new];
|
||||||
if (YES == mc) getLock = [NSLock new];
|
if (YES == mc) getLock = [NSLock new];
|
||||||
name = [n copy];
|
name = [n copy];
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void) put: (id)item
|
- (void) put: (void*)item
|
||||||
{
|
{
|
||||||
NSTimeInterval sum;
|
NSTimeInterval sum;
|
||||||
uint32_t old;
|
uint32_t old;
|
||||||
|
@ -199,17 +199,17 @@
|
||||||
[putLock unlock];
|
[putLock unlock];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id) tryGet
|
- (void*) tryGet
|
||||||
{
|
{
|
||||||
id obj;
|
void *item;
|
||||||
|
|
||||||
if (nil == getLock)
|
if (nil == getLock)
|
||||||
{
|
{
|
||||||
if (_head > _tail)
|
if (_head > _tail)
|
||||||
{
|
{
|
||||||
obj = _items[_tail % _capacity];
|
item = _items[_tail % _capacity];
|
||||||
_tail++;
|
_tail++;
|
||||||
return obj;
|
return item;
|
||||||
}
|
}
|
||||||
emptyCount++;
|
emptyCount++;
|
||||||
}
|
}
|
||||||
|
@ -218,18 +218,18 @@
|
||||||
[getLock lock];
|
[getLock lock];
|
||||||
if (_head > _tail)
|
if (_head > _tail)
|
||||||
{
|
{
|
||||||
obj = _items[_tail % _capacity];
|
item = _items[_tail % _capacity];
|
||||||
_tail++;
|
_tail++;
|
||||||
[getLock unlock];
|
[getLock unlock];
|
||||||
return obj;
|
return item;
|
||||||
}
|
}
|
||||||
emptyCount++;
|
emptyCount++;
|
||||||
[getLock unlock];
|
[getLock unlock];
|
||||||
}
|
}
|
||||||
return nil;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL) tryPut: (id)item
|
- (BOOL) tryPut: (void*)item
|
||||||
{
|
{
|
||||||
if (nil == putLock)
|
if (nil == putLock)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue