diff options
author | Thomas Van Lenten <thomasvl@google.com> | 2017-03-29 10:46:04 -0400 |
---|---|---|
committer | Bo Yang <paulyang1211@gmail.com> | 2017-04-04 10:17:52 -0700 |
commit | 7d758c00bc55a8997d29dedbc50de7eaf4d71280 (patch) | |
tree | 9eb231a14630464409ee2a40b96d7fb70358c6ca /objectivec/GPBMessage.m | |
parent | 4f44a0f4bb55997522e5e9574f143d5e63ec1936 (diff) | |
download | protobuf-7d758c00bc55a8997d29dedbc50de7eaf4d71280.tar.gz protobuf-7d758c00bc55a8997d29dedbc50de7eaf4d71280.tar.bz2 protobuf-7d758c00bc55a8997d29dedbc50de7eaf4d71280.zip |
Remove the use of dispatch_once that is heap backed.
Apple recently updated the docs on dispatch_once to point out
that the storage for the dispatch_once_t must be static or global,
but not something that was ever used before as the implementation
doesn't use a memory barrier. So we drop the use and create the
semaphore when needed and use an atomic swap deal with any
threading races.
Diffstat (limited to 'objectivec/GPBMessage.m')
-rw-r--r-- | objectivec/GPBMessage.m | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/objectivec/GPBMessage.m b/objectivec/GPBMessage.m index 58a10fdb..78935b19 100644 --- a/objectivec/GPBMessage.m +++ b/objectivec/GPBMessage.m @@ -738,6 +738,25 @@ void GPBClearMessageAutocreator(GPBMessage *self) { self->autocreatorExtension_ = nil; } +// Call this before using the readOnlySemaphore_. This ensures it is created only once. +void GPBPrepareReadOnlySemaphore(GPBMessage *self) { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdirect-ivar-access" +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + + // Create the semaphore on demand (rather than init) as developers might not cause them + // to be needed, and the heap usage can add up. The atomic swap is used to avoid needing + // another lock around creating it. + if (self->readOnlySemaphore_ == nil) { + dispatch_semaphore_t worker = dispatch_semaphore_create(1); + if (!OSAtomicCompareAndSwapPtrBarrier(NULL, worker, (void * volatile *)&(self->readOnlySemaphore_))) { + dispatch_release(worker); + } + } + +#pragma clang diagnostic pop +} + static GPBUnknownFieldSet *GetOrMakeUnknownFields(GPBMessage *self) { if (!self->unknownFields_) { self->unknownFields_ = [[GPBUnknownFieldSet alloc] init]; |