2012-03-27 17:05:19 +00:00
|
|
|
/* Support header for conditionally enabling use of libdispatch.
|
|
|
|
Copyright (C) 2012 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Niels Grewe <niels.grewe@halbordnung.de>
|
|
|
|
Date: March 2012
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2019-12-09 23:36:00 +00:00
|
|
|
Lesser General Public License for more details.
|
2012-03-27 17:05:19 +00:00
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
2019-12-09 23:36:00 +00:00
|
|
|
Boston, MA 02110 USA.
|
2012-03-27 17:05:19 +00:00
|
|
|
*/
|
|
|
|
|
2013-08-22 15:44:54 +00:00
|
|
|
#import "GNUstepBase/GSBlocks.h"
|
|
|
|
|
2012-03-27 17:05:19 +00:00
|
|
|
#if HAVE_DISPATCH_H
|
|
|
|
#include <dispatch.h>
|
|
|
|
#elif HAVE_DISPATCH_DISPATCH_H
|
|
|
|
#include <dispatch/dispatch.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If gnustep-base is built with libdispatch support, these macros will expand
|
|
|
|
* to code for creating and cleaning up after libdispach queues to which blocks
|
|
|
|
* can be submitted. If libdispatch is not available, setup and teardown will
|
|
|
|
* be no-ops, and the block will simply be executed on the calling thread.
|
|
|
|
*/
|
|
|
|
#if __has_feature(blocks) && (GS_USE_LIBDISPATCH == 1)
|
|
|
|
|
|
|
|
/*
|
2013-08-22 15:44:54 +00:00
|
|
|
* Older versions of libdispatch do not support concurrent queues.
|
|
|
|
* We define away the attributes in this case.
|
2012-03-27 17:05:19 +00:00
|
|
|
*/
|
|
|
|
#ifndef DISPATCH_QUEUE_SERIAL
|
|
|
|
#define DISPATCH_QUEUE_SERIAL NULL
|
|
|
|
#endif
|
|
|
|
#ifndef DISPATCH_QUEUE_CONCURRENT
|
|
|
|
#define DISPATCH_QUEUE_CONCURRENT NULL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define GS_DISPATCH_GET_DEFAULT_CONCURRENT_QUEUE() dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
|
|
|
|
/**
|
|
|
|
* This macro creates a dispatch queue using the attributes
|
|
|
|
*/
|
|
|
|
#define GS_DISPATCH_QUEUE_CREATE(attr) dispatch_queue_create(NULL, attr)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a dispatch group
|
|
|
|
*/
|
|
|
|
#define GS_DISPATCH_GROUP_CREATE() dispatch_group_create()
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait for the dispatch group to finish
|
|
|
|
*/
|
|
|
|
#define GS_DISPATCH_GROUP_FINISH(group) dispatch_group_wait(group, DISPATCH_TIME_FOREVER)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release an dispatch object.
|
|
|
|
*/
|
|
|
|
#define GS_DISPATCH_RELEASE(x) dispatch_release(x)
|
2019-09-25 09:14:43 +00:00
|
|
|
|
2012-03-27 17:05:19 +00:00
|
|
|
/**
|
2019-09-25 09:14:43 +00:00
|
|
|
* Allows an arbitrary block to be submitted to the queue (if available) or run
|
|
|
|
* in place. Since dispatch blocks return nothing and take no arguments, the
|
|
|
|
* caller can use the before and after arguments to guard calling the block as
|
|
|
|
* required.
|
2012-03-27 17:05:19 +00:00
|
|
|
*/
|
|
|
|
#define GS_DISPATCH_SUBMIT_BLOCK(group, queue, before, after, block, args, ...) \
|
2019-09-25 09:14:43 +00:00
|
|
|
if (queue != NULL) {\
|
|
|
|
dispatch_group_async(group, queue, ^(void){before block(args, ## __VA_ARGS__); after});\
|
|
|
|
} else {\
|
|
|
|
before\
|
|
|
|
block(args, ## __VA_ARGS__);\
|
|
|
|
after\
|
|
|
|
}
|
2012-03-27 17:05:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Submits a block without special provisions.
|
|
|
|
*/
|
|
|
|
#define GS_DISPATCH_SUBMIT_BLOCK_NO_ARGS(group, queue, block) dispatch_group_async(group, queue, block)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-09-25 09:14:43 +00:00
|
|
|
* Convenience macro to create concurrent dispatch queues for the various
|
|
|
|
* -enumerateUsingBlock: methods. Non-concurrent will be run in place.
|
2012-03-27 17:05:19 +00:00
|
|
|
*/
|
2024-07-03 15:21:13 +00:00
|
|
|
#define GS_DISPATCH_CREATE_QUEUE_AND_GROUP_FOR_ENUMERATION(queue, opts) {\
|
2019-09-25 09:14:43 +00:00
|
|
|
dispatch_queue_t queue = NULL;\
|
|
|
|
dispatch_group_t queue ## Group = NULL;\
|
2012-03-27 17:05:19 +00:00
|
|
|
if (opts & NSEnumerationConcurrent)\
|
2024-07-03 15:21:13 +00:00
|
|
|
{\
|
|
|
|
queue = GS_DISPATCH_GET_DEFAULT_CONCURRENT_QUEUE();\
|
|
|
|
queue ## Group = GS_DISPATCH_GROUP_CREATE();\
|
|
|
|
}
|
2012-03-27 17:05:19 +00:00
|
|
|
|
|
|
|
/**
|
2019-09-25 09:14:43 +00:00
|
|
|
* Convenience macro to destroy concurrent dispatch queues for the various
|
|
|
|
* -enumerateUsingBlock: methods.
|
2012-03-27 17:05:19 +00:00
|
|
|
*/
|
|
|
|
#define GS_DISPATCH_TEARDOWN_QUEUE_AND_GROUP_FOR_ENUMERATION(queue, opts)\
|
2019-09-25 09:14:43 +00:00
|
|
|
if (queue != NULL) { \
|
|
|
|
GS_DISPATCH_GROUP_FINISH(queue ## Group);\
|
|
|
|
GS_DISPATCH_RELEASE(queue ## Group);\
|
|
|
|
if (NO == (opts & NSEnumerationConcurrent))\
|
2024-07-03 15:21:13 +00:00
|
|
|
{\
|
|
|
|
GS_DISPATCH_RELEASE(queue);\
|
|
|
|
}\
|
|
|
|
}\
|
|
|
|
}
|
2012-03-27 17:05:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No-Op defintions if libdispatch is not supposed to be used.
|
|
|
|
*/
|
|
|
|
#define DISPATCH_QUEUE_SERIAL 0
|
|
|
|
#define DISPATCH_QUEUE_CONCURRENT 0
|
|
|
|
#define dispatch_queue_attr_t int
|
|
|
|
#define dispatch_queue_t int
|
|
|
|
#define dispatch_group_t int
|
|
|
|
#define GS_DISPATCH_GET_DEFAULT_CONCURRENT_QUEUE() 0
|
|
|
|
#define GS_DISPATCH_QUEUE_CREATE(attr) 0
|
|
|
|
#define GS_DISPATCH_GROUP_CREATE() 0
|
|
|
|
#define GS_DISPATCH_GROUP_FINISH(group)
|
|
|
|
#define GS_DISPATCH_RELEASE(x)
|
|
|
|
#define GS_DISPATCH_SUBMIT_BLOCK(group, queue, before, after, block, args...) CALL_BLOCK(block, args)
|
|
|
|
#define GS_DISPATCH_SUBMIT_BLOCK_NO_ARGS(group, queue, block) CALL_BLOCK_NO_ARGS(block)
|
|
|
|
#define GS_DISPATCH_CREATE_QUEUE_AND_GROUP_FOR_ENUMERATION(queue, opts)
|
|
|
|
#define GS_DISPATCH_TEARDOWN_QUEUE_AND_GROUP_FOR_ENUMERATION(queue, opts)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|