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