Skip to content

Commit

Permalink
Remove IceUtil::Mutex from objc (#1684)
Browse files Browse the repository at this point in the history
  • Loading branch information
externl authored Jan 17, 2024
1 parent 9e40ca3 commit d42a669
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 22 deletions.
29 changes: 10 additions & 19 deletions objective-c/src/Ice/Initialize.mm
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@

namespace
{
typedef std::map<int, std::string> CompactIdMap;
using CompactIdMap = std::map<int, std::string>;

IceUtil::Mutex* _compactIdMapMutex = 0;
// The std::mutex consturctor is constexpr so this mutex is statically initialized
std::mutex _compactIdMapMutex;
CompactIdMap* _compactIdMap = 0;

class CompactIdResolverI : public Ice::CompactIdResolver
Expand All @@ -59,10 +60,9 @@
virtual ::std::string
resolve(::Ice::Int value) const
{
assert(_compactIdMapMutex);
assert(_compactIdMap);

IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(_compactIdMapMutex);
std::lock_guard lock(_compactIdMapMutex);
CompactIdMap::iterator p = _compactIdMap->find(value);
if(p != _compactIdMap->end())
{
Expand All @@ -73,8 +73,7 @@
};

//
// We don't use the constructor to initialize the compactIdMap as
// static initializtion takes place after +load is called.
// We don't use the constructor to initialize the compactIdMap as the initializtion takes place after +load is called.
//
class Init
{
Expand All @@ -87,12 +86,6 @@
delete _compactIdMap;
_compactIdMap = 0;
}

if(_compactIdMapMutex)
{
delete _compactIdMapMutex;
_compactIdMapMutex = 0;
}
}
};
Init init;
Expand All @@ -102,18 +95,15 @@
@implementation CompactIdMapHelper
+(void) initialize
{
assert(!_compactIdMapMutex);
assert(!_compactIdMap);
_compactIdMapMutex = new ::IceUtil::Mutex;
_compactIdMap = new CompactIdMap;
}

+(void) registerClass:(NSString*)type value:(ICEInt)value
{
assert(_compactIdMapMutex);
assert(_compactIdMap);

IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(_compactIdMapMutex);
std::lock_guard lock(_compactIdMapMutex);
CompactIdMap::iterator p = _compactIdMap->find(value);
if(p != _compactIdMap->end())
{
Expand All @@ -126,7 +116,7 @@ +(void) registerClass:(NSString*)type value:(ICEInt)value
namespace IceObjC
{

class ThreadNotification : public Ice::ThreadNotification, public IceUtil::Mutex
class ThreadNotification : public Ice::ThreadNotification
{
public:

Expand All @@ -136,13 +126,13 @@ +(void) registerClass:(NSString*)type value:(ICEInt)value

virtual void start()
{
Lock sync(*this);
std::lock_guard lock(_mutex);
_pools.insert(std::make_pair(IceUtil::ThreadControl().id(), [[NSAutoreleasePool alloc] init]));
}

virtual void stop()
{
Lock sync(*this);
std::lock_guard lock(_mutex);
std::map<IceUtil::ThreadControl::ID, NSAutoreleasePool*>::iterator p =
_pools.find(IceUtil::ThreadControl().id());
[p->second drain];
Expand All @@ -152,6 +142,7 @@ virtual void stop()
private:

std::map<IceUtil::ThreadControl::ID, NSAutoreleasePool*> _pools;
std::mutex _mutex;
};

};
Expand Down
2 changes: 1 addition & 1 deletion objective-c/src/Ice/PropertiesI.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

@interface ICENativePropertiesAdmin : ICEServantWrapper<ICENativePropertiesAdmin>
{
IceUtil::Mutex mutex_;
std::mutex mutex_;
std::vector<std::pair<id, std::function<void()>>> callbacks_;
}
@end
4 changes: 2 additions & 2 deletions objective-c/src/Ice/PropertiesI.mm
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ -(void) load:(NSString*)file
@implementation ICENativePropertiesAdmin
-(void) addUpdateCallback:(id<ICEPropertiesAdminUpdateCallback>)cb
{
IceUtil::Mutex::Lock sync(mutex_);
std::lock_guard lock(mutex_);

std::function<void()> remover = std::dynamic_pointer_cast<Ice::NativePropertiesAdmin>(object_)->addUpdateCallback(
[cb](const Ice::PropertyDict& properties)
Expand Down Expand Up @@ -254,7 +254,7 @@ -(void) addUpdateCallback:(id<ICEPropertiesAdminUpdateCallback>)cb

-(void) removeUpdateCallback:(id<ICEPropertiesAdminUpdateCallback>)cb
{
IceUtil::Mutex::Lock sync(mutex_);
std::lock_guard lock(mutex_);

// Each removeUpdateCallback only removes the first occurrence
auto p = std::find_if(callbacks_.begin(), callbacks_.end(), [cb](const auto& q) { return q.first == cb; });
Expand Down

0 comments on commit d42a669

Please sign in to comment.