aboutsummaryrefslogtreecommitdiff
path: root/objectivec/GPBMessage_PackagePrivate.h
diff options
context:
space:
mode:
authorThomas Van Lenten <thomasvl@google.com>2016-03-21 11:11:14 -0400
committerThomas Van Lenten <thomasvl@google.com>2016-03-21 11:11:14 -0400
commitbd41a39f693d8307d407e42b634b315e075b6c8f (patch)
tree018b7755a546f18ee54e3fc48d6f8c69fdbcc67d /objectivec/GPBMessage_PackagePrivate.h
parent5e933847cc9e7826f1a9ee8b3dc1df4960b1ea5d (diff)
downloadprotobuf-bd41a39f693d8307d407e42b634b315e075b6c8f.tar.gz
protobuf-bd41a39f693d8307d407e42b634b315e075b6c8f.tar.bz2
protobuf-bd41a39f693d8307d407e42b634b315e075b6c8f.zip
Only create the readonlySemaphore on demand.
This will lower the amount of dispatch_semaphores created per Message when the full object tree isn't walked in a way that would require them to be created. Uses a dispatch_once_t for one time init of the dispatch_semaphore.
Diffstat (limited to 'objectivec/GPBMessage_PackagePrivate.h')
-rw-r--r--objectivec/GPBMessage_PackagePrivate.h12
1 files changed, 12 insertions, 0 deletions
diff --git a/objectivec/GPBMessage_PackagePrivate.h b/objectivec/GPBMessage_PackagePrivate.h
index b7e24fc9..478db2cf 100644
--- a/objectivec/GPBMessage_PackagePrivate.h
+++ b/objectivec/GPBMessage_PackagePrivate.h
@@ -67,6 +67,10 @@ typedef struct GPBMessage_Storage *GPBMessage_StoragePtr;
// priority inversion:
// http://mjtsai.com/blog/2015/12/16/osspinlock-is-unsafe/
// https://lists.swift.org/pipermail/swift-dev/Week-of-Mon-20151214/000372.html
+ // Use of readOnlySemaphore_ must be prefaced by a call to
+ // GPBPrepareReadOnlySemaphore to ensure it has been created. This allows
+ // readOnlySemaphore_ to be only created when actually needed.
+ dispatch_once_t readOnlySemaphoreCreationOnce_;
dispatch_semaphore_t readOnlySemaphore_;
}
@@ -103,6 +107,14 @@ typedef struct GPBMessage_Storage *GPBMessage_StoragePtr;
CF_EXTERN_C_BEGIN
+
+// Call this before using the readOnlySemaphore_. This ensures it is created only once.
+NS_INLINE void GPBPrepareReadOnlySemaphore(GPBMessage *self) {
+ dispatch_once(&self->readOnlySemaphoreCreationOnce_, ^{
+ self->readOnlySemaphore_ = dispatch_semaphore_create(1);
+ });
+}
+
// Returns a new instance that was automatically created by |autocreator| for
// its field |field|.
GPBMessage *GPBCreateMessageWithAutocreator(Class msgClass,