176: def generate(format = :default)
177: template = FORMATS[format]
178:
179: raise ArgumentError, "invalid UUID format #{format.inspect}" unless template
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191: clock = @mutex.synchronize do
192: clock = (Time.new.to_f * CLOCK_MULTIPLIER).to_i & 0xFFFFFFFFFFFFFFF0
193:
194: if clock > @last_clock then
195: @drift = 0
196: @last_clock = clock
197: elsif clock == @last_clock then
198: drift = @drift += 1
199:
200: if drift < 10000 then
201: @last_clock += 1
202: else
203: Thread.pass
204: nil
205: end
206: else
207: next_sequence
208: @last_clock = clock
209: end
210: end until clock
211:
212: template % [
213: clock & 0xFFFFFFFF,
214: (clock >> 32) & 0xFFFF,
215: ((clock >> 48) & 0xFFFF | VERSION_CLOCK),
216: @sequence & 0xFFFF,
217: @mac & 0xFFFFFFFFFFFF
218: ]
219: end