diff --git a/framework/system/foundation/Bee_AutoLock.h b/framework/system/foundation/Bee_AutoLock.h new file mode 100644 index 0000000..456b36a --- /dev/null +++ b/framework/system/foundation/Bee_AutoLock.h @@ -0,0 +1,45 @@ +// +// ______ ______ ______ +// /\ __ \ /\ ___\ /\ ___\ +// \ \ __< \ \ __\_ \ \ __\_ +// \ \_____\ \ \_____\ \ \_____\ +// \/_____/ \/_____/ \/_____/ +// +// +// Copyright (c) 2014-2015, Geek Zoo Studio +// http://www.bee-framework.com +// +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. +// + +#import + +#define Bee_AutoLock(__lock) \ +[__lock lock]; \ +void (^ __strong autoLock)() __attribute__((cleanup(_autoLock), unused)) = ^\ +{[__lock unlock];}; + +@interface BeeAutoLock : NSObject + ++ (instancetype)lock:(NSLock *)lock; +- (instancetype)initWithLock:(NSLock *)lock; + +@end + diff --git a/framework/system/foundation/Bee_AutoLock.m b/framework/system/foundation/Bee_AutoLock.m new file mode 100644 index 0000000..47bb5a9 --- /dev/null +++ b/framework/system/foundation/Bee_AutoLock.m @@ -0,0 +1,61 @@ +// +// ______ ______ ______ +// /\ __ \ /\ ___\ /\ ___\ +// \ \ __< \ \ __\_ \ \ __\_ +// \ \_____\ \ \_____\ \ \_____\ +// \/_____/ \/_____/ \/_____/ +// +// +// Copyright (c) 2014-2015, Geek Zoo Studio +// http://www.bee-framework.com +// +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. +// + +#import "Bee_AutoLock.h" +#import "Bee_Thread.h" + +@implementation BeeAutoLock +{ + id _lock; +} + ++ (instancetype)lock:(NSLock *)lock +{ + return [[self alloc] initWithLock:lock]; +} + +- (instancetype)initWithLock:(NSLock *)lock +{ + if (self = [super init]) { + _lock = lock; + [_lock lock]; + } + return self; +} + +- (void)dealloc +{ + [super dealloc]; + [_lock unlock]; +} + +@end + diff --git a/projects/scaffold/scaffold.xcodeproj/project.pbxproj b/projects/scaffold/scaffold.xcodeproj/project.pbxproj index e6fea69..27cf681 100644 --- a/projects/scaffold/scaffold.xcodeproj/project.pbxproj +++ b/projects/scaffold/scaffold.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 2271980D1A36E81A00933DCA /* Bee_AutoLock.m in Sources */ = {isa = PBXBuildFile; fileRef = 2271980C1A36E81A00933DCA /* Bee_AutoLock.m */; }; CB5F01AD1A12466E0055CF23 /* BeeUIStack+Popover.m in Sources */ = {isa = PBXBuildFile; fileRef = CB5F01AC1A12466E0055CF23 /* BeeUIStack+Popover.m */; }; CBA903ED183F578A00CB3FD5 /* libxml2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CBA903EB183F578A00CB3FD5 /* libxml2.dylib */; }; CBA903EE183F578A00CB3FD5 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CBA903EC183F578A00CB3FD5 /* libz.dylib */; }; @@ -389,6 +390,8 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 2271980B1A36E81A00933DCA /* Bee_AutoLock.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Bee_AutoLock.h; sourceTree = ""; }; + 2271980C1A36E81A00933DCA /* Bee_AutoLock.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Bee_AutoLock.m; sourceTree = ""; }; CB5F01AB1A12466E0055CF23 /* BeeUIStack+Popover.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "BeeUIStack+Popover.h"; sourceTree = ""; }; CB5F01AC1A12466E0055CF23 /* BeeUIStack+Popover.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "BeeUIStack+Popover.m"; sourceTree = ""; }; CBA903EB183F578A00CB3FD5 /* libxml2.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libxml2.dylib; path = ../../../../../../../usr/lib/libxml2.dylib; sourceTree = ""; }; @@ -2148,6 +2151,8 @@ E96D7F2A19A48C89005D467C /* foundation */ = { isa = PBXGroup; children = ( + 2271980B1A36E81A00933DCA /* Bee_AutoLock.h */, + 2271980C1A36E81A00933DCA /* Bee_AutoLock.m */, E96D7F2B19A48C89005D467C /* Bee_Assertion.h */, E96D7F2C19A48C89005D467C /* Bee_Assertion.mm */, E96D7F2D19A48C89005D467C /* Bee_Foundation.h */, @@ -2720,6 +2725,7 @@ E96D80A819A48C8A005D467C /* Bee_UILayoutContainer.m in Sources */, E96D811119A48C8A005D467C /* NSObject+BeeMacros.mm in Sources */, E96D806219A48C8A005D467C /* UIView+BeeExtension.m in Sources */, + 2271980D1A36E81A00933DCA /* Bee_AutoLock.m in Sources */, E96D812519A48C8A005D467C /* NSObject+BeeHTTPRequest.m in Sources */, E96D811819A48C8A005D467C /* NSString+BeeEmoji.m in Sources */, E96D804219A48C8A005D467C /* UIImageView+BeeUIStyle.m in Sources */,