@@ -45,7 +45,7 @@ ServiceRegistry& ServiceRegistry::operator=(ServiceRegistry const& other)
4545ServiceRegistry::ServiceRegistry ()
4646{
4747 for (size_t i = 0 ; i < MAX_SERVICES ; ++i) {
48- mServicesKey [i].store (0L );
48+ mServicesKey [i].store ({ 0L , 0L } );
4949 }
5050
5151 mServicesValue .fill (nullptr );
@@ -79,8 +79,8 @@ void ServiceRegistry::registerService(ServiceTypeHash typeHash, void* service, S
7979 if (mServicesBooked [i + index.index ].compare_exchange_strong (expected, true ,
8080 std::memory_order_seq_cst)) {
8181 mServicesValue [i + index.index ] = service;
82- mServicesMeta [i + index.index ] = Meta{kind, salt };
83- mServicesKey [i + index.index ] = typeHash. hash ;
82+ mServicesMeta [i + index.index ] = Meta{kind, name ? strdup (name) : nullptr };
83+ mServicesKey [i + index.index ] = Key{. store = {. typeHash = typeHash, . salt = salt}} ;
8484 std::atomic_thread_fence (std::memory_order_release);
8585 return ;
8686 }
@@ -270,7 +270,7 @@ int ServiceRegistry::getPos(ServiceTypeHash typeHash, Salt salt) const
270270 InstanceId instanceId = instanceFromTypeSalt (typeHash, salt);
271271 Index index = indexFromInstance (instanceId);
272272 for (uint8_t i = 0 ; i < MAX_DISTANCE ; ++i) {
273- if (mServicesKey [i + index.index ].load () == typeHash.hash ) {
273+ if (mServicesKey [i + index.index ].load (). value == Key{ typeHash.hash , salt}. value ) {
274274 return i + index.index ;
275275 }
276276 }
@@ -279,30 +279,36 @@ int ServiceRegistry::getPos(ServiceTypeHash typeHash, Salt salt) const
279279
280280void * ServiceRegistry::get (ServiceTypeHash typeHash, Salt salt, ServiceKind kind, char const * name) const
281281{
282+ // Cannot find a stream service using a global salt.
283+ if (salt.context .streamId == GLOBAL_CONTEXT_SALT .value && kind == ServiceKind::Stream) {
284+ throwError (runtime_error (" Cannot find a global service using a stream salt." ));
285+ }
282286 // Look for the service. If found, return it.
283287 // Notice how due to threading issues, we might
284288 // find it with getPos, but the value can still
285289 // be nullptr.
286290 auto pos = getPos (typeHash, salt);
287- if (pos != -1 && mServicesMeta [pos].kind == ServiceKind::Stream && mServicesMeta [pos].salt .value != salt.value ) {
288- throwError (runtime_error_f (" Inconsistent registry for thread %d. Expected %d" , salt.context .streamId , mServicesMeta [pos].salt .context .streamId ));
291+ // If we are here it means we never registered a
292+ // service for the 0 thread (i.e. the main thread).
293+ if (pos != -1 && mServicesMeta [pos].kind == ServiceKind::Stream && mServicesKey [pos].load ().store .salt .value != salt.value ) {
294+ throwError (runtime_error_f (" Inconsistent registry for thread %d. Expected %d" , salt.context .streamId , mServicesKey [pos].load ().store .salt .context .streamId ));
289295 O2_BUILTIN_UNREACHABLE ();
290296 }
291297
292- bool isStream = mServicesMeta [pos].kind == ServiceKind::DataProcessorStream || mServicesMeta [pos].kind == ServiceKind::DeviceStream;
293- bool isDataProcessor = mServicesMeta [pos].kind == ServiceKind::DataProcessorStream || mServicesMeta [pos].kind == ServiceKind::DataProcessorGlobal || mServicesMeta [pos].kind == ServiceKind::DataProcessorSerial;
298+ if (pos != -1 ) {
299+ bool isStream = mServicesMeta [pos].kind == ServiceKind::DataProcessorStream || mServicesMeta [pos].kind == ServiceKind::DeviceStream;
300+ bool isDataProcessor = mServicesMeta [pos].kind == ServiceKind::DataProcessorStream || mServicesMeta [pos].kind == ServiceKind::DataProcessorGlobal || mServicesMeta [pos].kind == ServiceKind::DataProcessorSerial;
294301
295- if (pos != - 1 && isStream && salt.context .streamId <= 0 ) {
296- throwError (runtime_error_f (" A stream service cannot be retrieved from a non stream salt %d" , salt.context .streamId ));
297- O2_BUILTIN_UNREACHABLE ();
298- }
299-
300- if (pos != - 1 && isDataProcessor && salt.context .dataProcessorId < 0 ) {
301- throwError (runtime_error_f (" A data processor service cannot be retrieved from a non dataprocessor context %d" , salt.context .dataProcessorId ));
302- O2_BUILTIN_UNREACHABLE ();
303- }
302+ if ( isStream && salt.context .streamId <= 0 ) {
303+ throwError (runtime_error_f (" A stream service cannot be retrieved from a non stream salt %d" , salt.context .streamId ));
304+ O2_BUILTIN_UNREACHABLE ();
305+ }
306+
307+ if ( isDataProcessor && salt.context .dataProcessorId < 0 ) {
308+ throwError (runtime_error_f (" A data processor service cannot be retrieved from a non dataprocessor context %d" , salt.context .dataProcessorId ));
309+ O2_BUILTIN_UNREACHABLE ();
310+ }
304311
305- if (pos != -1 ) {
306312 mServicesKey [pos].load ();
307313 std::atomic_thread_fence (std::memory_order_acquire);
308314 void * ptr = mServicesValue [pos];
@@ -328,8 +334,6 @@ void* ServiceRegistry::get(ServiceTypeHash typeHash, Salt salt, ServiceKind kind
328334 throwError (runtime_error_f (" Unable to find requested service %s" , name));
329335 }
330336 }
331- // If we are here it means we never registered a
332- // service for the 0 thread (i.e. the main thread).
333337 return nullptr ;
334338}
335339
0 commit comments