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