276: def generate(format = :default)
277: template = FORMATS[format]
278:
279: raise ArgumentError, "invalid UUID format #{format.inspect}" unless template
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291: clock = @mutex.synchronize do
292: clock = (Time.new.to_f * CLOCK_MULTIPLIER).to_i & 0xFFFFFFFFFFFFFFF0
293:
294: if clock > @last_clock then
295: @drift = 0
296: @last_clock = clock
297: elsif clock == @last_clock then
298: drift = @drift += 1
299:
300: if drift < 10000 then
301: @last_clock += 1
302: else
303: Thread.pass
304: nil
305: end
306: else
307: next_sequence
308: @last_clock = clock
309: end
310: end until clock
311:
312: template % [
313: clock & 0xFFFFFFFF,
314: (clock >> 32) & 0xFFFF,
315: ((clock >> 48) & 0xFFFF | VERSION_CLOCK),
316: @sequence & 0xFFFF,
317: @mac & 0xFFFFFFFFFFFF
318: ]
319: end