diff --git a/ruby/lib/google/protobuf/descriptor_dsl.rb b/ruby/lib/google/protobuf/descriptor_dsl.rb index 3c1b8e41d0..ba1a255f04 100644 --- a/ruby/lib/google/protobuf/descriptor_dsl.rb +++ b/ruby/lib/google/protobuf/descriptor_dsl.rb @@ -7,7 +7,24 @@ require 'google/protobuf/descriptor_pb' module Google module Protobuf module Internal + class AtomicCounter + def initialize + @n = 0 + @mu = Mutex.new + end + + def get_and_increment + n = @n + @mu.synchronize { + @n += 1 + } + return n + end + end + class Builder + @@file_number = AtomicCounter.new + def initialize(pool) @pool = pool @default_file = nil # Constructed lazily @@ -42,7 +59,9 @@ module Google end private def internal_default_file - @default_file ||= FileBuilder.new(@pool, "ruby_default_file.proto") + number = @@file_number.get_and_increment + filename = "ruby_default_file#{number}.proto" + @default_file ||= FileBuilder.new(@pool, filename) end end